Skip to content
0

Kubernetes - 高级调度

CronJob 计划任务

在 k8s 中周期性运行计划任务,与 linux 中的 crontab 相同

注意点:CronJob 执行的时间是 controller-manager 的时间,所以一定要确保 controller-manager 时间是准确的,另外 cronjob

cron 表达式

sh
# ┌───────────── 分钟 (0 - 59)
# │ ┌───────────── 小时 (0 - 23)
# │ │ ┌───────────── 月的某天 (1 - 31)
# │ │ │ ┌───────────── 月份 (1 - 12)
# │ │ │ │ ┌───────────── 周的某天 (0 - 6)(周日到周一;在某些系统上,7 也是星期日)
# │ │ │ │ │                          或者是 sun,mon,tue,web,thu,fri,sat
# │ │ │ │ │
# │ │ │ │ │
# * * * * *

配置文件:

yml
apiVersion: batch/v1
kind: CronJob # 定时任务
metadata:
  name: hello
spec:
  concurrencyPolicy: Allow # 并发调度策略:Allow 允许并发调度,Forbid:不允许并发执行,Replace:如果之前的任务还没执行完,就直接执行新的,放弃上一个任务
  failedJobsHistoryLimit: 1 # 保留多少个失败的任务
  successfulJobsHistoryLimit: 3 # 保留多少个成功的任务
  suspend: false # 是否挂起任务,若为 true 则该任务不会执行
  #  startingDeadlineSeconds: 30 # 间隔多长时间检测失败的任务并重新执行,时间不能小于 10
  schedule: "* * * * *" # 调度策略,这里每分钟执行一次
  jobTemplate:
    spec:
      template:
        spec:
          containers:
            - name: hello
              image: busybox:1.28
              imagePullPolicy: IfNotPresent
              command:
                - /bin/sh
                - -c
                - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure

初始化容器 InitContainer

在真正的容器启动之前,先启动 InitContainer,在初始化容器中完成真实容器所需的初始化操作,完成后再启动真实的容器。

相对于 postStart 来说,首先 InitController 能够保证一定在 EntryPoint 之前执行,而 postStart 不能,其次 postStart 更适合去执行一些命令操作,而 InitController 实际就是一个容器,可以在其他基础容器环境下执行更复杂的初始化功能。

在 pod 创建的模板中配置 initContainers 参数:

yml
spec:
  initContainers: # 与 containers 同级
    - image: nginx
      imagePullPolicy: IfNotPresent
      command: ["sh", "-c", "echo 'inited;' >> ~/.init"]
      name: init-test

污点和容忍

k8s 集群中可能管理着非常庞大的服务器,这些服务器可能是各种各样不同类型的,比如机房、地理位置、配置等,有些是计算型节点,有些是存储型节点,此时我们希望能更好的将 pod 调度到与之需求更匹配的节点上。

此时就需要用到污点(Taint)和容忍(Toleration),这些配置都是 key: value 类型的。

污点(Taint)

污点:是标注在节点上的,当我们在一个节点上打上污点以后,k8s 会认为尽量不要将 pod 调度到该节点上,除非该 pod 上面表示可以容忍该污点,且一个节点可以打多个污点,此时则需要 pod 容忍所有污点才会被调度该节点。

sh
# 为节点打上污点
kubectl taint node k8s-master key=value:NoSchedule

# 移除污点
kubectl taint node k8s-master key=value:NoSchedule-

# 查看污点
kubectl describe no k8s-master

污点的影响:

  • NoSchedule:不能容忍的 pod 不能被调度到该节点,但是已经存在的节点不会被驱逐
  • NoExecute:不能容忍的节点会被立即清除,能容忍且没有配置 tolerationSeconds 属性,则可以一直运行,设置了 tolerationSeconds: 3600 属性,则该 pod 还能继续在该容忍的节点运行 3600 秒

NoSchedule:如果不能容忍该污点,那么 Pod 就无法调度到该节点上。

NoExecute:

  • 如果 Pod 不能忍受这类污点,Pod 会马上被驱逐。
  • 如果 Pod 能够忍受这类污点,但是在容忍度定义中没有指定 tolerationSeconds,则 Pod 还会一直在这个节点上运行。
  • 如果 Pod 能够忍受这类污点,而且指定了 tolerationSeconds, 则 Pod 还能在这个节点上继续运行这个指定的时间长度。

容忍(Toleration)

容忍:是标注在 pod 上的,当 pod 被调度时,如果没有配置容忍,则该 pod 不会被调度到有污点的节点上,只有该 pod 上标注了满足某个节点的所有污点,则会被调度到这些节点(注意是立即,原本在其他节点的 Node 打上容忍后,就会在其他节点删除,然后在允许容忍的节点创建 Pod)。

