How to change host name resolve like host file in coredns
Asked Answered
J

2

6

I have a CoreFile configutation like this

.: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
}

I would like all my pods to be able to resolve myapi.local to a specific IP ( 192.168.49.2 ) Is there any easy way to achieve this like the what I can do with OS's host file

Joist answered 14/12, 2020 at 4:58 Comment(0)
H
9

Below configuration should do the trick

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

Reference https://medium.com/@hjrocha/add-a-custom-host-to-kubernetes-a06472cedccb

Or you can try using the hosts plugin https://coredns.io/plugins/hosts/

Heiskell answered 14/12, 2020 at 5:17 Comment(1)
I did that but i have this warning in coredns log and my host not resolved in cluster. do you what is the reason? [WARNING] plugin/hosts: File does not exist: custom.hostsIrrefrangible
I
5

If you didn't want to resolve the entry with coredns the method there is a method for setting entries in specific pod's host files which would mirror having /etc/hosts set on a node:

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"

https://kubernetes.io/docs/concepts/services-networking/add-entries-to-pod-etc-hosts-with-host-aliases/

Impeachable answered 14/12, 2020 at 6:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.