1.6 DNS 服务

目前常用的 dns 组件有 kube-dns 和 coredns 两个,用于解析 k8s 集群中 service name 所对应得到 IP 地址。

早期版本有 skyDNS(已经废弃),后来被 kube-dns 取代,在1.18.0版本以后已经废弃,现在使用 coredns。

在kubeasz项目中准备目录

[root@k8s-master1 ~]# cd /etc/kubeasz/
[root@k8s-master1 /etc/kubeasz]# mkdir manifests/dns/{kube-dns,coredns} -pv
mkdir: created directory 'manifests/dns'
mkdir: created directory 'manifests/dns/kube-dns'
mkdir: created directory 'manifests/dns/coredns'
[root@k8s-master1 /etc/kubeasz]# 

1.6.1 部署 coredns

github 项目地址:

https://github.com/coredns/coredns  # 项目地址
https://github.com/coredns/deployment   # 克隆项目
https://github.com/coredns/deployment/tree/master/kubernetes    # 部署教程

部署 coredns

# 准备文件
[root@k8s-master1 /etc/kubeasz]# cd manifests/dns/coredns/
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# ll
total 3912
drwxr-xr-x 2 root root    4096 Nov 14 15:04 ./
drwxr-xr-x 4 root root    4096 Nov 14 15:04 ../
-rw-r--r-- 1 root root 3983872 Feb 26  2021 busybox-online.tar.gz
-rw-r--r-- 1 root root     286 Nov  9 22:47 busybox.yaml
-rw-r--r-- 1 root root    4542 Nov  9 20:52 waluna-coredns.yml
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# 

# 查看镜像
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# grep image waluna-coredns.yml
        #image: coredns/coredns:1.8.6
        image: harbor.waluna.top/baseimages/coredns:v1.8.6
        imagePullPolicy: IfNotPresent
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# 

# 拉取镜像并打标签上传至本地harbor
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# docker pull coredns/coredns:1.8.6
1.8.6: Pulling from coredns/coredns
d92bdee79785: Pull complete 
6e1b7c06e42d: Pull complete 
Digest: sha256:5b6ec0d6de9baaf3e92d0f66cd96a25b9edbce8716f5f15dcd1a616b3abd590e
Status: Downloaded newer image for coredns/coredns:1.8.6
docker.io/coredns/coredns:1.8.6
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# docker tag coredns/coredns:1.8.6 harbor.waluna.top/baseimages/coredns:v1.8.6
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# docker push harbor.waluna.top/baseimages/coredns:v1.8.6
The push refers to repository [harbor.waluna.top/baseimages/coredns]
80e4a2390030: Pushed 
256bc5c338a6: Pushed 
v1.8.6: digest: sha256:8916c89e1538ea3941b58847e448a2c6d940c01b8e716b20423d2d8b189d3972 size: 739
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# 

# 修改镜像地址
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# vim waluna.coredns.yml
......
      containers:
      - name: coredns
        #image: coredns/coredns:1.8.6
        image: harbor.waluna.top/baseimages/coredns:v1.8.6
......

# 安装coredns
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# kubectl apply -f waluna-coredns.yml
serviceaccount/coredns created
clusterrole.rbac.authorization.k8s.io/system:coredns created
clusterrolebinding.rbac.authorization.k8s.io/system:coredns created
configmap/coredns created
deployment.apps/coredns created
service/kube-dns created
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# 

# 查看pod
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# kubectl get pod -A|grep coredns
kube-system            coredns-597578ff46-x2s95                     1/1     Running   0          10s
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# 

# 验证dns
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# kubectl exec net-test1 -- ping baidu.com -c1 
PING baidu.com (220.181.38.148): 56 data bytes
64 bytes from 220.181.38.148: seq=0 ttl=127 time=18.889 ms

--- baidu.com ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 18.889/18.889/18.889 ms
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# 

1.6.2 域名解析测试

下面使用 busybox 进行测试

# 准备busybox镜像
[root@k8s-master1 ~]# cd /etc/kubeasz/manifests/dns/coredns 
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# ll busybox-online.tar.gz
-rw-r--r-- 1 root root 3983872 Feb 26  2021 busybox-online.tar.gz
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# docker load -i busybox-online.tar.gz
6a749002dd6a: Loading layer [==================================================>]  1.338MB/1.338MB
5f70bf18a086: Loading layer [==================================================>]  1.024kB/1.024kB
1692ded805c8: Loading layer [==================================================>]  2.629MB/2.629MB
Loaded image: quay.io/prometheus/busybox:latest
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# docker tag quay.io/prometheus/busybox:latest harbor.waluna.top/baseimages/busybox:latest
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# docker push harbor.waluna.top/baseimages/busybox:latest
The push refers to repository [harbor.waluna.top/baseimages/busybox]
1692ded805c8: Pushed 
5f70bf18a086: Pushed 
6a749002dd6a: Pushed 
latest: digest: sha256:51ffe0b73b49d0e7045f7037fb9f8360604121533d90881c08ad3a21a5e18843 size: 943
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# 

