当前位置:  首页>> 技术小册>> Kubernets合辑5-Pod控制器

Node节点上污点管理

  1. 用法:
  2. 增加污点: kubectl taint node <node_name> key=value:effect
  3. 取消污点: kubectl taint node <node_name> key=value:effect-
  4. 查看污点: kubectl describe node <node_name>
  5. effect:
  6. PreferNoSchedule: 优先不调度,但是其它节点不满足时可以调度
  7. NoSchedule: 禁止新的Pod调度,已经调度的Pod不会被驱逐
  8. NoExecute: 禁止新的Pod调度,并且已经运行在该节点时的,其不能容忍污点的Pod将被驱逐
  1. # 查看master的污点
  2. [root@maxiaoke local-k8s-yaml]# kubectl describe node centos-7-51
  3. Name: centos-7-51
  4. Roles: master
  5. Labels: beta.kubernetes.io/arch=amd64
  6. beta.kubernetes.io/os=linux
  7. kubernetes.io/arch=amd64
  8. kubernetes.io/hostname=centos-7-51
  9. kubernetes.io/os=linux
  10. node-role.kubernetes.io/master=
  11. Annotations: flannel.alpha.coreos.com/backend-data: {"VNI":1,"VtepMAC":"52:8a:0e:48:b4:92"}
  12. flannel.alpha.coreos.com/backend-type: vxlan
  13. flannel.alpha.coreos.com/kube-subnet-manager: true
  14. flannel.alpha.coreos.com/public-ip: 10.4.7.51
  15. kubeadm.alpha.kubernetes.io/cri-socket: /var/run/dockershim.sock
  16. node.alpha.kubernetes.io/ttl: 0
  17. volumes.kubernetes.io/controller-managed-attach-detach: true
  18. CreationTimestamp: Fri, 04 Dec 2020 21:49:43 +0800
  19. Taints: node-role.kubernetes.io/master:NoSchedule # 不可调度
  20. ......
  1. # 以此作为示例Pod
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: nginx-deploy
  6. spec:
  7. replicas: 6
  8. selector:
  9. matchLabels:
  10. app: nginx
  11. template:
  12. metadata:
  13. labels:
  14. app: nginx
  15. spec:
  16. containers:
  17. - name: nginx-demo
  18. image: linuxmaxiaoke/nginx:v1.0.0
  1. # kubectl apply 部署上述的deployment后,Pod分散在三个不同的node上
  2. [root@maxiaoke local-k8s-yaml]# kubectl get pod -o wide
  3. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  4. nginx-deploy-8697d45cb8-4x564 1/1 Running 0 4m9s 172.16.4.9 centos-7-55 <none> <none>
  5. nginx-deploy-8697d45cb8-bxms4 1/1 Running 0 4m9s 172.16.5.55 centos-7-56 <none> <none>
  6. nginx-deploy-8697d45cb8-c4rbf 1/1 Running 0 4m9s 172.16.3.171 centos-7-54 <none> <none>
  7. nginx-deploy-8697d45cb8-hvs92 1/1 Running 0 4m9s 172.16.3.172 centos-7-54 <none> <none>
  8. nginx-deploy-8697d45cb8-sbfvj 1/1 Running 0 4m9s 172.16.5.54 centos-7-56 <none> <none>
  9. nginx-deploy-8697d45cb8-sw5m4 1/1 Running 0 4m9s 172.16.4.10 centos-7-55 <none> <none>
  1. # 使用NoSchedule污点,发现并不会使得现有的Pod发生重调度
  2. [root@maxiaoke local-k8s-yaml]# kubectl taint node centos-7-54 monitor=true:NoSchedule
  3. node/centos-7-54 tainted
  4. [root@maxiaoke local-k8s-yaml]# kubectl get pod -o wide
  5. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  6. nginx-deploy-8697d45cb8-4x564 1/1 Running 0 5m18s 172.16.4.9 centos-7-55 <none> <none>
  7. nginx-deploy-8697d45cb8-bxms4 1/1 Running 0 5m18s 172.16.5.55 centos-7-56 <none> <none>
  8. nginx-deploy-8697d45cb8-c4rbf 1/1 Running 0 5m18s 172.16.3.171 centos-7-54 <none> <none>
  9. nginx-deploy-8697d45cb8-hvs92 1/1 Running 0 5m18s 172.16.3.172 centos-7-54 <none> <none>
  10. nginx-deploy-8697d45cb8-sbfvj 1/1 Running 0 5m18s 172.16.5.54 centos-7-56 <none> <none>
  11. nginx-deploy-8697d45cb8-sw5m4 1/1 Running 0 5m18s 172.16.4.10 centos-7-55 <none> <none>
  12. # 更新deployment后,新的Pod不再调到 NoSchedule 节点
  13. root@maxiaoke local-k8s-yaml]# kubectl set image deployment nginx-deploy nginx-demo=linuxmaxiaoke/nginx:v1.0.1
  14. deployment.apps/nginx-deploy image updated
  15. [root@maxiaoke local-k8s-yaml]# kubectl get pod -o wide
  16. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  17. nginx-deploy-8494c5b6c5-5ddd9 1/1 Running 0 8s 172.16.4.13 centos-7-55 <none> <none>
  18. nginx-deploy-8494c5b6c5-5zdjg 1/1 Running 0 10s 172.16.5.56 centos-7-56 <none> <none>
  19. nginx-deploy-8494c5b6c5-bqpgn 1/1 Running 0 10s 172.16.5.57 centos-7-56 <none> <none>
  20. nginx-deploy-8494c5b6c5-dq44w 1/1 Running 0 9s 172.16.4.12 centos-7-55 <none> <none>
  21. nginx-deploy-8494c5b6c5-pnvw6 1/1 Running 0 8s 172.16.5.58 centos-7-56 <none> <none>
  22. nginx-deploy-8494c5b6c5-xnf77 1/1 Running 0 10s 172.16.4.11 centos-7-55 <none> <none>
  1. # 设置NoExecute后,现有Pod会被驱逐
  2. [root@maxiaoke local-k8s-yaml]# kubectl taint node centos-7-55 monitor=true:NoExecute
  3. node/centos-7-55 tainted
  4. [root@maxiaoke local-k8s-yaml]# kubectl get pod -o wide
  5. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  6. nginx-deploy-8494c5b6c5-5zdjg 1/1 Running 0 3m8s 172.16.5.56 centos-7-56 <none> <none>
  7. nginx-deploy-8494c5b6c5-bqpgn 1/1 Running 0 3m8s 172.16.5.57 centos-7-56 <none> <none>
  8. nginx-deploy-8494c5b6c5-bzc2c 1/1 Running 0 14s 172.16.5.60 centos-7-56 <none> <none>
  9. nginx-deploy-8494c5b6c5-f7k2b 1/1 Running 0 14s 172.16.5.62 centos-7-56 <none> <none>
  10. nginx-deploy-8494c5b6c5-pnvw6 1/1 Running 0 3m6s 172.16.5.58 centos-7-56 <none> <none>
  11. nginx-deploy-8494c5b6c5-s57tv 1/1 Running 0 14s 172.16.5.61 centos-7-56 <none> <none>

