kubernetes上的服务发现-CoreDNS配置

1.当前已经开启ipvs的kube-proxy,集群DNS的IP为10.0.0.2
下载coredns项目的kubernetes
wget https://github.com/coredns/deployment/archive/master.zip
unzip master.zip

因为项目使用了jq命令,需要安装jq程序
由于jq来源于epel,因此需要安装扩展的yum仓库
rpm -ivh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install jq
yum -y install conntrack-tools

上面下载master.zip解压缩至deployment-master文件夹
cd deployment-master/kubernetes
./deploy.sh -h #查看部署帮助
[root@master kubernetes]# ./deploy.sh -h
usage: ./deploy.sh [ -r REVERSE-CIDR ] [ -i DNS-IP ] [ -d CLUSTER-DOMAIN ] [ -t YAML-TEMPLATE ]

    -r : Define a reverse zone for the given CIDR. You may specifcy this option more
         than once to add multiple reverse zones. If no reverse CIDRs are defined,
         then the default is to handle all reverse zones (i.e. in-addr.arpa and ip6.arpa)
    -i : Specify the cluster DNS IP address. If not specificed, the IP address of
         the existing "kube-dns" service is used, if present.
    -s : Skips the translation of kube-dns configmap to the corresponding CoreDNS Corefile configuration.



2.修改每台node上的 kubelet 启动参数
kubernetes上的服务发现-CoreDNS配置_第1张图片
image.png
3.创建coredns 和svc
./deploy.sh -r 10.0.0.0/24 -i 10.0.0.2 -d cluster.local | kubectl apply -f -

kubernetes上的服务发现-CoreDNS配置_第2张图片

创建成功

4.验证coreDNS是否正常工作
[root@master yaml]# cat busybox.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: busybox
  namespace: default
spec:
  containers:
  - name: busybox
    image: busybox:1.28
    command:
      - sleep
      - "3600"
    imagePullPolicy: IfNotPresent
  restartPolicy: Always

5.创建Busybox pod
kubernetes上的服务发现-CoreDNS配置_第3张图片
6.busybox的resolv.conf内容
[root@master yaml]# kubectl exec busybox cat /etc/resolv.conf
nameserver 10.0.0.2
search default.svc.cluster.local. svc.cluster.local. cluster.local.
options ndots:5
[root@master yaml]# kubectl get pod
NAME                                            READY     STATUS    RESTARTS   AGE
busybox                                         1/1       Running   0          54m
memory-tomcat-deployment-75c49974b5-swvpl       1/1       Running   1          8h
memory-tomcat-deployment-75c49974b5-z49tv       1/1       Running   1          8h
nginx-deployment-966857787-65mtm                1/1       Running   0          1h
nginx-deployment-966857787-99l7s                1/1       Running   0          1h
quizii-tomcat-deployment-54cd6ffb5c-lpbn6       1/1       Running   0          1h

7.在busybox 的pod里解析kubernetes.default 的IP地址
[root@master kubernetes]# kubectl exec -ti busybox -- nslookup kubernetes.default
Server:    10.0.0.2
Address 1: 10.0.0.2 kube-dns.kube-system.svc.cluster.local

Name:      kubernetes.default
Address 1: 10.0.0.1 kubernetes.default.svc.cluster.local


8.在busybox 的pod里解析外部IP地址 ,按照前文coreDNS的配置,是通过pod所在node上的/etc/resolv.conf 来代理解析的
[root@master kubernetes]# kubectl exec -ti busybox -- nslookup www.sina.com.cn
Server:    10.0.0.2
Address 1: 10.0.0.2 kube-dns.kube-system.svc.cluster.local

Name:      www.sina.com.cn
Address 1: 219.238.4.9


9.在busybox 的pod里解析创建的服务
[root@master kubernetes]# kubectl get svc
NAME                    TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
kubernetes              ClusterIP   10.0.0.1             443/TCP          12d
memory-tomcat-svc       NodePort    10.0.0.101           8082:32000/TCP   10d
nginx-svc               NodePort    10.0.0.13            8080:30000/TCP   3h
quizii-tomcat-svc       NodePort    10.0.0.66            8081:31000/TCP   1h
usercenter-tomcat-svc   NodePort    10.0.0.112           8083:33000/TCP   1h
[root@master kubernetes]# kubectl exec -ti busybox -- nslookup usercenter-tomcat-svc
Server:    10.0.0.2
Address 1: 10.0.0.2 kube-dns.kube-system.svc.cluster.local

Name:      usercenter-tomcat-svc
Address 1: 10.0.0.112 usercenter-tomcat-svc.default.svc.cluster.local
[root@master kubernetes]# kubectl exec -ti busybox -- nslookup quizii-tomcat-svc
Server:    10.0.0.2
Address 1: 10.0.0.2 kube-dns.kube-system.svc.cluster.local

Name:      quizii-tomcat-svc
Address 1: 10.0.0.66 quizii-tomcat-svc.default.svc.cluster.local

总结:通过以上例子可见,coredns工作正常。在我们创建的Kubernetes 1.12.3 cluster中,coredns既可以管理新生成的service的域名,又可以解析出外部域名

你可能感兴趣的:(kubernetes上的服务发现-CoreDNS配置)