Search code examples
ipkubernetesendpointheadlesskube-dns

Can't connect Kubernetes endpoints IPs with created as an headless service


Created a headless service:

myapp-service-headless.yaml

apiVersion: v1
kind: Service
metadata:
  name: myapp-service-headless
spec:
  ports:
    - port: 8000
  selector:
    app: myapp
  clusterIP: None

After create it to Kubernetes cluster, check its service status:

$ kubectl create -f myapp-service-headless.yaml
$ kubectl describe service myapp-service-headless
Name:              myapp-service-headless
Namespace:         default
Labels:            <none>
Annotations:       <none>
Selector:          app=myapp
Type:              ClusterIP
IP:                None
Port:              <unset>  8000/TCP
TargetPort:        8000/TCP
Endpoints:         172.17.0.11:8000,172.17.0.9:8000
Session Affinity:  None
Events:            <none>

Try to connect 172.17.0.11:8000 or 172.17.0.9:8000, pending and no result.

Here using kube-dns: myapp-service-headless.default.svc.cluster.local in the application. Now it's in the container of pods.

So how to connect to these applications from other application via API? Which IP can been used?


Solution

  • Did you expose your container port inside your Endpoint (Pod)?

    From what i knew, the headless service can be used to generate entries in kube-dns based on how you configure the Service, as discussed in this doc.

    However, you can create another Service with type: NodePort that selects your backend Pods, and expose them on a NodePort/LB.