kubernetes cannot ping another service

6/14/2018

DNS resolution looks fine, but I cannot ping my service. What could be the reason?

From another pod in the cluster:

$ ping backend
PING backend.default.svc.cluster.local (10.233.14.157) 56(84) bytes of data.


^C
--- backend.default.svc.cluster.local ping statistics ---
36 packets transmitted, 0 received, 100% packet loss, time 35816ms

EDIT:

The service definition:

apiVersion: v1
kind: Service
metadata:
  labels:
    app: backend
  name: backend
spec:
  ports:
  - name: api
    protocol: TCP
    port: 10000
  selector:
    app: backend

The deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend
  labels:
    app: backend
spec:
  replicas: 1
  selector:
    matchLabels:
      run: backend
  replicas: 1
  template:
    metadata:
      labels:
        run: backend
    spec:
      containers:
      - name: backend
        image: nha/backend:latest
        imagePullPolicy: Always
        ports:
        - name: api
          containerPort: 10000

I can curl my service from the same container:

kubectl exec -it backend-7f67c8cbd8-mf894 -- /bin/bash
root@backend-7f67c8cbd8-mf894:/# curl localhost:10000/my-endpoint
{"ok": "true"}

It looks like the endpoint on port 10000 does not get exposed though:

 kubectl get ep
NAME         ENDPOINTS                                                       AGE
backend      <none>                                                          2h
-- nha
kubernetes

3 Answers

6/14/2018

Ping doesn't work with service's cluster IPs like 10.233.14.157, as it is a virtual IP. You should be able to ping a specific pod, but no a service.

-- Ignacio Millán
Source: StackOverflow

6/14/2018

You can't ping a service. You can curl it.

-- suren
Source: StackOverflow

6/14/2018

It turns out my selector was wrong, I had to replace run: backend with app: backend

-- nha
Source: StackOverflow