カスタム kube-dns Deployment を設定する

このドキュメントでは、デフォルトの GKE マネージド kube-dns を独自のデプロイメントに置き換えて、Google Kubernetes Engine(GKE)Standard クラスタの DNS 設定をカスタマイズする方法について説明します。これにより、クラスタの DNS プロバイダをより細かく制御できます。たとえば、次のようなことを行えます。

  • DNS コンポーネントの CPU とメモリリソースを調整する。
  • 特定の kube-dns イメージ バージョンを使用する。
  • Kubernetes DNS 仕様に準拠した代替 DNS プロバイダ(CoreDNS など)をデプロイする。

このドキュメントは Standard クラスタ専用です。Autopilot クラスタでは、Google が DNS 構成を管理します。GKE の DNS プロバイダの詳細については、サービス ディスカバリについてkube-dns をご覧ください。

注意: カスタム DNS Deployment を実行する場合は、その継続的なメンテナンスはユーザーの責任となります。これには、kube-dns とオートスケーラー コンテナ イメージが最新バージョンとセキュリティ パッチで最新の状態になっていることを確認することが含まれます。最新の推奨イメージを確認するには、GKE クラスタの kube-system Namespace のデフォルトの kube-dns Deployment を調べます。

このドキュメントは、デベロッパー、管理者、アーキテクトなどの GKE ユーザーを対象としています。 Google Cloudの一般的なロールとタスクの例の詳細については、一般的な GKE Enterprise ユーザーのロールとタスクをご覧ください。

このドキュメントは、次の内容を理解していることを前提としています。

カスタム kube-dns Deployment を設定する

このセクションでは、GKE マネージド kube-dns を独自のデプロイメントに置き換える方法について説明します。

