09-1.部署 coredns 插件

注意:如果没有特殊指明,本文档的所有操作均在 m7-autocv-gpu01 节点上执行

修改配置文件

将下载的 kubernetes-server-linux-amd64.tar.gz 解压后,再解压其中的 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-77c989547b-6l6jr 1/1 Running 0 3m
  4. pod/coredns-77c989547b-d9lts 1/1 Running 0 3m
  5. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  6. service/coredns ClusterIP 10.254.0.2 <none> 53/UDP,53/TCP 3m
  7. NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
  8. deployment.apps/coredns 2 2 2 2 3m
  9. NAME DESIRED CURRENT READY AGE
  10. replicaset.apps/coredns-77c989547b 2 2 2 3m

新建一个 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.92.83 <none> 80/TCP 6s

创建另一个 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 exec dnsutils-ds-c8kcw 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
  6. $ kubectl exec dnsutils-ds-c8kcw nslookup www.baidu.com # 解析外部域名时,需要以 . 结尾
  7. Server: 10.254.0.2
  8. Address: 10.254.0.2#53
  9. Non-authoritative answer:
  10. *** Can't find www.baidu.com: No answer
  11. $ kubectl exec dnsutils-ds-c8kcw nslookup www.baidu.com.
  12. Server: 10.254.0.2
  13. Address: 10.254.0.2#53
  14. Non-authoritative answer:
  15. www.baidu.com canonical name = www.a.shifen.com.
  16. Name: www.a.shifen.com
  17. Address: 61.135.169.125
  18. Name: www.a.shifen.com
  19. Address: 61.135.169.121
  20. $ kubectl exec dnsutils-ds-c8kcw nslookup my-nginx
  21. Server: 10.254.0.2
  22. Address: 10.254.0.2#53
  23. Name: my-nginx.default.svc.cluster.local
  24. Address: 10.254.229.163
  25. $ kubectl exec dnsutils-ds-c8kcw nslookup kube-dns.kube-system.svc.cluster
  26. Server: 10.254.0.2
  27. Address: 10.254.0.2#53
  28. Non-authoritative answer:
  29. *** Can't find kube-dns.kube-system.svc.cluster: No answer
  30. $ kubectl exec dnsutils-ds-c8kcw nslookup kube-dns.kube-system.svc
  31. Server: 10.254.0.2
  32. Address: 10.254.0.2#53
  33. Name: kube-dns.kube-system.svc.cluster.local
  34. Address: 10.254.0.2
  35. $ kubectl exec dnsutils-ds-c8kcw nslookup kube-dns.kube-system.svc.cluster.local
  36. Server: 10.254.0.2
  37. Address: 10.254.0.2#53
  38. Non-authoritative answer:
  39. *** Can't find kube-dns.kube-system.svc.cluster.local: No answer
  40. $ kubectl exec dnsutils-ds-c8kcw nslookup kube-dns.kube-system.svc.cluster.local.
  41. Server: 10.254.0.2
  42. Address: 10.254.0.2#53
  43. Name: kube-dns.kube-system.svc.cluster.local
  44. 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