一. Pod的DNS策略
- Default: 继承节点的DNS配置;
- ClusterFirst: 使用coredns作为DNS配置;
-
ClusterFirstWithHostNet:
- 当Pod.spec.hostNetwork=true时,Pod的DNS策略被强制转换为Default,即继承节点的DNS配置;
- 若Pod要使用coredns作为DNS配置,则需配置pod.spec.dnsPolicy=ClusterFirstWithHostNet;
- None: 没有DNS配置;
若未指定dnsPolicy,则默认=ClusterFirst。
二. pod.spec.dnsPolicy=None
pod.spec.dnsPolicy=None时,pod中没有任何的dns配置;
此时必须在spec中配置dnsConfig配置,给pod提供自定义的dns配置:
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
containers:
- name: busybox
image: busybox
command: ["/bin/sh", "-c", "uname -r && tail -f /dev/null"]
dnsPolicy: None
dnsConfig:
nameservers:
- 192.168.0.1
容器中可以看到,自定义的dns配置:
# kubectl exec -it test -c busybox -- sh
/ # cat /etc/resolv.conf
nameserver 192.168.0.1
/ # exit
三. pod.spec.dnsPolicy=Default
该模式下,pod会继承节点的dns配置。
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
containers:
- name: busybox
image: busybox
command: ["/bin/sh", "-c", "uname -r && tail -f /dev/null"]
dnsPolicy: Default
查看节点的dns配置:
# cat /etc/resolv.conf
search default.svc.cluster.local svc.cluster.local
nameserver 8.8.8.8
options attempts:2
容器中可以看到,pod的dns配置与节点的一致:
# cat /etc/resolv.conf
search default.svc.cluster.local svc.cluster.local
nameserver 8.8.8.8
options attempts:2
四. pod.spec.dnsPolicy=ClusterFirst
若pod未显示指定dnsPolicy,则默认=ClusterFirst。
该模式下,pod会使用coredns作为pod的dns配置。
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
containers:
- name: busybox
image: busybox
command: ["/bin/sh", "-c", "uname -r && tail -f /dev/null"]
dnsPolicy: ClusterFirst
coredns的svc:
# kubectl get svc -A|grep dns
kube-system kube-dns ClusterIP 10.96.0.10 53/UDP,53/TCP,9153/TCP 177d
容器中可以看到,pod使用coredns作为dns配置:
# kubectl exec -it test -c busybox -- sh
/ #
/ # cat /etc/resolv.conf
search default.svc.cluster.local svc.cluster.local cluster.local
nameserver 10.96.0.10
options ndots:5
五. pod.spec.dnsPolicy=ClusterFirstWithHostNet
若Pod使用hostNetwork,pod的ClusterFirst会被强制转换为Default,即继承宿主机的dns配置:
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
containers:
- name: busybox
image: busybox
command: ["/bin/sh", "-c", "uname -r && tail -f /dev/null"]
dnsPolicy: ClusterFirst
hostNetwork: true
容器中的dns配置,与宿主机的一致:
# kubectl exec -it test -c busybox -- sh
/ # cat /etc/resolv.conf
search default.svc.cluster.local svc.cluster.local
nameserver 8.8.8.8
options attempts:2
若pod使用hostNetwork时,仍然使用coredns作为dns配置,则需要将pod.spec.dnsPolicy配置为ClusterFirstWithHostNet:
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
containers:
- name: busybox
image: busybox
command: ["/bin/sh", "-c", "uname -r && tail -f /dev/null"]
dnsPolicy: ClusterFirstWithHostNet
hostNetwork: true
容器中的dns配置,使用coredns:
# kubectl exec -it test -c busybox -- sh
/ #
/ # cat /etc/resolv.conf
search default.svc.cluster.local svc.cluster.local cluster.local
nameserver 10.96.0.10
options ndots:5
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net