カスタム マニフェストを作成してデプロイする

  1. 次の Deployment マニフェストを custom-kube-dns.yaml として保存します。 このマニフェストは kube-dns を使用します。

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: DNS_DEPLOYMENT_NAME
      namespace: kube-system
      labels:
        k8s-app: kube-dns
      annotations:
        deployment.kubernetes.io/revision: "1"
    spec:
      selector:
        matchLabels:
          k8s-app: kube-dns
      strategy:
        rollingUpdate:
          maxSurge: 10%
          maxUnavailable: 0
        type: RollingUpdate
      template:
        metadata:
          creationTimestamp: null
          labels:
            k8s-app: kube-dns
        spec:
          containers:
          - name: kubedns
            image: registry.k8s.io/dns/k8s-dns-kube-dns:1.22.28
            resources:
              limits:
                memory: '170Mi'
              requests:
                cpu: 100m
                memory: '70Mi'
            livenessProbe:
              httpGet:
                path: /healthcheck/kubedns
                port: 10054
                scheme: HTTP
              initialDelaySeconds: 60
              timeoutSeconds: 5
              successThreshold: 1
              failureThreshold: 5
            readinessProbe:
              httpGet:
                path: /readiness
                port: 8081
                scheme: HTTP
              initialDelaySeconds: 3
              timeoutSeconds: 5
            args:
            - --domain=cluster.local.
            - --dns-port=10053
            - --config-dir=/kube-dns-config
            - --v=2
            env:
            - name: PROMETHEUS_PORT
              value: "10055"
            ports:
            - containerPort: 10053
              name: dns-local
              protocol: UDP
            - containerPort: 10053
              name: dns-tcp-local
              protocol: TCP
            - containerPort: 10055
              name: metrics
              protocol: TCP
            volumeMounts:
            - name: kube-dns-config
              mountPath: /kube-dns-config
            securityContext:
              allowPrivilegeEscalation: false
              readOnlyRootFilesystem: true
              runAsUser: 1001
              runAsGroup: 1001
          - name: dnsmasq
            image: registry.k8s.io/dns/k8s-dns-dnsmasq-nanny:1.22.28
            livenessProbe:
              httpGet:
                path: /healthcheck/dnsmasq
                port: 10054
                scheme: HTTP
              initialDelaySeconds: 60
              timeoutSeconds: 5
              successThreshold: 1
              failureThreshold: 5
            args:
            - -v=2
            - -logtostderr
            - -configDir=/etc/k8s/dns/dnsmasq-nanny
            - -restartDnsmasq=true
            - --
            - -k
            - --cache-size=1000
            - --no-negcache
            - --dns-forward-max=1500
            - --log-facility=-
            - --server=/cluster.local/127.0.0.1#10053
            - --server=/in-addr.arpa/127.0.0.1#10053
            - --server=/ip6.arpa/127.0.0.1#10053
            ports:
            - containerPort: 53
              name: dns
              protocol: UDP
            - containerPort: 53
              name: dns-tcp
              protocol: TCP
            resources:
              requests:
                cpu: 150m
                memory: 20Mi
            volumeMounts:
            - name: kube-dns-config
              mountPath: /etc/k8s/dns/dnsmasq-nanny
            securityContext:
              capabilities:
                drop:
                - all
                add:
                - NET_BIND_SERVICE
                - SETGID
          - name: sidecar
            image: registry.k8s.io/dns/k8s-dns-sidecar:1.22.28
            livenessProbe:
              httpGet:
                path: /metrics
                port: 10054
                scheme: HTTP
              initialDelaySeconds: 60
              timeoutSeconds: 5
              successThreshold: 1
              failureThreshold: 5
            args:
            - --v=2
            - --logtostderr
            - --probe=kubedns,127.0.0.1:10053,kubernetes.default.svc.cluster.local,5,SRV
            - --probe=dnsmasq,127.0.0.1:53,kubernetes.default.svc.cluster.local,5,SRV
            ports:
            - containerPort: 10054
              name: metrics
              protocol: TCP
            resources:
              requests:
                memory: 20Mi
                cpu: 10m
            securityContext:
              allowPrivilegeEscalation: false
              readOnlyRootFilesystem: true
              runAsUser: 1001
              runAsGroup: 1001
          dnsPolicy: Default
          restartPolicy: Always
          schedulerName: default-scheduler
          securityContext: {}
          serviceAccount: kube-dns
          serviceAccountName: kube-dns
          terminationGracePeriodSeconds: 30
          tolerations:
          - key: CriticalAddonsOnly
            operator: Exists
          volumes:
          - configMap:
              defaultMode: 420
              name: kube-dns
              optional: true
            name: kube-dns-config
    

    DNS_DEPLOYMENT_NAME は、カスタム DNS Deployment の名前に置き換えます。

  2. マニフェストをクラスタに適用します。

    kubectl create -f custom-kube-dns.yaml
    

GKE マネージド kube-dns をスケールダウンする

競合を回避するには、GKE マネージド kube-dns Deployment と kube-dns-autoscaler Deployment を 0 レプリカにスケーリングして無効にします。

kubectl scale deployment --replicas=0 kube-dns-autoscaler kube-dns --namespace=kube-system

カスタム オートスケーラーを構成する

