更新时间:2024-02-19 GMT+08:00
分享

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

应用现状

在使用CCE时,可能会有解析自定义内部域名的需求,例如:

  • 存量代码配置了用固定域名调用内部其他服务,如果要切换到Kubernetes Service方式,修改配置工作量大。
  • 在集群外自建了一个其他服务,需要将集群中的数据通过固定域名发送到这个服务。

解决方案

使用CoreDNS有以下几种自定义域名解析的方案。

注意事项

CoreDNS修改配置需额外谨慎,因为CoreDNS负责集群的域名解析任务,修改不当可能会导致集群解析出现异常。请做好修改前后的测试验证。

为CoreDNS配置存根域

集群管理员可以修改CoreDNS Corefile的ConfigMap以更改服务发现的工作方式。

若集群管理员有一个位于10.150.0.1的Consul域名解析服务器,并且所有Consul的域名都带有.consul.local的后缀。

  1. 登录CCE控制台,单击集群名称进入集群。
  2. 在左侧导航栏中选择“插件中心”,在CoreDNS下单击“编辑”,进入插件详情页。
  3. “参数配置”下添加存根域。格式为一个键值对,键为DNS后缀域名,值为一个或一组DNS IP地址,如 'consul.local --10.150.0.1'。

  4. 单击“确定”完成配置更新。
  5. 在左侧导航栏中选择“配置与密钥”,在“kube-system”命名空间下,查看名为coredns的配置项数据,确认是否更新成功。

    对应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
        ready {$POD_IP}:8081
    }
    consul.local:5353 {
        bind {$POD_IP}
        errors
        cache 30
        forward . 10.150.0.1
    }

修改CoreDNS Hosts配置

在CoreDNS中修改hosts后,可以不用单独在每个Pod中配置hosts添加解析记录。

  1. 登录CCE控制台,单击集群名称进入集群。
  2. 在左侧导航栏中选择“插件中心”,在CoreDNS下单击“编辑”,进入插件详情页。
  3. “参数配置”下编辑高级配置,在plugins字段添加以下内容。

    {
      "configBlock": "192.168.1.1 www.example.com\nfallthrough",
      "name": "hosts"
    }

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

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

  4. 单击“确定”完成配置更新。
  5. 在左侧导航栏中选择“配置与密钥”,在“kube-system”命名空间下,查看名为coredns的配置项数据,确认是否更新成功。

    对应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服务。

  1. 登录CCE控制台,单击集群名称进入集群。
  2. 在左侧导航栏中选择“插件中心”,在CoreDNS下单击“编辑”,进入插件详情页。
  3. “参数配置”下编辑高级配置,在plugins字段添加以下内容。

    {
       "name": "rewrite",
       "parameters": "name example.com example.default.svc.cluster.local"
    }

  4. 单击“确定”完成配置更新。
  5. 在左侧导航栏中选择“配置与密钥”,在“kube-system”命名空间下,查看名为coredns的配置项数据,确认是否更新成功。

    对应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级联自建DNS

CoreDNS默认使用节点的/etc/resolv.conf文件进行解析,您也可以修改成外部DNS的解析地址。

  1. 登录CCE控制台,单击集群名称进入集群。
  2. 在左侧导航栏中选择“插件中心”,在CoreDNS下单击“编辑”,进入插件详情页。
  3. “参数配置”下编辑高级配置,在plugins字段修改以下内容。

    {
        "configBlock": "policy random",
        "name": "forward",
        "parameters": ". 192.168.1.1"
    }

  4. 单击“确定”完成配置更新。
  5. 在左侧导航栏中选择“配置与密钥”,在“kube-system”命名空间下,查看名为coredns的配置项数据,确认是否更新成功。

    对应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
    }

分享:

    相关文档

    相关产品