github地址: https://github.com/coredns/deployment/tree/master/kubernetes
1.安装
mkdir -p /home/yaml/coredns
cd /home/yaml/coredns
git clone https://github.com/coredns/deployment.git
cd /home/yaml/coredns/deployment/kubernetes/
#-i 指定集群dns,预先设置的可以查看 kubelet-config.yml
./deploy.sh -i 10.0.0.2 > coredns.yaml
mv coredns.yaml /home/yaml/coredns/
cd /home/yaml/coredns
rm -rf deployment
#创建执行
kubectl apply -f coredns.yaml
2.验证dns
#新建一个测试nginx服务
kubectl run nginx --image=nginx --expose --port=80
#确认nginx服务
kubectl get svc,pod | grep nginx
service/nginx ClusterIP 10.0.0.160 <none> 80/TCP 2m50s
pod/nginx 1/1 Running 0 2m50s
#测试pod alpine
kubectl run test --rm -it --image=alpine /bin/sh
#查看pod当前dns
/ # cat /etc/resolv.conf
nameserver 10.0.0.2
search default.svc.cluster.local svc.cluster.local cluster.local
options ndots:5
#测试集群内部服务解析
/ # nslookup nginx.default.svc.cluster.local
Server: 10.0.0.2
Address: 10.0.0.2:53
*** Cant find nginx.default.svc.cluster.local: No answer
Name: nginx.default.svc.cluster.local
Address: 10.0.0.160
#测试外部域名的解析,默认集成node的dns解析
/ # nslookup www.baidu.com
Server: 10.0.0.2
Address: 10.0.0.2:53
Non-authoritative answer:
www.baidu.com canonical name = www.a.shifen.com
Non-authoritative answer:
www.baidu.com canonical name = www.a.shifen.com
Name: www.a.shifen.com
Address: 180.101.49.12
Name: www.a.shifen.com
Address: 180.101.49.11
#外部测试
dig -t A www.baidu.com @10.0.0.2 +short
www.a.shifen.com.
180.101.49.11
180.101.49.12
如果出现nslookup: cant resolve 'kubernetes.default' 则说明DNS有问题,通过日志排查错误。
3. Loop (127.0.0.1:55751 -> :53) 错误解决
如果出现 [FATAL] plugin/loop: Loop (127.0.0.1:55751 -> :53) detected for zone "."
表示服务器的/etc/resolv.conf 的dns 设置 有包含 127.0.* 的字段,它识别失败
修改dns,在重启coredns服务
#临时修改
cat /etc/resolv.conf
nameserver 114.114.114.114
#永久增加 (ubuntu)
echo "nameserver 114.114.114.114" >> /etc/resolvconf/resolv.conf.d/tail
#去掉配置文件默认DNS
vim /etc/resolvconf/update.d/libc
#135 行,将它注释掉
for N in $NMSRVRS ; do echo "nameserver $N" >> "$TMPFILE" ; done
#更换成
for N in $NMSRVRS
do
if [[ $N != *"^127.0"* ]];then
echo "nameserver $N" >> "$TMPFILE"
fi
done
...
#重启resolvconf服务
/etc/init.d/resolvconf restart
#删除coredns 的pod,重新启动它
kubectl delete pod coredns-867bfd96bd-qv4r5 -n kube-system