Adding entries to Pod /etc/hosts with HostAliases

Adding entries to Pod /etc/hosts with HostAliases

To add host to /etc/hosts file inside the container, you can define the hosts aliases inside your deployment yaml file.

For example like the following yaml file:

1
apiVersion : apps/v1
2
kind: Deployment
3
metadata:
4
name: "backend-cluster"
5
spec:
6
replicas: 1
7
selector:
8
matchLabels:
9
app: "backend"
10
template:
11
metadata:
12
labels:
13
app: "backend"
14
spec:
15
containers:
16
- name: "backend"
17
image: "exampleregistry.gcr.io/backend"
18
ports:
19
- containerPort: 80
20
hostAliases:
21
- hostnames:
22
- "www.example.com"
23
ip: "10.0.2.4"

The important part is hostAliases:

1
...
2
hostAliases:
3
- hostnames:
4
- "www.example.com"
5
ip: "10.0.2.4"