Can't ping postgres pod from another pod in kubernetes

6/5/2019

I created one busy pod to test db connection by following yaml

pod.yaml

kind: Pod
apiVersion: v1
metadata:
  name: marks-dummy-pod
spec:
  containers:
    - name: marks-dummy-pod
      image: djtijare/ubuntuping:v1
      command: ["/bin/bash", "-ec", "while :; do echo '.'; sleep 5 ; done"]
  restartPolicy: Never

Dockerfile used :-

FROM ubuntu
RUN apt-get update && apt-get install -y iputils-ping
CMD bash

I create service as

postgresservice.yaml

kind: Service
apiVersion: v1
metadata:
 name: postgressvc
spec:
 type: ClusterIP
 ports:
 - port: 5432
   targetPort: 5432

Endpoint for created service as

kind: Endpoints
apiVersion: v1
metadata:
 name: postgressvc
subsets:
 - addresses:
     - ip: 172.31.6.149
   ports:
     - port: 5432

Then i ran ping 172.31.6.149 inside pod (kubectl exec -it mark-dummy-pod bash) but not working.(ping localhost is working)

output of kubectl get pods,svc,ep -o wide

NAME                                       READY   STATUS    RESTARTS   AGE     IP             NODE              NOMINATED NODE   READINESS GATES
pod/marks-dummy-pod                        1/1     Running   0          43m     192.168.1.63   ip-172-31-11-87   <none>           <none>


NAME                       TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE     SELECTOR
service/postgressvc        ClusterIP   10.107.58.81     <none>        5432/TCP         33m     <none>


NAME                         ENDPOINTS           AGE
endpoints/postgressvc        172.31.6.149:5432   32m

Output for answer by P Ekambaram

kubectl get pods,svc,ep -o wide gives

NAME                            READY   STATUS    RESTARTS   AGE    IP             NODE              NOMINATED NODE   READINESS GATES
pod/postgres-855696996d-w6h6c   1/1     Running   0          44s    192.168.1.66   ip-172-31-11-87   <none>           <none>


NAME                    TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE    SELECTOR
service/postgres        NodePort    10.110.203.204   <none>        5432:31076/TCP   44s    app=postgres



 NAME                      ENDPOINTS           AGE
endpoints/postgres        192.168.1.66:5432   44s
-- Dhanraj
kubernetes
kubernetes-pod

3 Answers

6/5/2019

you should create deployment for your database and then make a service that target this deployment and then ping using this service why ping with ip ?

-- Semah Mhamdi
Source: StackOverflow

6/5/2019

postgres pod is missing?

did you create endpoint object or was it auto generated?

share the pod definition YAML

you shouldnt be creating endpoint. it is wrong. follow the below deployment for postgres.

apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-config
  labels:
    app: postgres
data:
  POSTGRES_DB: postgres
  POSTGRES_USER: postgres
  POSTGRES_PASSWORD: example
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: postgres
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:11
          imagePullPolicy: Always
          ports:
            - containerPort: 5432
          envFrom:
            - configMapRef:
                name: postgres-config
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgres-data
      volumes:
        - name: postgres-data
          emptyDir:
---       
apiVersion: v1
kind: Service
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  type: NodePort
  ports:
   - port: 5432
  selector:
   app: postgres

undeploy postgres service and endpoint and deploy the above YAML. it should work

why NODE ip is prefixed with ip-

enter image description here

-- P Ekambaram
Source: StackOverflow

6/6/2019

So problem was in my DNS pod in namespace=kube-system

I just create new kubernetes setup and make sure that DNS is working

For new setup refer to my answer of another question

How to start kubelet service??

-- Dhanraj
Source: StackOverflow