k8s 自定义pod hosts的几种方法
日常中我们可能会碰到不少关于自定义pod hosts的场景,对于不同的k8s 版本处理的不一样的
以下整理一些参考方法
自定义coredns
自定义coredns 可以直接让解析的域名使用coredns 处理,好处是可以全局生效,不好的地方就是全局可能会影响其他不需要自定义hosts的
扩展coredns 的方法就比较多了,可以基于dns 的转发以及直接配置coredns 的hosts
hosts 模式参考
. {
hosts example.hosts example.org example.net {
fallthrough
}
}
对于k8s 小于1.7 版本的
可以基于声明周期钩子,添加hosts 文件
参考
yaml 定义,添加如下
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "echo <ip> <domain> >> /etc/hosts"]
对于比较多的,我们可以直接使用文件配置,格式和hosts 一致
对于大于等于1.7 版本的
使用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:1.28
command:
- cat
args:
- "/etc/hosts"
参考资料
https://kubernetes.io/docs/tasks/network/customize-hosts-file-for-pods/
https://coredns.io/plugins/hosts/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2020-06-16 使用openresty+ xip 服务暴露k8s 部署的spring cloud 服务为外部可访问的地址
2019-06-16 rustup 使用
2016-06-16 Oboe 提升web 用户体验以及性能