• kubeadm系列-02-kubelet的配置和启动


    overview

    不管是在 control plane 节点还是在普通的 worker 节点,kubelet 是必须要启动的进程,而 kubelet 的安装方式可以用 rpm 包也可以用二进制

    关于 kube init 里关于 kubelet 启动的日志,复习一下

    # 开始启动kubelet
    [kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
    [kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
    [kubelet-start] Starting the kubelet
    
    • 1
    • 2
    • 3
    • 4

    默认的配置

    对于默认配置,我们可以不用每个参数都非常了解,可以有需要的时候再去查相关的文档和代码

    # kubeadm config print init-defaults --component-configs KubeletConfiguration
    # 节选了kind: KubeletConfiguration的部分
    ---
    apiVersion: kubelet.config.k8s.io/v1beta1
    authentication:
      anonymous:
        enabled: false
      webhook:
        cacheTTL: 0s
        enabled: true
      x509:
        clientCAFile: /etc/kubernetes/pki/ca.crt
    authorization:
      mode: Webhook
      webhook:
        cacheAuthorizedTTL: 0s
        cacheUnauthorizedTTL: 0s
    cgroupDriver: cgroupfs
    clusterDNS:
    - 10.96.0.10
    clusterDomain: cluster.local
    cpuManagerReconcilePeriod: 0s
    evictionPressureTransitionPeriod: 0s
    fileCheckFrequency: 0s
    healthzBindAddress: 127.0.0.1
    healthzPort: 10248
    httpCheckFrequency: 0s
    imageMinimumGCAge: 0s
    kind: KubeletConfiguration
    logging: {}
    nodeStatusReportFrequency: 0s
    nodeStatusUpdateFrequency: 0s
    rotateCertificates: true
    runtimeRequestTimeout: 0s
    shutdownGracePeriod: 0s
    shutdownGracePeriodCriticalPods: 0s
    staticPodPath: /etc/kubernetes/manifests
    streamingConnectionIdleTimeout: 0s
    syncFrequency: 0s
    volumeStatsAggPeriod: 0s
    
    • 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

    drop in配置

    kubelet 有一些特殊的配置文件需要注意一下,不明白的可以查看官方文档

    在不用那些包管理器的情况下,徒手装的文档可以参考这里

    启动

    kubelet 真正被启动的逻辑在下面的代码里,首先会先尝试停止进程(防止有多个进程启动了),本质上就是通过 systemctl status kubelet 先检查服务的状态,如果没有 enable 就会提醒,如果 enable 了就会通过 systemctl stop kubelet 来停止服务,然后会吧 ClusterConfigurationNodeRegistration 的参数(并不是所有,主要是启动进程启动的参数args之类的写到KUBELET_KUBEADM_ARGS)合并,并且会写到默认的路径 /var/lib/kubelet/kubeadm-flags.env,然后再把 ClusterConfiguration (只针对kubelet.config.k8s.io)写到磁盘 /var/lib/kubelet/config.yaml,最后就是通过 systemctl start kubelet 来启动进程了

    // runKubeletStart executes kubelet start logic.
    func runKubeletStart(c workflow.RunData) error {
    	data, ok := c.(InitData)
    	if !ok {
    		return errors.New("kubelet-start phase invoked with an invalid data struct")
    	}
    
    	// First off, configure the kubelet. In this short timeframe, kubeadm is trying to stop/restart the kubelet
    	// Try to stop the kubelet service so no race conditions occur when configuring it
    	if !data.DryRun() {
    		klog.V(1).Infoln("Stopping the kubelet")
    		kubeletphase.TryStopKubelet()
    	}
    
    	// Write env file with flags for the kubelet to use. We do not need to write the --register-with-taints for the control-plane,
    	// as we handle that ourselves in the mark-control-plane phase
    	// TODO: Maybe we want to do that some time in the future, in order to remove some logic from the mark-control-plane phase?
    	if err := kubeletphase.WriteKubeletDynamicEnvFile(&data.Cfg().ClusterConfiguration, &data.Cfg().NodeRegistration, false, data.KubeletDir()); err != nil {
    		return errors.Wrap(err, "error writing a dynamic environment file for the kubelet")
    	}
    
    	// Write the kubelet configuration file to disk.
    	if err := kubeletphase.WriteConfigToDisk(&data.Cfg().ClusterConfiguration, data.KubeletDir()); err != nil {
    		return errors.Wrap(err, "error writing kubelet configuration to disk")
    	}
    
    	// Try to start the kubelet service in case it's inactive
    	if !data.DryRun() {
    		fmt.Println("[kubelet-start] Starting the kubelet")
    		kubeletphase.TryStartKubelet()
    	}
    
    	return nil
    }
    
    • 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

    来看下

    # cat /var/lib/kubelet/kubeadm-flags.env
    KUBELET_KUBEADM_ARGS="--network-plugin=cni --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.4.1"
    # cat /var/lib/kubelet/config.yaml
    apiVersion: kubelet.config.k8s.io/v1beta1
    authentication:
      anonymous:
        enabled: false
      webhook:
        cacheTTL: 0s
        enabled: true
      x509:
        clientCAFile: /etc/kubernetes/pki/ca.crt
    authorization:
      mode: Webhook
      webhook:
        cacheAuthorizedTTL: 0s
        cacheUnauthorizedTTL: 0s
    cgroupDriver: cgroupfs
    clusterDNS:
    - 10.96.0.10
    clusterDomain: cluster.local
    cpuManagerReconcilePeriod: 0s
    evictionPressureTransitionPeriod: 0s
    fileCheckFrequency: 0s
    healthzBindAddress: 127.0.0.1
    healthzPort: 10248
    httpCheckFrequency: 0s
    imageMinimumGCAge: 0s
    kind: KubeletConfiguration
    logging: {}
    nodeStatusReportFrequency: 0s
    nodeStatusUpdateFrequency: 0s
    rotateCertificates: true
    runtimeRequestTimeout: 0s
    shutdownGracePeriod: 0s
    shutdownGracePeriodCriticalPods: 0s
    staticPodPath: /etc/kubernetes/manifests
    streamingConnectionIdleTimeout: 0s
    syncFrequency: 0s
    volumeStatsAggPeriod: 0s
    
    • 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

    其他

    下面是在 control plane 上完整启动了 kubelet 之后通过 ps -ef|cat 获得的进程以及参数

    # kubeadm init --upload-certs --image-repository registry.aliyuncs.com/google_containers --kubernetes-version 1.21.7 --pod-network-cidr=10.244.0.0/16
    /usr/bin/kubelet --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf --config=/var/lib/kubelet/config.yaml --network-plugin=cni --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.4.1
    
    • 1
    • 2
  • 相关阅读:
    某模块休眠时接收短信不能在串口显示的问题
    若要对多态类进行深拷贝,应使用虚函数的clone,而不是公开的拷贝构造赋值
    信息学奥赛一本通 1339:【例3-4】求后序遍历 | 洛谷 P1827 [USACO3.4] 美国血统 American Heritage
    K8S资源对象:StatefulSet简介
    [问题记录]Qt使用QPainter在QImage、QBitmap、QPixmap上面绘图时出现杂色
    卡尔曼滤波(Kalman Filter)原理浅析-数学理论推导-1
    共享盘的文件删除后能找回吗
    ROC-RK3588-PC 八核8K人工智能开源主板
    Docker进阶:Docker Compose(容器编排) 管理多容器应用—实战案例演示
    Git使用教程
  • 原文地址:https://blog.csdn.net/oscarun/article/details/125611766