无法在Kubernetes群集中设置服务DNS

时间:2017-12-20 16:35:22

标签: dns kubernetes skydns

Kubernetes版本 - > 1.5.2

我是第一次为Kubernetes服务设置DNS,我遇到了SkyDNS。 所以关注文档,我的skydns-svc.yaml文件是:

apiVersion: v1
kind: Service
spec:
  clusterIP: 10.100.0.100
  ports:
  - name: dns
    port: 53
    protocol: UDP
    targetPort: 53
  - name: dns-tcp
    port: 53
    protocol: TCP
    targetPort: 53
  selector:
    k8s-app: kube-dns
  sessionAffinity: None
  type: ClusterIP

我的skydns-rc.yaml文件是:

apiVersion: v1
kind: ReplicationController
spec:
  replicas: 1
  selector:
    k8s-app: kube-dns
    version: v18
  template:
    metadata:
      creationTimestamp: null
      labels:
        k8s-app: kube-dns
        kubernetes.io/cluster-service: "true"
        version: v18
    spec:
      containers:
      - args:
        - --domain=kube.local
        - --dns-port=10053
        image: gcr.io/google_containers/kubedns-amd64:1.6
        imagePullPolicy: IfNotPresent
        name: kubedns
        ports:
        - containerPort: 10053
          name: dns-local
          protocol: UDP
        - containerPort: 10053
          name: dns-tcp-local
          protocol: TCP
        resources:
          limits:
            cpu: 100m
            memory: 200Mi
          requests:
            cpu: 100m
            memory: 100Mi
        terminationMessagePath: /dev/termination-log
      - args:
        - --cache-size=1000
        - --no-resolv
        - --server=127.0.0.1#10053
        image: gcr.io/google_containers/kube-dnsmasq-amd64:1.3
        imagePullPolicy: IfNotPresent
        name: dnsmasq
        ports:
        - containerPort: 53
          name: dns
          protocol: UDP
        - containerPort: 53
          name: dns-tcp
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
      - args:
        - -cmd=nslookup kubernetes.default.svc.kube.local 127.0.0.1 >/dev/null &&
          nslookup kubernetes.default.svc.kube.local 127.0.0.1:10053 >/dev/null
        - -port=8080
        - -quiet
        image: gcr.io/google_containers/exechealthz-amd64:1.0
        imagePullPolicy: IfNotPresent
        name: healthz
        ports:
        - containerPort: 8080
          protocol: TCP
        resources:
          limits:
            cpu: 10m
            memory: 20Mi
          requests:
            cpu: 10m
            memory: 20Mi

同样在我的爪牙上,我更新了/etc/systemd/system/multi-user.target.wants/kubelet.service文件,并在ExecStart部分下添加了以下内容:

ExecStart=/usr/bin/kubelet \
        $KUBE_LOGTOSTDERR \
        $KUBE_LOG_LEVEL \
        $KUBELET_API_SERVER \
        $KUBELET_ADDRESS \
        $KUBELET_PORT \
        $KUBELET_HOSTNAME \
        $KUBE_ALLOW_PRIV \
        $KUBELET_POD_INFRA_CONTAINER \
        $KUBELET_ARGS \
            --cluster-dns=10.100.0.100 \
            --cluster-domain=kubernetes \

完成所有这些并成功提升了rc& svc

[root@kubernetes-master DNS]# kubectl get po | grep dns
kube-dns-v18-hl8z6                                                3/3       Running             0          6s
[root@kubernetes-master DNS]# kubectl get svc | grep dns
kube-dns                            10.100.0.100     <none>        53/UDP,53/TCP                                                    20m

这就是我从配置的角度来看的全部内容。现在,为了测试我的设置,我下载了busybox并测试了nslookup

[root@kubernetes-master DNS]# kubectl get svc | grep kubernetes
kubernetes                          10.100.0.1       <none>        443/TCP 

[root@kubernetes-master DNS]# kubectl exec busybox -- nslookup kubernetes
nslookup: can't resolve 'kubernetes'
Server:    10.100.0.100
Address 1: 10.100.0.100

有什么我错过的吗?

编辑::

通过日志,我看到一些可以解释为什么这不起作用的东西:

kubectl logs $(kubectl get pods -l k8s-app=kube-dns -o name) -c kubedns
.
.
.
E1220 17:44:48.403976       1 reflector.go:216] pkg/dns/dns.go:154: Failed to list *api.Endpoints: Get https://10.100.0.1:443/api/v1/endpoints?resourceVersion=0: x509: failed to load system roots and no roots provided
E1220 17:44:48.487169       1 reflector.go:216] pkg/dns/dns.go:155: Failed to list *api.Service: Get https://10.100.0.1:443/api/v1/services?resourceVersion=0: x509: failed to load system roots and no roots provided
I1220 17:44:48.487716       1 dns.go:172] Ignoring error while waiting for service default/kubernetes: Get https://10.100.0.1:443/api/v1/namespaces/default/services/kubernetes: x509: failed to load system roots and no roots provided. Sleeping 1s before retrying.
E1220 17:44:49.410311       1 reflector.go:216] pkg/dns/dns.go:154: Failed to list *api.Endpoints: Get https://10.100.0.1:443/api/v1/endpoints?resourceVersion=0: x509: failed to load system roots and no roots provided
I1220 17:44:49.492338       1 dns.go:172] Ignoring error while waiting for service default/kubernetes: Get https://10.100.0.1:443/api/v1/namespaces/default/services/kubernetes: x509: failed to load system roots and no roots provided. Sleeping 1s before retrying.
E1220 17:44:49.493429       1 reflector.go:216] pkg/dns/dns.go:155: Failed to list *api.Service: Get https://10.100.0.1:443/api/v1/services?resourceVersion=0: x509: failed to load system roots and no roots provided
.
.
.

看起来kubedns无法针对K8S主节点进行授权。我甚至尝试过手动呼叫:

curl -k https://10.100.0.1:443/api/v1/endpoints?resourceVersion=0
Unauthorized

1 个答案:

答案 0 :(得分:1)

看起来kube-dns pod无法通过kubernetes api服务器进行身份验证。我在secret广告连播的YAML文件中看不到任何serviceaccountkube-dns

我建议做以下事情:

  1. 使用secret pod为kubectl create secret创建一个k8s kube-dns,其中包含正确的证书文件ca.crttoken

    $ kubectl get secrets -n=kube-system | grep dns
    kube-dns-token-66tfx     kubernetes.io/service-account-token   3      1d
    
  2. 使用serviceaccount广告单元kubectl create serviceaccount创建k8s kube-dns

    $ kubectl get serviceaccounts -n=kube-system | grep dns
    kube-dns               1         1d`
    
  3. secret安装在YAML文件中/var/run/secrets/kubernetes.io/serviceaccount容器内的kube-dns处:

    ...
    kind: Pod
    ...
    spec:
      ...
      containers:
        ...
        volumeMounts:
        - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
          name: kube-dns-token-66tfx
          readOnly: true
      ...
      volumes:
      - name: kube-dns-token-66tfx
        secret:
          defaultMode: 420
          secretName: kube-dns-token-66tfx
    
  4. 以下是有关为pod创建服务帐户的链接:

    https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/

    https://kubernetes.io/docs/admin/service-accounts-admin/