デフォルトの kube-dns-autoscaler は、GKE マネージド kube-dns Deployment のみをスケーリングします。カスタム DNS プロバイダで自動スケーリングが必要な場合は、個別のオートスケーラーをデプロイし、カスタム DNS Deployment を変更する権限を付与する必要があります。

  1. 次のマニフェストを作成し、custom-dns-autoscaler.yaml として保存します。

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: custom-dns-autoscaler
      namespace: kube-system
    data:
      linear: |-
        {
          "coresPerReplica": 256,
          "nodesPerReplica": 16,
          "preventSinglePointFailure": true
        }
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRoleBinding
    metadata:
      name: system:custom-dns-autoscaler
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: ClusterRole
      name: system:custom-dns-autoscaler
    subjects:
    - kind: ServiceAccount
      name: kube-dns-autoscaler
      namespace: kube-system
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRole
    metadata:
      name: system:custom-dns-autoscaler
    rules:
    - apiGroups:
      - ""
      resources:
      - nodes
      verbs:
      - list
      - watch
    - apiGroups:
      - apps
      resourceNames:
      - DNS_DEPLOYMENT_NAME
      resources:
      - deployments/scale
      verbs:
      - get
      - update
    - apiGroups:
      - ""
      resources:
      - configmaps
      verbs:
      - get
      - create
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: custom-dns-autoscaler
      namespace: kube-system
      labels:
        k8s-app: custom-dns-autoscaler
    spec:
      selector:
        matchLabels:
          k8s-app: custom-dns-autoscaler
      template:
        metadata:
          labels:
            k8s-app: custom-dns-autoscaler
        spec:
          priorityClassName: system-cluster-critical
          securityContext:
            seccompProfile:
              type: RuntimeDefault
            supplementalGroups: [ 65534 ]
            fsGroup: 65534
          nodeSelector:
            kubernetes.io/os: linux
          containers:
          - name: autoscaler
            image: registry.k8s.io/autoscaling/cluster-proportional-autoscaler:1.8.9
            resources:
              requests:
                cpu: "20m"
                memory: "10Mi"
            command:
            - /cluster-proportional-autoscaler
            - --namespace=kube-system
            - --configmap=custom-dns-autoscaler
            - --target=Deployment/DNS_DEPLOYMENT_NAME
            - --default-params={"linear":{"coresPerReplica":256,"nodesPerReplica":16,"preventSinglePointFailure":true}}
            - --logtostderr=true
            - --v=2
          tolerations:
          - key: "CriticalAddonsOnly"
            operator: "Exists"
          serviceAccountName: kube-dns-autoscaler
    

    resourceNames フィールドと command フィールドの DNS_DEPLOYMENT_NAME を、カスタム DNS Deployment の名前に置き換えます。

  2. マニフェストをクラスタに適用します。

    kubectl create -f custom-dns-autoscaler.yaml
    

デプロイを確認する

カスタム DNS Pod が実行されていることを確認します。

kubectl get pods -n kube-system -l k8s-app=kube-dns

GKE マネージド kube-dns Deployment を 0 レプリカにスケーリングしたため、出力にはカスタム Deployment の Pod のみが表示されます。ステータスが Running であることを確認します。

GKE マネージド kube-dns を復元する

カスタム kube-dns 構成をデプロイし、デフォルトの GKE マネージド設定に戻す必要がある場合は、カスタム リソースを削除して、マネージド kube-dns Deployment を再度有効にする必要があります。

GKE マネージド kube-dns を復元する手順は次のとおりです。

  1. カスタム kube-dns Deployment とそのオートスケーラーを削除します。マニフェストを custom-kube-dns.yamlcustom-dns-autoscaler.yaml として保存した場合は、次のコマンドを実行してリソースを削除します。

    kubectl delete -f custom-dns-autoscaler.yaml
    kubectl delete -f custom-kube-dns.yaml
    

    マニフェストを保存していない場合は、カスタム デプロイ用に作成した Deployment、ClusterRole、ClusterRoleBinding を手動で削除します。

  2. GKE マネージド kube-dns-autoscaler を復元します。次のコマンドを実行して、kube-dns-autoscaler Deployment を 1 つのレプリカにスケールバックします。

    kubectl scale deployment --replicas=1 kube-dns-autoscaler --namespace=kube-system
    

    このコマンドは、マネージド kube-dns-autoscaler を再度有効にします。これにより、マネージド kube-dns Deployment がクラスタのサイズに適した数のレプリカに自動的にスケーリングされます。

  3. 復元を確認します。

    kube-dns Pod と kube-dns-autoscaler Pod をチェックして、正しく実行されていることを確認します。

    kubectl get pods -n kube-system -l k8s-app=kube-dns
    

    出力には、GKE マネージド kube-dns Pod が Running 状態であることが示されます。

次のステップ