跳转至

K8s 常用命令

1. 人工删除驱逐pod

kubectl get pods -A | grep Evicted | awk '{print $1 " "$2}' | xargs -n2 kubectl delete pod -n $1 $2

2. 定时清理垃圾

0 3 * * 0 nerdctl system prune -af

3. k8s磁盘压力

grep -i  disk  /var/log/messages | head -10

...
Apr 23 09:08:10 TOS-1566 kubelet[2599]: I0423 09:08:10.290870    2599 image_gc_manager.go:356] "Disk usage on image filesystem is over the high threshold, trying to free bytes down to the low threshold" usage=85 highThreshold=85 amountToFree=10315730124 lowThreshold=80
...

3.1 解决方案1:

1、清理磁盘空间,将磁盘使用率降到80%以下 2、重启kubelet服务

systemctl daemon-reload
systemctl restart kubelet

3.1 解决方案2:

  1. 看现存配置

    grep Percentage /var/log/messages
    
    image.png

  2. 修改配置文件增加传参数,添加此配置项–eviction-hard=nodefs.available<5%

    #1、备份原先配置文件
    cp /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf.bak
    
    #2、修改配置文件,在Environment中增加--eviction-hard=nodefs.available<5%
    vim /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf
    # Note: This dropin only works with kubeadm and kubelet v1.11+
    [Service]
    Environment="KUBELET_KUBECONFIG_ARGS=--bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf --eviction-hard=nodefs.available<5%"
    Environment="KUBELET_CONFIG_ARGS=--config=/var/lib/kubelet/config.yaml"
    # This is a file that "kubeadm init" and "kubeadm join" generates at runtime, populating the KUBELET_KUBEADM_ARGS variable dynamically
    EnvironmentFile=-/var/lib/kubelet/kubeadm-flags.env
    # This is a file that the user can use for overrides of the kubelet args as a last resort. Preferably, the user should use
    # the .NodeRegistration.KubeletExtraArgs object in the configuration files instead. KUBELET_EXTRA_ARGS should be sourced from this file.
    EnvironmentFile=-/etc/sysconfig/kubelet
    ExecStart=
    ExecStart=/usr/bin/kubelet $KUBELET_KUBECONFIG_ARGS $KUBELET_CONFIG_ARGS $KUBELET_KUBEADM_ARGS $KUBELET_EXTRA_ARGS
    
    #3、重启kubelet服务
    systemctl daemon-reload
    systemctl restart kubelet
    

  3. k8s 看service 端口占用
    iptables -L -t nat