K8S DNS服务搭建和配置--CoreDNS应用

DNS服务在k8s发展过程中经历了3个阶段

  1. 在k8s 1.2版本时,DNS服务由SkyDNS提供

  2. 在k8s 1.4版本时,SkyDNS组件被KubeDNS替换

  3. 从k8s 1.11版本开始,K8S集群的DNS服务由CoreDNS提供。是用Go语言实现的高性能、插件式、易扩展的DNS服务端。

CoreDNS支持自定义DNS记录及配置upstream DNS Server,可以统一管理K8S基于服务的内部DNS和数据中心的物理DNS。

在创建DNS服务之前修改每个Node上 kubelet 的启动参数,加上以下两个参数:

    DNS服务的ClusterIP地址:--cluster-dns=169.169.0.100

    在DNS服务中设置的域名:--cluster-domain=cluster.local

然后重启kubelet服务


创建CoreDNS应用


在部署CoreDNS应用前,至少需要创建一个ConfigMap、一个Deployment和一个Service 共三个资源对象。

在启用了RBAC的集群中,还可以设置ServiceAccount、ClusterRole、ClusterRoleBinding 对CoreDNS容器进行权限设置。

ConfigMap“”coredns“”主要设置CoreDNS的主配置文件Corefile的内容。

其中可以定义各种域名的解析方式和使用的插件。

Deployment“”coredns“”主要设置CoreDNS容器应用的内容。

其中,replicas副本的数量通常应该根据集群的规模和服务数量来确定,如果单个CoreDNS进程不足以支撑整个集群的DNS查询,则可以通过水平扩展提高查询能力。由于DNS服务是K8S集群的关键核心服务,所以建议为其Deployment设置自动扩缩容控制器,自动管理其副本数量。另外,对资源限制部分(CPU限制和内存限制)的设置也应根据实际环境进行调整。

Service“”kube-dns“”是DNS服务的配置。

这个服务需要设置固定的 ClusterIP,也需要将所有node上的 kubelet启动参数 --cluster-dns设置为这个ClusterIP。


vim  coredns.yaml

apiVersion: v1

kind: ConfigMap

metadata:

  name: coredns

  namespace: kube-system

  labels:

      addonmanager.kubernetes.io/mode: EnsureExists

data:

  Corefile: |

    cluster.local {

        errors

        health

        kubernetes cluster.local in-addr.arpa ip6.arpa {

            pods insecure

            upstream

            fallthrough in-addr.arpa ip6.arpa

        }

        prometheus :9153

        forward . /etc/resolv.conf

        cache 30

        loop

        reload

        loadbalance

    }

    . {

        cache 30

        loadbalance

        forward . /etc/resolv.conf

    }

---

apiVersion: apps/v1

kind: Deployment

metadata:

  name: coredns

  namespace: kube-system

  labels:

    k8s-app: kube-dns

    kubernetes.io/cluster-service: "true"

    addonmanager.kubernetes.io/mode: Reconcile

    kubernetes.io/name: "CoreDNS"

spec:

  replicas: 1

  strategy:

    type: RollingUpdate

    rollingUpdate:

      maxUnavailable: 1

  selector:

    matchLabels:

      k8s-app: kube-dns

  template:

    metadata:

      labels:

        k8s-app: kube-dns

      annotations:

        seccomp.security.alpha.kubernetes.io/pod: 'docker/default'

    spec:

      priorityClassName: system-cluster-critical

      tolerations:

        - key: "CriticalAddonsOnly"

          operator: "Exists"

      nodeSelector:

        beta.kubernetes.io/os: linux

      containers:

      - name: coredns

        image: coredns/coredns:1.3.1

        imagePullPolicy: IfNotPresent

        resources:

          limits:

            memory: 170Mi

          requests:

            cpu: 100m

            memory: 70Mi

        args: [ "-conf", "/etc/coredns/Corefile" ]

        volumeMounts:

        - name: config-volume

          mountPath: /etc/coredns

          readOnly: true

        ports:

        - containerPort: 53

          name: dns

          protocol: UDP

        - containerPort: 53

          name: dns-tcp

          protocol: TCP

        - containerPort: 9153

          name: metrics

          protocol: TCP

        livenessProbe:

          httpGet:

            path: /health

            port: 8080

            scheme: HTTP

          initialDelaySeconds: 60

          timeoutSeconds: 5

          successThreshold: 1

          failureThreshold: 5

        securityContext:

          allowPrivilegeEscalation: false

          capabilities:

            add:

            - NET_BIND_SERVICE

            drop:

            - all

          readOnlyRootFilesystem: true

      dnsPolicy: Default

      volumes:

        - name: config-volume

          configMap:

            name: coredns

            items:

            - key: Corefile

              path: Corefile

---

apiVersion: v1

kind: Service

metadata:

  name: kube-dns

  namespace: kube-system

  annotations:

    prometheus.io/port: "9153"

    prometheus.io/scrape: "true"

  labels:

    k8s-app: kube-dns

    kubernetes.io/cluster-service: "true"

    addonmanager.kubernetes.io/mode: Reconcile

    kubernetes.io/name: "CoreDNS"

spec:

  selector:

    k8s-app: kube-dns

  clusterIP: 169.169.0.100

  ports:

  - name: dns

    port: 53

    protocol: UDP

  - name: dns-tcp

    port: 53

    protocol: TCP

  - name: metrics

    port: 9153

    protocol: TCP



通过 kubectl create 完成CoreDNS服务的创建:


kubectl create -f coredns.yaml


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章