Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add host mapping to /etc/hosts in Kubernetes

I have an issue with the DNS mapping in kubernetes.

We have some servers which can be accessed from internet. The global DNS translates these servers's domain names to public internet IPs. Some services can't access through public IPs for security consideration.

From company internal, we add the DNS mappings with private IPs to /etc/hosts inside docker containers managed by kubernetes to access these servers manually.

I know that docker supports command --add-host to change /etc/hosts when executing docker run. I'm not sure if this command supported in latest kubernetes, such as kuber 1.4 or 1.5 ?

On the other hand, we can wrap the startup script for the docker container,

  • append the mappings to /etc/hosts firstly
  • start our application

I only want to change the file once after first run in each container. Is there an easy way to do this because the mapping relations maybe different between develop and production environments or any commands related to this provided by kubernetes itself?

like image 713
qingdaojunzuo Avatar asked Dec 24 '16 07:12

qingdaojunzuo


2 Answers

It is now possible to add a hostAliases section directly in the description of the deployment.

As a full example of how to use the hostAliases section I have included the surrounding code for an example deployment as well.

apiVersion : apps/v1
kind: Deployment
metadata:
  name: "backend-cluster"
spec:
  replicas: 1
  selector:
    matchLabels:
      app: "backend"
  template:
    metadata:
      labels:
        app: "backend"
    spec:
      containers:
      - name: "backend"
        image: "exampleregistry.azurecr.io/backend"
        ports:
        - containerPort: 80
      hostAliases:
      - hostnames:
        - "www.example.com"
        ip: "10.0.2.4"

The important part is only a part of the file and here it is omitted for clarity:

...
      hostAliases:
      - hostnames:
        - "www.example.com"
        ip: "10.0.2.4"
like image 139
eikooc Avatar answered Sep 18 '22 14:09

eikooc


++ Found this article to add /etc/hosts entries in a pod:

Adding entries to Pod /etc/hosts with HostAliases: service/networking/hostaliases-pod.yaml

In addition to the default boilerplate, you can add additional entries to the hosts file. For example: to resolve foo.local, bar.local to 127.0.0.1 and foo.remote, bar.remote to 10.1.2.3, you can configure HostAliases for a Pod under .spec.hostAliases:

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"

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

like image 35
Syed Faraz Umar Avatar answered Sep 17 '22 14:09

Syed Faraz Umar