溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

kubernetes實踐之六十四:CoreDNS

發(fā)布時間:2020-08-10 12:46:18 來源:ITPUB博客 閱讀:284 作者:百聯(lián)達 欄目:云計算
一:簡介
1.Kubernetes包括用于服務(wù)發(fā)現(xiàn)的DNS服務(wù)器Kube-DNS。 該DNS服務(wù)器利用SkyDNS的庫來為Kubernetes pod和服務(wù)提供DNS請求。SkyDNS2的作者,Miek Gieben,創(chuàng)建了一個新的DNS服務(wù)器,CoreDNS,它采用更模塊化,可擴展的框架構(gòu)建。 Infoblox已經(jīng)與Miek合作,將此DNS服務(wù)器作為Kube-DNS的替代品。
2.CoreDNS利用作為Web服務(wù)器Caddy的一部分而開發(fā)的服務(wù)器框架。該框架具有非常靈活,可擴展的模型,用于通過各種中間件組件傳遞請求。這些中間件組件根據(jù)請求提供不同的操作,例如記錄,重定向,修改或維護。雖然它一開始作為Web服務(wù)器,但是Caddy并不是專門針對HTTP協(xié)議的,而是構(gòu)建了一個基于CoreDNS的理想框架。
3.在這種靈活的模型中添加對Kubernetes的支持,相當(dāng)于創(chuàng)建了一個Kubernetes中間件。該中間件使用Kubernetes API來滿足針對特定Kubernetes pod或服務(wù)的DNS請求。而且由于Kube-DNS作為Kubernetes的另一項服務(wù),kubelet和Kube-DNS之間沒有緊密的綁定。您只需要將DNS服務(wù)的IP地址和域名傳遞給kubelet,而Kubernetes并不關(guān)心誰在實際處理該IP請求。
4.CoreDNS可以在具有標(biāo)準(zhǔn)的Kube-DNS的Kubernetes集群中運行。作為Kubernetes 的插件使用,CoreDNS將從 Kubernetes集群中讀取區(qū)(zone)數(shù)據(jù)。它實現(xiàn)了為Kubernetes的DNS服務(wù)發(fā)現(xiàn)定義的規(guī)范:Kubernetes DNS-Based Service Discovery。

二:部署

部署CoreDNS需要使用到官方提供的兩個文件 deploy.sh和coredns.yaml.sed

1.deploy.sh 是一個用于在已經(jīng)運行kube-dns的集群中生成運行CoreDNS部署文件(manifest)的工具腳本。它使用 coredns.yaml.sed文件作為模板,創(chuàng)建一個ConfigMap和CoreDNS的deployment,然后更新集群中已有的kube-dns 服務(wù)的selector使用CoreDNS的deployment。重用已有的服務(wù)并不會在服務(wù)的請求中發(fā)生沖突。

2.deploy.sh文件并不會刪除kube-dns的deployment或者replication controller。如果要刪除kube-dns,你必須在部署CoreDNS后手動的刪除kube-dns。

3.使用CoreDNS替換Kube-DNS只需要使用下面的兩個命令:

點擊(此處)折疊或打開

  1. $ ./deploy.sh | kubectl apply -f -
  2. $ kubectl delete --namespace=kube-system deployment kube-dns
4.deploy.sh(https://github.com/coredns/deployment/tree/master/kubernetes)

點擊(此處)折疊或打開

  1. #!/bin/bash

  2. # Deploys CoreDNS to a cluster currently running Kube-DNS.

  3. show_help () {
  4. cat << USAGE
  5. usage: $0 [ -r REVERSE-CIDR ] [ -i DNS-IP ] [ -d CLUSTER-DOMAIN ] [ -t YAML-TEMPLATE ]
  6.     -r : Define a reverse zone for the given CIDR. You may specifcy this option more
  7.          than once to add multiple reverse zones. If no reverse CIDRs are defined,
  8.          then the default is to handle all reverse zones (i.e. in-addr.arpa and ip6.arpa)
  9.     -i : Specify the cluster DNS IP address. If not specificed, the IP address of
  10.          the existing "kube-dns" service is used, if present.
  11. USAGE
  12. exit 0
  13. }

  14. # Simple Defaults
  15. CLUSTER_DOMAIN=cluster.local
  16. YAML_TEMPLATE=`pwd`/coredns.yaml.sed


  17. # Get Opts
  18. while getopts "hr:i:d:t:" opt; do
  19.     case "$opt" in
  20.     h) show_help
  21.         ;;
  22.     r) REVERSE_CIDRS="$REVERSE_CIDRS $OPTARG"
  23.         ;;
  24.     i) CLUSTER_DNS_IP=$OPTARG
  25.         ;;
  26.     d) CLUSTER_DOMAIN=$OPTARG
  27.         ;;
  28.     t) YAML_TEMPLATE=$OPTARG
  29.         ;;
  30.     esac
  31. done

  32. # Conditional Defaults
  33. if [[ -z $REVERSE_CIDRS ]]; then
  34.   REVERSE_CIDRS="in-addr.arpa ip6.arpa"
  35. fi
  36. if [[ -z $CLUSTER_DNS_IP ]]; then
  37.   # Default IP to kube-dns IP
  38.   CLUSTER_DNS_IP=$(kubectl get service --namespace kube-system kube-dns -o jsonpath="{.spec.clusterIP}")
  39.   if [ $? -ne 0 ]; then
  40.       >&2 echo "Error! The IP address for DNS service couldn't be determined automatically. Please specify the DNS-IP with the '-i' option."
  41.       exit 2
  42.   fi
  43. fi

  44. sed -e s/CLUSTER_DNS_IP/$CLUSTER_DNS_IP/g -e s/CLUSTER_DOMAIN/$CLUSTER_DOMAIN/g -e "s?REVERSE_CIDRS?$REVERSE_CIDRS?g" $YAML_TEMPLATE