yml
# pod 的 spec 下面配置容忍
tolerations: # 与 containers 同级
  - key: "污点的 key"
    value: "污点的 value"
    offect: "NoSchedule" # 污点产生的影响
    operator: "Equal" # 表名 value 与污点的 value 要相等,也可以设置为 Exists 表示存在 key 即可,此时可以不用配置 value
    # tolerationSeconds: 3600 # 设置 offect: "NoExecute" 会用到

Equal:比较操作类型为 Equal,则意味着必须与污点值做匹配,key/value 都必须相同,才表示能够容忍该污点。

Exists:容忍与污点的比较只比较 key,不比较 value,不关心 value 是什么东西,只要 key 存在,就表示可以容忍。

亲和力(Affinity)

NodeAffinity

节点亲和力:进行 Pod 调度时,优先调度到符合条件的亲和力节点上。

  • RequiredDuringSchedulingIgnoredDuringExecution:硬亲和力,即支持必须部署在指定的节点上,也支持必须不部署在指定的节点上
  • PreferredDuringSchedulingIgnoredDuringExecution:软亲和力:尽量部署在满足条件的节点上,或尽量不要部署在被匹配的节点上

应用:

匹配类型:

  • In:部署在满足条件的节点上
  • NotIn:匹配不在条件中的节点,实现节点反亲和性
  • Exists:只要存在 key 名字就可以,不关心值是什么
  • DoesNotExist:匹配指定 key 名不存在的节点,实现节点反亲和性
  • Gt:value 为数值,且节点上的值小于指定的条件
  • Lt:value 为数值,且节点上的值大于指定条件

配置模板:

yml
apiVersion: v1
kind: Pod
metadata:
  name: with-node-affinity
spec:
  affinity: # 亲和力配置
    nodeAffinity: # 节点亲和力
      requiredDuringSchedulingIgnoredDuringExecution: # 节点必须匹配下方配置
        nodeSelectorTerms: # 选择器
          - matchExpressions: # 匹配表达式
              - key: topology.kubernetes.io/zone # 匹配 label 的 key
                operator: In # 匹配方式,只要匹配成功下方的一个 value 即可
                values:
                  - antarctica-east1 # 匹配的 value
                  - antarctica-west1 # 匹配的 value
      preferredDuringSchedulingIgnoredDuringExecution: # 节点尽量匹配下方配置
        - weight: 1 # 权重[1,100],按照匹配规则对所有节点累加权重,最终之和会加入优先级评分,优先级越高被调度的可能性越高
          preference:
            matchExpressions: # 匹配表达式
              - key: another-node-label-key # label 的 key
                operator: In # 匹配方式,满足一个即可
                values:
                  - another-node-label-value # 匹配的 value
  #      - weight: 20
  # ......
  containers:
    - name: with-node-affinity
      image: pause:2.0

PodAffinity & PodAntiAffinity

PodAffinity

Pod 亲和力:将与指定 Pod 亲和力相匹配的 Pod 部署在同一节点。

RequiredDuringSchedulingIgnoredDuringExecution:必须将应用部署在一块

PreferredDuringSchedulingIgnoredDuringExecution:尽量将应用部署在一块

PodAntiAffinity

Pod 反亲和力:根据策略尽量部署或不部署到一块。

RequiredDuringSchedulingIgnoredDuringExecution:不会将应用部署在一块

PreferredDuringSchedulingIgnoredDuringExecution:尽量不要将应用部署到一块

配置模板

yml
apiVersion: v1
kind: Pod
metadata:
  name: with-pod-affinity
spec:
  affinity: # 亲和力配置
    podAffinity: # pod 亲和力配置
      requiredDuringSchedulingIgnoredDuringExecution: # 当前 pod 必须匹配到对应条件 pod 所在的 node 上
        - labelSelector: # 标签选择器
            matchExpressions: # 匹配表达式
              - key: security # 匹配的 key
                operator: In # 匹配方式
                values: # 匹配其中的一个 value
                  - S1
          topologyKey: topology.kubernetes.io/zone # Node 节点必须有这个标签
    podAntiAffinity: # pod 反亲和力配置
      preferredDuringSchedulingIgnoredDuringExecution: # 尽量不要将当前节点部署到匹配下列参数的 pod 所在的 node 上
        - weight: 100 # 权重
          podAffinityTerm: # pod 亲和力配置条件
            labelSelector: # 标签选择器
              matchExpressions: # 匹配表达式
                - key: security # 匹配的 key
                  operator: In # 匹配的方式
                  values:
                    - S2 # 匹配的 value
            topologyKey: topology.kubernetes.io/zone
  containers:
    - name: with-pod-affinity
      image: pause:2.0
最近更新