配置集群 DNS 提供商

IBM Cloud® Kubernetes Service 集群中的每个服务都分配有一个域名系统 (DNS) 名称,集群 DNS 提供程序将注册该名称以解析 DNS 请求。 有关服务和 Pod 的 DNS 的更多信息,请参阅 Kubernetes 文档

集群 DNS 提供商是 CoreDNS,它是一个通用的权威 DNS 服务器,可与 Kubernetes 进行向后兼容但可扩展的集成。 由于 CoreDNS 是一个单一的可执行文件且仅运行一个进程,因此与其他集群 DNS 提供商相比,其依赖项和可能出现问题的组件更少。 此外,该项目的编写语言与 Kubernetes 项目相同,都是 Go,这有助于保护内存。 最后,CoreDNS 比其他集群 DNS 提供商支持更灵活的使用场景,因为您可以创建自定义 DNS 条目,例如《 CoreDNS 》文档中提到的常见配置。

CoreDNS 以及 NodeLocal DNS 缓存配置在版本 1.34 及更高版本中的更改
在 NodeLocalCoreDNS 和DNS配置中,默认DNS缓存时间已从30秒延长至120秒。 此更改适用于 DNS ConfigMap 缓存的设置。 由于缓存持续时间较短,导致 DNS 查找频繁,一些用户触及 pDNS (或 UDP )流量限制。 30秒的缓存间隔通常没有必要,除非是频繁变更的DNS记录,例如健康检查端点。 对于大多数工作负载而言,120秒的默认缓存时间能提供更优性能,并减少不必要的DNS流量。 需要为特定用例设置更短缓存时间的用户可以覆盖此设置。

自动缩放集群 DNS 提供程序

默认情况下,CoreDNS 包含一个部署,可根据集群中的工作节点和核心数量自动调整 CoreDNS pod 的规模。 您可以通过编辑 CoreDNS 中的自动缩放配置( ConfigMap )来微调 CoreDNS 的自动缩放参数。 例如,如果您的应用大量使用集群 DNS 提供程序,您可能需要增加“CoreDNS”Pod 的最小数量,以支持该应用。 如需了解更多信息,请参阅 Kubernetes 上的文档

开始之前:请登录您的账户。 如果适用,请将相应的资源组设定为目标。 设置集群的上下文。

  1. 请确认 CoreDNS 自动缩放器的部署是否可用。 在 CLI 输出中,验证一个部署是否为 AVAILABLE

    kubectl get deployment -n kube-system coredns-autoscaler
    

    示例输出

    NAME                 READY   UP-TO-DATE   AVAILABLE   AGE
    coredns-autoscaler   1/1     1            1           69d
    
  2. 编辑 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 )中配置 多个Corefile。 如需了解更多信息,请参阅 Kubernetes 上的文档

NodeLocal DNS缓存依赖于 CoreDNS 来维护DNS解析的缓存。 保持 NodeLocal 适用的DNS缓存和 CoreDNS 配置(如存根域)一致,以维持DNS解析的一致性。

开始之前:请登录您的账户。 如果适用,请将相应的资源组设定为目标。 设置集群的上下文。

  1. 验证 CoreDNS 部署是否可用。 在 CLI 输出中,验证一个部署是否为 AVAILABLE

    kubectl get deployment -n kube-system coredns
    

    示例输出

    NAME      READY   UP-TO-DATE   AVAILABLE   AGE
    coredns   3/3     3            3           69d
    
  2. 编辑 CoreDNS 配置映射的缺省设置。 在 ConfigMap 文件的 data 部分中使用Corefile,以自定义代理域名和上游DNS服务器。 如需了解更多信息,请参阅 Kubernetes 上的文档

    proxyCoreDNS 插件已被弃用,并被 插件 forward 取代。 若需更新,ConfigMap,CoreDNS 请务必将所有 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
            }
    
  3. 可选:向 CoreDNS 配置映射添加定制 Corefile。 在下面的示例中,在 data.Corefile 部分包含 import <MyCoreFile>,并用自定义 Corefile 信息完成 data.<MyCorefile> 部分。 如需了解更多信息,请参阅 Corefile 导入文档

    proxyCoreDNS 插件已被弃用,并被 插件 forward 取代。 若需更新,ConfigMap,CoreDNS 请务必将所有 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
        }
    
  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解析的一致性。

