【博客474】为什么k8s控制面pod使用的ip是node ip,而非pod cidr中的ip
为什么k8s控制面pod使用的ip是node ip,而不是pod cidr中的ip
Why do certain kube-system Pods such as kube-proxy have the same Pod IP as the node that they are on?
场景
执行 kubectl get pod -n kube-system -o wide 的时候,发现控制面的pod的ip是其所在的node ip,而不是从集群pod cidr中分配的ip
原因:控制面pod设置了hostnetwork = true
当使用kubeadm init来初始化集群的时候,kubeadm会自动生成部署的控制面的static pod yaml,而此时生成的控制面的pod的网络模式跟普通的pod并不一样,而是采用了hostnetwork的网络模式
example:例如控制面中的kube-proxy组件中设置路hostNetwork: true
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
labels:
component: kube-proxy
k8s-app: kube-proxy
kubernetes.io/cluster-service: "true"
name: kube-proxy
tier: node
name: kube-proxy
namespace: kube-system
spec:
selector:
matchLabels:
component: kube-proxy
k8s-app: kube-proxy
kubernetes.io/cluster-service: "true"
name: kube-proxy
tier: node
template:
metadata:
annotations:
scheduler.alpha.kubernetes.io/affinity: '{"nodeAffinity":{"requiredDuringSchedulingIgnoredDuringExecution":{"nodeSelectorTerms":[{"matchExpressions":[{"key":"beta.kubernetes.io/arch","operator":"In","values":["amd64"]}]}]}}}'
scheduler.alpha.kubernetes.io/tolerations: '[{"key":"dedicated","value":"master","effect":"NoSchedule"}]'
labels:
component: kube-proxy
k8s-app: kube-proxy
kubernetes.io/cluster-service: "true"
name: kube-proxy
tier: node
spec:
hostNetwork: true
containers:
- name: kube-proxy
image: gcr.io/google_containers/kube-proxy-amd64:v1.5.3
imagePullPolicy: IfNotPresent
command:
- kube-proxy
- --kubeconfig=/run/kubeconfig
securityContext:
privileged: true
volumeMounts:
- mountPath: /var/run/dbus
name: dbus
- mountPath: /run/kubeconfig
name: kubeconfig
volumes:
- hostPath:
path: /etc/kubernetes/kubelet.conf
name: kubeconfig
- hostPath:
path: /var/run/dbus
name: dbus
这么设计的理由
官方解析参考稳定:
https://kubernetes.io/docs/reference/setup-tools/kubeadm/implementation-details/
根本原因:
hostNetwork: true is set on all static Pods to allow control plane startup before a network is configured.
kubeadm deploys the control plane components with hostNetwork: true because they may have to interact the host network interfaces.
即:hostNetwork在所有控制面静态 Pod 上设置为 true,以允许在配置网络之前启动控制平面
kubeadm 使用 hostNetwork: true 部署控制平面组件,因为它们可能必须与主机网络接口交互。
不这么设计会怎么样
你无法通过拽自己的头发来让自己飞翔!
假设所有的pod都从pod cidr中来分配,那么pod cidr又是控制面来分配的,但是控制面启动时自己的pod ip不能依赖自己来分配。而且控制面在启动的时候,不同node上的控制面之间要互相交互,那么此时只能依赖node的网络来进行交互,因为这时候cni集群网络还未就绪。因此为了能够在集群网络未就绪时,控制面组件能够启动并进行交互,只能使用node的网络,因此设置了hostNetwork: true,这也就是为什么控制面pod的ip是node的ip。