kubernetes(k8s)中如何在coredns中更改主机名解析?

发表于: 2020-12-14   最后更新时间: 2020-12-14 22:54:12   1,626 游览

我有一个CoreFile的配置是这样的:

.:53 {
    errors
    health {
       lameduck 5s
    }
    ready
    kubernetes cluster.local in-addr.arpa ip6.arpa {
       pods insecure
       fallthrough in-addr.arpa ip6.arpa
       ttl 30
    }
    prometheus :9153
    forward . /etc/resolv.conf {
       max_concurrent 1000
    }
    cache 30
    loop
    reload
    loadbalance
}

我想让我所有的pods能够将myapi.local解析到一个特定的IP(192.168.49.2),有没有什么简单的方法来实现这个,就像我可以用操作系统的host文件一样。

发表于 2020-12-14

下面的配置应该可以做到这一点

.:53 {
    errors
    health
    ready
    kubernetes cluster.local in-addr.arpa ip6.arpa {
        pods insecure
        fallthrough in-addr.arpa ip6.arpa
    }
    prometheus :9153
    forward . 8.8.8.8 8.8.4.4
    cache 30
    loop
    reload
    loadbalance
    hosts custom.hosts myapi.local {
        192.168.49.2 myapi.local
        fallthrough
    }
}

或者您可以尝试使用hosts插件:https://coredns.io/plugins/hosts/

如果你不想用coredns的方法来解决这个问题,有一种方法可以在特定的pod的host文件中设置条目,这将镜像在节点上设置/etc/hosts

apiVersion: v1
kind: Pod
metadata:
  name: hostaliases-pod
spec:
  restartPolicy: Never
  hostAliases:
    - ip: 127.0.0.1
      hostnames:
        - foo.local
        - bar.local
    - ip: 10.1.2.3
      hostnames:
        - foo.remote
        - bar.remote
  containers:
    - name: cat-hosts
      image: busybox
      command:
        - cat
      args:
        - /etc/hosts
你的答案

查看kubernetes相关的其他问题或提一个您自己的问题