1.34 及更早版本
NodeLocal DNS 缓存是选择性的。 您必须在工作节点上添加 ibm-cloud.kubernetes.io/node-local-dns-enabled=true 标签,手动启用它。
1.35 及更高版本
NodeLocal 默认情况下,所有工作节点都已启用 DNS 缓存。 升级到 1.35 版本的群集会自动启用 NodeLocal DNS,除非您选择退出。 为防止自动启用,请在升级前将 ibm-cloud.kubernetes.io/node-local-dns-disabled=true 标签应用到工作节点。

有关 1.35 版本变更的更多信息,请参阅 1.35 版本信息

启用 NodeLocal 的DNS缓存

为 Kubernetes 集群中的一个或多个工作程序节点启用 NodeLocal DNS 高速缓存。

1.35 及更高版本
NodeLocal 默认情况下,所有工作节点都已启用 DNS。 只有在以前禁用 NodeLocal DNS 或添加新工作节点时禁用 NodeLocal DNS 的情况下,才能执行这些步骤。
1.34 及更早版本
NodeLocal DNS 是选择性的。 请按照以下步骤在工作节点上启用它。

以下步骤将更新在特定工作程序节点上运行的 DNS pod。 您还可以向工作程序池添加标签,以便未来的节点继承该标签。

开始之前,请更新受此功能影响的任何 DNS 出站网络策略,例如那些依赖 Pod 或命名空间选择器进行 DNS 出站的策略。

kubectl get networkpolicy --all-namespaces -o yaml
  1. 请登录您的账户。 如果适用,请将相应的资源组设定为目标。 设置集群的上下文。

  2. 如果 为 CoreDNS 自定义了存根域和上游 DNS 服务器,则还必须 使用这些存根域和上游 DNS 服务器自定义 NodeLocal DNS 缓存

  3. 列出集群中的节点。 NodeLocal DNS 高速缓存代理程序 pod 是在每个节点上运行的守护程序集的一部分。

    kubectl get nodes
    
  4. 向工作程序节点添加 ibm-cloud.kubernetes.io/node-local-dns-enabled=true 标签。 该标签可在工作程序节点上启动 DNS 高速缓存代理程序 pod。

    1. 将标签添加到一个或多个工作程序节点。
      • 要标注集群中的所有工作程序节点: 将标签添加到所有现有工作程序池

      • 要标注单个工作程序节点:

        kubectl label node <node_name> --overwrite "ibm-cloud.kubernetes.io/node-local-dns-enabled=true"
        
    2. 通过检查 NODE-LOCAL-DNS-ENABLED 字段是否设置为 true,验证节点是否具有标签。
        kubectl get nodes -L "ibm-cloud.kubernetes.io/node-local-dns-enabled"
        ```
        示例输出
    
        ```sh {: screen}
        NAME          STATUS                      ROLES    AGE   VERSION       NODE-LOCAL-DNS-ENABLED
        10.xxx.xx.xxx Ready,SchedulingDisabled    <none>   28h   v1.35+IKS   true
        ```
    3. 验证 DNS 高速缓存代理程序 pod 是否在工作程序节点上运行。
    
    ```sh {: pre}
        kubectl get pods -n kube-system -l k8s-app=node-local-dns -o wide
        ```
        示例输出
    
        ```sh {: screen}
        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>
        ```
    
  5. 对每个工作程序节点重复先前的步骤以启用 DNS 高速缓存。

禁用 NodeLocal 的DNS缓存

您可以禁用一个或多个工作节点的 NodeLocal DNS缓存。

1.35 及更高版本

要禁用默认启用的 NodeLocal DNS,请在工作节点上添加 ibm-cloud.kubernetes.io/node-local-dns-disabled=true 标签。

kubectl label node --all --overwrite "ibm-cloud.kubernetes.io/node-local-dns-disabled=true"
1.34 及更早版本

要禁用 NodeLocal DNS,请从工作节点上删除 ibm-cloud.kubernetes.io/node-local-dns-enabled 标签。

  1. 请登录您的账户。 如果适用,请将相应的资源组设定为目标。 设置集群的上下文。

  2. 从工作程序节点中除去 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-"
    
    1. 通过检查 NODE-LOCAL-DNS-ENABLED 字段是否为空,验证是否除去了标签。
        kubectl get nodes -L "ibm-cloud.kubernetes.io/node-local-dns-enabled"
        ```
        示例输出
    
        ```sh {: screen}
        NAME          STATUS                      ROLES    AGE   VERSION       NODE-LOCAL-DNS-ENABLED
        10.xxx.xx.xxx Ready,SchedulingDisabled    <none>   28h   v1.35+IKS   
        ```
    2. 验证 pod 是否不再在禁用了 DNS 高速缓存的节点上运行。 输出不会显示任何 pod。
    
    ```sh {: pre}
        kubectl get pods -n kube-system -l k8s-app=node-local-dns -o wide
        ```
        示例输出
    
        ```sh {: screen}
        No resources found.
        ```
    
  3. 对每个工作程序节点重复先前的步骤以禁用 DNS 高速缓存。

