08-2. 部署 coredns 插件

如果没有特殊指明,本文档的所有操作均在 zhangjun-k8s-01 节点上执行

下载和配置 coredns

  1. cd /opt/k8s/work
  2. git clone https://github.com/coredns/deployment.git
  3. mv deployment coredns-deployment

创建 coredns

  1. cd /opt/k8s/work/coredns-deployment/kubernetes
  2. source /opt/k8s/bin/environment.sh
  3. ./deploy.sh -i ${CLUSTER_DNS_SVC_IP} -d ${CLUSTER_DNS_DOMAIN} | kubectl apply -f -

检查 coredns 功能

  1. $ kubectl get all -n kube-system -l k8s-app=kube-dns
  2. NAME READY STATUS RESTARTS AGE
  3. pod/coredns-76b74f549-cwm8d 1/1 Running 0 62s
  4. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  5. service/kube-dns ClusterIP 10.254.0.2 <none> 53/UDP,53/TCP,9153/TCP 62s
  6. NAME READY UP-TO-DATE AVAILABLE AGE
  7. deployment.apps/coredns 1/1 1 1 62s
  8. NAME DESIRED CURRENT READY AGE
  9. replicaset.apps/coredns-76b74f549 1 1 1 62s

新建一个 Deployment:

  1. cd /opt/k8s/work
  2. cat > my-nginx.yaml <<EOF
  3. apiVersion: apps/v1
  4. kind: Deployment
  5. metadata:
  6. name: my-nginx
  7. spec:
  8. replicas: 2
  9. selector:
  10. matchLabels:
  11. run: my-nginx
  12. template:
  13. metadata:
  14. labels:
  15. run: my-nginx
  16. spec:
  17. containers:
  18. - name: my-nginx
  19. image: nginx:1.7.9
  20. ports:
  21. - containerPort: 80
  22. EOF
  23. kubectl create -f my-nginx.yaml

export 该 Deployment, 生成 my-nginx 服务:

  1. $ kubectl expose deploy my-nginx
  2. service "my-nginx" exposed
  3. $ kubectl get services my-nginx -o wide
  4. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
  5. my-nginx ClusterIP 10.254.67.218 <none> 80/TCP 5s run=my-nginx

创建另一个 Pod,查看 /etc/resolv.conf 是否包含 kubelet 配置的 --cluster-dns--cluster-domain,是否能够将服务 my-nginx 解析到上面显示的 Cluster IP 10.254.40.167

  1. cd /opt/k8s/work
  2. cat > dnsutils-ds.yml <<EOF
  3. apiVersion: v1
  4. kind: Service
  5. metadata:
  6. name: dnsutils-ds
  7. labels:
  8. app: dnsutils-ds
  9. spec:
  10. type: NodePort
  11. selector:
  12. app: dnsutils-ds
  13. ports:
  14. - name: http
  15. port: 80
  16. targetPort: 80
  17. ---
  18. apiVersion: apps/v1
  19. kind: DaemonSet
  20. metadata:
  21. name: dnsutils-ds
  22. labels:
  23. addonmanager.kubernetes.io/mode: Reconcile
  24. spec:
  25. selector:
  26. matchLabels:
  27. app: dnsutils-ds
  28. template:
  29. metadata:
  30. labels:
  31. app: dnsutils-ds
  32. spec:
  33. containers:
  34. - name: my-dnsutils
  35. image: tutum/dnsutils:latest
  36. command:
  37. - sleep
  38. - "3600"
  39. ports:
  40. - containerPort: 80
  41. EOF
  42. kubectl create -f dnsutils-ds.yml
  1. $ kubectl get pods -lapp=dnsutils-ds -o wide
  2. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  3. dnsutils-ds-7h9np 1/1 Running 0 69s 172.30.244.3 zhangjun-k8s-01 <none> <none>
  4. dnsutils-ds-fthdl 1/1 Running 0 69s 172.30.82.131 zhangjun-k8s-02 <none> <none>
  5. dnsutils-ds-w69zp 1/1 Running 0 69s 172.30.184.132 zhangjun-k8s-03 <none> <none>
  1. $ kubectl -it exec dnsutils-ds-7h9np cat /etc/resolv.conf
  2. search default.svc.cluster.local svc.cluster.local cluster.local 4pd.io
  3. nameserver 10.254.0.2
  4. options ndots:5
  1. $ kubectl -it exec dnsutils-ds-7h9np nslookup kubernetes
  2. Server: 10.254.0.2
  3. Address: 10.254.0.2#53
  4. Name: kubernetes.default.svc.cluster.local
  5. Address: 10.254.0.1
  1. $ kubectl -it exec dnsutils-ds-7h9np nslookup www.baidu.com
  2. Server: 10.254.0.2
  3. Address: 10.254.0.2#53
  4. Non-authoritative answer:
  5. *** Can't find www.baidu.com: No answer
  1. $ kubectl -it exec dnsutils-ds-7h9np nslookup www.baidu.com.
  2. Server: 10.254.0.2
  3. Address: 10.254.0.2#53
  4. Non-authoritative answer:
  5. www.baidu.com canonical name = www.a.shifen.com.
  6. Name: www.a.shifen.com
  7. Address: 220.181.38.150
  8. Name: www.a.shifen.com
  9. Address: 220.181.38.149
  1. $ kubectl -it exec dnsutils-ds-7h9np nslookup my-nginx
  2. Server: 10.254.0.2
  3. Address: 10.254.0.2#53
  4. Name: my-nginx.default.svc.cluster.local
  5. Address: 10.254.67.218

参考

  1. https://community.infoblox.com/t5/Community-Blog/CoreDNS-for-Kubernetes-Service-Discovery/ba-p/8187
  2. https://coredns.io/2017/03/01/coredns-for-kubernetes-service-discovery-take-2/
  3. https://www.cnblogs.com/boshen-hzb/p/7511432.html
  4. https://github.com/kubernetes/kubernetes/tree/master/cluster/addons/dns