Satyashil Deshpande
Satyashil Deshpande

Reputation: 196

How to add extra hosts entries in helm charts

So i'm deploying my application stack on kubernetes sing helm charts and now i need to add some dependant server ip's and hostnames inside my pods /etc/hosts file so need help on this scenario

Upvotes: 6

Views: 17647

Answers (3)

Christian
Christian

Reputation: 1975

A helm templated solution to the original question. I tested this with helm 3.

apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
    {{- with .Values.hostAliases }}
      hostAliases:
{{ toYaml . | indent 8 }}
    {{- end }}

For values such as:

hostAliases:
  - ip: "10.0.0.1"
    hostnames:
    - "host.domain.com"

If the hostAliases is omitted or commented out in the values, the hostAliases section is omitted when the template is rendered.

Upvotes: 13

David Maze
David Maze

Reputation: 158977

Kubernetes provides a DNS service that all pods get to use. In turn, you can define an ExternalName service that just defines a DNS record. Once you do that, your pods can talk to that service the same way they'd talk to any other Kubernetes service, and reach whatever server.

You could deploy a set of ExternalName services globally. You could do it in a Helm chart too, if you wanted, something like

apiVersion: v1
kind: Service
metadata:
  name: {{ .Release.Name }}-{{ .Chart.Name }}-foo
spec:
  type: ExternalName
  externalName: {{ .Values.fooHostname }}

The practice I've learned is that you should avoid using /etc/hosts if at all possible.

Upvotes: 3

Jakub Bujny
Jakub Bujny

Reputation: 4628

As standing in documentation you can add extra hosts to POD by using host aliases feature

Example from docs:

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"

Upvotes: 4

Related Questions