# 创建pod
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# kubectl apply -f busybox.yaml
pod/busybox createdpod/busybox created
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# 

# 域名测试
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# kubectl get svc -A
NAMESPACE              NAME                        TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                  AGE
default                kubernetes                  ClusterIP   10.20.0.1       <none>        443/TCP                  19m
kube-system            kube-dns                    ClusterIP   10.20.0.2       <none>        53/UDP,53/TCP,9153/TCP   7m53s
kubernetes-dashboard   dashboard-metrics-scraper   ClusterIP   10.20.164.123   <none>        8000/TCP                 11m
kubernetes-dashboard   kubernetes-dashboard        NodePort    10.20.242.83    <none>        443:30002/TCP            11m
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# kubectl get pod -A
NAMESPACE              NAME                                         READY   STATUS    RESTARTS   AGE
default                busybox                                      1/1     Running   0          66s
default                net-test1                                    1/1     Running   1          15m
default                net-test2                                    1/1     Running   1          15m
default                net-test3                                    1/1     Running   1          15m
default                net-test4                                    1/1     Running   1          15m
default                net-test5                                    1/1     Running   1          15m
default                net-test6                                    1/1     Running   1          15m
kube-system            coredns-597578ff46-x2s95                     1/1     Running   1          8m41s
kube-system            kube-flannel-ds-amd64-2np97                  1/1     Running   1          17m
kube-system            kube-flannel-ds-amd64-45brr                  1/1     Running   1          17m
kube-system            kube-flannel-ds-amd64-7gglf                  1/1     Running   1          17m
kube-system            kube-flannel-ds-amd64-j2wf9                  1/1     Running   1          17m
kube-system            kube-flannel-ds-amd64-k4bcl                  1/1     Running   1          17m
kube-system            kube-flannel-ds-amd64-s8rkq                  1/1     Running   1          17m
kubernetes-dashboard   dashboard-metrics-scraper-5b8df6cd57-jmznl   1/1     Running   1          12m
kubernetes-dashboard   kubernetes-dashboard-56dc757f5f-8sv8d        1/1     Running   2          12m
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# 

# 重启机器dns不通的话需要删除pod重建就好了
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# kubectl delete pod coredns-597578ff46-x2s95 -n kube-system 
pod "coredns-597578ff46-x2s95" deleted
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# kubectl exec busybox nslookup baidu.com
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
Server:    10.20.0.2
Address 1: 10.20.0.2 kube-dns.kube-system.svc.waluna.local

Name:      baidu.com
Address 1: 220.181.38.148
Address 2: 220.181.38.251
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# kubectl exec busybox nslookup kubernetes
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
Server:    10.20.0.2
Address 1: 10.20.0.2 kube-dns.kube-system.svc.waluna.local

Name:      kubernetes
Address 1: 10.20.0.1 kubernetes.default.svc.waluna.local
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# kubectl exec busybox nslookup kubernetes.default.svc.waluna.local
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
Server:    10.20.0.2
Address 1: 10.20.0.2 kube-dns.kube-system.svc.waluna.local

Name:      kubernetes.default.svc.waluna.local
Address 1: 10.20.0.1 kubernetes.default.svc.waluna.local
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# kubectl exec busybox nslookup kubernetes.default.svc.waluna.local
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
Server:    10.20.0.2
Address 1: 10.20.0.2 kube-dns.kube-system.svc.waluna.local

Name:      kubernetes.default.svc.waluna.local
Address 1: 10.20.0.1 kubernetes.default.svc.waluna.local
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# kubectl exec busybox nslookup kube-dns.kube-system.svc.waluna.local
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
Server:    10.20.0.2
Address 1: 10.20.0.2 kube-dns.kube-system.svc.waluna.local

Name:      kube-dns.kube-system.svc.waluna.local
Address 1: 10.20.0.2 kube-dns.kube-system.svc.waluna.local
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# kubectl exec busybox nslookup dashboard-metrics-scraper.kubernetes-dashboard.svc.waluna.local
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
Server:    10.20.0.2
Address 1: 10.20.0.2 kube-dns.kube-system.svc.waluna.local

Name:      dashboard-metrics-scraper.kubernetes-dashboard.svc.waluna.local
Address 1: 10.20.164.123 dashboard-metrics-scraper.kubernetes-dashboard.svc.waluna.local
[root@k8s-master1 /etc/kubeasz/manifests/dns/coredns]# 

打包 kubeasz 项目,方便后期使用

[root@k8s-master1 /etc/kubeasz]# cd /etc/
[root@k8s-master1 /etc]# tar czvf kubeasz-v3.1.1.tar.gz kubeasz/

[root@k8s-master1 /etc]# sz kubeasz-v3.1.1.tar.gz