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.
/etc/hosts
inside the podsTo 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.
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
.
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.
Tags: custom /etc/hosts in containers · custom /etc/hosts in kubernetes pods · Custom Entries in /etc/hosts File in Kubernetes Containers · Custom Entries in /etc/hosts File in OpenShift Containers · edit /etc/hosts in openshift container · edit /etc/hosts in openshift pods · kubernetes edit /etc/hosts
Gineesh Madapparambath
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)
This site uses Akismet to reduce spam. Learn how your comment data is processed.3 Responses
Leave a Reply Cancel reply
Thanks for this useful information, do you have please a reference to add a custom dns to openshift?
Thanks in advance.
Note sure which DNS you meant here.
If its about modifying DNS via mc, then check here:
https://access.redhat.com/solutions/6250271
[…] Adding Custom Entries in /etc/hosts File in Kubernetes and OpenShift Containers […]