1.coredns介绍

coredns是go编写的插件是dns,在k8s1.13后内置的默认dns。 github

2.coredns安装

容器安装 yaml来自

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
apiVersion: v1
kind: ConfigMap
metadata:
  name: coredns
  namespace: tool
data:
  Corefile: |
    .:53 {
        log
        errors
        hosts {
          8.8.8.9 TestCoreDNS.com
          8.8.8.10 a.com
          fallthrough
        }
        health {
          lameduck 5s
        }
        prometheus :9153
        forward . 114.114.114.114 8.8.8.8
        cache 30
        loop
        reload
        loadbalance
    }
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: coredns
  namespace: tool
  labels:
    k8s-app: coredns
    kubernetes.io/name: "CoreDNS"
    app.kubernetes.io/name: coredns
spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
  selector:
    matchLabels:
      k8s-app: coredns
      app.kubernetes.io/name: coredns
  template:
    metadata:
      labels:
        k8s-app: coredns
        app.kubernetes.io/name: coredns
    spec:
      containers:
      - name: coredns
        image: k8s.gcr.io/coredns:1.7.0
        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
      dnsPolicy: Default
      volumes:
        - name: config-volume
          configMap:
            name: coredns
            items:
            - key: Corefile
              path: Corefile
---
apiVersion: v1
kind: Service
metadata:
  name: coredns
  namespace: tool
  annotations:
    prometheus.io/port: "9153"
    prometheus.io/scrape: "true"
  labels:
    k8s-app: coredns
    kubernetes.io/cluster-service: "true"
    kubernetes.io/name: "CoreDNS"
    app.kubernetes.io/name: coredns
spec:
  selector:
    k8s-app: coredns
    app.kubernetes.io/name: coredns
  ports:
  - name: dns
    port: 53
    protocol: UDP
  - name: dns-tcp
    port: 53
    protocol: TCP
  - name: metrics
    port: 9153
    protocol: TCP

3.coredns配置

  • errors:错误信息到标准输出
  • health:自身健康状态报告
  • ready:可读性检查
  • kubernetes:提供集群内服务解析能力
  • prometheus:开启监控
  • forward:当查询域名不在本配置中请求转发到指定的dns服务器
  • cache:dns缓存
  • loop:环路检测
  • reload:自动重新加载corefile
  • loadbalance:循环dns负载均衡器

4.验证

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
[root@node1 ~]# dig @10.233.92.7 a.com		# hosts

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.10 <<>> @10.233.92.7 a.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 25381
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;a.com.				IN	A

;; ANSWER SECTION:
a.com.			30	IN	A	8.8.8.10

;; Query time: 0 msec
;; SERVER: 10.233.92.7#53(10.233.92.7)
;; WHEN: 三 12月 07 15:57:39 CST 2022
;; MSG SIZE  rcvd: 55

[root@node1 ~]# dig @10.233.92.7 qq.com		# 外网

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.10 <<>> @10.233.92.7 qq.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 30478
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;qq.com.				IN	A

;; ANSWER SECTION:
qq.com.			30	IN	A	123.151.137.18
qq.com.			30	IN	A	61.129.7.47
qq.com.			30	IN	A	183.3.226.35

;; Query time: 13 msec
;; SERVER: 10.233.92.7#53(10.233.92.7)
;; WHEN: 三 12月 07 15:57:44 CST 2022
;; MSG SIZE  rcvd: 101