09-1.部署 coredns 插件

修改配置文件

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

coredns 对应的目录是:cluster/addons/dns

  1. $ pwd
  2. /opt/k8s/kubernetes/cluster/addons/dns
  3. $ cp coredns.yaml.base coredns.yaml
  4. $ diff coredns.yaml.base coredns.yaml
  5. 61c61
  6. < kubernetes __PILLAR__DNS__DOMAIN__ in-addr.arpa ip6.arpa {
  7. ---
  8. > kubernetes cluster.local. in-addr.arpa ip6.arpa {
  9. 153c153
  10. < clusterIP: __PILLAR__DNS__SERVER__
  11. ---
  12. > clusterIP: 10.254.0.2

创建 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. $ cat > my-nginx.yaml <<EOF
  2. apiVersion: extensions/v1beta1
  3. kind: Deployment
  4. metadata:
  5. name: my-nginx
  6. spec:
  7. replicas: 2
  8. template:
  9. metadata:
  10. labels:
  11. run: my-nginx
  12. spec:
  13. containers:
  14. - name: my-nginx
  15. image: nginx:1.7.9
  16. ports:
  17. - containerPort: 80
  18. EOF
  19. $ 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.242.255 <none> 80/TCP 9s

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

  1. $ cat > pod-nginx.yaml <<EOF
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5. name: nginx
  6. spec:
  7. containers:
  8. - name: nginx
  9. image: nginx:1.7.9
  10. ports:
  11. - containerPort: 80
  12. EOF
  13. $ kubectl create -f pod-nginx.yaml
  14. $ kubectl exec nginx -i -t -- /bin/bash
  15. root@nginx:/# cat /etc/resolv.conf
  16. nameserver 10.254.0.2
  17. search default.svc.cluster.local. svc.cluster.local. cluster.local. 4pd.io
  18. options ndots:5
  19. root@nginx:/# ping my-nginx
  20. PING my-nginx.default.svc.cluster.local (10.254.242.255): 48 data bytes
  21. 56 bytes from 10.254.242.255: icmp_seq=0 ttl=64 time=0.115 ms
  22. ^C--- my-nginx.default.svc.cluster.local ping statistics ---
  23. 1 packets transmitted, 1 packets received, 0% packet loss
  24. round-trip min/avg/max/stddev = 0.115/0.115/0.115/0.000 ms
  25. root@nginx:/# ping my-nginx
  26. PING my-nginx.default.svc.cluster.local (10.254.63.136): 48 data bytes
  27. ^C--- my-nginx.default.svc.cluster.local ping statistics ---
  28. 4 packets transmitted, 0 packets received, 100% packet loss
  29. root@nginx:/# ping kubernetes
  30. PING kubernetes.default.svc.cluster.local (10.254.0.1): 48 data bytes
  31. 56 bytes from 10.254.0.1: icmp_seq=0 ttl=64 time=0.097 ms
  32. 56 bytes from 10.254.0.1: icmp_seq=1 ttl=64 time=0.123 ms
  33. ^C--- kubernetes.default.svc.cluster.local ping statistics ---
  34. 2 packets transmitted, 2 packets received, 0% packet loss
  35. round-trip min/avg/max/stddev = 0.097/0.110/0.123/0.000 ms
  36. root@nginx:/# ping coredns.kube-system.svc.cluster.local
  37. PING coredns.kube-system.svc.cluster.local (10.254.0.2): 48 data bytes
  38. 56 bytes from 10.254.0.2: icmp_seq=0 ttl=64 time=0.129 ms
  39. ^C--- coredns.kube-system.svc.cluster.local ping statistics ---
  40. 1 packets transmitted, 1 packets received, 0% packet loss
  41. round-trip min/avg/max/stddev = 0.129/0.129/0.129/0.000 ms

参考

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