zshuangyan / blog

我的个人博客
2 stars 0 forks source link

解决kubernetes不支持修改容器hosts文件的问题 #9

Open zshuangyan opened 6 years ago

zshuangyan commented 6 years ago

在kubernetes部署了一个微服务,这个微服务需要连接到公司的大数据集群的多个节点上,由于运维管理的需要,配置文件保存的是这些节点的主机名而不是ip本身,因此需要把主机名映射到IP地址上。之前部署在公司内部服务器上时,是通过内部的域名服务器对主机名进行解析的。在没有域名服务器的情况下,也可以通过在/etc/hosts文件中添加主机名和ip地址的条目来达到目的。

但是/etc/resolv.conf(配置域名服务器的文件)和/etc/hosts文件都是Kubernetes的管理文件,即使我们在Dockerfile中修改了它们,Kubernetes也会把它们改回去,下面是一个验证测试:

Step 9 : RUN cat /etc/hosts
 ---> Running in f5fee576e51e
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.3  bcad5a346f31
 ---> 8ae569bc36eb
Removing intermediate container f5fee576e51e
Step 10 : ADD docker/hosts /etc/
 ---> 8084abbb776b
Removing intermediate container 1f4d9bb8f589
Step 11 : ADD docker/hosts /tmp/
 ---> 4c270b24385b
Removing intermediate container bf5a12b2b74c
Step 12 : RUN cat /etc/hosts
 ---> Running in d9149d6d4562
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.3  bcad5a346f31
 ---> a1b99bc9e8a1
Removing intermediate container d9149d6d4562
Step 13 : RUN cat /tmp/hosts
 ---> Running in 7c8b6339aef2
127.0.0.1 google.com
 ---> 867edf6691b6

我们可以在run.sh中修改hosts文件以达到目的: Dockerfile

COPY hosts /root/

run.sh

cat /root/hosts >> /etc/hosts