代码改变世界

定义pod的hosts文件(HostAliases)

  假面Wilson  阅读(336)  评论(0编辑  收藏  举报

通过HostAliases 向 Pod /etc/hosts 文件添加条目

当 DNS 配置以及其它选项不合理的时候,通过向 Pod 的 /etc/hosts 文件中添加条目, 可以在 Pod 级别覆盖对主机名的解析。你可以通过 PodSpec 的 HostAliases 字段来添加这些自定义条目.

复制代码
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 查看 Hosts

kubectl exec -ti {pod} -n {namespace} --kubeconfig {kubeconfig}  -- /bin/sh

cat /etc/hosts

pod打印/etc/hosts文件,多了以下内容

#Entries added by HostAliases.
127.0.0.1   foo.local   bar.local
10.1.2.3    foo.remote  bar.remote

避免手动去修改/etc/hosts文件,通过HostAliases进行修改,因为该文件由 Kubelet 管理,并且 可以在 Pod 创建/重启过程中被重写

(评论功能已被禁用)
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示