Kubernetes v1.7新特性解析-自定義Hosts

默認情況下,容器的/etc/hosts是kubelet自動生成的,並且僅包含localhost和podName等。不建議在容器內直接修改/etc/hosts文件,因為在Pod啟動或重啟時會被覆蓋。

默認的/etc/hosts文件格式如下,其中nginx-4217019353-fb2c5是podName:

$ kubectl exec nginx-4217019353-fb2c5 -- cat /etc/hostsn# Kubernetes-managed hosts file.n127.0.0.1 localhostn::1 localhost ip6-localhost ip6-loopbacknfe00::0 ip6-localnetnfe00::0 ip6-mcastprefixnfe00::1 ip6-allnodesnfe00::2 ip6-allroutersn10.244.1.4 nginx-4217019353-fb2c5n

從v1.7開始,可以通過pod.Spec.HostAliases來增加hosts內容,如

apiVersion: v1nkind: Podnmetadata:n name: hostaliases-podnspec:n hostAliases:n - ip: "127.0.0.1"n hostnames:n - "foo.local"n - "bar.local"n - ip: "10.1.2.3"n hostnames:n - "foo.remote"n - "bar.remote"n containers:n - name: cat-hostsn image: busyboxn command:n - catn args:n - "/etc/hosts"n

$ kubectl logs hostaliases-podn# Kubernetes-managed hosts file.n127.0.0.1 localhostn::1 localhost ip6-localhost ip6-loopbacknfe00::0 ip6-localnetnfe00::0 ip6-mcastprefixnfe00::1 ip6-allnodesnfe00::2 ip6-allroutersn10.244.1.5 hostaliases-podn127.0.0.1 foo.localn127.0.0.1 bar.localn10.1.2.3 foo.remoten10.1.2.3 bar.remoten

本文已發布到《Kubernetes指南》開源書,歡迎關注。

推薦閱讀:

Docker的一個簡單示例
Hyper:一款新推出的免費容器(類vps)
Docker 可以用於生產環境了嗎?

TAG:Kubernetes | Docker | 容器云 |