1.概述
coreDNS的作用主要是作为DNS服务器,在集群内提供服务发现功能,也就是服务之间的互相定位的过程。他监听集群中service和pod的创建和销毁事件,当serivice或者pod被创建时,记录对应的解析记录。当其他pod通过域名来访问集群中的service或pod时,会向coreDNS服务查询解析记录,然后访问解析到的IP地址
coreDNS:就是将svc资源的名称解析成ClusterIP
coreDNS效率高、资源占用率少,已经替换kube-dns,成为了kubernetes集群的默认DNS服务
kubeadm部署的k8s集群自带coreDNS组件,二进制部署需要自己手动部署
k8s系统中安装了coreDNS组件后,会有一个coreDNS开头的pod资源
·查看k8s集群的coreDNS的IP地址
[root@master job]# cat /var/lib/kubelet/config.yaml
[root@master job]# kubectl get svc -A
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default kubernetes ClusterIP 10.200.0.1 <none> 443/TCP 91m
kube-system kube-dns ClusterIP 10.200.0.10 <none> 53/UDP,53/TCP,9153/TCP 23d
查看k8s集群中DNS的svc的详细信息,可以看到,里面解析了coreDNS的地址在endpoint列表中
[root@master job]# kubectl describe -n kube-system svc kube-dns
Name: kube-dns
Namespace: kube-system
Labels: k8s-app=kube-dnskubernetes.io/cluster-service=truekubernetes.io/name=CoreDNS
Annotations: prometheus.io/port: 9153prometheus.io/scrape: true
Selector: k8s-app=kube-dns
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.200.0.10
IPs: 10.200.0.10
Port: dns 53/UDP
TargetPort: 53/UDP
Endpoints: 10.100.0.2:53,10.100.0.3:53
Port: dns-tcp 53/TCP
TargetPort: 53/TCP
Endpoints: 10.100.0.2:53,10.100.0.3:53
Port: metrics 9153/TCP
TargetPort: 9153/TCP
Endpoints: 10.100.0.2:9153,10.100.0.3:9153
Session Affinity: None
Events: <none>
2.coreDNS组件是否正常
·第一种方式验证
1.利用之前的wordpress案例
[root@master job]# cat deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: dm-wordpress
spec:replicas: 1selector:matchLabels:k8s: k8stemplate:metadata:labels:k8s: k8sspec:containers:- name: wp-cimage: wordpress:latestports:- name: wp-portcontainerPort: 80env:- name: WORDPRESS_DB_HOSTvalue: 10.200.200.200:3306- name: WORDPRESS_DB_USERvalue: wordpress- name: WORDPRESS_DB_PASSWORDvalue: wordpress- name: WORDPRESS_DB_NAMEvalue: wordpress
[root@master job]# cat db.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: wordpress-db
spec:replicas: 1selector:matchLabels:k8s: dbtemplate:metadata:labels:k8s: dbspec:containers:- name: db-cimage: mysql:8.0ports:- name: db-portcontainerPort: 3306env:- name: MYSQL_DATABASEvalue: wordpress- name: MYSQL_USERvalue: wordpress- name: MYSQL_PASSWORDvalue: wordpress- name: MYSQL_ROOT_PASSWORDvalue: wordpress
[root@master job]# cat svc.yaml
apiVersion: v1
kind: Service
metadata:name: svc-db
spec:type: NodePortclusterIP: 10.200.200.200selector:k8s: dbports:- protocol: TCPport: 3306targetPort: 3306nodePort: 30001
---
apiVersion: v1
kind: Service
metadata:name: svc-wp
spec:type: NodePortselector:k8s: k8sports:- protocol: TCPport: 80targetPort: 80nodePort: 31000
2.创建查看资源
[root@master job]# kubectl apply -f deploy.yaml
deployment.apps/dm-wordpress created
[root@master job]# kubectl apply -f db.yaml
deployment.apps/wordpress-db created
[root@master job]# kubectl apply -f svc.yaml
service/svc-db created
service/svc-wp created
[root@master job]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.200.0.1 <none> 443/TCP 101m
svc-db NodePort 10.200.200.200 <none> 3306:30001/TCP 11s
svc-wp NodePort 10.200.34.64 <none> 80:31000/TCP 11s
3.响应式创建一个pod容器
[root@master ~]# kubectl run demo-dns --rm -it --image=alpine:latest -- sh
If you don't see a command prompt, try pressing enter.
/ # ping svc-wp
PING svc-wp (10.200.34.64): 56 data bytes
·第二种方式验证
1.dig一个svc名称
[root@master ~]# dig @10.200.0.10 svc-wp.default.svc.test.com +short
10.200.34.64
3.使用coreDNS
·将wordpress指向数据库svc的IP换成名称
[root@master job]# cat deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: dm-wordpress
spec:replicas: 1selector:matchLabels:k8s: k8stemplate:metadata:labels:k8s: k8sspec:containers:- name: wp-cimage: wordpress:latestports:- name: wp-portcontainerPort: 80env:- name: WORDPRESS_DB_HOST#value: 10.200.200.200:3306#将IP改成svc的名字value: svc-db:3306- name: WORDPRESS_DB_USERvalue: wordpress- name: WORDPRESS_DB_PASSWORDvalue: wordpress- name: WORDPRESS_DB_NAMEvalue: wordpress