IT干货网

配置k8s dns

wyy 2022年03月19日 DevOps 334 0

DNS (domain name system),提供域名解析服务,解决了难于记忆的IP地址问题,以更人性可读可记忆可标识的方式映射对应IP地址。

Cluster DNS扩展插件用于支持k8s集群系统中各服务之间发现与调用。

  

组件:
  •SkyDNS 提供DNS解析服务
  •Etcd 存储DNS信息
  •Kube2sky 监听kubernetes,当有Service创建时,生成相应的记录到SkyDNS。

1.修改所有node的配置文件:/etc/kubernetes/kubelet  kubernetes-node1,kubernetes-node2,kubernetes-node3:

[root@kubernetes-node2 ~]# tail -n 1 /etc/kubernetes/kubelet
KUBELET_ARGS="--cluster-dns=10.254.254.254 --cluster-domain=cluster.local"

2.所有node节点都重新启动服务

systemctl restart kubelet.service

 

3.根据namespace分为不同的名称空间,将系统服务统一放到一个单独的空间:kube-system,由于默认空间只有default,我们首先要创建kube-system空间。

[root@kubernetes-master pods]# cat kube-system.yml
apiVersion: v1
kind: Namespace
metadata:
      name: kube-system

4.在master服务器上编辑ReplicationController文件dns-rc.yml

  1 apiVersion: v1 
  2 kind: ReplicationController 
  3 metadata: 
  4   name: kube-dns-v11 
  5   namespace: kube-system 
  6   labels: 
  7     k8s-app: kube-dns 
  8     version: v11 
  9     kubernetes.io/cluster-service: "true" 
 10 spec: 
 11   replicas: 1 
 12   selector: 
 13     k8s-app: kube-dns 
 14     version: v11 
 15   template: 
 16     metadata: 
 17       labels: 
 18         k8s-app: kube-dns 
 19         version: v11 
 20         kubernetes.io/cluster-service: "true" 
 21     spec: 
 22       containers: 
 23       - name: etcd 
 24         image: docker.gaoxiaobang.com/kubernetes/etcd-amd64:2.2.1   
 25         resources: 
 26           limits: 
 27             cpu: 100m 
 28             memory: 500Mi 
 29           requests: 
 30             cpu: 100m 
 31             memory: 50Mi 
 32         command: 
 33         - /usr/local/bin/etcd 
 34         - -data-dir 
 35         - /var/etcd/data 
 36         - -listen-client-urls 
 37         - http://127.0.0.1:2379,http://127.0.0.1:4001 
 38         - -advertise-client-urls 
 39         - http://127.0.0.1:2379,http://127.0.0.1:4001 
 40         - -initial-cluster-token 
 41         - skydns-etcd 
 42         #volumeMounts: 
 43         #- name: etcd-storage 
 44         #  mountPath: /var/etcd/data 
 45       - name: kube2sky 
 46         image: docker.gaoxiaobang.com/kubernetes/kube2sky:1.14 
 47         args: 
 48         # command = "/kube2sky" 
 49         - --domain=cluster.local 
 50         - --kube-master-url=http://10.64.8.68:8080 
 51         resources: 
 52           limits: 
 53             cpu: 100m 
 54             memory: 200Mi 
 55           requests: 
 56             cpu: 100m 
 57             memory: 50Mi 
 58       - name: skydns 
 59         image: docker.gaoxiaobang.com/kubernetes/skydns:2015-10-13-8c72f8c 
 60         resources: 
 61           limits: 
 62             cpu: 100m 
 63             memory: 200Mi 
 64           requests: 
 65             cpu: 100m 
 66             memory: 50Mi 
 67         args: 
 68         - -machines=http://127.0.0.1:4001 
 69         - -addr=0.0.0.0:53 
 70         - -ns-rotate=false 
 71         - -domain=cluster.local. 
 72         ports: 
 73         - containerPort: 53 
 74           name: dns 
 75           protocol: UDP 
 76         - containerPort: 53 
 77           name: dns-tcp 
 78           protocol: TCP 
 79         livenessProbe: 
 80           httpGet: 
 81             path: /healthz 
 82             port: 8080 
 83             scheme: HTTP 
 84           initialDelaySeconds: 60 
 85           timeoutSeconds: 5 
 86           successThreshold: 1 
 87           failureThreshold: 5 
 88         readinessProbe: 
 89           httpGet: 
 90             path: /readiness 
 91             port: 8080 
 92             scheme: HTTP 
 93           initialDelaySeconds: 30 
 94           timeoutSeconds: 5 
 95       - name: healthz 
 96         image: docker.gaoxiaobang.com/kubernetes/exechealthz:1.0 
 97         resources: 
 98           limits: 
 99             cpu: 10m 
100             memory: 20Mi 
101           requests: 
102             cpu: 10m 
103             memory: 20Mi 
104         args: 
105         - -cmd=nslookup kubernetes.default.svc.cluster.local localhost >/dev/null 
106         - -port=8080 
107         ports: 
108         - containerPort: 8080 
109           protocol: TCP 
110       volumes: 
111       - name: etcd-storage 
112         emptyDir: {} 
113       dnsPolicy: Default
dns-rc.yml

注意dns-rc.yml:修改文件中的master ip

dns service服务yml

 1 apiVersion: v1 
 2 kind: Service 
 3 metadata: 
 4   name: kube-dns 
 5   namespace: kube-system 
 6   labels: 
 7     k8s-app: kube-dns 
 8     kubernetes.io/cluster-service: "true" 
 9     kubernetes.io/name: "KubeDNS" 
10 spec: 
11   selector: 
12     k8s-app: kube-dns 
13   clusterIP:  10.254.254.254 
14   ports: 
15   - name: dns 
16     port: 53 
17     protocol: UDP 
18   - name: dns-tcp 
19     port: 53 
20     protocol: TCP
dns-svc.yml

创建namespace(有就不用在创建了)

kubectl create -f kube-system.yml

创建ReplicationController

[root@kubernetes-master pods]# kubectl create -f dns-rc.yml

创建Service

[root@kubernetes-master pods]# kubectl create -f dns-svc.yml

 查看dns-rc.yml文件中定义pod的启动状态,如下则启动成功。

[root@kubernetes-master pods]#  kubectl get pod --namespace=kube-system

 查看dns-svc.yml文件中定义的Service的信息。

[root@kubernetes-master pods]# kubectl get svc -l k8s-app=kube-dns --namespace=kube-system

 

测试DNS的效果。
部署一个busybox的Pod

 1 apiVersion: v1 
 2 kind: Pod 
 3 metadata: 
 4   name: busybox 
 5   namespace: default 
 6 spec: 
 7   containers: 
 8   - image: busybox 
 9     command: 
10       - sleep 
11       - "3600" 
12     imagePullPolicy: IfNotPresent 
13     name: busybox 
14   restartPolicy: Always
busybox.yml

验证kubernetes服务的地址解析,出现如下所示则证明DNS解析正常。


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!