手动搭建k8s-1-10-4集群插件之coredns
文章发布较早,内容可能过时,阅读注意甄别。
# 1,修改配置文件
将下载的 kubernetes-server-linux-amd64.tar.gz 解压后,再解压其中的 kubernetes-src.tar.gz 文件。
coredns 对应的目录是:cluster/addons/dns。
$pwd
/home/k8s/k8s/kubernetes/cluster/addons/dns
$ cp coredns.yaml.base coredns.yaml
#修改第61行以及第153行的内容。
$vim coredns.yaml
#修改后内容与原内容对比如下:
$diff coredns.yaml.base coredns.yaml
61c61
< kubernetes __PILLAR__DNS__DOMAIN__ in-addr.arpa ip6.arpa {
---
> kubernetes cluster.local. in-addr.arpa ip6.arpa {
153c153
< clusterIP: __PILLAR__DNS__SERVER__
---
> clusterIP: 10.254.0.2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 2,创建 coredns
$ kubectl create -f coredns.yaml
1
# 3, 检查 coredns 功能
$ kubectl get all -n kube-system
NAME READY STATUS RESTARTS AGE
pod/coredns-77c989547b-j77lp 1/1 Running 0 15h
pod/coredns-77c989547b-t6mxc 1/1 Running 0 15h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/coredns ClusterIP 10.254.0.2 <none> 53/UDP,53/TCP 15h
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.apps/coredns 2 2 2 2 15h
NAME DESIRED CURRENT READY AGE
replicaset.apps/coredns-77c989547b 2 2 2 15h
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
新建一个 Deployment。
$ 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Export 该 Deployment, 生成 my-nginx
服务:
创建另一个 Pod
,查看 /etc/resolv.conf
是否包含 kubelet
配置的 --cluster-dns
和 --cluster-domain
,是否能够将服务 my-nginx
解析到上面显示的 Cluster IP 10.254.22.37
。
验证一下:
$ kubectl create -f pod-nginx.yaml
$ kubectl exec nginx -i -t -- /bin/bash
$kubectl exec -it nginx bash
root@nginx:/# cat /etc/resolv.conf
nameserver 10.254.0.2
search default.svc.cluster.local. svc.cluster.local. cluster.local.
options ndots:5
root@nginx:/# ping my-nginx
PING my-nginx.default.svc.cluster.local (10.254.22.37): 48 data bytes
56 bytes from 10.254.22.37: icmp_seq=0 ttl=64 time=0.118 ms
56 bytes from 10.254.22.37: icmp_seq=1 ttl=64 time=0.085 ms
^C--- my-nginx.default.svc.cluster.local ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.085/0.102/0.118/0.000 ms
root@nginx:/# ping kubernetes
PING kubernetes.default.svc.cluster.local (10.254.0.1): 48 data bytes
56 bytes from 10.254.0.1: icmp_seq=0 ttl=64 time=0.111 ms
56 bytes from 10.254.0.1: icmp_seq=1 ttl=64 time=0.098 ms
^C--- kubernetes.default.svc.cluster.local ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.098/0.105/0.111/0.000 ms
root@nginx:/# ping coredns.kube-system.svc.cluster.local
PING coredns.kube-system.svc.cluster.local (10.254.0.2): 48 data bytes
56 bytes from 10.254.0.2: icmp_seq=0 ttl=64 time=0.120 ms
56 bytes from 10.254.0.2: icmp_seq=1 ttl=64 time=0.083 ms
^C--- coredns.kube-system.svc.cluster.local ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.083/0.102/0.120/0.000 ms
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
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
# 4,参考
- 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
上次更新: 2024/11/19, 23:11:42