docker k8s修改容器hosts檔案

2021-09-24 19:32:37 字數 1081 閱讀 6027

docker映象本質上是乙個包含了整個作業系統的檔案和目錄的rootfs,使用者製作映象的每一步操作都會生成乙個層,也就是乙個增量的rootfs。

docker容器的rootfs由唯讀層,init層和可讀寫層。

我們的/etc/hosts和/etc/resolv.conf等只對當前容器生效的資訊會保留在init層,進行docker commit時不會提交這一層的資訊。

所以dockerfile中對/etc/hosts進行修改,或進入映象中修改後進行commit都無法真正的修改/etc/hosts的內容。

可以使用以下方法進行修改:

docker修改/etc/hosts

docker修改/etc/hosts,在容器啟動時新增如下引數即可向/etc/hosts中新增內容:

--add-host="hostname:host_ip"
k8s修改/etc/hosts

k8s可以通過hostaliases 向 pod /etc/hosts 檔案新增內容,具體如下:

apiversion: v1

kind: pod

metadata:

name: hostaliases-pod

spec:

hostaliases:

- ip: "127.0.0.1"

hostnames:

- "foo.local"

- "bar.local"

- ip: "10.1.2.3"

hostnames:

- "foo.remote"

- "bar.remote"

containers:

- name: cat-hosts

image: busybox

command:

- cat

args:

- "/etc/hosts"

注:k8s中的/etc/hosts檔案由kubelet管理,無論容器重啟或pod重啟排程,使用者修改該hosts檔案的任何內容都會在kubelet重新安裝後覆蓋,因此不要在容器啟動後修改hosts檔案的內容。

Docker K8s 解決容器內時區不一致方案

自 我們知道,使用 docker 容器啟動服務後,如果使用預設 centos 系統作為基礎映象,就會出現系統時區不一致的問題,因為預設 centos 系統時間為 utc 協調世界時 universal time coordinated 一般本地所屬時區為 cst 8 時區,上海時間 時間上剛好相差 ...

k8s 容器鉤子

有兩個鉤子暴露在容器中 poststart 這個鉤子在建立容器之後立即執行。但是,不能保證鉤子會在容器入口點之前執行。沒有引數傳遞給處理程式。kind deployment metadata name testlifecycle labels spec replicas 1 selector mat...

容器編排工具k8s

如何實踐 1 搜尋kubunetes online kubernetes playground katacoda pod k8s最小排程單位 docker pause 二個容器 deployments 維持pod一定數量 service 解決deploytemnets內部之間的乙個負載均衡 dns解...