Question
I will use a very specific way to explain my problem, but I think this is better to be specific than explain in an abstract way...
Say, there is a MongoDB replica set outside of a Kubernetes cluster but in a network. The ip addresses of all members of the replica set were resolved by /etc/hosts in app servers and db servers.
In an experiment/transition phase, I need to access those mongo db servers from kubernetes pods. However, kubernetes doesn't seem to allow adding custom entries to /etc/hosts in pods/containers.
The MongoDB replica sets are already working with large data set, creating a new replica set in the cluster is not an option.
Because I use GKE, changing any of resources in kube-dns namespace should be avoided I suppose. Configuring or replace kube-dns to be suitable for my need are last thing to try.
Is there a way to resolve ip address of custom hostnames in a Kubernetes cluster?
It is just an idea, but if kube2sky can read some entries of configmap and use
them as dns records, it colud be great. e.g. repl1.mongo.local: 192.168.10.100
.
EDIT: I referenced this question from https://github.com/kubernetes/kubernetes/issues/12337
Answer
UPDATE: 2017-07-03 Kunbernetes 1.7 now support [Adding entries to Pod /etc/hosts with HostAliases](https://kubernetes.io/docs/concepts/services- networking/add-entries-to-pod-etc-hosts-with-host-aliases/).
The solution is not about kube-dns, but /etc/hosts. Anyway, following trick seems to work so far...
EDIT: Changing /etc/hosts may has race condition with kubernetes system. Let it retry.
-
create a configMap
apiVersion: v1 kind: ConfigMap metadata: name: db-hosts data: hosts: | 10.0.0.1 db1 10.0.0.2 db2
-
Add a script named
ensure_hosts.sh
.#!/bin/sh
while true do grep db1 /etc/hosts > /dev/null || cat /mnt/hosts.append/hosts >> /etc/hosts sleep 5 done
Don't forget chmod a+x ensure_hosts.sh
.
-
Add a wrapper script
start.sh
your image#!/bin/sh $(dirname "$(realpath "$0")")/ensure_hosts.sh & exec your-app args...
Don't forget chmod a+x start.sh
-
Use the configmap as a volume and run start.sh
apiVersion: extensions/v1beta1 kind: Deployment ... spec: template: ... spec: volumes: - name: hosts-volume configMap: name: db-hosts ... containers: command: - ./start.sh ... volumeMounts: - name: hosts-volume mountPath: /mnt/hosts.append ...