首页 > 解决方案 > Docker for desktop - kubernetes - postgres 本地客户端

问题描述

我是 Kubernetes 新手,我正在尝试开始在本地部署 Postgres 并能够与客户端连接。

这是设置

分泌物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

邮局

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

标签: postgresqldockerkubernetes

解决方案


我发现您为 postgresdb-service 指定了错误的选择器。

所有 pod 都有标签app: postgres-db,并且您正在app: postgres使用服务进行选择。这就是为什么kuebctl get endpoints显示:

$ kubectl get endpoints
NAME                            ENDPOINTS             AGE
postgresdb-service              <none>                7m39s

更改选择器后,一切都按预期工作。


推荐阅读