5.coredns.yaml.sed

點擊(此處)折疊或打開

  1. apiVersion: v1
  2. kind: ServiceAccount
  3. metadata:
  4.   name: coredns
  5.   namespace: kube-system
  6. ---
  7. apiVersion: rbac.authorization.k8s.io/v1beta1
  8. kind: ClusterRole
  9. metadata:
  10.   labels:
  11.     kubernetes.io/bootstrapping: rbac-defaults
  12.   name: system:coredns
  13. rules:
  14. - apiGroups:
  15.   - ""
  16.   resources:
  17.   - endpoints
  18.   - services
  19.   - pods
  20.   - namespaces
  21.   verbs:
  22.   - list
  23.   - watch
  24. ---
  25. apiVersion: rbac.authorization.k8s.io/v1beta1
  26. kind: ClusterRoleBinding
  27. metadata:
  28.   annotations:
  29.     rbac.authorization.kubernetes.io/autoupdate: "true"
  30.   labels:
  31.     kubernetes.io/bootstrapping: rbac-defaults
  32.   name: system:coredns
  33. roleRef:
  34.   apiGroup: rbac.authorization.k8s.io
  35.   kind: ClusterRole
  36.   name: system:coredns
  37. subjects:
  38. - kind: ServiceAccount
  39.   name: coredns
  40.   namespace: kube-system
  41. ---
  42. apiVersion: v1
  43. kind: ConfigMap
  44. metadata:
  45.   name: coredns
  46.   namespace: kube-system
  47. data:
  48.   Corefile: |
  49.     .:53 {
  50.         errors
  51.         health
  52.         kubernetes CLUSTER_DOMAIN REVERSE_CIDRS {
  53.           pods insecure
  54.           upstream
  55.           fallthrough in-addr.arpa ip6.arpa
  56.         }
  57.         prometheus :9153
  58.         proxy . /etc/resolv.conf
  59.         cache 30
  60.         reload
  61.     }
  62. ---
  63. apiVersion: extensions/v1beta1
  64. kind: Deployment
  65. metadata:
  66.   name: coredns
  67.   namespace: kube-system
  68.   labels:
  69.     k8s-app: kube-dns
  70.     kubernetes.io/name: "CoreDNS"
  71. spec:
  72.   replicas: 2
  73.   strategy:
  74.     type: RollingUpdate
  75.     rollingUpdate:
  76.       maxUnavailable: 1
  77.   selector:
  78.     matchLabels:
  79.       k8s-app: kube-dns
  80.   template:
  81.     metadata:
  82.       labels:
  83.         k8s-app: kube-dns
  84.     spec:
  85.       serviceAccountName: coredns
  86.       tolerations:
  87.         - key: "CriticalAddonsOnly"
  88.           operator: "Exists"
  89.       containers:
  90.       - name: coredns
  91.         image: coredns/coredns:1.1.3
  92.         imagePullPolicy: IfNotPresent
  93.         args: [ "-conf", "/etc/coredns/Corefile" ]
  94.         volumeMounts:
  95.         - name: config-volume
  96.           mountPath: /etc/coredns
  97.           readOnly: true
  98.         ports:
  99.         - containerPort: 53
  100.           name: dns
  101.           protocol: UDP
  102.         - containerPort: 53
  103.           name: dns-tcp
  104.           protocol: TCP
  105.         - containerPort: 9153
  106.           name: metrics
  107.           protocol: TCP
  108.         securityContext:
  109.           allowPrivilegeEscalation: false
  110.           capabilities:
  111.             add:
  112.             - NET_BIND_SERVICE
  113.             drop:
  114.             - all
  115.           readOnlyRootFilesystem: true
  116.         livenessProbe:
  117.           httpGet:
  118.             path: /health
  119.             port: 8080
  120.             scheme: HTTP
  121.           initialDelaySeconds: 60
  122.           timeoutSeconds: 5
  123.           successThreshold: 1
  124.           failureThreshold: 5
  125.       dnsPolicy: Default
  126.       volumes:
  127.         - name: config-volume
  128.           configMap:
  129.             name: coredns
  130.             items:
  131.             - key: Corefile
  132.               path: Corefile
  133. ---
  134. apiVersion: v1
  135. kind: Service
  136. metadata:
  137.   name: kube-dns
  138.   namespace: kube-system
  139.   annotations:
  140.     prometheus.io/scrape: "true"
  141.   labels:
  142.     k8s-app: kube-dns
  143.     kubernetes.io/cluster-service: "true"
  144.     kubernetes.io/name: "CoreDNS"
  145. spec:
  146.   selector:
  147.     k8s-app: kube-dns
  148.   clusterIP: CLUSTER_DNS_IP
  149.   ports:
  150.   - name: dns
  151.     port: 53
  152.     protocol: UDP
  153.   - name: dns-tcp
  154.     port: 53
  155.     protocol: TCP
三:備注
對于非RBAC部署,你需要編輯生成的結(jié)果yaml文件:
1.從yaml文件的Deployment部分刪除 serviceAccountName: coredns
2.刪除 ServiceAccount、 ClusterRole和 ClusterRoleBinding 部分



向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI