When using CCE, you may need to resolve custom internal domain names in the following scenarios:
There are several CoreDNS-based solutions for custom domain name resolution:
Improper modification on CoreDNS configuration may cause domain name resolution failures in the cluster. Perform tests before and after the modification.
Cluster administrators can modify the ConfigMap for the CoreDNS Corefile to change how service discovery works.
Assume that a cluster administrator has a Consul DNS server located at 10.150.0.1 and all Consul domain names have the suffix .consul.local.
The corresponding Corefile content is as follows:
.: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 }
After modifying the hosts file in CoreDNS, you do not need to configure the hosts file in each pod to add resolution records.
{ "configBlock": "192.168.1.1 www.example.com\nfallthrough", "name": "hosts" }
The fallthrough field must be configured. fallthrough indicates that when the domain name to be resolved cannot be found in the hosts file, the resolution task is transferred to the next CoreDNS plug-in. If fallthrough is not specified, the task ends and the domain name resolution stops. As a result, the domain name resolution in the cluster fails.
For details about how to configure the hosts file, visit https://coredns.io/plugins/hosts/.
The corresponding Corefile content is as follows:
.: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 }
Use the Rewrite plug-in of CoreDNS to resolve a specified domain name to the domain name of a Service. For example, the request for accessing the example.com domain name is redirected to the example.default.svc.cluster.local domain name, that is, the example service in the default namespace.
{ "name": "rewrite", "parameters": "name example.com example.default.svc.cluster.local" }
The corresponding Corefile content is as follows:
.: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 }
By default, CoreDNS uses the /etc/resolv.conf file of the node for resolution. You can also change the resolution address to that of the external DNS.
{ "configBlock": "policy random", "name": "forward", "parameters": ". 192.168.1.1" }
The corresponding Corefile content is as follows:
.: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 }