我是Kubernetes的新手,我正在尝试开始在本地部署Postgres,并能够与客户端连接。
这是设置
Secretes postgres-secrets.yaml
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
MAPPAPI_DATABASE_NAME: d2hhdGV2ZXIx
MAPPAPI_DATABASE_USERNAME: d2hhdGV2ZXIy
MAPPAPI_DATABASE_PASSWORD: d2hhdGV2ZXIz存储postgres-storage.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: db-data-pv
labels:
type: local
spec:
storageClassName: generic
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
hostPath:
path: "/var/lib/postgresql/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: db-data-pvc
spec:
storageClassName: generic
accessModes:
- ReadWriteMany
resources:
requests:
storage: 500Mi部署postgres-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: postgres-db
name: postgres-db
spec:
replicas: 1
selector:
matchLabels:
app: postgres-db
template:
metadata:
labels:
app: postgres-db
spec:
containers:
- name: postgres-db
image: postgres:12.4
ports:
- containerPort: 5432
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: postgres-db
env:
- name: POSTGRES_USER
valueFrom:
secretKeyRef:
name: mysecret
key: MAPPAPI_DATABASE_NAME
- name: POSTGRES_DB
valueFrom:
secretKeyRef:
name: mysecret
key: MAPPAPI_DATABASE_NAME
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: MAPPAPI_DATABASE_PASSWORD
volumes:
- name: postgres-db
persistentVolumeClaim:
claimName: db-data-pvc服务svc.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: postgres-db
name: postgresdb-service
spec:
type: NodePort
ports:
- port: 5432
selector:
app: postgres我处死了$ kubectl get svc postgresdb-service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
postgresdb-service NodePort 10.101.101.67 <none> 5432:32043/TCP 15s我正在尝试使用Postico或命令行连接到localhost:32043
Postico
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.命令行
psql: error: could not connect to server: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.如果我检查pod日志
PostgreSQL Database directory appears to contain a database; Skipping initialization
2020-10-11 19:30:41.296 UTC [1] LOG: starting PostgreSQL 12.4 (Debian 12.4-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2020-10-11 19:30:41.297 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2020-10-11 19:30:41.297 UTC [1] LOG: listening on IPv6 address "::", port 5432
2020-10-11 19:30:41.300 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-10-11 19:30:41.321 UTC [25] LOG: database system was shut down at 2020-10-11 01:05:04 UTC
2020-10-11 19:30:41.332 UTC [1] LOG: database system is ready to accept connections发布于 2020-10-15 19:33:02
我发现您为postgresdb-service指定了错误的选择器。
所有pods都有标签app: postgres-db,并且您选择的是带服务的app: postgres。这就是为什么kuebctl get endpoints会显示:
$ kubectl get endpoints
NAME ENDPOINTS AGE
postgresdb-service <none> 7m39s更改选择器后,一切都按预期运行。
https://stackoverflow.com/questions/64308371
复制相似问题