Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kubernetes cannot ping another service

Tags:

kubernetes

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
like image 607
nha Avatar asked Jun 14 '18 08:06

nha


People also ask

Can you ping a Kubernetes service?

Other tools to the rescue While it's sad that we can't use ping to test a Kubernetes Service, we abso-freakin-lutely can use others tools to test connectivity.

How do I connect one pod to another pod in Kubernetes?

A Pod can communicate with another Pod by directly addressing its IP address, but the recommended way is to use Services. A Service is a set of Pods, which can be reached by a single, fixed DNS name or IP address. In reality, most applications on Kubernetes use Services as a way to communicate with each other.

How do I see what services are running in Kubernetes?

Using kubectl describe pods to check kube-system If the output from a specific pod is desired, run the command kubectl describe pod pod_name --namespace kube-system . The Status field should be "Running" - any other status will indicate issues with the environment.

Why Ping doesn't work with Kubernetes services?

So… here’s a technical explanation why ping doesn’t work with Kubernetes Services. A Kubernetes Service is a stable networking endpoint that sits in front of a set of application Pods. Instead of accessing Pods directly you access them through the Service.

What is a Kubernetes Service?

A Kubernetes Service is a stable networking endpoint that sits in front of a set of application Pods. Instead of accessing Pods directly you access them through the Service. The Service exposes a DNS name, virtual IP, and network port that you can use to connect to the Pods behind it.

How do containers in a Kubernetes cluster communicate with each other?

Kubernetes gives every pod its own cluster-private IP address, so you do not need to explicitly create links between pods or map container ports to host ports. This means that containers within a Pod can all reach each other's ports on localhost, and all pods in a cluster can see each other without NAT.

Why does my Kubernetes pod have an ordering problem?

When a Pod runs on a Node, the kubelet adds a set of environment variables for each active Service. This introduces an ordering problem. To see why, inspect the environment of your running nginx Pods (your Pod name will be different):


2 Answers

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.

like image 161
Ignacio Millán Avatar answered Oct 08 '22 15:10

Ignacio Millán


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

like image 11
suren Avatar answered Oct 08 '22 16:10

suren