如何重置K3s集群中的Pod?含指定系统Pod的命令行及脚本实现方案
Hey there! Let's break down how to safely restart those core kube-system pods in your K3s cluster. Since these pods are managed by Kubernetes controllers (DaemonSets or Deployments), we don't manually "stop" them—instead, we trigger a restart that the controller will handle to automatically recreate the pods.
Option 1: Restart Individual Pods
If you need to restart a single specific pod (e.g., a misbehaving calico-node-xxxx), you can delete it directly. The associated controller will immediately spin up a replacement:
# Replace "calico-node-xxxx" with your actual pod name kubectl delete pod calico-node-xxxx -n kube-system
You can get the full list of pod names with:
kubectl get pods -n kube-system
Option 2: Batch Restart All Pods of a Component
For restarting all instances of a component (e.g., every calico-node across your cluster), use label-based deletion or controller rollouts. This is more efficient than deleting pods one by one.
Using Label-Based Deletion
Each component has unique labels you can target:
- calico-node:
k8s-app=calico-node - calico-kube-controllers:
k8s-app=calico-kube-controllers - metrics-server:
k8s-app=metrics-server - local-path-provisioner:
app=local-path-provisioner - coredns:
k8s-app=kube-dns
Run this command to delete all pods matching a label (the controller will recreate them automatically):
# Example: Restart all calico-node pods kubectl delete pods -n kube-system -l k8s-app=calico-node
Using Rollout Restart (Recommended for Production)
This method performs a rolling restart—it replaces pods one at a time, minimizing downtime. It's the safest approach for core cluster components:
- For DaemonSets (like
calico-node):kubectl rollout restart daemonset calico-node -n kube-system - For Deployments (all other listed components):
kubectl rollout restart deployment calico-kube-controllers -n kube-system kubectl rollout restart deployment metrics-server -n kube-system kubectl rollout restart deployment local-path-provisioner -n kube-system kubectl rollout restart deployment coredns -n kube-system
Option 3: Automated Script for Full Restart
If you want to restart all these components in one go, use this bash script. It uses rollout restarts (the safest method) and waits for pods to become ready:
#!/bin/bash echo "Initiating rolling restart of core kube-system components..." # Restart DaemonSets kubectl rollout restart daemonset calico-node -n kube-system # Restart Deployments kubectl rollout restart deployment calico-kube-controllers -n kube-system kubectl rollout restart deployment metrics-server -n kube-system kubectl rollout restart deployment local-path-provisioner -n kube-system kubectl rollout restart deployment coredns -n kube-system echo "Waiting for all pods to reach ready state..." # Wait for calico-related pods kubectl wait --for=condition=ready pod -n kube-system -l k8s-app in (calico-node,calico-kube-controllers,metrics-server,kube-dns) --timeout=5m # Wait for local-path-provisioner kubectl wait --for=condition=ready pod -n kube-system -l app=local-path-provisioner --timeout=5m echo "All core kube-system pods have been restarted and are ready!"
Important Notes
- Timing: Restart these components during low-traffic periods to minimize impact on your workloads.
- Network Impact: Restarting
calico-nodewill briefly interrupt network connectivity for pods on the affected node—plan accordingly. - Verify State: After restarting, check pod status with
kubectl get pods -n kube-systemto ensure all are in theRunningstate.
内容的提问来源于stack exchange,提问作者solveit




