Resolving a url as a different url inside kubernetes pod

11/13/2018

My pod (pod1) internally can connect to another pod using its service like the following:

pod2-service.namespace.svc.cluster.local

However, I want pod1 to connect to pod2 using a URL like abc.com which is not registered in a DNS. Basically, I want pod1 to resolve abc.com as pod2-service.namespace.svc.cluster.local.

I was looking at hostAliases here:

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

However, it needs an IP. How can I do this in Kubernetes?

-- kosta
alias
google-kubernetes-engine
kubernetes

3 Answers

11/14/2018

I think you can use a fixed ip as the service ip of your pod2, then use this ip in your hostalias definition.

-- Kun Li
Source: StackOverflow

11/14/2018

There are a couple of things:

However, pod to pod doesn't seem to be natively supported by Kubernetes in Deployments, my guess the rationale here is that the pods can constantly change IP addresses and names. You could use Pod default DNS entries but again the DNS entries will vary depending on the IP addresses that are assigned to pods.

The other solution that I can think of for Deployments is to use something like Consul with stub domains, then on each pod you will have to add an initContainer or consul agent sidecar to register its IP with the consul service, every time a pod restarts it will need to overwrite the DNS registration in Consul.

If you don't want to use stub domain there's also the option of using Pod DNS Configs.

-- Rico
Source: StackOverflow

11/14/2018

you can get the service ip and append to /etc/hosts in pod1 before your application code running.

echo "$(getent hosts pod2-service.namespace.svc.cluster.local | awk '{ print $1 }') abc.com" >> /etc/hosts

Notice: It is pretty hacky because you should guarantee service ip of pod2 is fixed. When service ip changed, pod1 will fail to reslove the host.

-- Lifei Chen
Source: StackOverflow