目录

在k8s中,如何在kube-dns组件中实现coredns中的hosts plugin插件的功能

前言

在coredns中,如果需要实现自定义域名解析,将某个域名解析到自己所期望的特定地址,可以通过coredns中的hosts plugin实现。

但在早期的k8s集群中,使用的是kube-dns,并无此插件,本文介绍,如何在kube-dns的场景中,实现自定义域名解析。

概念

kube-dns中,依赖于dnsmasq的能力,在dnsmasq中,可以通过-addn-hosts参数,去读取指定的hosts文件,进而实现该文件中的hosts解析,也可通过--hostsdir参数,后面会讲到

1
2
3
4
5
6
7
8
9
dnsmasq --help
...
-H, --addn-hosts=<file>    Additional hosts file. Read the specified file as well as
                           /etc/hosts. If -h is given, read only the specified file.
                           This option may be repeated for more than one additional
                           hosts file. If a directory is given, then read all the files
                           contained in that directory.
--hostsdir=<path>
        Read all the hosts files contained in the directory. New or changed files are read automatically. See --dhcp-hostsdir for details.

实践

  1. 在kube-system命名空间下,创建名为kube-dns-hosts的configmap文件,新增一个名为hosts的键,值为所需要的hosts解析

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    apiVersion: v1
    data:
      hosts: |-
        1.1.1.1 www.baidu.com
        2.2.2.2 www.qq.com
    kind: ConfigMap
    metadata:
      name: kube-dns-hosts
      namespace: kube-system
    
  2. 修改workload:kube-dns的yaml文件,为dnsmasq增加如上configmap挂载(示例yaml已删除非相关字段)

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    
    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      name: kube-dns
      namespace: kube-system
    
    	...
    	其他配置
    	...
    
    spec:
      selector:
        matchLabels:
          k8s-app: kube-dns
      template:
        metadata:
          labels:
            k8s-app: kube-dns
          name: kube-dns
          namespace: kube-system
        spec:
          containers:
    
          	...
    	      其他容器
    	      ...
    
          - args:
            - -v=2
            - -logtostderr
            - -configDir=/etc/k8s/dns/dnsmasq-nanny
            - -restartDnsmasq=true
            - --
            - -k
            - --cache-size=1000
            - --log-facility=-
            - --server=/cluster.local/127.0.0.1#10053
            - --server=/in-addr.arpa/127.0.0.1#10053
            - --server=/ip6.arpa/127.0.0.1#10053
            - --addn-hosts=/kube-dns-hosts/hosts   #增加该参数,将hosts指向configmap中
            image: ccr.ccs.tencentyun.com/library/k8s-dns-dnsmasq-nanny-amd64:1.14.4
            imagePullPolicy: IfNotPresent
            name: dnsmasq
            volumeMounts:
            - mountPath: /etc/k8s/dns/dnsmasq-nanny
              name: kube-dns-config
            - mountPath: /kube-dns-hosts     #本例将此configmap挂载至/kube-dns-hosts目录
              name: kube-dns-hosts
          volumes:
          - configMap:
              defaultMode: 420
              name: kube-dns-hosts
            name: kube-dns-hosts                  #使用步骤1中创建的configmap文件
    				...
            ...
    
  3. 保存后等待kube-dns重建,重建完后测试,结果符合预期

    /images/%E5%9C%A8k8s%E4%B8%AD%EF%BC%8C%E5%A6%82%E4%BD%95%E5%9C%A8kube-dns%E7%BB%84%E4%BB%B6%E4%B8%AD%E5%AE%9E%E7%8E%B0coredns%E4%B8%AD%E7%9A%84hosts%20plugin%E6%8F%92%E4%BB%B6%E5%8A%9F%E8%83%BD/image-20211120133520897.png

  4. 后续如想新增或者修改解析,需修改kube-dns-hosts的configmap配置即可,修改后,需要重建kube-dns的pod,才会生效,如有多个pod,请依次重建,此操作可能会导致依赖于kube-dns解析的服务出现抖动

Q&A

Q:kubernetes针对于挂载的configmap,不是会自动更新吗?为何在修改完configmap配置后,还需要重建pod?

A:在上述的挂载方式下,configmap的修改的确会被自动更新到pod中容器对应的文件中,但是因为dnsmasq的缘故,虽说更新了,但是并不生效。

经过网上查询了解,让dnsmasq更新的方式有2种:

  • 使用SIGHUP,让其重新加载

    涉及到kill等操作,不如重建来的快捷直观,没有详细测试过,如有兴趣,可自行研究

  • 使用--hostsdir参数

    使用该参数,虽说不用发送SIGHUP,但有个弊端,只对新增生效,而对现有的文件进行修改或者删除是不生效的

    同时我自行测试该参数,发现即便是新增一个新的hosts文件,也依然不生效,仍需要重建pod才能奏效,不确定是不我的方法问题。

相关参考文档:

dnsmasq doesn’t automatically reload when entry is added to /etc/hosts

DNSMASQ