2017-06-27 16:19:41 +00:00
---
2018-02-27 18:51:46 +00:00
reviewers:
2017-06-27 16:19:41 +00:00
- rickypai
- thockin
title: Adding entries to Pod /etc/hosts with HostAliases
2018-06-11 19:38:26 +00:00
content_template: templates/concept
2018-06-06 23:51:26 +00:00
weight: 60
2017-06-27 16:19:41 +00:00
---
2018-05-05 16:00:51 +00:00
{{< toc > }}
2017-06-27 16:19:41 +00:00
2018-06-11 19:38:26 +00:00
{{% capture overview %}}
2017-06-27 16:19:41 +00:00
Adding entries to a Pod's /etc/hosts file provides Pod-level override of hostname resolution when DNS and other options are not applicable. In 1.7, users can add these custom entries with the HostAliases field in PodSpec.
2017-06-27 16:21:29 +00:00
Modification not using HostAliases is not suggested because the file is managed by Kubelet and can be overwritten on during Pod creation/restart.
2018-06-11 19:38:26 +00:00
{{% /capture %}}
{{% capture body %}}
2017-06-27 16:19:41 +00:00
## Default Hosts File Content
2018-05-04 04:20:51 +00:00
Lets start an Nginx Pod which is assigned a Pod IP:
2017-09-15 23:30:06 +00:00
```shell
$ kubectl run nginx --image nginx --generator=run-pod/v1
pod "nginx" created
2017-06-27 16:19:41 +00:00
$ kubectl get pods --output=wide
NAME READY STATUS RESTARTS AGE IP NODE
nginx 1/1 Running 0 13s 10.200.0.4 worker0
```
The hosts file content would look like this:
2017-09-15 23:30:06 +00:00
```shell
2017-06-27 16:19:41 +00:00
$ kubectl exec nginx -- 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.200.0.4 nginx
```
2018-07-10 15:56:25 +00:00
By default, the `hosts` file only includes IPv4 and IPv6 boilerplates like
`localhost` and its own hostname.
2017-06-27 16:19:41 +00:00
## Adding Additional Entries with HostAliases
2018-07-10 15:56:25 +00:00
In addition to the default boilerplate, we can add additional entries to the
`hosts` file to resolve `foo.local` , `bar.local` to `127.0.0.1` and `foo.remote` ,
`bar.remote` to `10.1.2.3` , we can by adding HostAliases to the Pod under
`.spec.hostAliases` :
2017-06-27 16:19:41 +00:00
2018-07-10 15:56:25 +00:00
{{< codenew file = "service/networking/hostaliases-pod.yaml" > }}
2017-06-27 16:19:41 +00:00
2017-09-15 23:30:06 +00:00
This Pod can be started with the following commands:
```shell
$ kubectl apply -f hostaliases-pod.yaml
pod "hostaliases-pod" created
2018-03-27 21:56:03 +00:00
$ kubectl get pod -o=wide
2017-09-15 23:30:06 +00:00
NAME READY STATUS RESTARTS AGE IP NODE
hostaliases-pod 0/1 Completed 0 6s 10.244.135.10 node3
2017-06-27 16:19:41 +00:00
```
2017-09-15 23:30:06 +00:00
2018-07-10 15:56:25 +00:00
The `hosts` file content would look like this:
2017-09-15 23:30:06 +00:00
```shell
2017-06-27 16:19:41 +00:00
$ kubectl logs hostaliases-pod
# 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
2017-09-15 23:30:06 +00:00
10.244.135.10 hostaliases-pod
2017-06-27 16:19:41 +00:00
127.0.0.1 foo.local
127.0.0.1 bar.local
10.1.2.3 foo.remote
10.1.2.3 bar.remote
```
With the additional entries specified at the bottom.
## Why Does Kubelet Manage the Hosts File?
2018-07-10 15:56:25 +00:00
Kubelet [manages ](https://github.com/kubernetes/kubernetes/issues/14633 ) the
`hosts` file for each container of the Pod to prevent Docker from
[modifying ](https://github.com/moby/moby/issues/17190 ) the file after the
containers have already been started.
2017-06-27 16:19:41 +00:00
2018-07-10 15:56:25 +00:00
Because of the managed-nature of the file, any user-written content will be
overwritten whenever the `hosts` file is remounted by Kubelet in the event of
a container restart or a Pod reschedule. Thus, it is not suggested to modify
the contents of the file.
2018-06-11 19:38:26 +00:00
{{% /capture %}}