nodelocaldns 造成 coredns hosts 插件失效

问题

因本地测试需要做些假域名映射到本地 ip, 查看 coredns 文档, 发现可以用如下方式来设置域名到 ip 的映射

.:53 {
        errors
        health
        ready
        kubernetes cluster.local in-addr.arpa ip6.arpa {
          pods insecure
          upstream /etc/resolv.conf
          fallthrough in-addr.arpa ip6.arpa
        }
        hosts {
          172.16.13.14 testk8s.xxx.com
          fallthrough
        }
        prometheus :9153
        forward . /etc/resolv.conf {
          prefer_udp
        }
        cache 30
        loop
        reload
        loadbalance
    }

但是一直都不成功

nodelocaldns 简介

NodeLocal DNSCache通过在集群上运行一个dnsCache daemonset来提高clusterDNS性能和可靠性。在ACK集群上的一些测试表明:相比于纯coredns方案,nodelocaldns + coredns方案能够大幅降低DNS查询timeout的频次,提升服务稳定性。

nodelocaldns通过添加iptables规则能够接收节点上所有发往169.254.20.10dns查询请求,把针对集群内部域名查询请求路由到coredns;把集群外部域名请求直接通过host网络发往集群外部dns服务器。

而使用 kubespray 部署 k8s 会默认部署 nodelocaldns

分析

之所以 corednshosts 插件不起作用, 是因为集群 pod 使用的 dns169.254.20.10, 也就是请求 nodelocaldns,而 nodelocaldns 配置的 forward 如下

Corefile: |
    cluster.local:53 {
        errors
        cache {
            success 9984 30
            denial 9984 5
        }
        reload
        loop
        bind 169.254.25.10
        forward . 10.233.0.3 {
            force_tcp
        }
        prometheus :9253
        health 169.254.25.10:9254
    }
    in-addr.arpa:53 {
        errors
        cache 30
        reload
        loop
        bind 169.254.25.10
        forward . 10.233.0.3 {
            force_tcp
        }
        prometheus :9253
    }
    ip6.arpa:53 {
        errors
        cache 30
        reload
        loop
        bind 169.254.25.10
        forward . 10.233.0.3 {
            force_tcp
        }
        prometheus :9253
    }
    .:53 {
        errors
        cache 30
        reload
        loop
        bind 169.254.25.10
        forward . /etc/resolv.conf
        prometheus :9253
    }

10.233.0.3corednsservice ip, 所以集群内部域名会转发给 coredns, 而非集群内部域名会转发给 /etc/resolv.conf, 根本就不会转发给 coredns, 所以 coredns 里面配置的 hosts 自然不会生效

直接在 nodelocaldns 中配置 rewritehosts ,结果发现 nodelocaldns 镜像集成的 coredns 版本不支持这 2 个插件(plugin)

解决

方法一

参考:https://github.com/coredns/coredns/issues/3298

nodelocaldnsforward 参数

kubectl edit configmap nodelocaldns -n kube-system

.:53 {
        errors
        cache 30
        reload 
        loop
        bind 169.254.25.10
        forward . /etc/resolv.conf
        prometheus :9253
    }

改为

.:53 {
        errors
        cache 30
        reload 
        loop
        bind 169.254.25.10
        forward . 10.233.0.3 {
            force_tcp
        }
        prometheus :9253
    }

然后重启nodelocaldns(重启节点上的 docker 容器或者 kubectl 删除 pod)

方法二

手动搭建一个 dns 服务器, 在自己搭建的 dns 服务器里面配置映射, 参考:https://hub.docker.com/r/jpillora/dnsmasq

先创建 /opt/dnsmasq.conf 文件

#dnsmasq config, for a complete example, see:
#  http://oss.segetech.com/intra/srv/dnsmasq.conf
#log all dns queries
log-queries
#dont use hosts nameservers
no-resolv
#use cloudflare as default nameservers, prefer 1^4
server=223.5.5.5
server=8.8.8.8
strict-order
#serve all .company queries using a specific nameserver
# server=/company/10.0.0.1
#explicitly define host-ip mappings
address=/myhost.company/10.0.0.2
address=/myhost1.company/10.0.0.3

启动容器

docker run  --name dnsmasq -d  -p 172.16.13.14:53:53/udp -p 5380:8080 -v /opt/dnsmasq.conf:/etc/dnsmasq.conf --log-opt "max-size=100m" -e "HTTP_USER=admin" -e "HTTP_PASS=admin" --restart always jpillora/dnsmasq

然后修改各节点上的 dns 配置为我们搭建的 dns 服务, 最后使用 kubectl 删除 nodelocaldns pod,让 k8s 重建 nodelocaldns

参考

终于解决 k8s 集群中部署 nodelocaldns 的问题

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值