終於解決 k8s 集羣中部署 nodelocaldns 的問題

自從開始在 kubernetes 集羣中部署 nodelocaldns 以提升 dns 解析性能以來,一直被一個問題困擾,只要一部署 nodelocaldns ,在 coredns 中添加的 rewrite 與 hosts 配置(以下)就失效,非常鬱悶。node

rewrite stop {
    name regex ([a-zA-Z0-9-]+)_([a-zA-Z0-9-]+)\.$ {1}-{2}.production.svc.cluster.local
    answer name ([a-zA-Z0-9-]+)-([a-zA-Z0-9-]+)\.production\.svc\.cluster\.local\.$ {1}_{2}
}
hosts {
    10.0.78.124   memcached
    ....
    fallthrough
}

部署使用的是下面的命令,在部署時將 nodelocaldns.yaml 中的幾個變量進行以下的替換。git

sed 's/k8s.gcr.io/gcr.azk8s.cn\/google_containers/g
s/__PILLAR__DNS__SERVER__/10.96.0.10/g
s/__PILLAR__LOCAL__DNS__/169.254.20.10/g
s/__PILLAR__DNS__DOMAIN__/cluster.local/g' nodelocaldns.yaml |
kubectl apply -n kube-system -f -

部署後其餘解析都正常,就是與 rewrite 與 hosts 配置相關的解析老是失敗。github

後來嘗試直接在 node-local-dns 中配置 rewrite 與 hosts ,結果發現 nodelocaldns 鏡像集成的 coredns 版本不支持這 2 個插件(plugin),更是鬱悶。bash

在準備放棄以前,今天再次嘗試解決這個問題,終於在 github 上一個 issue 的回覆中找到了解決方法,詳見 plugin/rewrite Not working in k8sapp

原來問題是 .:53 部分的 forward 配置引發的。tcp

進入 nodelocaldns 容器 cat /etc/Corefile 命令查看 .:53 部分的 forward 配置是 /etc/resolv.conf ,根本沒有轉發給集羣的 coredns ,難怪 rewrite 與 hosts 的配置不起做用。memcached

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

在 nodelocaldns.yaml 中這裏的 forward 配置對應的是一個變量 __PILLAR__UPSTREAM__SERVERS__oop

forward . __PILLAR__UPSTREAM__SERVERS__ {
        force_tcp
}

這個變量值是在部署 node-local-dns 時自動設置的。性能

The following variables will be set by the node-cache images - k8s.gcr.io/k8s-dns-node-cache:1.15.6 or later. The values will be determined by reading the kube-dns configMap for custom Upstream server configuration.google

只要將 __PILLAR__UPSTREAM__SERVERS__ 改成 kube-dns-upstream service 的 IP 地址(好比這裏是10.96.53.196)就能解決問題。

查看 kube-dns-upstream service IP 地址的命令:

kubectl get svc -n kube-system | grep kube-dns-upstream

改進後的部署命令:

sed 's/k8s.gcr.io/gcr.azk8s.cn\/google_containers/g
s/__PILLAR__DNS__SERVER__/10.96.0.10/g
s/__PILLAR__LOCAL__DNS__/169.254.20.10/g
s/__PILLAR__UPSTREAM__SERVERS__/10.96.53.196/g
s/__PILLAR__DNS__DOMAIN__/cluster.local/g' nodelocaldns.yaml |
kubectl apply -n kube-system -f -

終於搞定!

相關文章
相關標籤/搜索