部署环境


IP地址 主机名 功能
10.1.104.200 k8s-deploy 部署节点,不承担实际作用
10.1.104.201 k8s-master01 master节点
10.1.104.202 k8s-master02 master节点
10.1.104.203 k8s-master03 master节点
10.1.104.204 k8s-nginx 负载均衡节点,实际生产中应为HA架构
10.1.104.205 k8s-node01 node节点
10.1.104.206 k8s-node02 node节点
10.1.104.207 k8s-node03 node节点

部署插件_CoreDNS


CoreDNS提供pod使用域名访问Service的能力,即部署完CoreDNS之后,可使用ServiceName.default.svc.cluster.local的方式访问服务。


部署CoreDNS(k8s-deploy,k8s-master):

部署CoreDNS(k8s-deploy):

cd /opt/k8s/work/kubernetes
tar -xzvf kubernetes-src.tar.gz
cd /opt/k8s/work/kubernetes/cluster/addons/dns/coredns
cp coredns.yaml.base coredns.yaml

部署CoreDNS(k8s-deploy):

cd /opt/k8s/work/kubernetes
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}/" \
-e "s@k8s.gcr.io@registry.cn-hangzhou.aliyuncs.com/google_containers@" coredns.yaml
scp coredns.yaml root@10.1.104.201:/opt/k8s/work/

部署CoreDNS资源(k8s-master):

cd /opt/k8s/work/
kubectl apply -f coredns.yaml
kubectl get all -n kube-system

重启之前的nginx服务,检测是否可以获取dns服务器信息(k8s-master):

kubectl exec -it daemon-nginx-deployment-84f45d49d-4pjwq  -- cat /etc/resolv.conf


验证CoreDNS(k8s-master):

部署测试服务(k8s-master):

cd /opt/k8s/work/
source /opt/k8s/bin/environment.sh

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

创建服务并确认服务启动(k8s-master):

apply -f dnsutils-ds.yml
kubectl get pod | grep dnsutils

验证CoreDNS功能(k8s-master):

kubectl exec dnsutils-ds-cwmtv nslookup kubernetes
kubectl exec dnsutils-ds-cwmtv nslookup daemon-nginx-service
kubectl exec dnsutils-ds-cwmtv nslookup www.baidu.com
  • kubernetes为集群内部Kubernetes API的Service地址。
  • daemon-nginx-service为测试nginx的Service地址。
  • baidu.com为公网域名的测试地址。

文档更新时间: 2020-10-22 15:55   作者:闻骏