• 【博客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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    这么设计的理由

    官方解析参考稳定:

    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。

  • 相关阅读:
    web服务基础
    [重庆思庄每日技术分享]-SQLLOADER express加载数据报 KUP-04040
    【xxl-job】你与xxl-job仅差这个示例
    浅谈电弧光保护在10kV变电站高压室的应用方案
    世界前沿技术发展报告2023《世界航天技术发展报告》(四)载人航天技术
    MySQL高级篇之索引分类
    市值缩水90%以上,泛生子何以败退美股?
    什么是协程?
    SpringBoot
    CSS详解
  • 原文地址:https://blog.csdn.net/qq_43684922/article/details/126677769