Kubernetes - 服务群集IP地址与Pod IP地址

时间:2017-11-03 11:58:28

标签: azure networking dns kubernetes

我在Azure(ACS)上部署了一个带Kubernetes的Postgres数据库。

我使用了状态集和服务。

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: postgres
spec:
  selector:
    matchLabels:
      app: postgres
  serviceName: "postgres"
  # 1 instance
  replicas: 1
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      # Postgres database
      - name: postgres
        image: postgres:10
        ports:
        - containerPort: 5432
        volumeMounts:
          - name: pv-postgres
            mountPath: /var/lib/postgresql/data
        env:
        - name: POSTGRES_USER
          value: postgres
        - name: POSTGRES_DB
          value: tariffbook_db
        - name: PGDATA
          value: /var/lib/postgresql/data
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: postgres-secret
              key: password
      hostname: postgres
      volumes:
      - name: pv-postgres
      # Upon node restart, restart the container
      restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  ports:
  - port: 5432
    protocol: TCP
  selector:
    app: postgres

Kubernetes部署数据库并将其注册到kube-dns:

kubectl exec -ti busybox -- nslookup postgres
...
Name:      postgres
Address 1: 10.0.209.61 postgres.default.svc.cluster.local

现在,当我发布postgres时,我没有收到任何回复。

kubectl exec -ti busybox -- ping postgres
PING postgres (10.0.209.61): 56 data bytes

--- postgres ping statistics ---
14 packets transmitted, 0 packets received, 100% packet loss

可以通过其内部IP:10.244.0.46访问Pod,但该服务无法访问postgres Pod。

kubectl get pod --all-namespaces -o wide
NAMESPACE     NAME                                            READY     
STATUS    RESTARTS   AGE       IP             NODE
default       azure-files-pod                                 1/1       
Running   0          19h       10.244.2.16    k8s-agent-52f7ae4d-1
default       busybox                                         1/1       
Running   12         12h       10.244.2.49    k8s-agent-52f7ae4d-1
default       postgres-0                                      1/1       
Running   0          1m        10.244.0.46    k8s-agent-52f7ae4d-0

我已关注DNS troubleshooting tips,但我没有注意到DNS设置中的任何错误。

我的StatefulSet或服务中是否存在配置错误的内容?

非常感谢你的帮助! 最好的祝福, Eric MANUGUERRA

1 个答案:

答案 0 :(得分:0)

您只打开了端口5432,从不打开ping服务的端口。如果您尝试连接到端口5432,您应该能够:

kubectl exec -ti busybox -- telnet postgres.default.svc.cluster.local 5432

或(你真的不应该尝试直接连接到IP)

kubectl exec -ti busybox -- telnet 10.0.209.61 5432