iT邦幫忙

2021 iThome 鐵人賽

DAY 29
2
DevOps

k8s 入門學習 30天系列 第 29

IT 鐵人賽 k8s 入門30天 -- day29 Adding entries to Pod /etc/hosts with HostAliases

  • 分享至 

  • xImage
  •  

參考文件 https://kubernetes.io/docs/tasks/network/customize-hosts-file-for-pods/

前言

今天要來實作 Adding entries to Pod /etc/hosts with HostAliases 這個任務

當 DNS 無沒有辦法轉換時, 在 Pod 的 /etc/hosts 新增欄位能夠做到 Pod 級別的域名轉換

可以在 PodSpec 的 HostAlias 欄位新增要轉換的域名來達到上述的要求

/etc/hosts 主要是被 kubelet 所管理, 當 Pod 產生或是重啟時, 這個檔案的設定都有可能被複寫掉, 所以不建議用 HostAlias 的改法

佈署目標

1 建立一個 nginx 的 Pod, 並檢查原始 nginx container 內的 /etc/hosts

2 新增一個 Pod 使用 HostAlias 新增域名轉換並且透過 cat /etc/hosts 把值印出來

建立一個 nginx 的 Pod, 並檢查原始 nginx container 內的 /etc/hosts

建立 nginx Pod 使用以下指令

kubectl run nginx --image nginx

查看 Pod IP 使用以下指令

kubectl get pods --output=wide

查看 container 內的 /etc/hosts 檔案使用以下指令

kubectl exec nginx -- cat /etc/hosts

新增一個 Pod 使用 HostAlias 新增域名轉換並且透過 cat /etc/hosts 把值印出來

建立 hostaliases-pod.yaml 如下

apiVersion: v1
kind: Pod
metadata:
  name: hostaliases-pod
spec:
  restartPolicy: Never
  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"

建立一個 Pod

名稱設定為 hostaliases-pod

設定 container image 使用 busybox

設定 container 執行指令 如下

cat /etc/hosts

設定 hostAliases 如下

hostAliases:
- ip: "127.0.0.1"
  hostnames:
  - "foo.local"
  - "bar.local"
- ip: "10.1.2.3"
  hostnames:
  - "foo.remote"
  - "bar.remote"

建立佈署使用以下指令

kubectl apply -f hostaliases-pod.yaml

查看 Pod 的 IP 使用以下指令

kubectl get pod --output=wide

查看 Pod 的 log 使用以下指令

kubectl logs hostaliases-pod

後記

為何用 kubelet 管理 hosts 檔案?

主要是為了避免 Container 在啟動後去做修改

因為如果在 Container 做修改, 這些修改都會在 Container 重起之後消失


上一篇
IT 鐵人賽 k8s 入門30天 -- day28 k8s Service Catalog
下一篇
IT 鐵人賽 k8s 入門30天 -- day30 Share Process Namespace between Containers in a Pod
系列文
k8s 入門學習 30天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
雷N
iT邦研究生 1 級 ‧ 2021-10-13 13:30:16

我要跟GSON學習K8S /images/emoticon/emoticon08.gif

json_liang iT邦研究生 5 級 ‧ 2021-10-13 13:40:53 檢舉

/images/emoticon/emoticon41.gif

0

賀!!!即將完賽!

json_liang iT邦研究生 5 級 ‧ 2021-10-14 12:41:47 檢舉

/images/emoticon/emoticon08.gif

0
juck30808
iT邦研究生 1 級 ‧ 2021-10-14 12:36:20

恭喜即將邁入完賽階段~

json_liang iT邦研究生 5 級 ‧ 2021-10-14 12:42:13 檢舉

/images/emoticon/emoticon08.gif

我要留言

立即登入留言