K8s – CoreDNS

服务发现

服务之间互相定位的过程,以适应如下服务特点:

  • 动态性强;
  • 更新发布频繁;
  • 支持自动伸缩;

在k8s集群里,pod ip是不断变化的,如何找到对应的pods?首先集群网络抽象了集群IP,Service资源绑定Cluster IP并暴露这个资源,Service本身根据标签选择器,来锁定一组pods,并通过ipvs算法分配这组pods的资源。也可以参考上一篇的总结:管理Service资源

那么新的问题来了,虽然可以通过Service找到pod,但如果有很多不同的Services,很难把所有Services与Customer IP的关系记下来,这个是不是很像DNS被创造出来的初衷^_^~?那我们是否可以通过类似DNS的机制,绑定Service和Cluster IP呢?答案是肯定的:

  • Kube-dns,k8s v1.10之前默认使用;
  • Coredns,k8s v1.11以后默认使用;

安装部署CoreDNS

不在使用二进制的方式部署,而是采用容器的方式部署。

CoreDNS的Web Server

在f0-11的bind9上,增加k8s-yaml的域名:

root@f0-11:~# more /etc/bind/db.frank.com |grep k8s-yaml -C 2
dns             A       172.20.0.11
harbor          A       172.20.0.15
k8s-yaml        A       172.20.0.15
root@f0-11:~# systemctl restart bind9
root@f0-11:~# dig -t A k8s-yaml.frank.com @172.20.0.11 +short
172.20.0.15

在f0-15上,增加k8s-yaml的nginex配置文件

root@f0-15:/etc/nginx/conf.d# more k8s-yaml.frank.com.conf 
server {
    listen       80;
    server_name  k8s-yaml.frank.com;

    location / {
        autoindex on;
        default_type text/plain;
        root /data/k8s-yaml;
    }
}
root@f0-15:/etc/nginx/conf.d# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@f0-15:/etc/nginx/conf.d# systemctl restart nginx
root@f0-15:~# curl k8s-yaml.frank.com
<html>
<head><title>Index of /</title></head>
<body>
<h1>Index of /</h1><hr><pre><a href="../">../</a>
<a href="coredns/">coredns/</a>                                           14-Aug-2022 03:10                   -
</pre><hr></body>
</html>

上传CoreDNS的Image

!$”:把上一条命令的参数作为标准输入;

root@f0-15:~# docker pull coredns/coredns:1.6.5
1.6.5: Pulling from coredns/coredns
c6568d217a00: Pull complete 
fc6a9081f665: Pull complete 
Digest: sha256:7ec975f167d815311a7136c32e70735f0d00b73781365df1befd46ed35bd4fe7
Status: Downloaded newer image for coredns/coredns:1.6.5
docker.io/coredns/coredns:1.6.5
root@f0-15:~# docker images|grep coredns
coredns/coredns                 1.6.5                      70f311871ae1   2 years ago   41.6MB
root@f0-15:~# docker tag 70f311871ae1 harbor.frank.com/public/coredns:v1.6.5
root@f0-15:~# docker push !$
docker push harbor.frank.com/public/coredns:v1.6.5
The push refers to repository [harbor.frank.com/public/coredns]
7c9b0f448297: Pushed 
225df95e717c: Pushed 

资源配置清单

下面的配置清单都是参考https://github.com/kubernetes/kubernetes/blob/release-1.15/cluster/addons/dns/coredns/coredns.yaml.base

root@f0-15:~# mkdir -p /data/k8s-yaml/coredns

RBAC

root@f0-15:~# more /data/k8s-yaml/coredns/rbac.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: coredns
  namespace: kube-system
  labels:
      kubernetes.io/cluster-service: "true"
      addonmanager.kubernetes.io/mode: Reconcile
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  labels:
    kubernetes.io/bootstrapping: rbac-defaults
    addonmanager.kubernetes.io/mode: Reconcile
  name: system:coredns
rules:
- apiGroups:
  - ""
  resources:
  - endpoints
  - services
  - pods
  - namespaces
  verbs:
  - list
  - watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  annotations:
    rbac.authorization.kubernetes.io/autoupdate: "true"
  labels:
    kubernetes.io/bootstrapping: rbac-defaults
    addonmanager.kubernetes.io/mode: EnsureExists
  name: system:coredns
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: system:coredns
subjects:
- kind: ServiceAccount
  name: coredns
  namespace: kube-system

ConfigMap

cluster.local==Cluster IP网段,这个跟kube-controller和apiserver配置文件中的service-cluster-ip-range是一样的;而forward则是宿主机的DNS。

root@f0-15:~# more /data/k8s-yaml/coredns/cm.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: coredns
  namespace: kube-system
data:
  Corefile: |
    .:53 {
        errors
        log
        health
        ready
        kubernetes cluster.local 10.254.0.0/16
        forward . 172.20.0.11
        cache 30
        loop
        reload
        loadbalance
       }

Deployment

root@f0-15:~# more /data/k8s-yaml/coredns/dp.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: coredns
  namespace: kube-system
  labels:
    k8s-app: coredns
    kubernetes.io/name: "CoreDNS"
