调度管理


2021年9月24日 15:23     admin

限制注意:

1、同时指定nodeSelector 和 nodeAffinity,pod必须都满足

2、nodeAffinity有多个nodeSelectorTerms ,pod只需满足一个

3、nodeSelectorTerms多个matchExpressions ,pod必须都满足

4、由于IgnoredDuringExecution,所以改变labels不会影响已经运行pod


节点调度

方式一:直接指定

  1. apiVersion: v1
  2. kind: pod
  3. metadata:
  4. name: examplepod
  5. spec:
  6. containers:
  7. - name: examplepod-container
  8. image: busybox
  9. imagePullPolicy: IfNotPresent
  10. command: ['sh','-c]
  11. args: ['echo ""hello world";sleep 36000']
  12. nodeName: work01 #直接指定pod调度到work01节点上

方式二:节点标签

先给节点打标签

  1. kubectl label nodes work01 app=nginx

创建pod的yaml文件

  1. apiVersion: v1
  2. kind: pod
  3. metadata:
  4. name: examplepod
  5. spec:
  6. containers:
  7. - name: examplepod-container
  8. image: busybox
  9. imagePullPolicy: IfNotPresent
  10. command: ['sh','-c]
  11. args: ['echo ""hello world";sleep 36000']
  12. nodeSelector:
  13. app: nginx #调度到标签为app:nginx的节点上

亲和性调度

节点亲和性(基于标签指定法)

先给节点打标签

  1. kubectl label nodes work01 app=nginx,relase=state,version=1.12

创建pod的yaml文件

  1. apiVersion: v1
  2. kind: pod
  3. metadata:
  4. name: examplepod
  5. spec:
  6. containers:
  7. - name: examplepod-container
  8. image: busybox
  9. imagePullPolicy: IfNotPresent
  10. command: ['sh','-c]
  11. args: ['echo ""hello world";sleep 36000']
  12. affinity:
  13. nodeAffinity:
  14. requiredDuringSchedulingIgnoredDuringExecution:
  15. nodeSelectorTerms:
  16. - matchExpressions:
  17. - {key: app, operator: In, values: [nginx,web]}
  18. - {key: relase, operator:NotIn, values:[test]}
  19. preferredDuringSchedulingIgnoredDuringExecution:
  20. - weight: 1
  21. preference:
  22. matchExpressions:
  23. - {key:version operator: In, values:[1.12,1.13]}

注意:

1、requiredDuringScheduingIgnoredDuringExecution与requiredDuringScheduingRequiredDuringExecution的区别在于,后者Pod调度成功运行后,如果节点标签发生变化而不再满足条件,Pod将会驱逐出节点,而前者不会

2、requiredDuringSchedulingIgnoredDuringExecution为硬限制,preferredDuringSchedulingIgnoredDuringExecution为软限制。


Pod亲和性

注意:Pod的亲和性和反亲和性调度会涉及大量调度运算,会显著减慢在大型集群中的调度,不建议在大于几百哥节点的集群中使用它们


创建pod的yaml文件

  1. apiVersion: v1
  2. kind: pod
  3. metadata:
  4. name: examplepod
  5. spec:
  6. containers:
  7. - name: examplepod-container
  8. image: busybox
  9. imagePullPolicy: IfNotPresent
  10. command: ['sh','-c]
  11. args: ['echo ""hello world";sleep 36000']
  12. affinity:
  13. podAffinity: #亲和
  14. requiredDuringSchedulingIgnoredDuringExecution:
  15. - labelSelector:
  16. matchExpressions:
  17. - {key: app, operator: In, values: [nginx,web]}
  18. - {key: relase, operator:NotIn, values:[test]}
  19. topologyKey:'Kubernetes.io/hostname'
  20. preferredDuringSchedulingIgnoredDuringExecutio:
  21. - weight: 1
  22. podAffinityTerm:
  23. labelSelector:
  24. matchExpressions:
  25. - {key:version operator: In, values:[1.12,1.13]}
  26. topologyKey:'Kubernetes.io/hostname'

以上示例设置:

1、硬亲和条件,寻找app标签在[nginx,web]和relase寻找app标签在不在test中的Pod,硬亲和条件key标签在[1.12,1.13]中。

2、weight字段表示相对于其它软亲和条件的优先级,取值1~100,越大优先级越高

3、topologyKey:’Kubernetes.io/hostname’,表示如果满足亲和性条件,则会将Pod调度到和已有Pod所在节点的ubernetes.io/hostname标签值相同的节点上,对于此示例,也就是说,会将该Pod调度到同一台机器上。


Pod反亲和性

创建pod的yaml文件

  1. apiVersion: v1
  2. kind: pod
  3. metadata:
  4. name: examplepod
  5. spec:
  6. containers:
  7. - name: examplepod-container
  8. image: busybox
  9. imagePullPolicy: IfNotPresent
  10. command: ['sh','-c]
  11. args: ['echo ""hello world";sleep 36000']
  12. affinity:
  13. podAntiAffinity: #反亲和
  14. requiredDuringSchedulingIgnoredDuringExecution:
  15. - labelSelector:
  16. matchExpressions:
  17. - {key: app, operator: In, values: [nginx,web]}
  18. topologyKey:'Kubernetes.io/hostname'
  19. preferredDuringSchedulingIgnoredDuringExecutio:
  20. - weight: 1
  21. podAntiAffinityTerm:
  22. labelSelector:
  23. matchExpressions:
  24. - {key:version operator: In, values:[1.12,1.13]}
  25. topologyKey:'Kubernetes.io/hostname'

污点

添加污点

  1. kubectl taint node <节点名称> <污点名称>=<污点值>:<污点影响>

污点影响:

  1. NoExecute: 不将Pod调度到具备该污点的机器上,如果Pod已经在某台机器上运行,且设置了NoExecute污点,则不能容忍该污点的Pod将会被驱逐。
  2. NoSchedule: 不将Pod调度到具备该污点的机器上,对于已运行的Pod不会驱逐
  3. PreNoSchedule: 不推荐将Pod调度到具备该污点的机器上

删除污点

  1. kubectl taint node <节点名称> <污点名称>-

查看污点

  1. kubectl describe node <节点名称>
  2. #查看taint段内容


示例:开启master调度

  1. #开启(删除污点)
  2. kubectl taint node master node-role.Kubernetes.io/master-
  3. #关闭(添加污点,使Pod不调度到此机上)
  4. kubectl taint node master node-role.Kubernetes.io/master="":NoSchedule

容忍度

注意:让Pod调度到具备污点的机器上,则必须要为Pod设置容忍度,让她能接受这些污点。


  1. apiVersion: v1
  2. kind: pod
  3. metadata:
  4. name: examplepod
  5. spec:
  6. containers:
  7. - name: examplepod-container
  8. image: busybox
  9. imagePullPolicy: IfNotPresent
  10. command: ['sh','-c]
  11. args: ['echo ""hello world";sleep 36000']
  12. tolerations:
  13. - key: "restart"
  14. operator: "Equal"
  15. value: "hourly"
  16. effect: "NoSchedule"
  17. - key: "isMaintain"
  18. operator: "Equal"
  19. value: "true"
  20. effect: "NoExecute"
  21. tolerationSeconds: 3600

tolerations设置了两个容忍度:

1、第一个容忍度表示可以容忍restart等于hourly且影响为NoSchedule的污点

2、第二个容忍度表示可以容忍isMaintain等于true且影响为NoExecute的污点,tolerationSeconds表示可以容忍污点3600s,如果超过这个时间,Pod将会被驱逐


Pod优先级调度

注意:PriorityClass不属于任何命名空间!优先级超过一亿的数字被系统保留,用于指派给系统组件。


查看PriorityClass

  1. kubectl get priorityclass


创建PriorityClass

  1. vim high-priority.yaml
  2. ===========================================================
  3. apiVersion: scheduling.k8sio/v1beta1
  4. kind: PriorityClass
  5. metadata:
  6. name: high-priority
  7. value: 1000000
  8. globalDefault: false
  9. ===========================================================
  10. kubectl apply -f high-priority.yaml

关联PriorityClass

  1. apiVersion: v1
  2. kind: pod
  3. metadata:
  4. name: examplepod
  5. spec:
  6. containers:
  7. - name: examplepod-container
  8. image: busybox
  9. imagePullPolicy: IfNotPresent
  10. command: ['sh','-c]
  11. args: ['echo ""hello world";sleep 36000']
  12. priorityClassName: high-priority #关联优先级

容灾调度(Even Pod Spreading调度规则)(1.16版本及以上)

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx-deployment
  5. labels:
  6. app: nginx
  7. spec:
  8. topologySpreadConstraints: #容灾调度,均衡分布
  9. - maxSkew: 1 #不均衡数
  10. whenUnsatisfiable: DoNotSchedule
  11. topologyKey: topology.kubernetes.io/zone
  12. selector:
  13. matchLabels:
  14. app: nginx
  15. template:
  16. metadata:
  17. labels:
  18. app: nginx
  19. spec:
  20. containers:
  21. - name: nginx
  22. image: nginx:1.12.2
  23. ports:
  24. - containerPort: 80

maxSkew:用于指定Pod在各个Zone上调度时能容忍的最大不均衡数。(值越大,表示能接受的不均衡调度越大,值越小,表示各个ZONE的Pod数量分布越均匀。)


容灾部署:

1、将一个应用中需要部署在一起的几个Pod用亲和性调度声明捆绑。

2、选择其中一个Pod,加持Even Pod Spreading调度规则。