创建pod的时候,pod会在其/etc/hosts里面添加一个条目。
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
dns-test 1/1 Running 0 24h 10.233.90.31 node1 <none> <none>
[root@master ~]# kubectl exec -it dns-test sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
/ # cat /etc/hosts
# 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.233.90.31 dns-test #左边是pod的IP,右边是pod的名称
可不可以在创建pod的时候指定一些条目在hosts里面?这个是可以的!
上面就是可以将aa/bb.local可以解析为10.1.1.1的地址。 cc/dd.local就可以解析为10.1.1.2的地址。
[root@master pod]# cat nginx.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
app: nginx
name: nginx
namespace: default
spec:
hostAliases:
- ip: "192.168.26.101"
hostnames:
- "vm01"
- "vm02"
containers:
- image: nginx
imagePullPolicy: Always
name: nginx
dnsPolicy: ClusterFirst
[root@master pod]# kubectl apply -f nginx.yaml
pod/nginx created
[root@master pod]# kubectl exec -it nginx sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
# cat /etc/hosts
# 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.233.96.9 nginx
# Entries added by HostAliases.
192.168.26.101 vm01 vm02
这样就完成添加hosts解析了!