LAB-02:指定节点不可用
LAB 需求
将名为 k8s-node-1 的 node 设置为不可用,并且重新调度该 node 上所有的 pods。
LAB 预配
# 编写一个2副本的 deployments,至少一个 pod 调度到 k8s-node-1上。
user1@k8s-master:~/cka/2$ cat deployment-nginx-pod-nodes.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-pod-nodes
spec:
replicas: 2
selector:
matchLabels:
app: nginx-pod-nodes
template:
metadata:
labels:
app: nginx-pod-nodes
spec:
containers:
- name: nginx
image: nginx:1.18.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
# 部署 deployments 资源
user1@k8s-master:~/cka/2$ kubectl apply -f deployment-nginx-pod-nodes.yaml
deployment.apps/nginx-pod-nodes created
# 查看部署的资源,分别在 k8s-node-1 和 k8s-node-2 上
user1@k8s-master:~/cka/2$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-pod-nodes-859fcdcc9d-65bwd 1/1 Running 0 115s 10.244.2.2 k8s-node-2 <none> <none>
nginx-pod-nodes-859fcdcc9d-8jvdx 1/1 Running 0 115s 10.244.1.2 k8s-node-1 <none> <none>
LAB 答案
# 切换 content
$ kubectl config use-context k8s
# 设置节点是不可调度状态
$ kubectl cordon k8s-node-1
# 重新调度 pods,忽略 daemonsets,删除 local-data,不然无法驱逐
$ kubectl drain k8s-node-1 --delete-local-data --ignore-daemonsets --force
LAB 验证
# 查看 k8s-node-1 为不可以,原来在 k8s-node-1 上的 pod 调度到其他节点
user1@k8s-master:~$ kubectl get node
NAME STATUS ROLES AGE VERSION
k8s-master Ready control-plane,master 21h v1.22.1
k8s-node-1 Ready,SchedulingDisabled <none> 21h v1.22.1
k8s-node-2 Ready <none> 21h v1.22.1
k8s-node-3 Ready <none> 21h v1.22.1
参考资料
-
https://kubernetes.io/zh/docs/concepts/architecture/nodes/
-
https://kubernetes.io/zh/docs/tasks/administer-cluster/safely-drain-node/