You need to enable JavaScript to run this app.
导航
配置边缘 CoreDNS
最近更新时间:2024.08.06 16:58:19首次发布时间:2023.08.25 18:49:41

CoreDNS 通常部署在中心节点。在边缘托管服务下,边缘节点与管控面机器网络不通,导致 Pod 内 DNS 无法使用。本文内容将指导您配置边缘节点的 CoreDNS。

前提条件

已通过 kubectl 连接 Kubernetes 集群。

操作步骤

  1. 获取当前的 Service IP 的 CIDR,并手动为 CoreDNS 分配一个没用过的 IP;
  2. 在边缘节点部署一套 CoreDNS;
  3. 修改对应机器上的 Kubelet 的配置,将 ClusterDNS 修改为步骤 1 中创建的 Service IP;
  4. 重启 Kubelet。

步骤详情

  1. 获取当前的 Service IP 的 CIDR,手动为 CoreDNS 分配一个 Cluster IP:

可以通过以下命令获取到当前的 Service 的 CIDR。k8s 默认的 CoreDNS 使用的是第 10 个,我们可以使用第 11 个。

kubectl get cm -n kube-system kubeadm-config -o yaml | grep serviceSubnet

例如:Service 的 CIDR 为 172.25.***.***/17 区间范围, 我们可以选择 172.25.***.11 作为边缘 DNS Service 的 IP。

  1. 在边缘节点部署一套 CoreDNS:
CLUSTER_DNS_IP="172.25.***.11" # 边缘 DNS Service IP
kubectl apply -f - <<EOF
apiVersion: v1
kind: Service
metadata:
  labels:
    k8s-app: kube-dns-edge
    kubernetes.io/cluster-service: "true"
    kubernetes.io/name: KubeDNS
  name: kube-dns-edge
  namespace: kube-system
spec:
  clusterIP: ${CLUSTER_DNS_IP}
  ports:
  - name: dns
    port: 53
    protocol: UDP
    targetPort: 53
  - name: dns-tcp
    port: 53
    protocol: TCP
    targetPort: 53
  - name: metrics
    port: 9153
    protocol: TCP
    targetPort: 9153
  selector:
    k8s-app: kube-dns-edge
  sessionAffinity: None
  internalTrafficPolicy: Local
  type: ClusterIP
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: coredns
  namespace: kube-system
  labels:
    k8s-app: kube-dns-edge
spec:
  selector:
    matchLabels:
      k8s-app: kube-dns-edge
  template:
    metadata:
      labels:
        k8s-app: kube-dns-edge
    spec:
      volumes:
        - name: config-volume
          configMap:
            name: coredns
            items:
              - key: Corefile
                path: Corefile
            defaultMode: 420
      containers:
        - name: coredns
          image: $(kubectl get deployment -n kube-system coredns -o jsonpath="{$.spec.template.spec.containers[0].image}")
          args:
            - '-conf'
            - /etc/coredns/Corefile
          ports:
            - name: dns
              containerPort: 53
              protocol: UDP
            - name: dns-tcp
              containerPort: 53
              protocol: TCP
            - name: metrics
              containerPort: 9153
              protocol: TCP
          resources:
            limits:
              memory: 2000Mi
            requests:
              cpu: 100m
              memory: 200Mi
          volumeMounts:
            - name: config-volume
              readOnly: true
              mountPath: /etc/coredns
          livenessProbe:
            httpGet:
              path: /health
              port: 8080
              scheme: HTTP
            initialDelaySeconds: 60
            timeoutSeconds: 5
            periodSeconds: 10
            successThreshold: 1
            failureThreshold: 5
          readinessProbe:
            httpGet:
              path: /ready
              port: 8181
              scheme: HTTP
            timeoutSeconds: 1
            periodSeconds: 10
            successThreshold: 1
            failureThreshold: 3
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: IfNotPresent
          securityContext:
            capabilities:
              add:
                - NET_BIND_SERVICE
              drop:
                - all
            readOnlyRootFilesystem: true
            allowPrivilegeEscalation: false
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      dnsPolicy: Default
      nodeSelector:
        node.kubernetes.io/instance-type: edge-node
      serviceAccountName: coredns
      serviceAccount: coredns
      securityContext: {}
      schedulerName: default-scheduler
      tolerations:
        - key: CriticalAddonsOnly
          operator: Exists
        - key: node-role.kubernetes.io/master
          effect: NoSchedule
        - key: node-role.kubernetes.io/control-plane
          effect: NoSchedule
        - key: vei.bytedance.com/edge-node
          operator: Exists
          effect: NoSchedule
  updateStrategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
  revisionHistoryLimit: 10
EOF
  1. 修改 var/lib/kubelet/config.yaml 配置,将 ClusterDNS 改成上面相应的边缘 CoreDNS 的 Service IP。
  2. 重启 Kubelet:
sudo systemctl restart kubelet