使用CoreDNS实现自定义域名解析 |
您所在的位置:网站首页 › dns自建 › 使用CoreDNS实现自定义域名解析 |
应用现状
在使用CCE时,可能会有解析自定义内部域名的需求,例如: 存量代码配置了用固定域名调用内部其他服务,如果要切换到Kubernetes Service方式,修改配置工作量大。 在集群外自建了一个其他服务,需要将集群中的数据通过固定域名发送到这个服务。 解决方案使用CoreDNS有以下几种自定义域名解析的方案。 为CoreDNS配置存根域:可以直接在控制台添加,简单易操作。 使用 CoreDNS Hosts 插件配置任意域名解析:简单直观,可以添加任意解析记录,类似在本地/etc/hosts中添加解析记录。 使用 CoreDNS Rewrite 插件指向域名到集群内服务:相当于给Kubernetes中的Service名称取了个别名,无需提前知道解析记录的IP地址。 使用 CoreDNS Forward 插件将自建 DNS 设为上游 DNS:自建DNS中,可以管理大量的解析记录,解析记录专门管理,增删记录无需修改CoreDNS配置。 注意事项CoreDNS修改配置需额外谨慎,因为CoreDNS负责集群的域名解析任务,修改不当可能会导致集群解析出现异常。请做好修改前后的测试验证。 为CoreDNS配置存根域集群管理员可以修改CoreDNS Corefile的ConfigMap以更改服务发现的工作方式。 若集群管理员有一个位于10.150.0.1的Consul域名解析服务器,并且所有Consul的域名都带有.consul.local的后缀。 登录CCE控制台,单击集群名称进入集群。 在左侧导航栏中选择“插件中心”,在CoreDNS下单击“编辑”,进入插件详情页。 在“参数配置”下添加存根域。格式为一个键值对,键为DNS后缀域名,值为一个或一组DNS IP地址,如 'consul.local --10.150.0.1'。 图1 添加存根域![]() 对应Corefile内容如下: .:5353 { bind {$POD_IP} cache 30 { servfail 5s } errors health {$POD_IP}:8080 kubernetes cluster.local in-addr.arpa ip6.arpa { pods insecure fallthrough in-addr.arpa ip6.arpa } loadbalance round_robin prometheus {$POD_IP}:9153 forward . /etc/resolv.conf { policy random } reload ready {$POD_IP}:8081 } consul.local:5353 { bind {$POD_IP} errors cache 30 forward . 10.150.0.1 } 修改CoreDNS Hosts配置在CoreDNS中修改hosts后,可以不用单独在每个Pod中配置hosts添加解析记录。 登录CCE控制台,单击集群名称进入集群。 在左侧导航栏中选择“插件中心”,在CoreDNS下单击“编辑”,进入插件详情页。 在“参数配置”下编辑高级配置,在plugins字段添加以下内容。{ "configBlock": "192.168.1.1 www.example.com\nfallthrough", "name": "hosts" }![]() 此处配置不能遗漏fallthrough字段,fallthrough表示当在hosts找不到要解析的域名时,会将解析任务传递给CoreDNS的下一个插件。如果不写fallthrough的话,任务就此结束,不会继续解析,会导致集群内部域名解析失败的情况。 hosts的详细配置请参见https://coredns.io/plugins/hosts/。 图2 修改CoreDNS Hosts配置![]() 对应Corefile内容如下: .:5353 { bind {$POD_IP} hosts { 192.168.1.1 www.example.com fallthrough } cache 30 errors health {$POD_IP}:8080 kubernetes cluster.local in-addr.arpa ip6.arpa { pods insecure fallthrough in-addr.arpa ip6.arpa } loadbalance round_robin prometheus {$POD_IP}:9153 forward . /etc/resolv.conf { policy random } reload ready {$POD_IP}:8081 } 添加CoreDNS Rewrite配置指向域名到集群内服务使用 CoreDNS 的 Rewrite 插件,将指定域名解析到某个 Service 的域名。例如,将访问example.com域名的请求重新指向到example.default.svc.cluster.local域名,即指向到default命名空间下的example服务。 登录CCE控制台,单击集群名称进入集群。 在左侧导航栏中选择“插件中心”,在CoreDNS下单击“编辑”,进入插件详情页。 在“参数配置”下编辑高级配置,在plugins字段添加以下内容。{ "name": "rewrite", "parameters": "name example.com example.default.svc.cluster.local" } 图3 添加CoreDNS Rewrite配置指向域名到集群内服务![]() 对应Corefile内容如下: .:5353 { bind {$POD_IP} rewrite name example.com example.default.svc.cluster.local cache 30 errors health {$POD_IP}:8080 kubernetes cluster.local in-addr.arpa ip6.arpa { pods insecure fallthrough in-addr.arpa ip6.arpa } loadbalance round_robin prometheus {$POD_IP}:9153 forward . /etc/resolv.conf { policy random } reload ready {$POD_IP}:8081 } 使用CoreDNS级联自建DNSCoreDNS默认使用节点的/etc/resolv.conf文件进行解析,您也可以修改成外部DNS的解析地址。 登录CCE控制台,单击集群名称进入集群。 在左侧导航栏中选择“插件中心”,在CoreDNS下单击“编辑”,进入插件详情页。 在“参数配置”下编辑高级配置,在plugins字段修改以下内容。{ "configBlock": "policy random", "name": "forward", "parameters": ". 192.168.1.1" } 图4 使用CoreDNS级联自建DNS![]() 对应Corefile内容如下: .:5353 { bind {$POD_IP} cache 30 errors health {$POD_IP}:8080 kubernetes cluster.local in-addr.arpa ip6.arpa { pods insecure fallthrough in-addr.arpa ip6.arpa } loadbalance round_robin prometheus {$POD_IP}:9153 forward . 192.168.1.1 { policy random } reload ready {$POD_IP}:8081 } |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |