Skip to content

Adding Custom Entries in /etc/hosts File in Kubernetes and OpenShift Containers

Avatar photo

https://www.linkedin.com/in/gineesh/ https://twitter.com/GiniGangadharan

Introduction:

In Kubernetes and OpenShift container environments, it is sometimes necessary to add custom entries to the /etc/hosts file within pods. These entries allow pods to reach external endpoints using user-defined hostnames. In this blog post, we will explore how to add entries in the /etc/hosts file and the necessary permissions required to configure this in OpenShift 4.12.x.

Disclaimer: It is important to note that the recommended method for resolving hostnames is to add the appropriate entries in the enterprise DNS server. This method should only be used as an interim or workaround when the desired entry is not available in the DNS server or when the DNS server is not reachable from the pods. It is advised to consult with your system administrators and follow the established protocols to ensure proper network configuration and avoid potential complications.

Adding Custom Entries in /etc/hosts inside the pods

To add custom entries in the /etc/hosts file of pods, you can utilize the hostAliases field in the deployment configuration. This field allows you to define a list of custom hostname and IP address mappings.

Here’s an example of how you can add a custom entry using the oc command-line tool:

$ oc get deployment httpd1 -o yaml | grep -A4 -B14 hostAliases
    spec:
      containers:
      - image: image-registry.openshift-image-registry.svc:5000/iamgini-dev/httpd1@sha256:b58b0719265d8b1a5beacfb42f4f2e946905c1ba4069c9949834edf97db3ace2
        imagePullPolicy: Always
        name: httpd1
        ports:
        - containerPort: 8080
          protocol: TCP
        - containerPort: 8443
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      hostAliases:
      - hostnames:
        - myadded.example.com
        ip: 192.168.1.1
      restartPolicy: Always

In the above example, the hostAliases the field is used to add an entry mapping myadded.example.com to the IP address 192.168.1.1.

Users can directly edit the deployment (or in the deployment template) to add necessary hostAliases fields.

Verifying the Custom Entries:

Once the deployment is updated with the custom hostAliases entries, you can verify the changes by accessing a shell within the pod:

$ oc get po
NAME                                         READY   STATUS      RESTARTS   AGE
httpd1-697d4f764c-4xtwn                      1/1     Running     0          46m

$ oc rsh httpd1-697d4f764c-4xtwn
sh-4.2$ cat /etc/hosts
Kubernetes-managed hosts file.
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
fe00::0 ip6-mcastprefix
fe00::1 ip6-allnodes
fe00::2 ip6-allrouters
10.129.10.72    httpd1-697d4f764c-4xtwn
Entries added by HostAliases.
192.168.1.1     myadded.example.com
sh-4.2$

In the above example, the /etc/hosts file within the pod httpd1-697d4f764c-4xtwn contains the added entry 192.168.1.1 myadded.example.com.

Conclusion:

By leveraging the hostAliases field in the deployment configuration, it is possible to add custom entries in the /etc/hosts file of pods in Kubernetes and OpenShift containers. This capability enables pods to reach external endpoints using user-defined hostnames. With the proper permissions at the project level, even non-admin users can configure these custom entries. Understanding how to modify the /etc/hosts file in container environments expands the possibilities for networking and connectivity within your applications.

References

Disclaimer:

The views expressed and the content shared in all published articles on this website are solely those of the respective authors, and they do not necessarily reflect the views of the author’s employer or the techbeatly platform. We strive to ensure the accuracy and validity of the content published on our website. However, we cannot guarantee the absolute correctness or completeness of the information provided. It is the responsibility of the readers and users of this website to verify the accuracy and appropriateness of any information or opinions expressed within the articles. If you come across any content that you believe to be incorrect or invalid, please contact us immediately so that we can address the issue promptly.

Avatar photo


https://www.linkedin.com/in/gineesh/ https://twitter.com/GiniGangadharan
Gineesh Madapparambath is the founder of techbeatly and he is the co-author of The Kubernetes Bible, Second Edition. and the author of ๐—”๐—ป๐˜€๐—ถ๐—ฏ๐—น๐—ฒ ๐—ณ๐—ผ๐—ฟ ๐—ฅ๐—ฒ๐—ฎ๐—น-๐—Ÿ๐—ถ๐—ณ๐—ฒ ๐—”๐˜‚๐˜๐—ผ๐—บ๐—ฎ๐˜๐—ถ๐—ผ๐—ป. He has worked as a Systems Engineer, Automation Specialist, and content author. His primary focus is on Ansible Automation, Containerisation (OpenShift & Kubernetes), and Infrastructure as Code (Terraform). (aka Gini Gangadharan - iamgini.com)

Comments

3 Responses

  1. Fernando says:

    Thanks for this useful information, do you have please a reference to add a custom dns to openshift?

    Thanks in advance.

  2. […] Adding Custom Entries in /etc/hosts File in Kubernetes and OpenShift Containers […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.