配置群集 DNS 提供商
IBM Cloud® Kubernetes Service 集群中的每个服务都分配有一个域名系统 (DNS) 名称,集群 DNS 提供程序将注册该名称以解析 DNS 请求。 有关服务和 pod 的 DNS 的更多信息,请参阅 Kubernetes 文档。
集群 DNS 提供商是 CoreDNS,它是一个通用的权威 DNS 服务器,可与 Kubernetes 进行向后兼容但可扩展的集成。 由于 CoreDNS 是单个可执行文件和单个进程,因此与其他集群 DNS 提供商相比,它的依赖性和活动部件较少,不会出现问题。 此外,该项目的编写语言与 Kubernetes 项目相同,都是 Go
,这有助于保护内存。
最后,与其他集群 DNS 提供商相比,CoreDNS 支持更灵活的用例,因为您可以创建自定义 DNS 条目,如 CoreDNS 文档中的常用设置。
自动缩放集群 DNS 提供程序
默认情况下,CoreDNS 包括一个部署,用于根据集群内工作节点和内核的数量自动扩展 CoreDNS pod。 您可以通过编辑 CoreDNS autoscaling ConfigMap 来微调 CoreDNS autoscaler 参数。 例如,如果您的应用程序大量使用集群 DNS 提供商,您可能需要增加 CoreDNS pod 的最小数量以支持应用程序。 更多信息,请参阅 Kubernetes 文档。
开始之前 登录您的账户。 如果适用,请将相应的资源组设定为目标。 设置集群的上下文。
-
验证 CoreDNS autoscaler 部署是否可用。 在 CLI 输出中,验证一个部署是否为 AVAILABLE。
kubectl get deployment -n kube-system coredns-autoscaler
示例输出
NAME READY UP-TO-DATE AVAILABLE AGE coredns-autoscaler 1/1 1 1 69d
-
编辑 CoreDNS 自动分压器的默认设置。 查找
data.linear
字段,该字段缺省为每个 16 个工作程序节点或 256 个核心的一个 CoreDNS pod,其中至少有两个 CoreDNS pod,而不考虑集群大小 (preventSinglePointFailure: true
)。有关更多信息,请参阅 Kubernetes 文档。kubectl edit configmap -n kube-system coredns-autoscaler
示例输出
apiVersion: v1 data: linear: '{"coresPerReplica":256,"nodesPerReplica":16,"preventSinglePointFailure":true}' kind: ConfigMap metadata: ...
定制集群 DNS 提供程序
您可以通过编辑 CoreDNS ConfigMap 来定制 CoreDNS。 例如,您可能需要配置存根域和上游 DNS 服务器,以解析指向外部主机的服务。 此外,您还可以在 CoreDNS ConfigMap 中配置多个 核心文件。 更多信息,请参阅 Kubernetes 文档。
NodeLocal
DNS 高速缓存依赖于 CoreDNS 来维护 DNS 解析的高速缓存。 保持适用的 NodeLocal
DNS 高速缓存和 CoreDNS 配置 (例如存根域) 相同,以保持 DNS 解析一致性。
开始之前 登录您的账户。 如果适用,请将相应的资源组设定为目标。 设置集群的上下文。
-
验证 CoreDNS 部署是否可用。 在 CLI 输出中,验证一个部署是否为 AVAILABLE。
kubectl get deployment -n kube-system coredns
示例输出
NAME READY UP-TO-DATE AVAILABLE AGE coredns 3/3 3 3 69d
-
编辑 CoreDNS 配置映射的缺省设置。 在 ConfigMap 的
data
部分使用 Corefile 来定制存根域和上游 DNS 服务器。 更多信息,请参阅 Kubernetes 文档。不推荐使用 CoreDNS
proxy
插件,并将其替换为forward
插件。 如果更新 CoreDNS ConfigMap, 请确保将所有proxy
实例替换为forward
实例。kubectl edit configmap -n kube-system coredns
CoreDNS 输出示例
apiVersion: v1 kind: ConfigMap metadata: name: coredns namespace: kube-system data: Corefile: | import <MyCorefile> .:53 { errors health { lameduck 10s } ready kubernetes cluster.local in-addr.arpa ip6.arpa { pods insecure fallthrough in-addr.arpa ip6.arpa ttl 30 } prometheus :9153 forward . /etc/resolv.conf { max_concurrent 1000 } cache 30 { disable success cluster.local disable denial cluster.local } loop reload loadbalance } <MyCorefile>: | abc.com:53 { errors cache 30 loop forward . 1.2.3.4 }
-
可选:向 CoreDNS 配置映射添加定制 Corefile。 在下面的示例中,在
data.Corefile
部分包含import <MyCoreFile>
,并用自定义 Corefile 信息完成data.<MyCorefile>
部分。 更多信息,请参阅 Corefile 导入文档。不推荐使用 CoreDNS
proxy
插件,并将其替换为forward
插件。 如果更新 CoreDNS ConfigMap, 请确保将所有proxy
实例替换为forward
实例。kubectl edit configmap -n kube-system coredns
定制 Corefile 示例输出
apiVersion: v1 kind: ConfigMap metadata: name: coredns namespace: kube-system data: Corefile: | import <MyCorefile> .:53 { errors health kubernetes cluster.local in-addr.arpa ip6.arpa { pods insecure upstream 172.16.0.1 fallthrough in-addr.arpa ip6.arpa } prometheus :9153 forward . /etc/resolv.conf { max_concurrent 1000 } cache 30 { disable success cluster.local disable denial cluster.local } loop reload loadbalance } <MyCorefile>: | abc.com:53 { errors cache 30 loop forward . 1.2.3.4 }
-
几分钟后,CoreDNS pod 会选取 ConfigMap 更改。
设置 NodeLocal DNS 缓存
在选定的工作节点上设置 NodeLocal
DNS 缓存代理,以提高 IBM Cloud Kubernetes Service 集群的集群 DNS 性能和可用性。 更多信息,请参阅 Kubernetes 文档。
默认情况下,使用 ClusterFirst
DNS 策略的 pod 的群集 DNS 请求会发送到群集 DNS 服务。 如果在工作节点上启用 NodeLocal
DNS 缓存,那么工作节点上这些 pod 的群集 DNS 请求将被发送到本地 DNS 缓存,该缓存监听链接本地 IP 地址 169.254.20.10。 DNS 高速缓存还侦听 kube-system
名称空间中 kube-dns
服务的集群 IP。
在集群中已使用 专区感知 DNS 时,请勿添加 DNS 高速缓存标签。 此外,NodeLocal
DNS 高速缓存依赖于 CoreDNS 来维护 DNS 解析的高速缓存。 保持适用的 NodeLocal
DNS 高速缓存和 CoreDNS 配置 (例如存根域) 相同,以保持 DNS 解析一致性。
NodeLocal
DNS 高速缓存通常在运行 Kubernetes 1.18 或更高版本的集群中可用,但缺省情况下仍处于禁用状态。
启用 NodeLocal DNS 缓存
为 Kubernetes 集群中的一个或多个工作程序节点启用 NodeLocal
DNS 高速缓存。
以下步骤将更新在特定工作程序节点上运行的 DNS pod。 您还可以向工作程序池添加标签,以便未来的节点继承该标签。
在开始之前,请更新任何受此功能影响的 DNS 出口网络策略,如依赖 pod 或命名空间选择器进行 DNS 出口的策略。
kubectl get networkpolicy --all-namespaces -o yaml
-
如果 为 CoreDNS 自定义了存根域和上游 DNS 服务器,则还必须 使用这些存根域和上游 DNS 服务器自定义
NodeLocal
DNS 缓存。 -
列出集群中的节点。
NodeLocal
DNS 高速缓存代理程序 pod 是在每个节点上运行的守护程序集的一部分。kubectl get nodes
-
向工作程序节点添加
ibm-cloud.kubernetes.io/node-local-dns-enabled=true
标签。 该标签可在工作程序节点上启动 DNS 高速缓存代理程序 pod。-
将标签添加到一个或多个工作程序节点。
-
要标注集群中的所有工作程序节点: 将标签添加到所有现有工作程序池。
-
要标注单个工作程序节点:
kubectl label node <node_name> --overwrite "ibm-cloud.kubernetes.io/node-local-dns-enabled=true"
-
-
通过检查
NODE-LOCAL-DNS-ENABLED
字段是否设置为true
,验证节点是否具有标签。kubectl get nodes -L "ibm-cloud.kubernetes.io/node-local-dns-enabled"
示例输出
NAME STATUS ROLES AGE VERSION NODE-LOCAL-DNS-ENABLED 10.xxx.xx.xxx Ready,SchedulingDisabled <none> 28h v1.32+IKS true
-
验证 DNS 高速缓存代理程序 pod 是否在工作程序节点上运行。
kubectl get pods -n kube-system -l k8s-app=node-local-dns -o wide
示例输出
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES node-local-dns-pvnjn 1/1 Running 0 1m 10.xxx.xx.xxx 10.xxx.xx.xxx <none> <none>
-
-
对每个工作程序节点重复先前的步骤以启用 DNS 高速缓存。
禁用 NodeLocal DNS 缓存
您可以禁用一个或多个工作节点的 NodeLocal
DNS 缓存。
-
从工作程序节点中除去
ibm-cloud.kubernetes.io/node-local-dns-enabled
标签。 此操作会终止工作程序节点上的 DNS 高速缓存代理程序 pod。运行以下命令以从集群中的所有工作程序节点除去该标签。
kubectl label node --all --overwrite "ibm-cloud.kubernetes.io/node-local-dns-enabled-"
运行以下命令以从单个工作程序节点中除去标签。
kubectl label node <node_name> "ibm-cloud.kubernetes.io/node-local-dns-enabled-"
-
通过检查
NODE-LOCAL-DNS-ENABLED
字段是否为空,验证是否除去了标签。kubectl get nodes -L "ibm-cloud.kubernetes.io/node-local-dns-enabled"
示例输出
NAME STATUS ROLES AGE VERSION NODE-LOCAL-DNS-ENABLED 10.xxx.xx.xxx Ready,SchedulingDisabled <none> 28h v1.32+IKS
-
验证 pod 是否不再在禁用了 DNS 高速缓存的节点上运行。 输出不会显示任何 pod。
kubectl get pods -n kube-system -l k8s-app=node-local-dns -o wide
示例输出
No resources found.
-
-
对每个工作程序节点重复先前的步骤以禁用 DNS 高速缓存。
自定义 NodeLocal DNS 缓存
您可以通过编辑两个配置映射中的任何一个来定制 NodeLocal
DNS 高速缓存。
node-local-dns
ConfigMap:自定义NodeLocal
DNS 缓存配置。node-local-dns-config
ConfigMap: 通过自定义存根域或上游 DNS 服务器来扩展NodeLocal
DNS 缓存配置,以解析指向外部主机的服务。
NodeLocal
DNS 高速缓存依赖于 CoreDNS 来维护 DNS 解析的高速缓存。 保持适用的 NodeLocal
DNS 高速缓存和 CoreDNS 配置 (例如存根域) 相同,以保持 DNS 解析一致性。
编辑 node-local-dns
ConfigMap 以获取常规配置更新
编辑 node-local-dns
ConfigMap 以定制 NodeLocal
DNS 高速缓存配置。
开始之前 登录您的账户。 如果适用,请将相应的资源组设定为目标。 设置集群的上下文。
-
验证
NodeLocal
DNS 高速缓存守护程序集是否可用。kubectl get ds -n kube-system node-local-dns
示例输出
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE node-local-dns 4 4 4 4 4 ibm-cloud.kubernetes.io/node-local-dns-enabled=true 82d
-
编辑缺省设置或将定制 Corefile 添加到
NodeLocal
DNS 高速缓存 ConfigMap。 导入的每个 Corefile 都必须使用coredns
路径。 更多信息,请参阅 Kubernetes 文档。NodeLocal
DNS 高速缓存仅支持一组有限的 插件。kubectl edit configmap -n kube-system node-local-dns
示例输出
apiVersion: v1 kind: ConfigMap metadata: name: node-local-dns namespace: kube-system data: Corefile: | # Add your NodeLocal DNS customizations as import files under ./coredns directory. # Refer to /docs/containers?topic=containers-cluster_dns for details. import ./coredns/<MyCorefile> cluster.local:53 abc.com:53 { errors cache { success 9984 30 denial 9984 5 } reload loop bind 169.254.20.10 172.21.0.10 forward . __PILLAR__CLUSTER__DNS__ { force_tcp } prometheus :9253 health 169.254.20.10:8080 } in-addr.arpa:53 { errors cache 30 reload loop bind 169.254.20.10 172.21.0.10 forward . __PILLAR__CLUSTER__DNS__ { force_tcp } prometheus :9253 } ip6.arpa:53 { errors cache 30 reload loop bind 169.254.20.10 172.21.0.10 forward . __PILLAR__CLUSTER__DNS__ { force_tcp } prometheus :9253 } .:53 { errors cache 30 { disable success cluster.local disable denial cluster.local } reload loop bind 169.254.20.10 172.21.0.10 forward . __PILLAR__UPSTREAM__SERVERS__ { force_tcp } prometheus :9253 } <MyCorefile>: | # Add custom corefile content ...
-
几分钟后,
NodeLocal
DNS 高速缓存 pod 会获取 ConfigMap 更改。
编辑 node-local-dns-config
ConfigMap 以使用存根域或上游服务器进行扩展
编辑 node-local-dns-config
ConfigMap 以扩展 NodeLocal
DNS 高速缓存配置,例如通过定制存根域或上游 DNS 服务器。 更多信息,请参阅 Kubernetes 文档。
开始之前 登录您的账户。 如果适用,请将相应的资源组设定为目标。 设置集群的上下文。
-
验证
NodeLocal
DNS 高速缓存守护程序集是否可用。kubectl get ds -n kube-system node-local-dns
示例输出
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE node-local-dns 4 4 4 4 4 ibm-cloud.kubernetes.io/node-local-dns-enabled=true 82d
-
确认
NodeLocal
DNS 高速缓存具有 ConfigMap。-
确定
NodeLocal
DNS 高速缓存 ConfigMap 是否存在。kubectl get cm -n kube-system node-local-dns-config
不存在 ConfigMap 时的示例输出
Error from server (NotFound): ConfigMaps "node-local-dns-config" not found
-
如果 ConfigMap 不存在,请创建
NodeLocal
DNS 高速缓存 ConfigMap。kubectl create cm -n kube-system node-local-dns-config
示例输出
configmap/node-local-dns-config created
-
-
编辑
NodeLocal
DNS 高速缓存 ConfigMap。 ConfigMap 使用 KubeDNS 语法来定制存根域和上游 DNS 服务器。 更多信息,请参阅 Kubernetes 文档。kubectl edit cm -n kube-system node-local-dns-config
示例输出
apiVersion: v1 kind: ConfigMap metadata: name: node-local-dns-config namespace: kube-system data: stubDomains: | {"abc.com" : ["1.2.3.4"]}
-
几分钟后,
NodeLocal
DNS 高速缓存 pod 会获取 ConfigMap 更改。
设置区域感知 DNS
在 多专区 IBM Cloud Kubernetes Service 集群 中设置专区感知 DNS 以提高集群 DNS 性能和可用性。 此设置扩展 NodeLocal
DNS 高速缓存 以首选同一区域中的集群 DNS 流量。
缺省情况下,将使用集群范围的 DNS 资源 (而不是专区感知 DNS 资源) 来设置集群。 即使在设置区域感知 DNS 之后,集群范围的 DNS 资源仍作为备份 DNS 运行。 专区感知 DNS 资源与集群范围的 DNS 是分开的,更改专区感知 DNS 不会影响集群范围的 DNS。
在集群中使用专区感知 DNS 时,请勿使用 DNS 高速缓存标签。
设置区域感知 DNS
完成以下步骤以在多专区集群中设置专区感知 DNS。
-
更新受区域感知 DNS 影响的任何 DNS 出口网络策略,例如依赖于 DNS 出口的 pod 或名称空间选择器的策略。 运行以下命令,获取网络策略列表。
kubectl get networkpolicy --all-namespaces -o yaml
-
使用
ibm-cloud.kubernetes.io/deploy-zone-aware-dns=true
在kube-system
名称空间中标注coredns
ConfigMap。kubectl label cm -n kube-system coredns --overwrite "ibm-cloud.kubernetes.io/deploy-zone-aware-dns=true"
-
刷新集群主节点 以部署区域感知 DNS 资源。
ibmcloud ks cluster master refresh -c <cluster_name_or_ID>
-
通过 查看集群详细信息中的 主运行状况 来监视要完成的刷新操作。
ibmcloud ks cluster get -c <cluster_name_or_ID>
-
如果 为 CoreDNS 自定义了存根域和上游 DNS 服务器,则还必须 使用这些存根域和上游 DNS 服务器自定义
NodeLocal
DNS 缓存。 -
为集群的区域设置环境变量。
ZONES=$(kubectl get nodes --no-headers --ignore-not-found=true -o jsonpath='{range .items[*]}{.metadata.labels.topology\.kubernetes\.io/zone}{"\n"}{end}' | uniq)
-
在所有专区中启动 CoreDNS 和 CoreDNS 自动缩放器 pod。
for ZONE in ${ZONES}; do kubectl scale deployment -n kube-system "coredns-autoscaler-${ZONE}" --replicas=1 done
-
验证 CoreDNS 和 CoreDNS 自动缩放器 pod 是否正在所有区域中运行。
for ZONE in ${ZONES}; do kubectl get pods -n kube-system -l "k8s-app=coredns-autoscaler-${ZONE}" -o wide kubectl get pods -n kube-system -l "k8s-app=coredns-${ZONE}" -o wide done
-
在所有工作程序节点上启动
NodeLocal
DNS 高速缓存 pod。kubectl label nodes --all --overwrite "ibm-cloud.kubernetes.io/zone-aware-dns-enabled=true"
-
验证
NodeLocal
DNS 高速缓存 pod 是否正在所有工作程序节点上运行。for ZONE in ${ZONES}; do kubectl get pods -n kube-system -l "k8s-app=node-local-dns-${ZONE}" -o wide done
-
标注工作程序池,以便将来的工作程序节点继承
ibm-cloud.kubernetes.io/zone-aware-dns-enabled=true
标签。
禁用和删除区域感知 DNS
要除去专区感知 DNS,必须首先在多专区集群的每个专区中禁用专区感知 DNS。 然后,删除区域感知 DNS 资源。
-
从工作程序池中除去
ibm-cloud.kubernetes.io/zone-aware-dns-enabled=true
标签。 -
为集群中的区域设置环境变量。
ZONES=$(kubectl get nodes --no-headers --ignore-not-found=true -o jsonpath='{range .items[*]}{.metadata.labels.topology\.kubernetes\.io/zone}{"\n"}{end}' | uniq)
-
停止所有工作程序节点上的
NodeLocal
DNS 高速缓存 pod。kubectl label nodes --all --overwrite "ibm-cloud.kubernetes.io/zone-aware-dns-enabled-"
-
停止所有区域中的 CoreDNS 自动缩放器 pod。
for ZONE in ${ZONES}; do kubectl scale deployment -n kube-system "coredns-autoscaler-${ZONE}" --replicas=0 done
-
验证 CoreDNS 自动缩放器 pod 是否不再在所有区域中运行。
for ZONE in ${ZONES}; do kubectl get pods -n kube-system -l "k8s-app=coredns-autoscaler-${ZONE}" done
-
停止所有区域中的 CoreDNS pod。
for ZONE in ${ZONES}; do kubectl scale deployment -n kube-system "coredns-${ZONE}" --replicas=0 done
-
从
kube-system
名称空间中的coredns
ConfigMap 除去ibm-cloud.kubernetes.io/deploy-zone-aware-dns=true
标签。kubectl label cm -n kube-system coredns --overwrite "ibm-cloud.kubernetes.io/deploy-zone-aware-dns-"
-
刷新集群主节点 以删除区域感知 DNS 资源。
ibmcloud ks cluster master refresh --cluster <cluster-name-or-id>
-
通过 查看集群详细信息中的 主运行状况 来监视要完成的刷新操作。
ibmcloud ks cluster get -c <cluster_name_or_ID>