当 DNS 配置以及其它选项不合理的时候,通过向 Pod 的 `/etc/hosts` 文件中添加条目,可以在 Pod 级别覆盖对主机名的解析。在 1.7 版本,用户可以通过 PodSpec 的 HostAliases 字段来添加这些自定义的条目。
建议通过使用 HostAliases 来进行修改,因为该文件由 Kubelet 管理,并且可以在 Pod 创建/重启过程中被重写。
## 通过 HostAliases 增加额外的条目
除了默认的样板内容,我们可以向 hosts 文件添加额外的条目,将`foo.local`、`bar.local`解析为`127.0.0.1`,将`foo.remote`、`bar.remote`解析为`10.1.2.3`,我们可以在`.spec.hostAliases`下为 Pod 添加 HostAliases。
~~~yaml
apiVersion: v1
kind: Pod
metadata:
name: hostaliases-pod
spec:
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"
~~~
hosts 文件的内容看起来类似如下这样:
~~~shell
$ kubectl logs hostaliases-pod
# Kubernetes-managed hosts file.
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
fe00::0 ip6-mcastprefix
fe00::1 ip6-allnodes
fe00::2 ip6-allrouters
10.200.0.4 hostaliases-pod
127.0.0.1 foo.local
127.0.0.1 bar.local
10.1.2.3 foo.remote
10.1.2.3 bar.remote
~~~
> 在 1.7 版本,如果 Pod 启用 hostNetwork,那么将不能使用这个特性,因为 kubelet 只管理非 hostNetwork 类型 Pod 的 hosts 文件。后面的可能要改变这个情况
---
官方文档:[https://kubernetes.io/zh/docs/concepts/services-networking/add-entries-to-pod-etc-hosts-with-host-aliases/](https://kubernetes.io/zh/docs/concepts/services-networking/add-entries-to-pod-etc-hosts-with-host-aliases/)
- kubernetes基础
- 安装kubernetes
- kubeadm平滑升级群集
- Taint和Toleration
- 使用HostAliases向Pod /etc/hosts 文件添加条目
- ConfigMap
- 插件
- 支持外部dns
- 安装helm
- HPA
- 存储
- 本地存储
- 网络存储
- Secret
- ConfigMap
- QA
- k8s使用时需要注意的坑点
- 容器中的JVM资源该如何被安全的限制
- 项目实践
- eureka集群
- Traefik ingress服务发现与负载均衡
- etcd数据备份与恢复
- deployment滚动升级与回滚
- 监控
- prometheus operator初体验
- prometheus-operator监控
- metrics-server监控kubernetes资源
- weave scope可视化监控