将 spec.clusterIP 字段设置为 None, DNS 会返回 pod ip 而不是 service ip.

5.6.1 创建 headless 服务

将 service 中的 clusterIP 字段设置为 None 会使 service 成为 headless 服务, Kubernetes 不会为其分配集群 ip.

image.png

5.6.2 通过 DNS 发现 pod

不通过 YAML 文件运行 pod

只创建 pod, 不创建 ReplicationController:

  1. $ kubectl run dnsutils --image=tutum/dnsutils --command -- sleep infinity
  2. pod/dnsutils created

image.png

有问题:

  1. $ kubectl run dnsutils2 --image=tutum/dnsutils --generator=run-pod/v1 --command -- sleep infinity
  2. Flag --generator has been deprecated, has no effect and will be removed in the future.
  3. pod/dnsutils2 created

理解 headless 服务的 DNS A 记录解析

查找 DNS:

image.png

非 headless 服务返回集群 ip:

image.png

注意:

  • headless 服务仍然提供跨 pod 的负载平衡
  • 通过 DNS 轮询机制不是通过服务代理

5.6.3 发现所有的 pod - 包括未就绪的 pod

添加注解:

image.png