$ wget https://github.com/coredns/deployment/blob/master/kubernetes/deploy.sh
$ wget https://github.com/coredns/deployment/blob/master/kubernetes/coredns.yaml.sed
$ ./deploy.sh -r 10.254.0.0/16 -i 10.254.0.2 -d cluster.local -s | kubectl apply -f -
提示:关于脚本的内容可能会因为你使用的版本不同而参数不同,所以尽量在做的时候撸一眼脚本的内容。
$ kubectl get pod -n kube-system|grep coredns
coredns-7bb49b45c8-89d2v 1/1 Running 0 41s
coredns-7bb49b45c8-gr8dj 1/1 Running 0 41s
$ 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
$ kubectl expose deploy my-nginx
查看运行的nginx服务
$ kubectl get services --all-namespaces |grep my-nginx
default my-nginx ClusterIP 10.254.79.234 <none> 80/TCP 7s
创建另一个 Pod,查看 /etc/resolv.conf 是否包含 kubelet 配置的 --cluster-dns 和 --cluster-domain,是否能够将服务 my-nginx 解析到上面显示的 Cluster IP 10.254.79.234
$ cat > pod-nginx.yaml <<EOF
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
EOF
$ kubectl create -f pod-nginx.yaml
查看运行的pod
$ kubectl get pod --all-namespaces |grep nginx
default my-nginx-75bbc97767-hsqbt 1/1 Running 0 49s
default my-nginx-75bbc97767-sxtfj 1/1 Running 0 49s
default nginx 1/1 Running 0 18s
测试DNS是否正常
$ kubectl exec nginx -i -t -- /bin/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.155.155): 48 data bytes
56 bytes from 10.254.155.155: icmp_seq=0 ttl=64 time=0.523 ms
56 bytes from 10.254.155.155: icmp_seq=1 ttl=64 time=0.083 ms
^C--- my-nginx.default.svc.cluster.local ping statistics ---
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.055 ms
56 bytes from 10.254.0.1: icmp_seq=1 ttl=64 time=0.111 ms
56 bytes from 10.254.0.1: icmp_seq=2 ttl=64 time=0.103 ms
^C--- kubernetes.default.svc.cluster.local ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.055/0.090/0.111/0.025 ms
root@nginx:/# ping kube-dns.kube-system.svc.cluster.local
PING kube-dns.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.141 ms
56 bytes from 10.254.0.2: icmp_seq=1 ttl=64 time=0.110 ms
56 bytes from 10.254.0.2: icmp_seq=2 ttl=64 time=0.110 ms
^C--- kube-dns.kube-system.svc.cluster.local ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.110/0.120/0.141/0.000 ms
如果跟上面显示一样,就没有问题了。
kubectl delete --namespace=kube-system deployment kube-dns