[kubernetes]集群中部署CoreDNS服务

前言

从k8s 1.11版本开始,k8s集群的dns服务由CoreDNS提供。之前已经使用二进制文件部署了一个三master三node的k8s集群,现在需要在集群内部部署DNS服务。

  • 环境信息
IP 说明
192.168.8.21 部署了maser和node
192.168.8.22 部署了master和node
192.168.8.23 部署了master和node
169.169.0.100 集群内虚拟IP,DNS地址

步骤

1. 修改node的kubelet启动参数

修改node上的kubelet启动参数,添加以下两个参数,添加完成后重启kubelet。

  • --cluster-dns=169.169.0.100,这是集群内DNS的服务地址
  • --cluster-domain=cluster.local,为在dns服务中设置的域名

2. 部署CoreDNS服务

  1. 创建ConfigMap。主要设置CoreDNS的主配置文件Corefile的内容,其中可以定义各种域名的解析方式和使用的插件。
apiVersion: v1
kind: ConfigMap
metadata:
  name: coredns
  namespace: kube-system
  labels:
      addonmanager.kubernetes.io/mode: EnsureExists
data:
  Corefile: |
    cluster.local {
        errors
        health {
          lameduck 5s
        }
        ready
        kubernetes cluster.local 169.169.0.0/16 {
          fallthrough in-addr.arpa ip6.arpa
        }
        prometheus :9153
        forward . /etc/resolv.conf
        cache 30
        loop
        reload
        loadbalance
    }
    . {
        cache 30
        loadbalance
        forward . /etc/resolv.conf
    }  
  1. 创建deployment。
apiVersion: apps/v1
kind: Deployment
metadata:
  name: coredns
  namespace: kube-system
  labels:
    k8s-app: kube-dns
    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
    spec:
      priorityClassName: system-cluster-critical
      tolerations:
        - key: "CriticalAddonsOnly"
          operator: "Exists"
      nodeSelector:
        kubernetes.io/os: linux
      affinity:
         podAntiAffinity:
           preferredDuringSchedulingIgnoredDuringExecution:
           - weight: 100
             podAffinityTerm:
               labelSelector:
                 matchExpressions:
                   - key: k8s-app
                     operator: In
                     values: ["kube-dns"]
               topologyKey: kubernetes.io/hostname
      containers:
      - name: coredns
        image: coredns/coredns:1.10.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
        securityContext:
          allowPrivilegeEscalation: false
          capabilities:
            add:
            - NET_BIND_SERVICE
            drop:
            - all
          readOnlyRootFilesystem: true
        livenessProbe:
          httpGet:
            path: /health
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 60
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 5
        readinessProbe:
          httpGet:
            path: /ready
            port: 8181
            scheme: HTTP
      dnsPolicy: Default
      volumes:
        - name: config-volume
          configMap:
            name: coredns
            items:
            - key: Corefile
              path: Corefile
  1. 创建service
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"
    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

3. 验证

使用带有nslookup工具的pod来验证dns服务是否正常(《kubernetes权威指南》中用的busybox,但自己测试不行,所以改成了用ubuntu镜像,pod起来后在容器中安装nslookup。):

# pod-ubuntu.yaml
apiVersion: v1
kind: Pod
metadata:
  name: ubuntu
  namespace: default
spec:
  containers:
  - name: ubuntu
    image: ubuntu:22.04
    command:
      - sleep
      - "3600"
  1. 先创建pod
kubectl create -f pod-ubuntu.yaml
  1. 容器启动后,执行命令测试
apt update
apt install -y dnsutils
nslookup svc-nginx

Corefile配置说明

CoreDNS的主要功能是通过插件系统实现的。常用插件如下:

名称 说明
loadbalance 提供基于DNS的负载均衡功能
loop 检测在DNS解析过程中出现的简单循环问题
cache 提供前端缓存功能
health 对Endpoint进行健康检查
kubernetes 从k8s中读取zone数据
etcd 从etcd中读取数据,可用于自定义域名记录
file 从RFC1035格式文件中读取zone数据
hosts 使用/etc/hosts文件或者其他文件读取zone数据,可用于自定义域名记录
auto 从磁盘中自动加载区域文件
reload 定时自动重新加载Corefile配置文件的内容
forward 转发域名查询到上游DNS服务器上
prometheus 为Prometheus系统提供采集性能指标数据的URL
pprof 在URL路径/debug/pprof下提供运行时的性能数据
log 对DNS查询进行日志记录
error 对错误信息进行日志记录

参考

  • 《kubernetes权威指南》
posted @ 2023-02-19 16:38  花酒锄作田  阅读(316)  评论(0编辑  收藏  举报