Search code examples
kubernetesminikubekube-dnskubernetes-podkubernetes-statefulset

Can't ping other pods by hostname on minikube


I have a statefulSet with two replicas. It's headless service name is "gov-svc" It's ->

  1. .metadata.name: sts
  2. .metadata.namespace: default
  3. .spec.serviceName: gov-svc
  4. .spec.template.spec.subdomain: gov-svc
  5. .spec.replicas: 2

Before running statefulSet

kubectl get pods --all-namespaces
NAMESPACE     NAME                                    READY   STATUS             RESTARTS   AGE
kube-system   coredns-99b9bb8bd-qdnsb                 1/1     Running            0          4h
kube-system   etcd-minikube                           1/1     Running            0          4h
kube-system   kube-addon-manager-minikube             1/1     Running            0          4h
kube-system   kube-apiserver-minikube                 1/1     Running            0          4h
kube-system   kube-controller-manager-minikube        1/1     Running            1          4h
kube-system   kube-proxy-b9np6                        1/1     Running            0          4h
kube-system   kube-scheduler-minikube                 1/1     Running            0          4h
kube-system   kubernetes-dashboard-7db4dc666b-bsk8k   1/1     Running            0          4h
kube-system   storage-provisioner

After running both of the pods of this statefulSet, from pod sts-0, ping results:

$ ping  sts-0.gov-svc.default.svc.cluster.local
PING sts-0.gov-svc.default.svc.cluster.local (172.17.0.11): 56 data bytes
64 bytes from 172.17.0.11: seq=0 ttl=64 time=0.051 ms
64 bytes from 172.17.0.11: seq=1 ttl=64 time=0.444 ms
^C
--- redis-cluster-exp-0-0.redis-cluster-exp.default.svc.cluster.local ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.051/0.247/0.444 ms

But when I try to ping sts-1 from sts-0, it says:

$ ping sts-1.gov-svc.default.svc.cluster.local
ping: bad address 'sts-1.gov-svc.default.svc.cluster.local'

I need to ping other pods successfully by hostname. How can I do it?


Solution

  • You need to create headless service to be able to ping replicas from each to other within a StatefulSet. Something like:

    apiVersion: v1
    kind: Service
    metadata:
      name: gov-svc-headless
      labels:
        your_label: your_value
    spec:
      selector:
        your_label: your_value
      ports:
      - port: your_port
        name: transport
        protocol: TCP
      clusterIP: None <---
    

    Note:

    With selectors For headless services that define selectors, the endpoints controller creates Endpoints records in the API, and modifies the DNS configuration to return A records (addresses) that point directly to the Pods backing the Service.

    Note:

    or headless services that do not define selectors, the endpoints controller does not create Endpoints records. However, the DNS system looks for and configures either:

    CNAME records for ExternalName-type services. A records for any Endpoints that share a name with the service, for all other types.

    More info: https://kubernetes.io/docs/concepts/services-networking/service/#headless-services