【问题标题】:Postgres deploy using Kubernetes not workingPostgres 使用 Kubernetes 部署不工作
【发布时间】:2020-03-31 01:15:14
【问题描述】:

我是 Azure Kubernetes 服务 (AKS) 的新手,我为 postgres 创建了如下部署文件

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-pv-claim
  labels:
    app: postgres
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: postgres
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:xx.xx
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 5432
          env:
            - name: POSTGRES_DB
                value: postgresdb
            -   name: POSTGRES_USER
                value: postgresadmin
            - name: POSTGRES_PASSWORD
                value: admin123
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgredb
      volumes:
        - name: postgredb
          persistentVolumeClaim:
            claimName: postgres-pv-claim

apiVersion: v1
kind: Service
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  type: NodePort
  ports:
   - port: 5432
  selector:
   app: postgres

在 AKS 上部署 postgres 服务后,服务就创建好了。

abc@Azure:~$ kubectl get svc
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.0.0.1       <none>        443/TCP          6h45m
postgres     NodePort    10.0.165.161   <none>        5432:30692/TCP   9m8s

但是当我尝试使用psql -h localhost -U postgresadmin --password -p 30692 postgresdb 进入PSQL 时,它显示连接被拒绝。

abc@Azure:~$ psql -h localhost -U postgresadmin --password -p 30692 postgresdb
Password:
psql: error: could not connect to server: could not connect to server: Connection refused
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 30692?
could not connect to server: Connection refused
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 30692?

【问题讨论】:

  • 你需要把服务改成LoadBalancer的类型。
  • 在这种情况下,数据库在云中运行,而不是在您的本地系统上; localhost 将无法访问它。
  • 我试过LoadBalancer,也用过EXTERNAL-IP,但问题是一样的
  • 您可能需要在 AKS 防火墙规则中将您的 IP 列入白名单
  • 你用什么命令连接PostgreSQL?你把localhost改成服务的外网IP了吗?并在服务端口中添加targetPort。

标签: postgresql docker kubernetes azure-devops azure-aks


【解决方案1】:

对于您的问题,您似乎想将 PostgreSQL 公开给 Internet。我看到你犯了两个错误。

首先是YMAL文件,需要将服务改成如下:

apiVersion: v1
kind: Service
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  type: LoadBalancer
  ports:
   - port: 5432
     targetPort: 5432
  selector:
   app: postgres

第二个是connect命令。我想你是在 Azure 云 shell 中连接 PostgreSQL,所以需要使用服务的外部 IP 连接:

psql -h external-IP -U postgresadmin --password -p 5432 postgresdb

【讨论】:

  • abc@Azure:~$ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE postgres LoadBalancer 10.0.221.108 13.91.192.202 5432:32516/TCP 23s abc@Azure:~$ psql -h 13.91.192.202 -U postgresadmin --password -p 5432 postgresdb Password: psql: error: could not connect to server: could not connect to server: Connection timed abc@Azure:~$ psql -h 13.91.192.202 -U postgresadmin --password -p 32516 postgresdb Password: psql: error: could not connect to server: could not connect to server: Connection timed
  • @RavindraGupta 你能检查一下 PostgreSQL 是否运行良好吗?
  • “卷”有没有变化
  • @RavindraGupta 据我所知,挂载路径/var/lib/postgresql/data 不为空。如果你使用 Azure 文件共享挂载到路径,那么它将覆盖路径中的所有文件。所以我推荐你使用Azure磁盘挂载。
【解决方案2】:

仅当我将服务类型从 LoadBalancer 更改为 NodePort 时,它才有效

【讨论】:

    猜你喜欢
    • 2021-02-22
    • 2020-09-21
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 2022-06-28
    • 1970-01-01
    • 2018-01-28
    • 1970-01-01
    相关资源
    最近更新 更多