Kubernetes 无法删除pod实例的排查过程

以下内容来自博客:https://blog.csdn.net/qq_19674905/article/details/80887461

原本由两台Kubernetes组成的小集群,但是今天只开启了一台机器,也就是只有一个节点,造成了无法删除pod实例的原因。

先查看一下现在的容器的运行状态:

 
  1. [root@k8s ~]# kubectl get pods 
  2.  
  3. NAME                      READY     STATUS             RESTARTS   AGE 
  4. nginx-controller-lv8md    1/1       Unknown            0          16h 
  5. nginx-controller-sb3fx    1/1       Unknown            2          16h 
  6. nginx2-1216651254-4b2dw   0/1       ImagePullBackOff   0          8m 
  7. nginx2-1216651254-dbtms   0/1       ImagePullBackOff   0          8m 
  8. nginx2-1216651254-fhb4r   0/1       ImagePullBackOff   0          8m 

 

查看有哪些replicationcontroller   [简写rc]

 
  1. [root@k8s ~]# kubectl get rc 
  2.  
  3. No resources found. 

 

查看有哪些services

 
  1. [root@k8s ~]# kubectl get svc 
  2.  
  3. NAME         CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE 
  4. kubernetes   10.254.0.1   <none>        443/TCP   2d 

 

看到上面没有rc,也没有services,那尝试这样删除所有的pods:

 
  1. [root@k8s ~]# kubectl delete pods --all 
  2.  
  3. pod "nginx-controller-lv8md" deleted 
  4. pod "nginx-controller-sb3fx" deleted 
  5. pod "nginx2-1216651254-4b2dw" deleted 
  6. pod "nginx2-1216651254-dbtms" deleted 
  7. pod "nginx2-1216651254-fhb4r" deleted 

 

但是还是无法删除,查看已经部署的容器;

 
  1. [root@k8s ~]# kubectl get deployment  
  2.  
  3. NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE 
  4. nginx2    3         3         3            0           16h 
  5.  
  6. [root@k8s ~]# kubectl delete deployment nginx2  
  7. deployment "nginx2" deleted 

为什么这三个Pod实例没有rc或者services呢,因为创建它的时候是使用run来实现的;

但是剩下的两个实例怎么删除呢?

 
  1. [root@k8s ~]# kubectl get pods 
  2.  
  3. NAME                     READY     STATUS    RESTARTS   AGE 
  4.  
  5. nginx-controller-lv8md   1/1       Unknown   0          20h 
  6. nginx-controller-sb3fx   1/1       Unknown   2          20h 

 

查看一下实例的信息:

 
  1. [root@k8s ~]# kubectl describe pod nginx-controller-lv8md  
  2.  
  3. Name:               nginx-controller-lv8md 
  4. Namespace:          default 
  5. Node:               k8s-node/10.0.10.11           #这里是重点,因为这两个容器是分配到了k8s-node节点上,而这个节点现在宕机了。 
  6. Start Time:         Tue, 13 Jun 2017 02:01:45 +0800 
  7. Labels:             app=nginx 
  8. Status:             Terminating (expires Mon, 12 Jun 2017 21:46:21 +0800) 
  9. Termination Grace Period:   30s 
  10. Reason:             NodeLost 
  11. Message:            Node k8s-node which was running pod nginx-controller-lv8md is unresponsive 
  12. IP:             172.21.42.3 
  13. Controllers:            ReplicationController/nginx-controller 
  14. Containers: 
  15.   nginx: 
  16.     Container ID:       docker://03fa59f9efc06e43ed8c9acc7d4c7533983d5733223dbb2efa5f65928d965b5b 
  17.     Image:          reg.docker.lc/share/nginx:latest 
  18.     Image ID:           docker-pullable://reg.docker.lc/share/nginx@sha256:e5c82328a509aeb7c18c1d7fb36633dc638fcf433f651bdcda59c1cc04d3ee55 
  19.     Port:           80/TCP 
  20.     State:          Running 
  21.       Started:          Tue, 13 Jun 2017 02:01:47 +0800 
  22.     Ready:          True 
  23.     Restart Count:      0 
  24.     Volume Mounts:      <none> 
  25.     Environment Variables:  <none> 
  26. Conditions: 
  27.   Type      Status 
  28.   Initialized   True  
  29.   Ready     False  
  30.   PodScheduled  True  
  31. No volumes. 
  32. QoS Class:  BestEffort 
  33. Tolerations:    <none> 
  34. No events. 

 

因为这两个容器的rc,services都已经删除了,但是还保持这个Unknown状态是由于目标主机无法响应并返回信息导致;既然目标主机都已经宕机了,那就直接移除节点;

 
  1. [root@k8s ~]# kubectl delete node k8s-node  
  2. node "k8s-node" deleted 
  3.  
  4. [root@k8s ~]# kubectl get node  
  5.  
  6. NAME      STATUS    AGE 
  7. k8s       Ready     2d 

 

