Kubernetes的DNS服务是基于SkyDNS实现的,同时又需要和API Server紧密沟通,它的基本工作方式是通过API Server监视服务创建,一旦有新的服务创建就通知SkyDNS创建一条域名解析记录。沟通API Server和SkyDNS的工作都是由Kube2Sky完成的,Kube2sky和Skydns都需要使用ETCD实现共享配置和服务发现。以下是ETCD的架构图,它清晰地阐明了 
Kubernetes实现DNS的方式。

     Kubernetes DNS 服务安装配置_第1张图片


    关于Skydns和Kube2sky是在本地安装还是以Pod的方式安装到k8s集群里,笔者在网上看到两种方式都有,但是笔者对本地安装的方式持怀疑态度,主要是涉及到虚拟网络和物理网络的联通性问题,具体地说就是Skydns Server的IP应该kube集群虚拟网络中的某个IP地址,也就是说这个IP需要在kube-apiserver启动参数–service-cluster-ip-range指定的IP地址范围内。而Skydns如果是本地化安装,是无法绑定DNS Server的IP为一个虚拟网络的IP(就是参数-addr的值)。笔者倾向使用镜像方式安装。


安装


文件内容kubernetes-dns.yaml如下:

apiVersion: v1
kind: ReplicationController
metadata:
  name: kube-dns-v6
  namespace: default
  labels:
    k8s-app: kube-dns
    version: v6
    kubernetes.io/cluster-service: "true"
spec:
  replicas: 1
  selector:
    k8s-app: kube-dns
    version: v6
  template:
    metadata:
      labels:
        k8s-app: kube-dns
        version: v6
        kubernetes.io/cluster-service: "true"
    spec:
      containers:
      - name: etcd
        p_w_picpath: index.tenxcloud.com/google_containers/etcd-amd64:2.2.1
        command:
        - /bin/etcd
        - -listen-client-urls
        - http://0.0.0.0:2379,http://0.0.0.0:4001
        - -advertise-client-urls
        - http://127.0.0.1:2379,http://127.0.0.1:4001
        - -initial-cluster-token
        - skydns-etcd
        volumeMounts:
        - name: etcd-storage
          mountPath: /var/etcd/data
      - name: kube2sky
        p_w_picpath: index.tenxcloud.com/google_containers/kube2sky:1.14
        resources:
          limits:
            cpu: 100m
            memory: 50Mi
        livenssProbe:
          httpGet:
            path: /healthz
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 60
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 5
        readinessProbe:
          httpGet:
            path: /readiness
            port: 8081
            scheme: HTTP
        initalDelaySeconds: 30
        timeoutSeconds: 5
      args:
      - --domain=cluster.local
      - name: skydns
        p_w_picpath: index.tenxcloud.com/google_containers/skydns:2015-10-13-8c72f8c
        resources:
        args:
        - -machines=http://127.0.0.1:4001
        - -addr=0.0.0.0:53
        - -ns-rotate=false
        - -domain=cluster.local.
        ports:
        - containerPort: 53
          name: dns
          protocol: UDP
        - containerPort: 53
          name: dns-tcp
          protocol: TCP
      dnsPolicy: Default
     - name: healthz
       p_w_picpath: index.tenxcloud.com/google_containers/exechealthz:1.0
       resources:
         limits:
            cpu: 100m
            memory: 50Mi
       args:
       - -cmd=nslookup kubernetes.defaults.svc.cluster.local 127.0.0.1 >/dev/null
       - -port=8080
       ports:
       - containerPort: 8080
         protocol: TCP


使用如下命令:  kubectl create -f  kubernetes-dns.yaml