1. 程式人生 > 其它 >k8s下線node節點

k8s下線node節點

有時候我們想更新一些伺服器,或者維護/更新一些映象的時候,需要暫定一部分node節點,正確操作步驟如下:

1.獲取節點列表

root@k8s-master1:~# kubectl get nodes -o wide
NAME          STATUS   ROLES                  AGE    VERSION   INTERNAL-IP       EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION       CONTAINER-RUNTIME
k8s-master1   Ready    control-plane,master   103d   v1.22.0   192.168.255.100   <none>        Ubuntu 18.04.5 LTS   4.15.0-154-generic   docker://20.10.8
k8s-master2   Ready    control-plane,master   103d   v1.22.0   192.168.255.102   <none>        Ubuntu 18.04.5 LTS   4.15.0-154-generic   docker://20.10.8
k8s-node1     Ready    <none>                 103d   v1.22.0   192.168.255.103   <none>        Ubuntu 18.04.5 LTS   4.15.0-154-generic   docker://20.10.8
k8s-node2     Ready    <none>                 103d   v1.22.0   192.168.255.104   <none>        Ubuntu 18.04.5 LTS   4.15.0-154-generic   docker://20.10.8

2.設定不可排程

root@k8s-master1:~# kubectl cordon k8s-node2 
node/k8s-node2 cordoned
root@k8s-master1:~# kubectl get nodes -o wide
NAME          STATUS                     ROLES                  AGE    VERSION   INTERNAL-IP       EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION       CONTAINER-RUNTIME
k8s-master1   Ready                      control-plane,master   103d   v1.22.0   192.168.255.100   <none>        Ubuntu 18.04.5 LTS   4.15.0-154-generic   docker://20.10.8
k8s-master2   Ready                      control-plane,master   103d   v1.22.0   192.168.255.102   <none>        Ubuntu 18.04.5 LTS   4.15.0-154-generic   docker://20.10.8
k8s-node1     Ready                      <none>                 103d   v1.22.0   192.168.255.103   <none>        Ubuntu 18.04.5 LTS   4.15.0-154-generic   docker://20.10.8
k8s-node2     Ready,SchedulingDisabled   <none>                 103d   v1.22.0   192.168.255.104   <none>        Ubuntu 18.04.5 LTS   4.15.0-154-generic   docker://20.10.8
root@k8s-master1:~#

可以看到k8s-node2節點已經被暫停排程(SchedulingDisabled)

3.驅逐節點上執行的Pod

如果需要驅逐節點上的pod 可執行

root@k8s-master1:~# kubectl drain k8s-node2 
node/k8s-node2 already cordoned
DEPRECATED WARNING: Aborting the drain command in a list of nodes will be deprecated in v1.23.
The new behavior will make the drain command go through all nodes even if one or more nodes failed during the drain.
For now, users can try such experience via: --ignore-errors
error: unable to drain node "k8s-node2", aborting command...

There are pending nodes to be drained:
 k8s-node2
error: cannot delete DaemonSet-managed Pods (use --ignore-daemonsets to ignore): kube-system/kube-flannel-ds-wdwzs, kube-system/kube-proxy-dk4vn
root@k8s-master1:~#

注意:若node節點上存在daemonsets控制器建立的pod,則需要使用--ignore-daemonsets忽略錯誤錯誤警告

root@k8s-master1:~# kubectl drain k8s-node2 --ignore-daemonsets
node/k8s-node2 already cordoned
WARNING: ignoring DaemonSet-managed Pods: kube-system/kube-flannel-ds-wdwzs, kube-system/kube-proxy-dk4vn
node/k8s-node2 drained
root@k8s-master1:~# 

檢視pod,可以看到已經沒有pod執行在k8s-node2節點

root@k8s-master1:~# kubectl get pod -o wide
NAME                             READY   STATUS    RESTARTS       AGE   IP            NODE        NOMINATED NODE   READINESS GATES
php-fpm-nginx-5d8b7fd989-59ljf   2/2     Running   0              91m   10.10.3.243   k8s-node1   <none>           <none>
php-fpm-nginx-5d8b7fd989-kfmln   2/2     Running   22 (22h ago)   24d   10.10.3.238   k8s-node1   <none>           <none>
redis-svc-866fb777f8-4ps2s       1/1     Running   11 (22h ago)   24d   10.10.3.240   k8s-node1   <none>           <none>
redis-svc-866fb777f8-lflkv       1/1     Running   11 (22h ago)   24d   10.10.3.239   k8s-node1   <none>           <none>
test-www-7fcc4d5595-4mg5j        2/2     Running   0              91m   10.10.3.242   k8s-node1   <none>           <none>
test-www-7fcc4d5595-kcllj        2/2     Running   0              91m   10.10.3.241   k8s-node1   <none>           <none>

4、恢復可排程
當需要重新排程node節點的時候,執行

root@k8s-master1:~# kubectl uncordon k8s-node2
node/k8s-node2 uncordoned

檢視node狀態

root@k8s-master1:~# kubectl get nodes -o wide
NAME          STATUS   ROLES                  AGE    VERSION   INTERNAL-IP       EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION       CONTAINER-RUNTIME
k8s-master1   Ready    control-plane,master   103d   v1.22.0   192.168.255.100   <none>        Ubuntu 18.04.5 LTS   4.15.0-154-generic   docker://20.10.8
k8s-master2   Ready    control-plane,master   103d   v1.22.0   192.168.255.102   <none>        Ubuntu 18.04.5 LTS   4.15.0-154-generic   docker://20.10.8
k8s-node1     Ready    <none>                 103d   v1.22.0   192.168.255.103   <none>        Ubuntu 18.04.5 LTS   4.15.0-154-generic   docker://20.10.8
k8s-node2     Ready    <none>                 103d   v1.22.0   192.168.255.104   <none>        Ubuntu 18.04.5 LTS   4.15.0-154-generic   docker://20.10.8
root@k8s-master1:~#

5、刪除節點
如果需要直接刪除節點(此方式比較暴力,一般情況不推薦使用)
kubectl delete nodes k8s-node2
如果刪除的node想重新加入到k8s叢集中 則需要master重新生成加入token即可