1.hostAliases

在 Kubernetes (K8s) 中,hostAliases 是一种用于在 Pod 中配置主机名与 IP 地址映射的机制。

hostAliases:定义了Pod的hosts文件(比如/etc/hosts)里面的内容,用法如下:

apiVersion: v1
kind: Pod
...
spec:
  hostAliases:
    - ip: "10.1.2.3"
      hostnames:
        - "test1.com"
        - "test2.com"
    - ip: "10.1.2.4"
      hostnames:
        - "test3.com"
...

在这个pod的yaml中,我设置了2组IP和hostname的数据。这样,这个pod启动后,/etc/hosts文件内容如下:

cat /etc/hosts
#Kubernetes-manage hosts files
127.0.0.1 localhost
...
10.1.2.3 test1.com
10.1.2.3 test2.com
10.1.2.4 test3.com
...

2.volume挂载

apiVersion: v1
kind: Pod
...
spec:
  containers:
  - name: my-pod
    image: busybox
    volumeMounts:
    - name: app-hosts
      mountPath: /etc/hosts
...
  volumes:
  - hostPath:
      path: /etc/hosts
      type: DirectoryOrCreate
    name: app-hosts
...