一篇特别好的文章:http://ccnuo.com/

服务发现

1.部署kubernetesubernetes的内网资源配置清单

在k8s-60.host.com上,配置一个NGINX,用以提供kubernetes统一的访问资源清单入口

  1. [root@k8s-60 html]# vi /etc/nginx/conf.d/k8s-yaml.od.com.conf
  2. server {
  3. listen 80;
  4. server_name k8s-yaml.od.com;
  5. location / {
  6. autoindex on;
  7. default_type text/plain;
  8. root /data/k8s-yaml;
  9. }
  10. }
  11. [root@k8s-60 html]# nginx -t
  12. [root@k8s-60 html]# nginx -s reload
  13. [root@k8s-60 html]# cd /data
  14. [root@k8s-60 data]# mkdir k8s-yaml
  15. [root@k8s-60 data]# cd k8s-yaml/
  16. [root@k8s-60 k8s-yaml]# mkdir coredns

2.配置dns解析

  1. [root@k8s-20 ~]# vi /var/named/od.com.zone
  2. $ORIGIN od.com.
  3. $TTL 600 ; 10 minutes
  4. @ IN SOA dns.od.com. dnsadmin.od.com. (
  5. 2019111003 ; serial
  6. 10800 ; refresh (3 hours)
  7. 900 ; retry (15 minutes)
  8. 604800 ; expire (1 week)
  9. 86400 ; minimum (1 day)
  10. )
  11. NS dns.od.com.
  12. $TTL 60 ; 1 minute
  13. dns A 192.168.10.20
  14. harbor A 192.168.10.60
  15. k8s-yaml A 192.168.10.60
  16. [root@k8s-20 ~]# systemctl restart named
  17. [root@k8s-20 ~]# dig -t A k8s-yaml.od.com @192.168.10.20 +short
  18. 192.168.10.60

3.浏览器访问k8s-yaml.od.com

可以看到有coredns目录

4.下载docker镜像并打包上传到harbor仓库

  1. [root@hdss7-60 ~]# docker pull coredns/coredns:1.6.1
  2. [root@hdss7-60 coredns]# docker tag c0f6e815079e harbor.od.com/public/coredns:v1.6.1
  3. [root@hdss7-60 coredns]# docker push harbor.od.com/public/coredns:v1.6.1

5.准备资源配置清单

官方DNS插件—coredns 模板

  1. https://github.com/kubernetes/kubernetes/tree/master/cluster/addons/dns/coredns

rbac.yaml

权限相关

coredns需要用到集群底层的资源,需要拿到集群权限

  1. [root@k8s-60 coredns]# cd /data/k8s-yaml/coredns/
  2. [root@k8s-60 coredns]# vi rbac.yaml
  3. apiVersion: v1
  4. kind: ServiceAccount
  5. metadata:
  6. name: coredns
  7. namespace: kube-system
  8. labels:
  9. kubernetes.io/cluster-service: "true"
  10. addonmanager.kubernetes.io/mode: Reconcile
  11. ---
  12. apiVersion: rbac.authorization.k8s.io/v1
  13. kind: ClusterRole
  14. metadata:
  15. labels:
  16. kubernetes.io/bootstrapping: rbac-defaults
  17. addonmanager.kubernetes.io/mode: Reconcile
  18. name: system:coredns
  19. rules:
  20. - apiGroups:
  21. - ""
  22. resources:
  23. - endpoints
  24. - services
  25. - pods
  26. - namespaces
  27. verbs:
  28. - list
  29. - watch
  30. ---
  31. apiVersion: rbac.authorization.k8s.io/v1
  32. kind: ClusterRoleBinding
  33. metadata:
  34. annotations:
  35. rbac.authorization.kubernetes.io/autoupdate: "true"
  36. labels:
  37. kubernetes.io/bootstrapping: rbac-defaults
  38. addonmanager.kubernetes.io/mode: EnsureExists
  39. name: system:coredns
  40. roleRef:
  41. apiGroup: rbac.authorization.k8s.io
  42. kind: ClusterRole
  43. name: system:coredns
  44. subjects:
  45. - kind: ServiceAccount
  46. name: coredns
  47. namespace: kube-system

cm.yaml

