kubernetes(26):k8s的dns服务-coredns
k8s的dns服务-coredns
1 k8s dns简介
在kubernetes中每一个service都会被分配一个虚拟IP,每一个Service在正常情况下都会长时间不会改变,这个相对于pod的不定IP,对于集群中APP的使用相对是稳定的。但是Service的信息注入到pod目前使用的是环境变量的方式,并且十分依赖于pod(rc)和service的创建顺序,这使得这个集群看起来又不那么完美,于是kubernetes以插件的方式引入了DNS系统,利用DNS对Service进行一个映射,这样我们在APP中直接使用域名进行引用,避免了之前的变量泛滥问题,也避免了创建顺序的尴尬局面。
Dns解析的实际是svc的虚拟负载IP,调用的域名也就是设置的service名称
2 K8s dns安装配置
Yum安装的k8s需要手动配置dns服务
配置方法https://www.cnblogs.com/dribs/p/9120573.html
kubeadm安装的k8s,在部署的时候默认安装了,部署的是coredns
[root@k8s-master k8s]# kubectl get pods -n kube-system |grep dns coredns-5c98db65d4-7rqxc 1/1 Running 0 25h coredns-5c98db65d4-8w45r 1/1 Running 0 25h [root@k8s-master k8s]# [root@k8s-master k8s]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx-deployment-84cddf955f-4r2mx 1/1 Running 0 10m nginx-deployment-84cddf955f-q6xc5 1/1 Running 0 10m [root@k8s-master k8s]# kubectl exec -it nginx-deployment-84cddf955f-4r2mx /bin/bash root@nginx-deployment-84cddf955f-4r2mx:/# cat /etc/resolv.conf nameserver 10.96.0.10 search default.svc.cluster.local svc.cluster.local cluster.local options ndots:5 root@nginx-deployment-84cddf955f-4r2mx:/#
3 一个简单样例tomcat+mysql解析
我们之前yum安装的k8s,部署了web应用tomcat+mysql。pod的DNS不能解析,只能写死MySQL的IP地址
https://www.cnblogs.com/wangxu01/articles/11411113.html
现在有了kubeamd部署的k8s环境,我们重新来测试一下
3.1 下载官方的测试镜像和MySQL
其实无所谓,服务也会自动下载的,我这里没有配置本地仓库
3.2 部署mysql 服务
未持久化,这里只验证dns
3.2.1 创建一个mysql-deployment文件
apiVersion: extensions/v1beta1 kind: Deployment #副本控制器Deployment metadata: name: mysql #Deployment的名称,全局唯一 spec: replicas: 1 #Pod副本的期待数量 template: #根据此模版创建Pod的副本(实例) metadata: labels: app: mysql #Pod副本拥有的标签,对应Deployment的selector spec: containers: #Pod内,定义容器 - name: mysql #容器名称 image: mysql:5.7 #Docker image ports: - containerPort: 3306 #容器应用监听的端口 env: #注入容器内的环境变量 - name: MYSQL_ROOT_PASSWORD #这里设置root初始密码 value: "123456"
3.2.2 创建mysql-deployment
kubectl create -f mysql-deploy.yamlcat
3.2.3 验证mysql-deployment
[root@k8s-master tomcatapp+mysql]# kubectl get deployment NAME READY UP-TO-DATE AVAILABLE AGE mysql 1/1 1 1 3m9s nginx-deployment 2/2 2 2 20h [root@k8s-master tomcatapp+mysql]# kubectl get pod NAME READY STATUS RESTARTS AGE mysql-94f6bbcfd-4f4fr 1/1 Running 0 3m19s nginx-deployment-84cddf955f-4r2mx 1/1 Running 0 30m nginx-deployment-84cddf955f-q6xc5 1/1 Running 0 30m [root@k8s-master tomcatapp+mysql]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES mysql-94f6bbcfd-4f4fr 1/1 Running 0 4m33s 10.254.2.37 k8s-node-2 <none> <none> nginx-deployment-84cddf955f-4r2mx 1/1 Running 0 31m 10.254.1.39 k8s-node-1 <none> <none> nginx-deployment-84cddf955f-q6xc5 1/1 Running 0 31m 10.254.2.34 k8s-node-2 <none> <none> [root@k8s-master tomcatapp+mysql]#
3.2.4 创建MySQL service文件
#DNS根据标签搜索 apiVersion: v1 kind: Service metadata: name: tomcat-mysql #DNS根据标签搜索nslook和dig也根据这个解析 spec: ports: - port: 3306 selector: app: timcat-mysql
3.2.5 创建MySQL service
kubectl apply -f mysql-svc.yaml
3.2.6 连接MySQL测试
[root@k8s-master tomcatapp+mysql]# mysql -uroot -p123456 -h10.254.2.37 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.27 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.01 sec) MySQL [(none)]>
3.3 部署web服务
3.3.1 创建一个tomcat-app-deployment文件
iVersion: extensions/v1beta1 kind: Deployment metadata: name: tomcat-app spec: replicas: 3 template: metadata: labels: app: tomcat-app spec: containers: - name: tomcat-app image: kubeguide/tomcat-app:v1 ports: - containerPort: 8080 env: - name: MYSQL_SERVICE_HOST value: 'tomcat-mysql' #MySQL service标签 - name: MYSQL_SERVICE_PORT value: '3306'
3.3.2 创建tomcatAPP服务
kubectl create -f tomcat-app-deployment.yaml
3.3.3 验证tomcat-app-deployment
[root@k8s-master tomcatapp+mysql]# kubectl get deployment | grep tomcattomcat-app 3/3 3 3 88s [root@k8s-master tomcatapp+mysql]# kubectl get pod -o wide| grep tomcattomcat-app-8489b8fdbf-9hsdn 1/1 Running 0 92s 10.254.2.38 k8s-node-2 <none> <none> tomcat-app-8489b8fdbf-cl8kw 1/1 Running 0 92s 10.254.1.43 k8s-node-1 <none> <none>tomcat-app-8489b8fdbf-twj9h 1/1 Running 0 92s 10.254.1.42 k8s-node-1 <none> <none> [root@k8s-master tomcatapp+mysql]#
3.3.4 测试tomcat-app-deployment服务
[root@k8s-master tomcatapp+mysql]# curl -I 10.254.1.43:8080 HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Content-Type: text/html;charset=UTF-8 Transfer-Encoding: chunked Date: Fri, 30 Aug 2019 03:13:13 GMT [root@k8s-master tomcatapp+mysql]# curl -I 10.254.1.43:8080/demo/ HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Set-Cookie: JSESSIONID=6E07DBC15C31CE815EF57A5031F0F42B; Path=/demo/; HttpOnly Content-Type: text/html;charset=utf-8 Transfer-Encoding: chunked Date: Fri, 30 Aug 2019 03:13:27 GMT [root@k8s-master tomcatapp+mysql]#
3.3.5 创建tomcat-app service文件
apiVersion: v1 kind: Service metadata: name: tomcat-app spec: type: NodePort ports: - port: 8080 name: myweb-svc nodePort: 30020 selector: app: tomcat-app
3.3.6 创建tomcat-app service
kubectl create -f tomcatapp-svc.yaml
3.3.7 测试tomcat
1.1.1 测试tomcat [root@k8s-master tomcatapp+mysql]# curl -I 10.6.76.24:30020/demo/ HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Set-Cookie: JSESSIONID=E7DEB006D12FA0896D8E92E0EA62D337; Path=/demo/; HttpOnly Content-Type: text/html;charset=utf-8 Transfer-Encoding: chunked Date: Fri, 30 Aug 2019 03:17:00 GMT [root@k8s-master tomcatapp+mysql]# curl -I 10.6.76.23:30020/demo/ HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Set-Cookie: JSESSIONID=96B1298BB30F5C2BC57CFFF1228122D9; Path=/demo/; HttpOnly Content-Type: text/html;charset=utf-8 Transfer-Encoding: chunked Date: Fri, 30 Aug 2019 03:17:05 GMT [root@k8s-master tomcatapp+mysql]#
3.4 dig和nslookup测试DNS
# cat dig.yaml apiVersion: v1 kind: Pod metadata: name: dig namespace: default spec: containers: - name: dig image: docker.io/azukiapp/dig command: - sleep - "3600" imagePullPolicy: IfNotPresent restartPolicy: Always
[root@k8s-master tomcatapp+mysql]# kubectl apply -f dig.yaml pod/dig unchanged [root@k8s-master tomcatapp+mysql]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 27h nginx NodePort 10.105.15.44 <none> 80:30023/TCP 21h t-mysql ClusterIP 10.108.97.7 <none> 3306/TCP 9m50s test-tomcat-app NodePort 10.101.38.207 <none> 8080:30020/TCP 7m6s [root@k8s-master tomcatapp+mysql]# [root@k8s-master tomcatapp+mysql]# kubectl exec -it dig /bin/bash bash-4.3# nslookup test-tomcat-app Server: 10.96.0.10 Address: 10.96.0.10#53 Name: test-tomcat-app.default.svc.cluster.local Address: 10.101.38.207 bash-4.3# nslookup nginx Server: 10.96.0.10 Address: 10.96.0.10#53 Name: nginx.default.svc.cluster.local Address: 10.105.15.44 bash-4.3# dig nginx ; <<>> DiG 9.10.3-P3 <<>> nginx ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 42115 ;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;nginx. IN A ;; AUTHORITY SECTION: . 30 IN SOA a.root-servers.net. nstld.verisign-grs.com. 2019082901 1800 900 604800 86400 ;; Query time: 84 msec ;; SERVER: 10.96.0.10#53(10.96.0.10) ;; WHEN: Fri Aug 30 04:23:05 UTC 2019 ;; MSG SIZE rcvd: 109 bash-4.3#