0%

K3s配置内网域名解析

K3s 启动后,会自动帮我们安装好 CoreDNS,不需要手动安装。如果你想修改 CoreDNS 的配置,常用的有两种方式:

  • 直接修改 CoreDNS 的 configmap 来调整 CoreDNS 的参数,例如:kubectl -n kube-system edit configmap coredns
  • 修改 K3s manifests 中的 CoreDNS 配置文件,文件位置:/var/lib/rancher/k3s/server/manifests/coredns.yaml

这两种方式虽然简单,但都有相同的弊端:当你重启 K3s 服务或者升级 K3s 时,由于 K3s 会重新初始化 manifests 中的 CoreDNS 等配置,所以会覆盖掉你通过以上两种方式修改的 coredns 配置。

如果你想修改 K3s 中 CoreDNS 中的配置,并且持久生效的话,可以通过额外的 coredns-custom configmap 安装到 CoreDNS 容器中,并从包含的文件中导入覆盖和额外的 CoreDNS 配置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
apiVersion: v1
kind: ConfigMap
metadata:
name: coredns-custom
namespace: kube-system
data:
log.override: |
log
example.server: |
example.io {
errors
cache 30
hosts {
192.168.0.2 test1.example.io # 内网域名映射地址
fallthrough
}
}

ConfigMap 的 name 一定刚要是 coredns-custom 才能够被 coredns 的 deployment 识别并挂载。

在其他Pod中验证CoreDNS配置是否生效

1
2
3
4
5
6
7
kubectl create deploy nginx --image=nginx:latest # 创建deploy
kubectl get pod -w # 等待nginx状态变为running
kubectl exec -it ngix-***** -- /bin/bash # 切入容器,注意容器ID与上一步中查看容器ID一致

### 容器内操作
ping test1.example.io
nslookup test1.example.io