Kubernetes - service cluster IP address vs Pod IP address

11/3/2017

I have deployed a Postgres Database with Kubernetes on Azure (ACS).

I have used a StatefulSet combined with a Service.

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 deploys the database and registers it in kube-dns :

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

Now when I ping postgres, I don't receive any response.

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

The Pod can be reached through its internal IP : 10.244.0.46, but the service is not providing access to the 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

I have followed the DNS troubleshooting tips, but I have not noticed any errors in the DNS setup.

Is there something misconfigured in my StatefulSet or Service ?

Thank you very much for your help ! Best regards, Eric MANUGUERRA

-- Eric Manuguerra
azure
dns
kubernetes
networking

1 Answer

11/3/2017

You only opened port 5432, never a port to ping your service at. If you try to connect to port 5432 you should be able to:

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

or (you really shouldn't be trying to connect directly to IPs)

kubectl exec -ti busybox -- telnet 10.0.209.61 5432
-- vascop
Source: StackOverflow