【转】在Kubernetes的CoreDNS中插入自定义解析记录和外部DNS服务器

编辑coredns 的configmap

kubectl edit configmap coredns -n kube-system

添加hosts块,配置静态映射
添加外部域名服务器,如配置中的 baidu.com:53块,将使用114.114.114.114这个域名服务器解析。

apiVersion: v1
data:
  Corefile: |
    .:53 {
        errors
        health
        kubernetes cluster.local in-addr.arpa ip6.arpa {
          pods insecure
          upstream
          fallthrough in-addr.arpa ip6.arpa
          ttl 30
        }
        hosts {
            10.0.0.22 cos6-data1.test.alltest.com
            10.0.0.23 cos6-data2.test.alltest.com
            fallthrough
        }
        prometheus :9153
        forward . "/etc/resolv.conf"
        cache 30
        loop
        reload
        loadbalance
    }
    baidu.com:53 {
      errors
      cache 30
      forward . 114.114.114.114
      reload
    }
kind: ConfigMap
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","data":{"Corefile":".:53 {\n    errors\n    health\n    kubernetes cluster.local in-addr.arpa ip6.arpa {\n      pods insecure\n      upstream\n      fallthrough in-addr.arpa ip6.arpa\n      ttl 30\n    }\n    prometheus :9153\n    forward . \"/etc/resolv.conf\"\n    cache 30\n    loop\n    reload\n    loadbalance\n}\n"},"kind":"ConfigMap","metadata":{"annotations":{},"labels":{"addonmanager.kubernetes.io/mode":"EnsureExists"},"name":"coredns","namespace":"kube-system"}}
  creationTimestamp: "2019-08-19T09:14:15Z"
  labels:
    addonmanager.kubernetes.io/mode: EnsureExists
  name: coredns
  namespace: kube-system
  resourceVersion: "3231349"
  selfLink: /api/v1/namespaces/kube-system/configmaps/coredns
  uid: b791c47f-c261-11e9-b426-525400116042

重启coredns:

~ » kubectl scale deployment coredns -n kube-system --replicas=0
deployment.extensions/coredns scaled

~ » kubectl scale deployment coredns -n kube-system --replicas=2                                                     
deployment.extensions/coredns scaled

Syntax

hosts [FILE [ZONES...]] {
    [INLINE]
    ttl SECONDS
    no_reverse
    reload DURATION
    fallthrough [ZONES...]
}

FILE the hosts file to read and parse. If the path is relative the path from the root plugin will be prepended to it. Defaults to /etc/hosts if omitted. We scan the file for changes every 5 seconds.
ZONES zones it should be authoritative for. If empty, the zones from the configuration block are used.
INLINE the hosts file contents inlined in Corefile. If there are any lines before fallthrough then all of them will be treated as the additional content for hosts file. The specified hosts file path will still be read but entries will be overridden.
ttl change the DNS TTL of the records generated (forward and reverse). The default is 3600 seconds (1 hour).
reload change the period between each hostsfile reload. A time of zero seconds disables the feature. Examples of valid durations: “300ms”, “1.5h” or “2h45m”. See Go’s time. package.
no_reverse disable the automatic generation of the in-addr.arpa or ip6.arpa entries for the hosts
fallthrough If zone matches and no record can be generated, pass request to the next plugin. If [ZONES…] is omitted, then fallthrough happens for all zones for which the plugin is authoritative. If specific zones are listed (for example in-addr.arpa and ip6.arpa), then only queries for those zones will be subject to fallthrough.

你可能感兴趣的:(kubernetes)