七、k8s之集群调度

原创
2020/10/10 09:15
阅读数 819

调度过程说明

简介

scheduler:公平、资源高效利用、效率、灵活

调度过程

Detail:

自定义调度器

集群调度

查看pod标签:kubectl get pod --show-labels

节点亲和性

pod.spec.nodeAffinity:

  • preferredDuringSchedulingIgnoreDuringExecution:软策略
  • requiredDuringSchedulingIgnoreDuringExecution:硬策略

eg:

requiredDuringSchedulingIgnoreDuringExecution
apiVersion: v1
kind: Pod
metadata:
  name: affinity
  labels:
    app: node-affinity-pod
  spec:
    containers:
      - name: with-node-affinity
        image: hub.adaixuezhang.cn/library/myapp:v1
    affinity:
      nodeAffinity:
        requireDuringSchedulingIgnoreDuringExecution:
          nodeSelectorTerms:
            - matchExpressions:
                - key: kubernetes.io/hostname
                  operator: NotIn
                  values:
                    - k8s-node02
preferredDuringSchedulingIgnoreDuringExecution
apiVersion: v1
kind: Pod
metadata:
  name: affinity
  labels:
    app: node-affinity-pod
  spec:
    containers:
      - name: with-node-affinity
        image: hub.adaixuezhang.cn/library/myapp:v1
    affinity:
      nodeAffinity:
        preferredDuringSchedulingIgnoreDuringExecution:
          - weight: 1
            preference:
              matchExpressions:
                - key: kubernetes.io/hostname
                  operator: In
                  values:
                    - k8s-node03
合体
apiVersion: v1
kind: Pod
metadata:
  name: affinity
  labels:
    app: node-affinity-pod
  spec:
    containers:
      - name: with-node-affinity
        image: hub.adaixuezhang.cn/library/myapp:v1
    affinity:
      nodeAffinity:
        requireDuringSchedulingIgnoreDuringExecution:
          nodeSelectorTerms:
            - matchExpressions:
                - key: kubernetes.io/hostname
                  operator: NotIn
                  values:
                    - k8s-node03
        preferredDuringSchedulingIgnoreDuringExecution:
          - weight: 1
            preference:
              matchExpressions:
                - key: kubernetes.io/hostname
                  operator: In
                  values:
                    - k8s-node01
key:values 运算关系
  • In:label的值在某个列表中
  • NotIn:label的值不在某个列表中
  • Gt:label的值大于某个值
  • Lt:label的值小于某个值
  • Exists:某个label存在
  • DoseNotExist:某个label不存在

如果 nodeSelectorTerms 下面有多个选项的话,满足任何一个条件就可以了;如果 matchExpressions 有多个选项的话,则必须同时满足这些条件才能正常调度 POD。

Pod亲和性

pod.spec.affinity.podAffinity/podAntiAffinity:

  • preferredDuringSchedulingIgnoreDuringExecution:软策略
  • requiredDuringSchedulingIgnoreDuringExecution:硬策略

eg:

apiVersion: v1
kind: Pod
metadata:
  name: pod-3
  labels:
    app: pod-3
  spec:
    containers:
      - name: pod-3
        image: hub.adaixuezhang.cn/library/myapp:v1
    affinity:
      podAffinity:
        requireDuringSchedulingIgnoreDuringExecution:
          - labelSelector:
              - matchExpressions:
                  - key: app
                    operator: In
                    values:
                      - pod-1
            topologyKey: kubernetes.io/hostname
      podAntiAffinity:
        preferredDuringSchedulingIgnoreDuringExecution:
          - weight: 1
            podAffinityTerm:
              labelSelector:
                - matchExpressions:
                    - key: app
                      operator: In
                      values:
                        - pod-2
              topologyKey: kubernetes.io/hostname

亲和性/反亲和性调度策略比较

污点(Taint)和容忍(Toleration)

污点

1. 污点的组成

2. 污点的设置、查看和去除

# 设置污点
$ kubectl taint nodes node1 key1=value1:NoSchedule

# 节点说明中,查找Taints字段
$ kubectl describe pod pod-name

# 去除污点
$ kubectl taint nodes node2 key1:Noschedule-

容忍

pod.spec.tolerations:

tolerations:
  - key: "key1"
    operator: "Equal"
    value: "value1"
    effect: "NoSchedule"
    tolerationSeconds: 3600
  - key: "key1"
    operator: "Equal"
    value: "value1"
    effect: "NoExecute"
  - key: "key2"
    operator: "Exists"
    effect: "NoSchedule"
  • 其中,key、value、effect要与Node上设置的taint保持一致
  • operator的值为Exists将会忽略value值
  • tolerationSeconds用于描述当Pod需要被驱逐时可以在Pod上继续保留运行的时间

1. 当不指定key值时,表示容忍所有的污点key:

tolerations:
  - operator: "Exists"

2. 当不指定effect值时,表示容忍所有的污点作用

tolerations:
  - key: "key"
    operator: "Exists"

3. 有多个Master存在时,防止资源浪费,可以加如下设置

$ kubectl taint nodes Node-Name node-role.kubernetes.io/master=:PreferNoSchedule

指定调度节点

eg:

展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部