简体   繁体   English

"Kubernetes:无法从 Kubernetes 集群外部连接到 postgres"

[英]Kubernetes: Trouble connecting to postgres from outside Kubernetes cluster

I've launched a postgresql server in minikube, and I'm having difficulty connecting to it from outside the cluster.我在 minikube 中启动了一个 postgresql 服务器,但我很难从集群外部连接到它。


Update<\/strong>更新<\/strong>

It turned out my cluster was suffering from unrelated problems, causing all sorts of broken behavior.事实证明,我的集群遇到了不相关的问题,导致了各种破坏行为。 I ended up nuking the whole cluster and vm and starting from scratch.我最终对整个集群和虚拟机进行了核对,并从头开始。 Now I've got working.现在我有工作了。 I changed the deployment to a statefulset, though I think it could work either way.我将部署更改为有状态集,尽管我认为它可以以任何一种方式工作。

Setup and test:设置和测试:

kubectl --context=minikube create -f postgres-statefulset.yaml
kubectl --context=minikube create -f postgres-service.yaml

url=$(minikube service postgres --url --format={{.IP}}:{{.Port}})

psql --host=${url%:*} --port=${url#*:} --username=postgres --dbname=postgres \
     --command='SELECT refobjid FROM pg_depend LIMIT 1'
Password for user postgres:
 refobjid
----------
     1247

I just deployed postgres and exposed its service through NodePort and following is my pod and service. 我刚刚部署了postgres并通过NodePort公开了它的服务,以下是我的pod和服务。

[root@master postgres]# kubectl get pods
NAME                        READY     STATUS    RESTARTS   AGE
postgres-7ff9df5765-2mpsl   1/1       Running   0          1m

[root@master postgres]# kubectl get svc postgres
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
postgres     NodePort    10.100.199.212   <none>        5432:31768/TCP   20s

And this is how connected to postgres though the nodeport: 这是通过节点端口连接到postgres的方式:

[root@master postgres]# kubectl exec -it postgres-7ff9df5765-2mpsl --  psql -h 10.6.35.83 -U postgresadmin --password -p 31768 postgresdb
Password for user postgresadmin: 
psql (10.4 (Debian 10.4-2.pgdg90+1))
Type "help" for help.

postgresdb=# 

In above, 10.6.35.83 is my node/host IP (not pod IP or clusterIP) and port is the NodePort defined in service. 在上面,10.6.35.83是我的节点/主机IP(不是Pod IP或clusterIP),端口是service中定义的NodePort。 The issue is you're not using the right IP to connect to the postgresql. 问题是您没有使用正确的IP连接到Postgresql。

To be able to correctly translate outside packets inside of the postgres container, you have to explicitly define listen_address=0.0.0.0 config parameter.为了能够正确转换 postgres 容器内的外部数据包,您必须明确定义listen_address=0.0.0.0配置参数。

This can be done through the command line options:这可以通过命令行选项完成:

Look at this sample deployment config:看看这个示例部署配置:

deployment.yaml

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }}-postgres-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: {{ .Release.Name }}-postgres
  template:
    metadata:
      labels:
        app: {{ .Release.Name }}-postgres
    spec:
      containers:
      - name: postgres
        image: postgres
        args: ["-d", "postgres", "-c", "listen_addresses=0.0.0.0"]
        env:
          - name: "POSTGRES_PASSWORD"
            value: "postgres"
        ports:
        - containerPort: 5432

service.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: {{ .Release.Name }}-postgres-service
spec:
  type: {{ .Values.serviceType | default "ClusterIP" }}
  selector:
    app: {{ .Release.Name }}-postgres
  ports:
    - protocol: TCP
      port: 5432

That's really all you need to run your test container with postgresql in k8s.这就是在 k8s 中使用 postgresql 运行测试容器所需的全部内容。

I had this challenge when working with PostgreSQL<\/strong> database server in Kubernetes<\/strong> using minikube<\/strong> .在使用minikube<\/strong>在Kubernetes<\/strong>中使用PostgreSQL<\/strong>数据库服务器时,我遇到了这个挑战。

Below is my statefulset yaml file:下面是我的 statefulset yaml 文件:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgresql-db
spec:
  serviceName: postgresql-db-service
  replicas: 2
  selector:
    matchLabels:
      app: postgresql-db
  template:
    metadata:
      labels:
        app: postgresql-db
    spec:
      containers:
      - name: postgresql-db
        image: postgres:latest
        ports:
        - containerPort: 5432
          name: postgresql-db
        volumeMounts:
        - name: postgresql-db-data
          mountPath: /data
        env:
          - name: POSTGRES_PASSWORD
            valueFrom:
              secretKeyRef:
                name: postgresql-db-secret
                key: DATABASE_PASSWORD
          - name: PGDATA
            valueFrom:
              configMapKeyRef:
                name: postgresql-db-configmap
                key: PGDATA
  volumeClaimTemplates:
  - metadata:
      name: postgresql-db-data
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 25Gi

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

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