09-1.部署 coredns 插件
下载新版本的 kubernetes
mkdir -p /opt/k8s/work/kubernetes-v1.11.2
cd /opt/k8s/work/kubernetes-v1.11.2
wget https://dl.k8s.io/v1.11.2/kubernetes-server-linux-amd64.tar.gz
tar -xzvf kubernetes-server-linux-amd64.tar.gz
cd kubernetes
tar -xzvf kubernetes-src.tar.gz
修改配置文件
coredns 对应的目录是:/opt/k8s/work/kubernetes-v1.11.2/kubernetes/cluster/addons/dns/coredns
。
source /opt/k8s/bin/environment.sh
cd /opt/k8s/work/kubernetes-v1.11.2/kubernetes/cluster/addons/dns/coredns
cp coredns.yaml.base coredns.yaml
sed -i -e "s/__PILLAR__DNS__DOMAIN__/${CLUSTER_DNS_DOMAIN}/" -e "s/__PILLAR__DNS__SERVER__/${CLUSTER_DNS_SVC_IP}/" coredns.yaml
- ${CLUSTER_DNS_DOMAIN} 值为末尾不带点号的 cluster.local
创建 coredns
kubectl create -f coredns.yaml
设置副本数为 3,提高可用性
kubectl patch deploy coredns -n kube-system -p '{"spec": {"replicas": 3}}'
检查 coredns 功能
$ kubectl get all -n kube-system
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deploy/coredns 1 1 1 1 24s
NAME DESIRED CURRENT READY AGE
rs/coredns-6c64b7d656 1 1 1 24s
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deploy/coredns 1 1 1 1 24s
NAME DESIRED CURRENT READY AGE
rs/coredns-6c64b7d656 1 1 1 24s
NAME READY STATUS RESTARTS AGE
po/coredns-6c64b7d656-zgpb8 1/1 Running 0 24s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
svc/kube-dns ClusterIP 10.254.0.2 <none> 53/UDP,53/TCP 24s
新建一个 Deployment
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
服务:
$ kubectl expose deploy my-nginx
service "my-nginx" exposed
$ kubectl get services --all-namespaces |grep my-nginx
default my-nginx ClusterIP 10.254.0.140 <none> 80/TCP 4s
创建另一个 Pod,查看 /etc/resolv.conf
是否包含 kubelet
配置的 --cluster-dns
和 --cluster-domain
,是否能够将服务 my-nginx
解析到上面显示的 Cluster IP 10.254.9.189
:
cd /opt/k8s/work
cat > dnsutils-ds.yaml <<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.yaml
等待,直到 dnsutils Pod 处于 Running 状态
$ kubectl get pods |grep dnsutils
dnsutils-ds-58897 1/1 Running 0 28s
dnsutils-ds-jkp9v 1/1 Running 0 28s
dnsutils-ds-mvknd 1/1 Running 0 28s
$ kubectl exec -it dnsutils-ds-58897 cat /etc/resolv.conf
nameserver 10.254.0.2
search default.svc.cluster.local svc.cluster.local cluster.local 4pd.io
options ndots:5
$ kubectl exec -it dnsutils-ds-58897 nslookup kubernetes
Server: 10.254.0.2
Address: 10.254.0.2#53
Name: kubernetes.default.svc.cluster.local
Address: 10.254.0.1
$ kubectl exec -it dnsutils-ds-58897 nslookup my-nginx
Server: 10.254.0.2
Address: 10.254.0.2#53
Name: my-nginx.default.svc.cluster.local
Address: 10.254.0.140
- 使用 busybox 镜像中的 nslookup 解析域名失败,但是 alpine 和 tutum/dnsutils 可以正常解析,应该是 busybox 的问题;
参考
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 https://github.com/kubernetes/dns/issues/109