09-1.部署 coredns 插件

注意:

  1. 如果没有特殊指明,本文档的所有操作均在 zhangjun-k8s01 节点上执行
  2. kuberntes 自带插件的 manifests yaml 文件使用 gcr.io 的 docker registry,国内被墙,需要手动替换为其它 registry 地址(本文档未替换);
  3. 可以从微软中国提供的 gcr.io 免费代理下载被墙的镜像;

修改配置文件

将下载的 kubernetes-server-linux-amd64.tar.gz 解压后,再解压其中的 kubernetes-src.tar.gz 文件。

  1. cd /opt/k8s/work/kubernetes/
  2. tar -xzvf kubernetes-src.tar.gz

coredns 目录是 cluster/addons/dns

  1. cd /opt/k8s/work/kubernetes/cluster/addons/dns/coredns
  2. cp coredns.yaml.base coredns.yaml
  3. source /opt/k8s/bin/environment.sh
  4. sed -i -e "s/__PILLAR__DNS__DOMAIN__/${CLUSTER_DNS_DOMAIN}/" -e "s/__PILLAR__DNS__SERVER__/${CLUSTER_DNS_SVC_IP}/" coredns.yaml

创建 coredns

  1. kubectl create -f coredns.yaml

检查 coredns 功能

  1. $ kubectl get all -n kube-system
  2. NAME READY STATUS RESTARTS AGE
  3. pod/coredns-5b969f4c88-jb6jg 1/1 Running 0 61m
  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 61m
  6. NAME READY UP-TO-DATE AVAILABLE AGE
  7. deployment.apps/coredns 1/1 1 1 61m
  8. NAME DESIRED CURRENT READY AGE
  9. replicaset.apps/coredns-5b969f4c88 1 1 1 61m

新建一个 Deployment

  1. cd /opt/k8s/work
  2. cat > my-nginx.yaml <<EOF
  3. apiVersion: extensions/v1beta1
  4. kind: Deployment
  5. metadata:
  6. name: my-nginx
  7. spec:
  8. replicas: 2
  9. template:
  10. metadata:
  11. labels:
  12. run: my-nginx
  13. spec:
  14. containers:
  15. - name: my-nginx
  16. image: nginx:1.7.9
  17. ports:
  18. - containerPort: 80
  19. EOF
  20. 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 --all-namespaces |grep my-nginx
  4. default my-nginx ClusterIP 10.254.38.37 <none> 80/TCP 7s

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

  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: extensions/v1beta1
  19. kind: DaemonSet
  20. metadata:
  21. name: dnsutils-ds
  22. labels:
  23. addonmanager.kubernetes.io/mode: Reconcile
  24. spec:
  25. template:
  26. metadata:
  27. labels:
  28. app: dnsutils-ds
  29. spec:
  30. containers:
  31. - name: my-dnsutils
  32. image: tutum/dnsutils:latest
  33. command:
  34. - sleep
  35. - "3600"
  36. ports:
  37. - containerPort: 80
  38. EOF
  39. kubectl create -f dnsutils-ds.yml
  1. $ kubectl get pods -lapp=dnsutils-ds
  2. NAME READY STATUS RESTARTS AGE
  3. dnsutils-ds-57xqd 1/1 Running 0 101s
  4. dnsutils-ds-5fdmb 1/1 Running 0 101s
  5. dnsutils-ds-9twwf 1/1 Running 0 101s
  1. $ kubectl -it exec dnsutils-ds-57xqd bash
  2. root@dnsutils-ds-57xqd:/# cat /etc/resolv.conf
  3. nameserver 10.254.0.2
  4. search default.svc.cluster.local svc.cluster.local cluster.local onecloud.local
  5. options ndots:5
  1. $ kubectl exec dnsutils-ds-57xqd 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 exec dnsutils-ds-57xqd 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: 61.135.169.121
  8. Name: www.a.shifen.com
  9. Address: 61.135.169.125
  1. $ kubectl exec dnsutils-ds-57xqd 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.38.37
  1. $ kubectl exec dnsutils-ds-57xqd nslookup kube-dns.kube-system.svc.cluster
  2. Server: 10.254.0.2
  3. Address: 10.254.0.2#53
  4. ** server can't find kube-dns.kube-system.svc.cluster: SERVFAIL
  5. command terminated with exit code 1
  1. $ kubectl exec dnsutils-ds-57xqd nslookup kube-dns.kube-system.svc
  2. Server: 10.254.0.2
  3. Address: 10.254.0.2#53
  4. Name: kube-dns.kube-system.svc.cluster.local
  5. Address: 10.254.0.2
  1. $ kubectl exec dnsutils-ds-57xqd nslookup kube-dns.kube-system.svc.cluster.local
  2. Server: 10.254.0.2
  3. Address: 10.254.0.2#53
  4. Name: kube-dns.kube-system.svc.cluster.local
  5. Address: 10.254.0.2
  1. $ kubectl exec dnsutils-ds-57xqd nslookup kube-dns.kube-system.svc.cluster.local.
  2. Server: 10.254.0.2
  3. Address: 10.254.0.2#53
  4. Name: kube-dns.kube-system.svc.cluster.local
  5. Address: 10.254.0.2

参考

  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