Kubernetes CoreDNS 配置

Summary: Author: 张亚飞 | Read Time: 2 minute read | Published: 2022-03-28
Filed under Categories: KubernetesTags: Kubernetes, Cilium,

Kubernetes CoreDNS 配置

需要配置测试域名,在 k8s 宿主机配置 /etc/hosts 不生效

103.234.22.131 brtc-dataserver.example.com

vconsole 容器 ping 不同通

ping: bad address 'brtc-dataserver.example.com'

使用 nslookup 查看 dns 服务器

/app # nslookup brtc-dataserver.example.com
Server:		169.254.25.10
Address:	169.254.25.10:53

** server can't find brtc-dataserver.example.com: NXDOMAIN

** server can't find brtc-dataserver.example.com: NXDOMAIN

/app # nslookup brtc-dataserver.example.com
Server:		169.254.25.10
Address:	169.254.25.10:53

** server can't find brtc-dataserver.example.com: NXDOMAIN

** server can't find brtc-dataserver.example.com: NXDOMAIN

CoreDNS 有一个插件 hosts 可以解决此问题,修改 CoreDNS config-map coredns 配置

kubectl edit cm -n kube-system coredns

加上 hosts 配置

hosts /etc/coredns/customdomains.db example.com {
    103.234.22.131 brtc-dataserver.example.com
    fallthrough
}

完成配置如下:

apiVersion: v1
 data:
   Corefile: |
     .:53 {
         debug
         errors
         health {
            lameduck 5s
         }
         ready
         kubernetes cluster.local in-addr.arpa ip6.arpa {
            pods insecure
            fallthrough in-addr.arpa ip6.arpa
            ttl 30
         }
         prometheus :9153
         forward . /etc/resolv.conf {
            max_concurrent 1000
         }
         cache 30
         loop
         reload
         loadbalance
         hosts /etc/coredns/customdomains.db example.com {
           103.234.22.131 brtc-dataserver.example.com
           fallthrough
         }
     }

CoreDNS 需要重启才能生效

kubectl -n kube-system delete pod/coredns-58899f4d89-fflv2

或者使用以下命令 - 未验证

参考: [](https://blog.csdn.net/networken/article/details/132113051)

kubectl -n kube-system rollout restart deployment.apps/coredns

验证解析是否生效

/app # ping  brtc-dataserver.example.com
PING brtc-dataserver.example.com (103.234.22.131): 56 data bytes
64 bytes from 103.234.22.131: seq=0 ttl=60 time=6.060 ms

使用 nslookup 查看

/app # nslookup brtc-dataserver.example.com
Server:		169.254.25.10
Address:	169.254.25.10:53

Name:	brtc-dataserver.example.com
Address: 103.234.22.131

以上配置删除 /etc/coredns/customdomains.db example.com 改成下面等效

hosts {
    103.234.22.131 brtc-dataserver.example.com
    fallthrough
}

记得配置 nodelocaldns,否则配置不生效

kubectl -n kube-system edit cm nodelocaldns

例如:

brtc-dataserver.example.com:53 {
    errors
    cache 30
    reload
    loop
    bind 169.254.25.10
    forward . 10.233.0.3 {
        force_tcp
    }
}

Add a Custom Host to Kubernetes Add a Custom Host to Kubernetes coredns添加静态DNS的方法


如何进入 CoreDNS 容器内部

需要进 CoreDNS 容器查看服务配置,但是无法进入.

执行命令查看 CoreDNS 容器

$ kubectl get pod --all-namespaces -o=wide | grep coredns
kube-system                    coredns-58899f4d89-djmbx                                   1/1     Running     3 (16d ago)       72d     10.233.91.246   bjy-idc-bdata-k8s-test01   <none>           <none>
kube-system                    coredns-58899f4d89-mt8vx                                   1/1     Running     3 (16d ago)       19d     10.233.91.47    bjy-idc-bdata-k8s-test01   <none>           <none>

进入其中一个容器报错:

$ kubectl exec -it coredns-58899f4d89-mt8vx -n kube-system /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: "/bin/sh": stat /bin/sh: no such file or directory: unknown
command terminated with exit code 126

解决办法,直接登录容器所在的节点,查到 CoreDNS 容器的ID,使用 docker 进入

docker ps -a | grep coredns
ID=d2bb3366ec18
docker run -it --net=container:$ID --pid=container:$ID --volumes-from=$ID alpine sh

How to get into CoreDNS pod kuberrnetes?

其它

DNS解析过程分析

ping www.baidu.com时出现www.a.shifen.com

www.baidu.com 解析过程分析

nslookup www.baidu.com
dig +trace www.baidu.com

Comments

Cor-Ethan, the beverage → www.iirii.com