k8s如何为节点资源预留

问题:节点用户训练cpu过高或者内存消耗过高会对节点系统进程以及kube进程产生影响。
解决:
Kubelet Node Allocatable

  • Kubelet Node Allocatable用来为Kube组件和System进程预留资源,从而保证当节点出现满负荷时也能保证Kube和System进程有足够的资源。

  • 目前支持cpu, memory, ephemeral-storage三种资源预留。

  • Node Capacity是Node的所有硬件资源,kube-reserved是给kube组件预留的资源,system-reserved是给System进程预留的资源, eviction-threshold是kubelet eviction的阈值设定,allocatable才是真正scheduler调度Pod时的参考值(保证Node上所有Pods的request resource不超过Allocatable)。

  • Node Allocatable Resource = Node Capacity - Kube-reserved - system-reserved - eviction-threshold
    如何配置

  • –enforce-node-allocatable,默认为pods,要为kube组件和System进程预留资源,则需要设置为pods,kube-reserved,system-reserve。

  • –cgroups-per-qos,Enabling QoS and Pod level cgroups,默认开启。开启后,kubelet会将管理所有workload Pods的cgroups。

—cgroup-driver,默认为cgroupfs,另一可选项为systemd。取决于容器运行时使用的cgroup driver,kubelet与其保持一致。比如你配置docker使用systemd cgroup driver,那么kubelet也需要配置–cgroup-driver=systemd。

  • –kube-reserved,用于配置为kube组件(kubelet,kube-proxy,dockerd等)预留的资源量,比如—kube-reserved=cpu=1000m,memory=8Gi,ephemeral-storage=16Gi。

  • –kube-reserved-cgroup,如果你设置了–kube-reserved,那么请一定要设置对应的cgroup,并且该cgroup目录要事先创建好,否则kubelet将不会自动创建导致kubelet启动失败。比如设置为kube-reserved-cgroup=/kubelet.service 。

  • —system-reserved,用于配置为System进程预留的资源量,比如—system-reserved=cpu=500m,memory=4Gi,ephemeral-storage=4Gi。

  • –system-reserved-cgroup,如果你设置了–system-reserved,那么请一定要设置对应的cgroup,并且该cgroup目录要事先创建好,否则kubelet将不会自动创建导致kubelet启动失败。比如设置为system-reserved-cgroup=/system.slice。

  • –eviction-hard,用来配置kubelet的hard eviction条件,只支持memory和ephemeral-storage两种不可压缩资源。当出现MemoryPressure时,Scheduler不会调度新的Best-Effort QoS Pods到此节点。当出现DiskPressure时,Scheduler不会调度任何新Pods到此节点。关于Kubelet Eviction的更多解读,请参考我的相关博文。

  • Kubelet Node Allocatable的代码很简单,主要在pkg/kubelet/cm/node_container_manager.go,感兴趣的同学自己去走读一遍。
    举例:
    [root@node177 system]# cat /etc/systemd/system/kubelet.service.d/10-kubelet.conf

     [Service]
     Environment="KUBELET_POD_INFRA_CONTAINER=--pod-infra-container-image=registry.bst-1.cns.bstjpc.com:5000/k8s.gcr.io/pause-amd64:3.1"
     #Environment="KUBELET_KUBECONFIG_ARGS=--kubeconfig=/etc/kubernetes/kubelet.conf"
     Environment="KUBELET_KUBECONFIG_ARGS=--kubeconfig=/etc/kubernetes/admin.conf"
     Environment="KUBELET_SYSTEM_PODS_ARGS=--pod-manifest-path=/etc/kubernetes/manifests --allow-privileged=true"
     Environment="KUBELET_DNS_ARGS=--cluster-dns=10.96.0.10 --cluster-domain=cluster.local"
     Environment="KUBELET_CADVISOR_ARGS=--cadvisor-port=4194"
     Environment="KUBELET_VOLUME_ARGS=--volume-plugin-dir=/var/lib/kubelet/volumeplugins --feature-gates=DevicePlugins=true,BlockVolume=true,PodPriority=true --volume-stats-agg-period=0 "
     Environment="KUBELET_EXTRA_ARGS=--fail-swap-on=false --logtostderr=true --v=0"
     
     Environment="KUBELET_NETWORK_ARGS=--network-plugin=cni --cni-conf-dir=/etc/cni/net.d --cni-bin-dir=/opt/cni/bin"
     Environment="KUBELET_AUTHZ_ARGS=--authorization-mode=Webhook --client-ca-file=/etc/kubernetes/pki/ca.pem"
     Environment="KUBELET_CERTIFICATE_ARGS=--rotate-certificates=true --cert-dir=/var/lib/kubelet/pki"
     
     #ExecStart=/usr/local/bin/kubelet $KUBELET_KUBECONFIG_ARGS $KUBELET_SYSTEM_PODS_ARGS $KUBELET_DNS_ARGS $KUBELET_CADVISOR_ARGS $KUBELET_VOLUME_ARGS $KUBELET_EXTRA_ARGS
    

[root@node177 system]# cat /usr/lib/systemd/system/kubelet.service

	[Unit]
	Description=kubelet: The Kubernetes Node Agent
	Documentation=http://kubernetes.io/docs/
	#After=docker.service
	#Wants=dcoker.service

	[Service]
	#ExecStart=/usr/local/bin/kubelet
	#ExecStart=/usr/local/bin/kubelet $KUBELET_POD_INFRA_CONTAINER  $KUBELET_KUBECONFIG_ARGS $KUBELET_SYSTEM_PODS_ARGS $KUBELET_DNS_ARGS $KUBELET_CADVISOR_ARGS $KUBELET_VOLUME_ARGS $KUBELET_EXTRA_ARGS
	ExecStartPre=/usr/bin/mkdir -p /sys/fs/cgroup/cpuset/system.slice/kubelet.service
	ExecStartPre=/usr/bin/mkdir -p /sys/fs/cgroup/hugetlb/system.slice/kubelet.service
	ExecStart=/usr/local/bin/kubelet $KUBELET_POD_INFRA_CONTAINER $KUBELET_KUBECONFIG_ARGS $KUBELET_SYSTEM_PODS_ARGS $KUBELET_DNS_ARGS $KUBELET_CADVISOR_ARGS $KUBELET_VOLUME_ARGS $KUBELET_EXTRA_ARGS \
	--cgroup-driver=cgroupfs \
	--cgroup-root= \
	--enforce-node-allocatable=pods,kube-reserved,system-reserved \
	--kube-reserved-cgroup=/system.slice/kubelet.service \
	--system-reserved-cgroup=/system.slice \
	--kube-reserved=cpu=2,memory=8Gi \
	--system-reserved=cpu=6,memory=24Gi \
	Restart=always
	StartLimitInterval=0
	RestartSec=10

	[Install]
	WantedBy=multi-user.target

参考:https://my.oschina.net/jxcdwangtao/blog/1629059

你可能感兴趣的:(k8s)