自定义 NodeLocal 的DNS缓存

您可以通过编辑两个配置映射中的任何一个来定制 NodeLocal DNS 高速缓存。

NodeLocal DNS缓存依赖于 CoreDNS 来维护DNS解析的缓存。 保持 NodeLocal 适用的DNS缓存和 CoreDNS 配置(如存根域)一致,以维持DNS解析的一致性。

编辑 node-local-dns ConfigMap 以获取常规配置更新

编辑 node-local-dnsConfigMap 以自定义 NodeLocal DNS 缓存配置。

开始之前:请登录您的账户。 如果适用,请将相应的资源组设定为目标。 设置集群的上下文。

  1. 验证 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
    
  2. 编辑缺省设置或将定制 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 ...
    
  3. 几分钟后,NodeLocal DNS 高速缓存 pod 会获取 ConfigMap 更改。

编辑 node-local-dns-config ConfigMap 以使用存根域或上游服务器进行扩展

编辑 node-local-dns-configConfigMap 以扩展 DNS缓存 NodeLocal 配置,例如自定义 stub 域或上游 DNS 服务器。 如需了解更多信息,请参阅 Kubernetes 文档

开始之前:请登录您的账户。 如果适用,请将相应的资源组设定为目标。 设置集群的上下文。

  1. 验证 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
    
  2. 确认 NodeLocal DNS 高速缓存具有 ConfigMap。

    1. 确定 NodeLocal DNS 高速缓存 ConfigMap 是否存在。
        kubectl get cm -n kube-system node-local-dns-config
        ```
        不存在 ConfigMap 时的示例输出
    
        ```sh {: screen}
        Error from server (NotFound): ConfigMaps "node-local-dns-config" not found
        ```
    2. 如果 ConfigMap 不存在,请创建 `NodeLocal` DNS 高速缓存 ConfigMap。
    
    ```sh {: pre}
        kubectl create cm -n kube-system node-local-dns-config
        ```
        示例输出
    
        ```sh {: screen}
        configmap/node-local-dns-config created
        ```
    
  3. 编辑 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"]}
    
  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。

  1. 更新受区域感知 DNS 影响的任何 DNS 出口网络策略,例如依赖于 DNS 出口的 pod 或名称空间选择器的策略。 运行以下命令以获取您的网络策略列表。

    kubectl get networkpolicy --all-namespaces -o yaml
    
  2. 使用 ibm-cloud.kubernetes.io/deploy-zone-aware-dns=truekube-system 名称空间中标注 coredns ConfigMap。

    kubectl label cm -n kube-system coredns --overwrite "ibm-cloud.kubernetes.io/deploy-zone-aware-dns=true"
    
  3. 刷新集群主节点 以部署区域感知 DNS 资源。

    ibmcloud ks cluster master refresh -c CLUSTER_NAME_OR_ID
    
  4. 通过 查看集群详细信息中的 主运行状况 来监视要完成的刷新操作。

    ibmcloud ks cluster get -c CLUSTER_NAME_OR_ID
    
  5. 如果 为 CoreDNS 自定义了存根域和上游 DNS 服务器,则还必须 使用这些存根域和上游 DNS 服务器自定义 NodeLocal DNS 缓存

  6. 为集群的区域设置环境变量。

    ZONES=$(kubectl get nodes --no-headers --ignore-not-found=true -o jsonpath='{range .items[*]}{.metadata.labels.topology\.kubernetes\.io/zone}{"\n"}{end}' | uniq)
    
  7. 在所有专区中启动 CoreDNS 和 CoreDNS 自动缩放器 pod。

    for ZONE in ${ZONES}; do
        kubectl scale deployment -n kube-system "coredns-autoscaler-${ZONE}" --replicas=1
    done
    
  8. 验证 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
    
  9. 在所有工作程序节点上启动 NodeLocal DNS 高速缓存 pod。

    kubectl label nodes --all --overwrite "ibm-cloud.kubernetes.io/zone-aware-dns-enabled=true"
    
  10. 验证 NodeLocal DNS 高速缓存 pod 是否正在所有工作程序节点上运行。

    for ZONE in ${ZONES}; do
        kubectl get pods -n kube-system -l "k8s-app=node-local-dns-${ZONE}" -o wide
    done
    
  11. 标注工作程序池,以便将来的工作程序节点继承 ibm-cloud.kubernetes.io/zone-aware-dns-enabled=true 标签。

禁用和删除区域感知 DNS

要除去专区感知 DNS,必须首先在多专区集群的每个专区中禁用专区感知 DNS。 然后,删除区域感知 DNS 资源。

  1. 从工作程序池中除去 ibm-cloud.kubernetes.io/zone-aware-dns-enabled=true 标签

  2. 为集群中的区域设置环境变量。

    ZONES=$(kubectl get nodes --no-headers --ignore-not-found=true -o jsonpath='{range .items[*]}{.metadata.labels.topology\.kubernetes\.io/zone}{"\n"}{end}' | uniq)
    
  3. 停止所有工作程序节点上的 NodeLocal DNS 高速缓存 pod。

    kubectl label nodes --all --overwrite "ibm-cloud.kubernetes.io/zone-aware-dns-enabled-"
    
  4. 停止所有区域中的 CoreDNS 自动缩放器 pod。

    for ZONE in ${ZONES}; do
        kubectl scale deployment -n kube-system "coredns-autoscaler-${ZONE}" --replicas=0
    done
    
  5. 验证 CoreDNS 自动缩放器 pod 是否不再在所有区域中运行。

    for ZONE in ${ZONES}; do
        kubectl get pods -n kube-system -l "k8s-app=coredns-autoscaler-${ZONE}"
    done
    
  6. 停止所有区域中的 CoreDNS pod。

    for ZONE in ${ZONES}; do
        kubectl scale deployment -n kube-system "coredns-${ZONE}" --replicas=0
    done
    
  7. 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-"
    
  8. 刷新集群主节点 以删除区域感知 DNS 资源。

    ibmcloud ks cluster master refresh --cluster CLUSTER_NAME_OR_ID
    
  9. 通过 查看集群详细信息中的 主运行状况 来监视要完成的刷新操作。

    ibmcloud ks cluster get -c CLUSTER_NAME_OR_ID