Pod的污点容忍度

  1. # api-server 能容忍所有NoExecute的污点,因此能在Master上运行
  2. [root@maxiaoke local-k8s-yaml]# kubectl describe pod -n kube-system kube-apiserver-centos-7-51
  3. ......
  4. Tolerations: :NoExecute
  1. # 清除所有节点的污点后,执行以下操作。模拟Prometheus(Prometheus占用内存巨大,推荐单独部署到一个固有节点)的部署
  2. [root@maxiaoke local-k8s-yaml]# kubectl label node centos-7-56 prometheus=true
  3. node/centos-7-56 labeled
  4. [root@maxiaoke local-k8s-yaml]# kubectl taint node centos-7-56 monitor=true:NoSchedule
  5. node/centos-7-56 tainted
  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: prometheus
  5. spec:
  6. replicas: 1
  7. selector:
  8. matchLabels:
  9. app: prometheus
  10. template:
  11. metadata:
  12. labels:
  13. app: prometheus
  14. spec:
  15. containers:
  16. - name: prometheus-demo
  17. image: linuxmaxiaoke/nginx:v1.0.1
  18. nodeSelector:
  19. prometheus: "true"
  20. tolerations:
  21. - key: monitor
  22. operator: Exists
  23. effect: NoSchedule
  1. # 通过节点选择器和污点容忍度,实现独占一个节点
  2. [root@maxiaoke local-k8s-yaml]# kubectl get pod -o wide
  3. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  4. prometheus-76f64854b7-sxdq9 1/1 Running 0 2m21s 172.16.5.71 centos-7-56 <none> <none>

该分类下的相关小册推荐: