kubernetes无法从Node.js连接Postgres

时间:2019-10-25 10:36:33

标签: kubernetes

我有nodejs应用程序和postgres作为数据库,两者都通过kubernetes部署(以kubeadm和docker作为容器)。我为Postgres创建了clusterip服务,但无法从nodejs访问。

我登录到nodejs pod并对postgres的clusterip进行ping操作,它没有连接,但是我能够连接部署了postgres pod的主机ip(使用节点服务)。

以下是我的postgres配置

apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-config
  labels:
    app: kubia
    tier: database
data:
  POSTGRES_DB: postgresdb
  POSTGRES_USER: postgresadmin
  POSTGRES_PASSWORD: admin123
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: postgres-pv
  labels:
    type: local
    app: kubia
    tier: database
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/mnt/data"

---

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

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: kubia
      tier: database
  template:
    metadata:
      labels:
        app: kubia
        tier: database
    spec:
      containers:
        - name: postgres
          image: postgres:10.4
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 5432
          envFrom:
            - configMapRef:
                name: postgres-config
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgredb
      nodeSelector:
        nodetype: database
      volumes:
        - name: postgredb
          persistentVolumeClaim:
            claimName: postgres-pv-claim

---
apiVersion: v1
kind: Service
metadata:
  name: postgres-node
  labels:
    app: kubia
    tier: database
spec:
  type: NodePort
  ports:
   - port: 5432
     nodePort: 30010
  selector:
   app: kubia
   tier: database

---
apiVersion: v1
kind: Service
metadata:
  name: postgres
  labels:
    app: kubia
    tier: database
  namespace: default  
spec:
  type: ClusterIP
  ports:
   - port: 5432     
  selector:
   app: kubia
   tier: database

以下是pod和服务

NAME                                   READY   STATUS    RESTARTS   AGE     IP            NODE            NOMINATED NODE   READINESS GATES
pod/kubia-container-797dcf95c6-lgzkr   1/1     Running   0          3s      172.16.3.23   kube-worker-3   <none>           <none>
pod/postgres-75b85f8f5f-h9vrq          1/1     Running   0          2m31s   172.16.3.21   kube-worker-3   <none>           <none>

NAME                    TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE     SELECTOR
service/kubernetes      ClusterIP   10.96.0.1       <none>        443/TCP          10d     <none>
service/kubia-svc       NodePort    10.98.129.62    <none>        8080:30008/TCP   3s      app=kubia,tier=ui
service/postgres        ClusterIP   10.111.153.26   <none>        5432/TCP         2m31s   app=kubia,tier=database
service/postgres-node   NodePort    10.107.59.255   <none>        5432:30010/TCP   2m31s   app=kubia,tier=database

我在包含集群ip(10.111.153.26)和端口(5432)的nodejs应用程序中使用了POSTGRES_SERVICE_HOST和POSTGRES_SERVICE_PORT,与此应用程序无法连接(postgres:// postgresadmin:admin123@10.111.153.26:5432 / postgresdb )。

但是应用程序能够将Postgres与主机IP(10.30.111.108)和节点端口连接 喜欢 postgres:// postgresadmin:admin123@10.30.111.108:30010 / postgresdb

参考其他帖子,我甚至提到了服务名称 类似于postgres:// postgresadmin:admin123 @ postgres / postgresdb,但它也无法正常工作。

kubectl get ep

给出以下输出

NAME            ENDPOINTS          AGE
kubernetes      x.x.x.x:6443       11d
kubia-svc       172.16.3.25:8080   171m
postgres        172.16.3.24:5432   171m
postgres-node   172.16.3.24:5432   171m

我尝试从Postgres Pod连接Postgres,但无法连接

psql -h postgres -p 5432 -U postgresadmin -d postgresdb
psql: could not translate host name "postgres" to address: Temporary failure in name resolution

我无法对postgres主机进行硬编码,因为它可以随时更改,相反,我需要通过clusterip进行连接。这个缺少什么?。

代码可用here

1 个答案:

答案 0 :(得分:1)

我只是从yaml上面部署了postgres,没有看到任何问题。我尝试了clusterIP以及服务名称,并且可以使用两者连接postgres。有用。见下文

master $ kubectl get svc,po
NAME                    TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
service/kubernetes      ClusterIP   10.96.0.1      <none>        443/TCP          111m
service/postgres        ClusterIP   10.97.192.16   <none>        5432/TCP         11m
service/postgres-node   NodePort    10.103.72.80   <none>        5432:30010/TCP   11m

NAME                            READY   STATUS    RESTARTS   AGE
pod/postgres-848d4d8db8-n764v   1/1     Running   0          10m
master $ kubectl exec -it postgres-848d4d8db8-n764v sh


using service name
----------------

# psql -h postgres -p 5432 -U postgresadmin -d postgresdb
Password for user postgresadmin:
psql (10.4 (Debian 10.4-2.pgdg90+1))
Type "help" for help.

postgresdb=# \list
                                 List of databases
    Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges
------------+----------+----------+------------+------------+-----------------------
 postgres   | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
 postgresdb | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
 template0  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
            |          |          |            |            | postgres=CTc/postgres
 template1  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
            |          |          |            |            | postgres=CTc/postgres
(4 rows)

postgresdb=#\q

using clusterIP
----------------
postgresdb=# \q
#
#
# psql -h 10.97.192.16 -p 5432 -U postgresadmin -d postgresdb
Password for user postgresadmin:
psql (10.4 (Debian 10.4-2.pgdg90+1))
Type "help" for help.

postgresdb=# \list
                                 List of databases
    Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges
------------+----------+----------+------------+------------+-----------------------
 postgres   | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
 postgresdb | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
 template0  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
            |          |          |            |            | postgres=CTc/postgres
 template1  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
            |          |          |            |            | postgres=CTc/postgres
(4 rows)

postgresdb=#

建议您更新nodejs中的连接详细信息以使用服务名称或clusterIP。 nodejs和postgres都在同一个命名空间中运行。它应该工作