k8s强制删除

可使用kubectl中的强制删除命令

1
2
3
4
5
# 删除POD
kubectl delete pod PODNAME --force --grace-period=0

# 删除NAMESPACE
kubectl delete namespace NAMESPACENAME --force --grace-period=0

有时候这种方法也删除不掉,可能是之前删除顺序有问题,没有删干净pod,就删除命名空间,导致删除不掉

直接从ETCD中删除源数据

1
2
3
4
5
# 删除default namespace下的pod名为pod-to-be-deleted-0
ETCDCTL_API=3 etcdctl del /registry/pods/default/pod-to-be-deleted-0

# 删除需要删除的NAMESPACE
etcdctl del /registry/namespaces/NAMESPACENAME

添加别名

上面直接etcd删除,是证书直接能找到时候,如果证书配置方式不一样,就需要手动配一下!

配置别名etcdctl3,添加证书等参数

1
2
3
4
5
6
7
alias etcdctl3='docker run --rm -it \
--net host -e ETCDCTL_API=3 \
-v /etc/kubernetes:/etc/kubernetes k8s.gcr.io/etcd:3.3.10 etcdctl \
--cert /etc/kubernetes/pki/etcd/peer.crt \
--key /etc/kubernetes/pki/etcd/peer.key \
--cacert /etc/kubernetes/pki/etcd/ca.crt \
--endpoints https://192.168.3.101:2379,https://192.168.3.102:2379,https://192.168.3.103:2379'

查询都有哪些daemonsets

1
2
3
4
tcdctl3 get /registry/daemonsets/ --prefix --keys-only
/registry/daemonsets/default/testpod
/registry/daemonsets/kube-system/calico-node
/registry/daemonsets/kube-system/kube-proxy

与kubectl查看的结果一致

1
2
3
4
5
kubectl get daemonsets --all-namespaces 
NAMESPACE NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
default testpod 3 3 3 3 3 <none> 91m
kube-system calico-node 3 3 3 3 3 beta.kubernetes.io/os=linux 116m
kube-system kube-proxy 3 3 3 3 3 <none> 122m

在etcd中查询default namespace中的pod

1
2
3
4
etcdctl3 get /registry/pods/default --prefix --keys-only 
/registry/pods/default/testpod-5wtb7
/registry/pods/default/testpod-646d8
/registry/pods/default/testpod-t7ps7

kubectl命令看到结果与etcd中一致

1
2
3
4
5
kubectl get pods -l app=fortest
NAME READY STATUS RESTARTS AGE
testpod-5wtb7 1/1 Running 0 93m
testpod-646d8 1/1 Running 0 93m
testpod-t7ps7 1/1 Running 0 93m

在etcd中删除pod testpod-t7ps7

1
2
etcdctl3 del /registry/pods/default/testpod-t7ps7    
1

再次查看pod,发现testpod-t7ps7已经没有了

1
2
3
4
5
kubectl get pods
NAME READY STATUS RESTARTS AGE
testpod-5wtb7 1/1 Running 0 96m
testpod-646d8 1/1 Running 0 96m
testpod-qczvt 1/1 Running 0 17s
Donate