参考文件 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 使用以下指令
kubectl run nginx --image nginx
查看 Pod IP 使用以下指令
kubectl get pods --output=wide
查看 container 内的 /etc/hosts 档案使用以下指令
kubectl exec nginx -- 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 重起之後消失
这题写凌晨2、3点,本来要用c的,最後用JAVA,然後睡觉前送出去Time Limit Exceed...
前言(心得) 昨天在写Code的时候一不注意时间就超过了 其实我本来是很懒惰容易放弃的人 在之前的参...
出於书本 Chapter 4. Hacking Methodology 看看别人能看见什麽 - fo...
工程师太师了: 第12话 杂记: 今天看到一个议题 到底对自己做长远的规划是否比较好? 很多人都会对...
我们过去在写 JavaScript 使用的同步延迟,通常都是用非同步的 setTimeout 加 ...