简体   繁体   English

无法连接到 Kubernetes 集群上的远程 postgreSQL 数据库

[英]Cannot connect to remote postgreSQL database on Kubernetes cluster

I'm currently setting up a postgres instance on my Kubernetes cluster hosted on OVH public cloud.我目前正在我的 Kubernetes 集群上设置一个 postgres 实例,该集群托管在 OVH 公共云上。 The problem is that I can't access it.问题是我无法访问它。 I know that I have to make psql -h host -U user --password -p 30904 db to connect, but the problem is that i don't know what to put instead of host .我知道我必须让psql -h host -U user --password -p 30904 db连接,但问题是我不知道该放什么而不是host Localhost?本地主机? The ip of the master node?主节点的ip? Another ip?另一个ip?

Thank you for your time.感谢您的时间。

postgres-deploy.yaml: postgres-deploy.yaml:

apiVersion: v1
kind: Service
metadata:
  name: postgres
spec:
  type:
    NodePort
  ports:
  - port: 5432
  selector:
    app: postgres
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: postgres
spec:
  selector:
    matchLabels:
      app: postgres
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - image: postgres:13.1
        name: postgres
        env:
            - name: POSTGRES_DB
              valueFrom:
                secretKeyRef:
                  name: ident
                  key: POSTGRES_DB
          
              name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: ident
                  key: POSTGRES_USER

              name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: ident
                  key: POSTGRES_PASSWORD
        ports:
        - containerPort: 5432
          name: postgres
        volumeMounts:
        - name: postgres-persistent-storage
          mountPath: /var/lib/postgresql/data2
      volumes:
      - name: postgres-persistent-storage
        persistentVolumeClaim:
          claimName: postgres-pv-claim

You can try something like this:你可以尝试这样的事情:

kubectl exec -it $(kubectl get pods -n %YOURNAMESPACE% | grep postgres | cut -d " " -f1) -n %YOURNAMESPACE% -- bash -c "psql -U postgres -c 'SELECT current_database()'"

Or omit the namespace thing if you need it in current namespace:或者如果您在当前命名空间中需要它,则省略命名空间:

kubectl exec -it $(kubectl get pods | grep postgres | cut -d " " -f1) -n -- bash -c "psql -U postgres -c 'SELECT current_database()'"

I have deployed your YAMLs and I was able to connect.我已经部署了您的YAMLs ,并且能够连接。

In --h you have to provide IP of node or host machine where PostgreSQL pod was deployed.--h中,您必须提供部署PostgreSQL pod 的nodehost machine的 IP。

Test Case测试用例

I have tested this on my GKE cluster.我已经在我的 GKE 集群上对此进行了测试。

Based on your YAMLs and this tutorial.基于您的YAMLs教程。 Mainly for ConfigMap and PV/PVC configuration.主要用于ConfigMapPV/PVC配置。

  POSTGRES_DB: postgresdb
  POSTGRES_USER: postgresadmin
  POSTGRES_PASSWORD: admin123

Connection to PostgreSQL Database连接到 PostgreSQL 数据库

  • 1 - From SQL Pod 1 - 从 SQL 吊舱
    $ kubectl get po
    NAME                        READY   STATUS    RESTARTS   AGE
    postgres-5586dc9864-pwpsn   1/1     Running   0          37m

You have to kubectl exec to PostgreSQL pod.你必须kubectl execPostgreSQL pod。

$ kubectl exec -ti postgres-5586dc9864-pwpsn -- bin/bash
root@postgres-5586dc9864-pwpsn:/#

Use psql command to connect database .使用psql命令连接database

root@postgres-5586dc9864-pwpsn:/# psql -p 5432 -U postgresadmin -d postgresdb
psql (13.1 (Debian 13.1-1.pgdg100+1))
Type "help" for help.

postgresdb=#
  • 2 - From cluster 2 - 从集群

Service Details:服务详情:

$ kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.28.0.1    <none>        443/TCP          60m
postgres     NodePort    10.28.14.2   <none>        5432:31431/TCP   45m

Pod Details:吊舱详细信息:

$ kubectl get po -o wide
NAME                        READY   STATUS    RESTARTS   AGE   IP          NODE                                       NOMINATED NODE   READINESS GATES
postgres-5586dc9864-pwpsn   1/1     Running   0          41m   10.24.1.6   gke-cluster-1-default-pool-8baf2b67-jjjh   <none>           <none>

Node details, where PostgreSQL pod was deployed.节点详细信息,其中部署了PostgreSQL pod。

$ kubectl get node -o wide | grep gke-cluster-1-default-pool-8baf2b67-jjjh
gke-cluster-1-default-pool-8baf2b67-jjjh   Ready    <none>   56m   v1.16.15-gke.4300   10.154.15.222   35.197.210.241   Container-Optimized OS from Google   4.19.112+        docker://19.3.1

NodeIP address of node where PostgreSQL pod was deployed is 10.154.15.222 .部署PostgreSQL pod 的nodeNodeIP地址为10.154.15.222

Command:命令:

$ kubectl exec -ti <podname> -- psql -h <Internal IP address of hosting node> -U postgresadmin --password -p <nodeport number from service> <database name>

Output: Output:

$ kubectl exec -ti postgres-5586dc9864-pwpsn -- psql -h 10.154.15.222 -U postgresadmin --password -p 31431 postgresdb
Password:
psql (13.1 (Debian 13.1-1.pgdg100+1))
Type "help" for help.

postgresdb=#

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM