Life is too bug

CoreDNS-基于Caddy的DNS Server

2019.04.25

CoreDNS利用作为Web服务器Caddy的一部分而开发的服务器框架。该框架具有非常灵活,可扩展的模型,用于通过各种中间件组件传递请求。这些中间件组件根据请求提供不同的操作,例如记录,重定向,修改或维护。虽然它一开始作为Web服务器,但是Caddy并不是专门针对HTTP协议的,所以也是开发CoreDNS的理想框架。

Install

如果使用Kubeadm初始化的集群,默认已经安装了CoreDNS。默认配置文件如下:

cat /etc/coredns/Corefile
.:53 {
    errors
    health
    kubernetes cluster.local in-addr.arpa ip6.arpa {
       pods insecure
       upstream
       fallthrough in-addr.arpa ip6.arpa
    }
    prometheus :9153
    proxy . /etc/resolv.conf
    cache 30
    reload
    loadbalance
}

9153/metrics 是默认的metrics端口 :8080/health 是健康检查的端口,除此之外需要注意的是添加了一个kernel capabilities,DNS策略是默认。只会对cluster.local进行解析,其他域名则是转发到上游DNS服务器。

        securityContext:
          allowPrivilegeEscalation: false
          capabilities:
            add:
            - NET_BIND_SERVICE
            drop:
            - all

CoreDNS相当于创建了一个Kubernetes中间件。该中间件使用Kubernetes API来满足针对特定Kubernetes pod或服务的DNS请求。而且由于Kube-DNS作为Kubernetes的另一项服务,kubelet和CoreDNS之间没有紧密的绑定。只需要将DNS服务的IP地址和域名传递给kubelet,而Kubernetes并不关心谁在实际处理该IP请求。和DNS相关的配都在kubelet上面,分别是:

  • –cluster-dns
  • –cluster-domain

除此之外Pod的dnsPolicy 可选参数为’ClusterFirstWithHostNet', ‘ClusterFirst’, ‘Default’ or ‘None’,而当Pod设置dnsPolicy为ClusterFirst时 ,即可在pod内生成以下/etc/resolv.conf文件,注意此文件与namespace相关。

nameserver 10.96.0.10
search default.svc.cluster.local svc.cluster.local cluster.local
options ndots:5

记录类型

Service

普通Server会记录一个A记录一个SRV记录。

kubectl run nginx --image=nginx:alpine --port=80
kubectl expose deployment nginx
#A记录
dig +search nginx +short
10.109.36.35
#SRV记录
dig +search nginx SRV +short
0 100 80 nginx.default.svc.cluster.local.

Statefulset 也就是Headless Service

cat <<EOF | kubectl create -f -
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: "sts-ngx"
spec:
  serviceName: "nginx"
  replicas: 2
  template:
    metadata:
      name: "sts-ngx"
      labels:
        app: sts-ngx
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  clusterIP: None
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: sts-ngx
  sessionAffinity: None
  type: ClusterIP
EOF
#查询SVC
dig +search nginx

nginx.default.svc.cluster.local. 5 IN   A       10.244.0.120
nginx.default.svc.cluster.local. 5 IN   A       10.244.0.119
#查询单个Pod

dig sts-ngx-0.nginx.default.svc.cluster.local. +short
10.244.0.119

ExternalName 对接外部的已有服务(似乎没有什么卵用

cat <<EOF | kubectl create -f -
kind: Service
apiVersion: v1
metadata:
  name: extra
spec:
  type: ExternalName
  externalName: baidu.com
EOF
# 
nslookup extra
Server:         10.96.0.10
Address:        10.96.0.10#53

extra.default.svc.cluster.local canonical name = baidu.com.
Name:   baidu.com
Address: 220.181.57.216
Name:   baidu.com
Address: 123.125.114.144

Pod

第一种是基于IP的A记录

kubectl get po -owide
NAME                    READY   STATUS    RESTARTS   AGE   IP             NODE             NOMINATED NODE   READINESS GATES
curl-76745477df-p76b4   1/1     Running   1          17h   10.244.0.97    k8s-master        <none>           <none>

nslookup 10-244-0-97.default.pod.cluster.local
Server:         10.96.0.10
Address:        10.96.0.10#53

Name:   10-244-0-97.default.pod.cluster.local
Address: 10.244.0.97

第二种是基于pod的 hostname 和subdomain,不过我测试好像是失败的。

   subdomain	<string>
     If specified, the fully qualified Pod hostname will be
     "<hostname>.<subdomain>.<pod namespace>.svc.<cluster domain>". If not
     specified, the pod will not have a domainname at all.

其他插件

rewrite允许重定向解析的URL,支持substring以及正则

hosts 允许读取本地hosts文件等等

Ref

CoreDNS for kubernetes Service Discovery https://www.cnblogs.com/boshen-hzb/p/7511432.html DNS Pod 与 Service https://kubernetes.io/zh/docs/concepts/services-networking/dns-pod-service/

comments powered by Disqus