无法通过 minikube 上的主机名 ping 其他 pods

Can't ping other pods by hostname on minikube

我有一个有两个副本的 statefulSet。 它的无头服务名称是 "gov-svc" 这是 ->

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

在 运行 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

在这个 statefulSet 的 运行 两个 pods 之后,来自 pod sts-0ping 结果:

$ 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

但是当我尝试从 sts-0ping sts-1 时,它说:

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

我需要通过主机名成功 ping 通其他 pods。我该怎么做?

您需要创建 headless service 才能在 StatefulSet 中相互 ping 副本。类似于:

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 <---

注:

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.

注:

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.

更多信息:https://kubernetes.io/docs/concepts/services-networking/service/#headless-services