使用CoreDNS实现自定义域名解析

您所在的位置:网站首页 dns配置域名解析 使用CoreDNS实现自定义域名解析

使用CoreDNS实现自定义域名解析

2023-04-15 17:43| 来源: 网络整理| 查看: 265

应用现状

在使用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下单击“编辑”,进入插件详情页。 在“参数配置”下添加存根域。

修改stub_domains参数,格式为一个键值对,键为DNS后缀域名,值为一个或一组DNS IP地址,如下所示。 { "stub_domains": { "consul.local": [ "10.150.0.1" ] }, "upstream_nameservers": [] }

单击“确定”。

也可以通过修改ConfigMap,直接按如下方式添加:

示例中标红部分字段的参数值仅支持修改,不可删减。

$ kubectl edit configmap coredns -n kube-system apiVersion: v1 data: 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 . /etc/resolv.conf { policy random } reload } consul.local:5353 { bind {$POD_IP} errors cache 30 forward . 10.150.0.1 } kind: ConfigMap metadata: creationTimestamp: "2022-05-04T04:42:24Z" labels: app: coredns k8s-app: coredns kubernetes.io/cluster-service: "true" kubernetes.io/name: CoreDNS release: cceaddon-coredns name: coredns namespace: kube-system resourceVersion: "8663493" uid: bba87142-9f8d-4056-b8a6-94c3887e9e1d 修改CoreDNS Hosts配置 使用kubectl连接集群。 修改CoreDNS配置文件,将自定义域名添加到hosts中。

将www.example.com指向192.168.1.1,通过CoreDNS解析www.example.com时,会返回192.168.1.1。

此处配置不能遗漏fallthrough字段,fallthrough表示当在hosts找不到要解析的域名时,会将解析任务传递给CoreDNS的下一个插件。如果不写fallthrough的话,任务就此结束,不会继续解析,会导致集群内部域名解析失败的情况。

hosts的详细配置请参见https://coredns.io/plugins/hosts/。

$ kubectl edit configmap coredns -n kube-system apiVersion: v1 data: 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 } hosts { 192.168.1.1 www.example.com fallthrough } loadbalance round_robin prometheus {$POD_IP}:9153 forward . /etc/resolv.conf reload } kind: ConfigMap metadata: creationTimestamp: "2021-08-23T13:27:28Z" labels: app: coredns k8s-app: coredns kubernetes.io/cluster-service: "true" kubernetes.io/name: CoreDNS release: cceaddon-coredns name: coredns namespace: kube-system resourceVersion: "460" selfLink: /api/v1/namespaces/kube-system/configmaps/coredns uid: be64aaad-1629-441f-8a40-a3efc0db9fa9

在CoreDNS中修改hosts后,就不用单独在每个Pod中配置hosts了,带来了一定的方便性。

添加CoreDNS Rewrite配置指向域名到集群内服务

使用 CoreDNS 的 Rewrite 插件,将指定域名解析到某个 Service 的域名,相当于给Service取了个别名。

使用kubectl连接集群。 修改CoreDNS配置文件,将example.com指向default命名空间下的example服务。

$ kubectl edit configmap coredns -n kube-system apiVersion: v1 data: 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 } rewrite name example.com example.default.svc.cluster.local loadbalance round_robin prometheus {$POD_IP}:9153 forward . /etc/resolv.conf reload } kind: ConfigMap metadata: creationTimestamp: "2021-08-23T13:27:28Z" labels: app: coredns k8s-app: coredns kubernetes.io/cluster-service: "true" kubernetes.io/name: CoreDNS release: cceaddon-coredns name: coredns namespace: kube-system resourceVersion: "460" selfLink: /api/v1/namespaces/kube-system/configmaps/coredns uid: be64aaad-1629-441f-8a40-a3efc0db9fa9

使用CoreDNS级联自建DNS 使用kubectl连接集群。 修改CoreDNS配置文件,将forward后面的/etc/resolv.conf,改成外部DNS的地址,如下所示。

$ kubectl edit configmap coredns -n kube-system apiVersion: v1 data: 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 reload } kind: ConfigMap metadata: creationTimestamp: "2021-08-23T13:27:28Z" labels: app: coredns k8s-app: coredns kubernetes.io/cluster-service: "true" kubernetes.io/name: CoreDNS release: cceaddon-coredns name: coredns namespace: kube-system resourceVersion: "460" selfLink: /api/v1/namespaces/kube-system/configmaps/coredns uid: be64aaad-1629-441f-8a40-a3efc0db9fa9



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3