对coredns进行的配置 corefile

  1. [root@k8s-60 coredns]# vi cm.yaml
  2. apiVersion: v1
  3. kind: ConfigMap
  4. metadata:
  5. name: coredns
  6. namespace: kube-system
  7. data:
  8. Corefile: |
  9. .:53 {
  10. errors
  11. log
  12. health
  13. ready
  14. kubernetes cluster.local 10.96.0.0/22
  15. forward . 192.168.10.20
  16. cache 30
  17. loop
  18. reload
  19. loadbalance
  20. }

dp.yaml

coredns 的pod的控制器

  1. [root@k8s-60 coredns]# vi dp.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: coredns
  6. namespace: kube-system
  7. labels:
  8. k8s-app: coredns
  9. kubernetes.io/name: "CoreDNS"
  10. spec:
  11. replicas: 1
  12. selector:
  13. matchLabels:
  14. k8s-app: coredns
  15. template:
  16. metadata:
  17. labels:
  18. k8s-app: coredns
  19. spec:
  20. priorityClassName: system-cluster-critical
  21. serviceAccountName: coredns
  22. containers:
  23. - name: coredns
  24. image: harbor.od.com/public/coredns:v1.6.1
  25. args:
  26. - -conf
  27. - /etc/coredns/Corefile
  28. volumeMounts:
  29. - name: config-volume
  30. mountPath: /etc/coredns
  31. ports:
  32. - containerPort: 53
  33. name: dns
  34. protocol: UDP
  35. - containerPort: 53
  36. name: dns-tcp
  37. protocol: TCP
  38. - containerPort: 9153
  39. name: metrics
  40. protocol: TCP
  41. livenessProbe:
  42. httpGet:
  43. path: /health
  44. port: 8080
  45. scheme: HTTP
  46. initialDelaySeconds: 60
  47. timeoutSeconds: 5
  48. successThreshold: 1
  49. failureThreshold: 5
  50. dnsPolicy: Default
  51. volumes:
  52. - name: config-volume
  53. configMap:
  54. name: coredns
  55. items:
  56. - key: Corefile
  57. path: Corefile

svc.yaml

service

  1. [root@k8s-60 coredns]# vi svc.yaml
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. name: coredns
  6. namespace: kube-system
  7. labels:
  8. k8s-app: coredns
  9. kubernetes.io/cluster-service: "true"
  10. kubernetes.io/name: "CoreDNS"
  11. spec:
  12. selector:
  13. k8s-app: coredns
  14. clusterIP: 10.96.0.2
  15. ports:
  16. - name: dns
  17. port: 53
  18. protocol: UDP
  19. - name: dns-tcp
  20. port: 53
  21. - name: metrics
  22. port: 9153
  23. protocol: TCP

6.应用资源配置

陈述式资源管理方法

  1. [root@k8s-40 ~]# kubectl apply -f http://k8s-yaml.od.com/coredns/rbac.yaml
  2. [root@k8s-40 ~]# kubectl apply -f http://k8s-yaml.od.com/coredns/cm.yaml
  3. [root@k8s-40 ~]# kubectl apply -f http://k8s-yaml.od.com/coredns/dp.yaml
  4. [root@k8s-40 ~]# kubectl apply -f http://k8s-yaml.od.com/coredns/svc.yaml

7.检查创建的资源

  1. [root@k8s-40 ~]# kubectl get all -n kube-system
  2. NAME READY STATUS RESTARTS AGE
  3. pod/coredns-6b6c4f9648-8fzsf 1/1 Running 0 17s
  4. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  5. service/coredns ClusterIP 10.96.0.2 <none> 53/UDP,53/TCP,9153/TCP 13s
  6. ## 可以查看到ClusterIP,这个ClusterIP在之前配置的kubelet.sh文件中就已经定好的了
  7. NAME READY UP-TO-DATE AVAILABLE AGE
  8. deployment.apps/coredns 1/1 1 1 18s
  9. NAME DESIRED CURRENT READY AGE
  10. replicaset.apps/coredns-6b6c4f9648 1 1 1 17s

10.96.0.2在安装集群的时候已经定好了

/opt/kubernetes/server/bin/kubelet.sh

—cluster-dns 10.96.0.2

CoreNDS是使server_name和cluster_ip关联起来

8.验证coreDNS

  1. [root@k8s-40 ~]# dig -t A www.baidu.com @10.96.0.2 +short
  2. www.a.shifen.com.
  3. 183.232.231.172
  4. 183.232.231.174
  5. [root@k8s-40 ~]# dig -t A k8s-40.host.com @10.96.0.2 +short
  6. 192.168.10.40

coredns原理解析