部署coredns自动发现

简介

kube-dns,是 Kubernetes 官方的 DNS 解析组件。从 1.11 版本开始,kubeadm 已经使用第三方的 CoreDNS 替换官方的 kubedns 作为 Kubernetes 集群的内部域名解析组件。
CoreDNS是一个Go语言实现的链式插件DNS服务端,是CNCF成员,是一个高性能、易扩展的DNS服务端。可以很方便的部署在k8s集群中,用来代替kube-dns。
在K8s集群里,POD的IP是不断变化的,如何“以不变应万变”
  • 抽象出了Service资源,通过标签选择器,关联一组Pod
  • 抽象出了集群网络,通过相对固定的“集群IP”,使服务接入点固定
那么如何自动关联Service资源的“名称”和“集群网络IP”,从而达到服务被集群自动发现的目的呢
  • 考虑传统的DNS的模型:hdss7-21.host.com => 10.4.7.21
  • 能否在k8s里建立这样的模型:nginx-dp => 192.168.145.225

Kubernetes 中的域名是如何解析的

在 Kubernetes 中,比如服务 a 访问服务 b,对于同一个 Namespace下,可以直接在 pod 中,通过 curl b 来访问。对于跨 Namespace 的情况,服务名后边对应 Namespace即可。
DNS 如何解析,依赖容器内 resolv 文件的配置
[root@hdss7-21 ~]# kubectl exec -it nginx-ds-wkf9w sh
# cat /etc/resolv.conf
nameserver 192.168.0.2
search default.svc.cluster.local svc.cluster.local cluster.local host.com
options ndots:5

部署Coredns

配置k8s资源配置清单的内网http服务

[root@hdss7-200 conf.d]# vim k8s-yaml.conf
server {
    listen       80;
    server_name  k8s-yaml.od.com;


    location / {
        autoindex on;
        default_type text/plain;
        root /data/k8s-yaml;
    }
}
[root@hdss7-200 conf.d]# mkdir /data/k8s-yaml
[root@hdss7-200 conf.d]# nginx -s reload
[root@hdss7-200 k8s-yaml]# mkdir coredns
[root@hdss7-200 k8s-yaml]# cd coredns/
添加A记录
[root@hdss7-11 ~]# vim /var/named/od.com.zone
$ORIGIN od.com.
$TTL 600  ; 10 minutes
@ IN SOA dns.od.com. dnsadmin.od.com. (
       2019111403  ; serial     ; 滚动一位数
       10800     ; refresh (3 hours)
       900       ; retry (15 minutes)
       604800    ; expire (1 week)
       86400     ; minimum (1 day)
)
       NS dns.od.com.
$TTL 60  ; 1 minutes
dns       A  10.4.7.11
harbor    A  10.4.7.200
k8s-yaml  A  10.4.7.200
[root@hdss7-11 ~]# systemctl restart named
[root@hdss7-11 ~]# dig -t A k8s-yaml.od.com @10.4.7.11 +short
10.4.7.200

部署kube-dns(coredns)

准备coredns镜像
[root@hdss7-200 coredns]# docker pull coredns/coredns:1.6.5
[root@hdss7-200 coredns]# docker tag coredns/coredns:1.6.5 harbor.od.com/public/coredns:v1.6.5
[root@hdss7-200 coredns]# docker push harbor.od.com/public/coredns:v1.6.5
准备资源配置清单
rbac
[root@hdss7-200 coredns]# vim rabc.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
[root@hdss7-200 coredns]# vim configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: coredns
  namespace: kube-system
data:
  Corefile: |
    .:53 {
        errors
        log
        health
        ready
        kubernetes cluster.local 192.168.0.0/16  # service的虚拟IP地址范围
        forward . 10.4.7.11                                   # 指定上级dns
        cache 30
        loop
        reload
        loadbalance
       }
deployment
[root@hdss7-200 coredns]# vim deployment.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.od.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

[root@hdss7-200 coredns]# vim service.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: 192.168.0.2     # dns的
  ports:
  - name: dns
    port: 53
    protocol: UDP
  - name: dns-tcp
    port: 53
  - name: metrics
    port: 9153
    protocol: TCP
创建资源清单
[root@hdss7-21 ~]# kubectl apply -f http://k8s-yaml.od.com/coredns/rbac.yaml
serviceaccount/coredns created
clusterrole.rbac.authorization.k8s.io/system:coredns created
clusterrolebinding.rbac.authorization.k8s.io/system:coredns created
[root@hdss7-21 ~]# kubectl apply -f http://k8s-yaml.od.com/coredns/configmap.yaml
configmap/coredns created
[root@hdss7-21 ~]# kubectl apply -f http://k8s-yaml.od.com/coredns/deployment.yaml
deployment.apps/coredns created
[root@hdss7-21 ~]# kubectl apply -f http://k8s-yaml.od.com/coredns/service.yaml
service/coredns created
[root@hdss7-21 ~]# kubectl get all -n kube-system
NAME                          READY   STATUS    RESTARTS   AGE
pod/coredns-9bc44c684-wj2nb   1/1     Running   0          2m46s

NAME              TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)                  AGE
service/coredns   ClusterIP   192.168.0.2   <none>        53/UDP,53/TCP,9153/TCP   5m36s

NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/coredns   1/1     1            1           5m47s

NAME                                 DESIRED   CURRENT   READY   AGE
replicaset.apps/coredns-9bc44c684    1         1         1       5m50s

dns检查

dig解析
[root@hdss7-21 ~]# kubectl get svc -n kube-public
NAME       TYPE        CLUSTER-IP        EXTERNAL-IP   PORT(S)   AGE
nginx-dp   ClusterIP   192.168.145.225   <none>        80/TCP    3d20h
[root@hdss7-21 ~]# dig -t A nginx-dp.kube-public.svc.cluster.local. @192.168.0.2 +short
192.168.145.225

容器里curl

[root@hdss7-21 ~]# kubectl get pod
NAME             READY   STATUS    RESTARTS   AGE
nginx-ds-wkf9w   1/1     Running   0          2d19h
nginx-ds-x5lrc   1/1     Running   0          2d19h
[root@hdss7-21 ~]# kubectl exec -it nginx-ds-wkf9w sh
# curl -I 192.168.145.225
HTTP/1.1 200 OK
Server: nginx/1.7.9
Date: Mon, 02 Dec 2019 02:49:34 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 23 Dec 2014 16:25:09 GMT
Connection: keep-alive
ETag: "54999765-264"
Accept-Ranges: bytes

# curl -I nginx-dp.kube-public.svc.cluster.local. == curl -I nginx-dp.kube-public.
HTTP/1.1 200 OK
Server: nginx/1.7.9
Date: Mon, 02 Dec 2019 02:49:49 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 23 Dec 2014 16:25:09 GMT
Connection: keep-alive
ETag: "54999765-264"
Accept-Ranges: bytes
点赞

发表评论

电子邮件地址不会被公开。

5 − 2 =