Kubernetes 部署手册v1.12.2(18)

  1. 1. 09-1.部署 coredns 插件
    1. 1.1. 修改配置文件
    2. 1.2. 创建 coredns
    3. 1.3. 检查 coredns 功能
    4. 1.4. 参考

#Kubernetes部署手册v1.12.2(18)

09-1.部署 coredns 插件

修改配置文件

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

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

1
2
3
4
cd /opt/k8s/work/kubernetes/cluster/addons/dns/coredns
cp coredns.yaml.base coredns.yaml
source /opt/k8s/bin/environment.sh
sed -i -e "s/__PILLAR__DNS__DOMAIN__/${CLUSTER_DNS_DOMAIN}/" -e "s/__PILLAR__DNS__SERVER__/${CLUSTER_DNS_SVC_IP}/" coredns.yaml

创建 coredns

1
kubectl create -f coredns.yaml

检查 coredns 功能

1
2
3
4
5
6
7
8
9
10
11
12
13
$ kubectl get all -n kube-system
NAME READY STATUS RESTARTS AGE
pod/coredns-77c989547b-6l6jr 1/1 Running 0 3m
pod/coredns-77c989547b-d9lts 1/1 Running 0 3m

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/coredns ClusterIP 10.254.0.2 <none> 53/UDP,53/TCP 3m

NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.apps/coredns 2 2 2 2 3m

NAME DESIRED CURRENT READY AGE
replicaset.apps/coredns-77c989547b 2 2 2 3m

新建一个 Deployment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cd /opt/k8s/work
cat > my-nginx.yaml <<EOF
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: my-nginx
spec:
replicas: 2
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- name: my-nginx
image: nginx:1.7.9
ports:
- containerPort: 80
EOF
kubectl create -f my-nginx.yaml

Export 该 Deployment, 生成 my-nginx 服务:

1
2
3
4
5
$ kubectl expose deploy my-nginx
service "my-nginx" exposed

$ kubectl get services --all-namespaces |grep my-nginx
default my-nginx ClusterIP 10.254.229.163 <none> 80/TCP 12s

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
cd /opt/k8s/work
cat > dnsutils-ds.yml <<EOF
apiVersion: v1
kind: Service
metadata:
name: dnsutils-ds
labels:
app: dnsutils-ds
spec:
type: NodePort
selector:
app: dnsutils-ds
ports:
- name: http
port: 80
targetPort: 80
---
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
name: dnsutils-ds
labels:
addonmanager.kubernetes.io/mode: Reconcile
spec:
template:
metadata:
labels:
app: dnsutils-ds
spec:
containers:
- name: my-dnsutils
image: tutum/dnsutils:latest
command:
- sleep
- "3600"
ports:
- containerPort: 80
EOF
kubectl create -f dnsutils-ds.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
[root@zb-v-cld-k8sm-01 work]# kubectl exec dnsutils-ds-45ghm nslookup kubernetes
Server: 10.254.0.2
Address: 10.254.0.2#53

Name: kubernetes.default.svc.cluster.local
Address: 10.254.0.1

[root@zb-v-cld-k8sm-01 work]# kubectl exec dnsutils-ds-45ghm nslookup www.baidu.com # 解析外部域名时,需要以 . 结尾
Server: 10.254.0.2
Address: 10.254.0.2#53

Non-authoritative answer:
*** Can't find www.baidu.com: No answer

[root@zb-v-cld-k8sm-01 work]# kubectl exec dnsutils-ds-45ghm nslookup www.baidu.com.
Server: 10.254.0.2
Address: 10.254.0.2#53

Non-authoritative answer:
www.baidu.com canonical name = www.a.shifen.com.
Name: www.a.shifen.com
Address: 61.135.169.125
Name: www.a.shifen.com
Address: 61.135.169.121

[root@zb-v-cld-k8sm-01 work]# kubectl exec dnsutils-ds-45ghm nslookup my-nginx
Server: 10.254.0.2
Address: 10.254.0.2#53

Name: my-nginx.default.svc.cluster.local
Address: 10.254.229.163

[root@zb-v-cld-k8sm-01 work]# kubectl exec dnsutils-ds-45ghm nslookup kube-dns.kube-system.svc.cluster
Server: 10.254.0.2
Address: 10.254.0.2#53

Non-authoritative answer:
*** Can't find kube-dns.kube-system.svc.cluster: No answer

[root@zb-v-cld-k8sm-01 work]# kubectl exec dnsutils-ds-45ghm nslookup kube-dns.kube-system.svc
Server: 10.254.0.2
Address: 10.254.0.2#53

Name: kube-dns.kube-system.svc.cluster.local
Address: 10.254.0.2

[root@zb-v-cld-k8sm-01 work]# kubectl exec dnsutils-ds-45ghm nslookup kube-dns.kube-system.svc.cluster.local
Server: 10.254.0.2
Address: 10.254.0.2#53

Non-authoritative answer:
*** Can't find kube-dns.kube-system.svc.cluster.local: No answer

[root@zb-v-cld-k8sm-01 work]# kubectl exec dnsutils-ds-45ghm nslookup kube-dns.kube-system.svc.cluster.local.
Server: 10.254.0.2
Address: 10.254.0.2#53

Name: kube-dns.kube-system.svc.cluster.local
Address: 10.254.0.2

参考

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