因为节点都不存在了,那就没有了容器的状态信息;

 
  1. [root@k8s ~]# kubectl get pods 
  2.  
  3. No resources found. 

 

上面这样虽然是删除了那些已经也错的实例,但却删除了节点。 那么如果节点现在恢复了,是否会自动加入到集群呢?

主节点上的日志很快就发现了k8s-node节点了;

 

 
  1. Jun 13 15:06:13 k8s kube-controller-manager[34050]: E0613 15:06:13.917313   34050 actual_state_of_world.go:475] Failed to set statusUpdateNeeded to needed true because nodeName="k8s-node"  does not exist 
  2.  
  3. Jun 13 15:06:14 k8s kube-controller-manager[34050]: I0613 15:06:14.618864   34050 event.go:217] Event(api.ObjectReference{Kind:"Node", Namespace:"", Name:"k8s-node", UID:"c9864434-5006-11e7-ab16-000c29e9277a", APIVersion:"", ResourceVersion:"", FieldPath:""}): type: 'Normal' reason: 'RegisteredNode' Node k8s-node event: Registered Node k8s-node in NodeController 

 

节点自动又回来了,因为在配置文件中已经配置好了,只要节点一上线就会加入到集群中。 

 

 
  1. [root@k8s ~]# kubectl get nodes 
  2.  
  3. NAME       STATUS    AGE 
  4. k8s        Ready     2d 
  5. k8s-node   Ready     1m 

 

创建一个Nginx实例:

 
  1. [root@k8s ~]# kubectl create -f Nginx.yaml  
  2.  
  3. replicationcontroller "nginx-controller" created 
  4. service "nginx-service" created 

Kubernetes已经把此实例分配到刚启动的k8s-node节点上了;

 

 
  1. [root@k8s ~]# kubectl get pods 
  2.  
  3. NAME                     READY     STATUS    RESTARTS   AGE 
  4. nginx-controller-zsx2q   1/1       Running   0          6s 
  5.  
  6.  
  7.  
  8. [root@k8s ~]# kubectl describe pod nginx-controller-zsx2q |grep "Node" 
  9. Node:       k8s-node/10.0.10.11 

 

动态扩展,添加一个容器数量;

 
  1. [root@k8s ~]# kubectl scale replicationcontroller --replicas=2 nginx-controller  
  2.  
  3. replicationcontroller "nginx-controller" scaled 

 

查看集群现在的状态信息;

 
  1. [root@k8s ~]# kubectl describe svc nginx-service  
  2.  
  3. Name:           nginx-service 
  4. Namespace:      default 
  5. Labels:         <none> 
  6. Selector:       app=nginx 
  7. Type:           ClusterIP 
  8. IP:         10.254.132.82 
  9. External IPs:       10.0.10.10 
  10. Port:           <unset> 8000/TCP 
  11. Endpoints:      172.21.42.2:80,172.21.93.2:80 
  12. Session Affinity:   None 
  13. No events. 

 

此时再删除k8s-node节点又会如何呢?

 
  1. [root@k8s ~]# kubectl delete node k8s-node  
  2. node "k8s-node" deleted 

已经删除了一个节点了:

 

 
  1. [root@k8s ~]# kubectl get nodes 
  2.  
  3. NAME      STATUS    AGE 
  4. k8s       Ready     2d 

 

Pod的数量没有减少还是两个:

 
  1. [root@k8s ~]# kubectl get pods 
  2.  
  3. NAME                     READY     STATUS    RESTARTS   AGE 
  4.  
  5. nginx-controller-43qpx   1/1       Running   0          14m 
  6. nginx-controller-zsx2q   1/1       Running   0          19m 

 

查看集群中的分配信息时发现,两个容器的Ip段都是172.21.93的说明来自同一节点;

 

 
  1. [root@k8s ~]# kubectl describe svc nginx-service  
  2.  
  3. Name:           nginx-service 
  4. Namespace:      default 
  5. Labels:         <none> 
  6. Selector:       app=nginx 
  7. Type:           ClusterIP 
  8. IP:         10.254.132.82 
  9. External IPs:       10.0.10.10 
  10. Port:           <unset> 8000/TCP 
  11. Endpoints:      172.21.93.2:80,172.21.93.3:80 
  12. Session Affinity:   None 
  13. No events. 

 

重新加入k8s-node 节点,需要在k8s-node上重启一下服务:

 
  1. systemctl restart kubelet 

--------------------- 本文来自 yao不ke及 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/qq_19674905/article/details/80887461?utm_source=copy

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值