spec:
  replicas: 1
  selector:
    matchLabels:
      k8s-app: coredns
  template:
    metadata:
      labels:
        k8s-app: coredns
    spec:
      priorityClassName: system-cluster-critical
      serviceAccountName: coredns
      containers:
      - name: coredns
        image: harbor.frank.com/public/coredns:v1.6.5
        args:
        - -conf
        - /etc/coredns/Corefile
        volumeMounts:
        - name: config-volume
          mountPath: /etc/coredns
        ports:
        - containerPort: 53
          name: dns
          protocol: UDP
        - containerPort: 53
          name: dns-tcp
          protocol: TCP
        - containerPort: 9153
          name: metrics
          protocol: TCP
        livenessProbe:
          httpGet:
            path: /health
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 60
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 5
      dnsPolicy: Default
      volumes:
        - name: config-volume
          configMap:
            name: coredns
            items:
            - key: Corefile
              path: Corefile

Service

svc的clusterIP在部署k8s的kubelet时已经设置了,详细看这里

root@f0-15:~# more /data/k8s-yaml/coredns/svc.yaml 
apiVersion: v1
kind: Service
metadata:
  name: coredns
  namespace: kube-system
  labels:
    k8s-app: coredns
    kubernetes.io/cluster-service: "true"
    kubernetes.io/name: "CoreDNS"
spec:
  selector:
    k8s-app: coredns
  clusterIP: 10.254.0.2
  ports:
  - name: dns
    port: 53
    protocol: UDP
  - name: dns-tcp
    port: 53
  - name: metrics
    port: 9153
    protocol: TCP

部署coreDNS

root@f0-13:~# kubectl apply -f http://k8s-yaml.frank.com/coredns/rbac.yaml
serviceaccount/coredns created
clusterrole.rbac.authorization.k8s.io/system:coredns created
clusterrolebinding.rbac.authorization.k8s.io/system:coredns created
root@f0-13:~# 
root@f0-13:~# kubectl apply -f http://k8s-yaml.frank.com/coredns/cm.yaml
configmap/coredns created
root@f0-13:~# 
root@f0-13:~# kubectl apply -f http://k8s-yaml.frank.com/coredns/dp.yaml
deployment.apps/coredns created
root@f0-13:~# 
root@f0-13:~# kubectl apply -f http://k8s-yaml.frank.com/coredns/svc.yaml
service/coredns created
root@f0-13:~# 
root@f0-13:~# kubectl get all -n kube-system
NAME                         READY   STATUS    RESTARTS   AGE
pod/coredns-684d79cc-55sst   1/1     Running   0          29s


NAME              TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE
service/coredns   ClusterIP   10.254.0.2   <none>        53/UDP,53/TCP,9153/TCP   24s


NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/coredns   1/1     1            1           29s

NAME                               DESIRED   CURRENT   READY   AGE
replicaset.apps/coredns-684d79cc   1         1         1       29s

验证CoreDNS

通过diag指定coredns 10.254.0.2作为dns server,确认是否可以返回外网、内网域名解析信息,这里返回的信息是通过forward给172.20.0.11获得的:

root@f0-13:~# dig -t A www.baidu.com @10.254.0.2 +short
www.a.shifen.com.
180.101.49.12
180.101.49.11
root@f0-13:~# dig -t A harbor.frank.com @10.254.0.2 +short
172.20.0.15

确认coredns的核心功能,也就是service跟clusterIP的mapping关系:

root@f0-13:~# kubectl get svc -o wide -n kube-public
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE    SELECTOR
my-service   ClusterIP   10.254.157.92   <none>        82/TCP    2d5h   app=nginx-test
nginx-test   ClusterIP   10.254.111.99   <none>        80/TCP    3d     app=nginx-test

想在宿主机上通过coreDNS解析service名称,需要使用FQDN:

root@f0-13:~# dig -t A nginx-test @10.254.0.2 +short
root@f0-13:~# dig -t A nginx-test.kube-public.svc.cluster.local. @10.254.0.2 +short
10.254.111.99

或者设置search domain,但在宿主机上一般是不会设置coreDNS的search domain的,进入pod可以看出来,search domain已经默认加进去了,可以直接解析,不用FQDN;

root@f0-13:~# kubectl get pods -o wide -n kube-public
NAME                          READY   STATUS    RESTARTS   AGE   IP           NODE             NOMINATED NODE   READINESS GATES
nginx-test-7d6f7dbd58-4t4j9   1/1     Running   2          3d    172.1.13.2   f0-13.host.com   <none>           <none>
nginx-test-7d6f7dbd58-dgbvx   1/1     Running   1          3d    172.1.14.3   f0-14.host.com   <none>           <none>
root@f0-13:~# kubectl exec -ti nginx-test-7d6f7dbd58-dgbvx /bin/bash -n kube-public
root@nginx-test-7d6f7dbd58-dgbvx:/# ping nginx-test
PING nginx-test.kube-public.svc.cluster.local (10.254.111.99): 56 data bytes
64 bytes from 10.254.111.99: icmp_seq=0 ttl=64 time=0.480 ms
64 bytes from 10.254.111.99: icmp_seq=1 ttl=64 time=0.080 ms
^C--- nginx-test.kube-public.svc.cluster.local ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.080/0.280/0.480/0.200 ms
root@nginx-test-7d6f7dbd58-dgbvx:/# more /etc/resolv.conf 
nameserver 10.254.0.2
search kube-public.svc.cluster.local svc.cluster.local cluster.local host.com
options ndots:5
本文出自 Frank's Blog

版权声明:


本文链接:K8s – CoreDNS
版权声明:本文为原创文章,仅代表个人观点,版权归 Frank Zhao 所有,转载时请注明本文出处及文章链接
你可以留言,或者trackback 从你的网站

留言哦

blonde teen swallows load.xxx videos