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