09-1.部署 coredns 插件

下载新版本的 kubernetes

  1. mkdir -p /opt/k8s/work/kubernetes-v1.11.2
  2. cd /opt/k8s/work/kubernetes-v1.11.2
  3. wget https://dl.k8s.io/v1.11.2/kubernetes-server-linux-amd64.tar.gz
  4. tar -xzvf kubernetes-server-linux-amd64.tar.gz
  5. cd kubernetes
  6. tar -xzvf kubernetes-src.tar.gz

修改配置文件

coredns 对应的目录是:/opt/k8s/work/kubernetes-v1.11.2/kubernetes/cluster/addons/dns/coredns

  1. source /opt/k8s/bin/environment.sh
  2. cd /opt/k8s/work/kubernetes-v1.11.2/kubernetes/cluster/addons/dns/coredns
  3. cp coredns.yaml.base coredns.yaml
  4. sed -i -e "s/__PILLAR__DNS__DOMAIN__/${CLUSTER_DNS_DOMAIN}/" -e "s/__PILLAR__DNS__SERVER__/${CLUSTER_DNS_SVC_IP}/" coredns.yaml
  • ${CLUSTER_DNS_DOMAIN} 值为末尾不带点号的 cluster.local

创建 coredns

  1. kubectl create -f coredns.yaml

设置副本数为 3,提高可用性

  1. kubectl patch deploy coredns -n kube-system -p '{"spec": {"replicas": 3}}'

检查 coredns 功能

  1. $ kubectl get all -n kube-system
  2. NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
  3. deploy/coredns 1 1 1 1 24s
  4. NAME DESIRED CURRENT READY AGE
  5. rs/coredns-6c64b7d656 1 1 1 24s
  6. NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
  7. deploy/coredns 1 1 1 1 24s
  8. NAME DESIRED CURRENT READY AGE
  9. rs/coredns-6c64b7d656 1 1 1 24s
  10. NAME READY STATUS RESTARTS AGE
  11. po/coredns-6c64b7d656-zgpb8 1/1 Running 0 24s
  12. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  13. svc/kube-dns ClusterIP 10.254.0.2 <none> 53/UDP,53/TCP 24s

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

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

  1. cd /opt/k8s/work
  2. cat > dnsutils-ds.yaml <<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.yaml

等待,直到 dnsutils Pod 处于 Running 状态

  1. $ kubectl get pods |grep dnsutils
  2. dnsutils-ds-58897 1/1 Running 0 28s
  3. dnsutils-ds-jkp9v 1/1 Running 0 28s
  4. dnsutils-ds-mvknd 1/1 Running 0 28s
  1. $ kubectl exec -it dnsutils-ds-58897 cat /etc/resolv.conf
  2. nameserver 10.254.0.2
  3. search default.svc.cluster.local svc.cluster.local cluster.local 4pd.io
  4. options ndots:5
  5. $ kubectl exec -it dnsutils-ds-58897 nslookup kubernetes
  6. Server: 10.254.0.2
  7. Address: 10.254.0.2#53
  8. Name: kubernetes.default.svc.cluster.local
  9. Address: 10.254.0.1
  10. $ kubectl exec -it dnsutils-ds-58897 nslookup my-nginx
  11. Server: 10.254.0.2
  12. Address: 10.254.0.2#53
  13. Name: my-nginx.default.svc.cluster.local
  14. Address: 10.254.0.140
  • 使用 busybox 镜像中的 nslookup 解析域名失败,但是 alpine 和 tutum/dnsutils 可以正常解析,应该是 busybox 的问题;

参考

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