kubernetes之job,taints与tolerations

本文介绍了Kubernetes中的Job,包括不同类型的Job及其参数配置,如 `.spec.completions` 和 `.spec.Parallelism`。着重阐述了`.spec.restartPolicy` 和 `.spec.backoffLimit` 的作用。此外,还详细讲解了taints和tolerations的概念,如何通过设置taints来控制Pod的调度,并展示了如何在Pod定义中配置tolerations以允许调度到有taints的节点上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Job

使用对象:常用于运行那些仅需要执行一次的任务(数据库迁移、批处理脚本、kube-bench扫描等)
种类:

  • 非并行job:通常创建一个Pod直至其成功结果

  • 固定次数的job:通过设置.sepc.completions(需要成功运行pod的数量),创建多个pod,直到.sepc.completions个pod运行成功

  • 带有工作队列的并行Job:设置.spec.Parallelism但不设置.sepc.completions,当所有Pod结束并且至少一个成功时,Job就认为是成功的,根据.spec.completions和.spec.Parallelism的设置,可以将Job划分为以下几种pattern:
    在这里插入图片描述

参数解释
.spec.template.spec.restartPolicy该属性拥有三个候选值:OnFailure,Never和Always。默认值为Always。它主要用于描述Pod内容器的重启策略。在Job中只能将此属性设置为OnFailure或Never。

.spec.backoffLimit用于设置job的容错次数,默认值为6。默认情况下,除非Pod失败(restartPolicy=Never)或容器错误退出(restartPolicy=OnFailure),否则Job将不间断运行,此时Job遵循 .spec.backoffLimit上述说明。一旦.spec.backoffLimit达到,作业将被标记为失败,并且所有正在运行的Pod将被终止。

.spec.activeDeadlineSeconds作业字段设置秒数,一旦工作到达activeDeadlineSeconds,所有运行的Pod的终止和工作状态将成为type: Failed与reason: DeadlineExceeded。 .spec.backoffLimit与.spec.activeDeadlineSeconds都可以用作pod的终止

使用示例

apiVersion: batch/v1
kind: Job
metadata:
  name: kube-bench-master
spec:
  parallelism: 3
  completions: 3
  template:
    spec:
      hostPID: true
      nodeSelector:
        node-role.kubernetes.io/master: ""
      tolerations:
        - key: node-role.kubernetes.io/master
          operator: Exists
          effect: NoSchedule
      containers:
        - name: kube-bench
          image: harbor.in.fii-icloud.com/library/aquasec/kube-bench:latest
          command: ["kube-bench", "master"]
          volumeMounts:
            - name: var-lib-etcd
              mountPath: /var/lib/etcd
              readOnly: true
            - name: etc-kubernetes
              mountPath: /etc/kubernetes
              readOnly: true
              # /usr/local/mount-from-host/bin is mounted to access kubectl / kubelet, for auto-detecting the Kubernetes version.
              # You can omit this mount if you specify --version as part of the command.
            - name: usr-bin
              mountPath: /usr/local/mount-from-host/bin
              readOnly: true
      restartPolicy: Never
      volumes:
        - name: var-lib-etcd
          hostPath:
            path: "/var/lib/etcd"
        - name: etc-kubernetes
          hostPath:
            path: "/etc/kubernetes"
        - name: usr-bin
          hostPath:
            path: "/usr/bin"

taints与tolerations

tains污点,tolerations容忍。给某个node设置taints,node与pod之间就存在一个互斥关系。可以让node拒绝Pod的调度执行。taints组成如下:

key=value:effect

命令行使用示例

#设置taints
kubectl taint nodes node1 key=value:Noschedule
#去除taints
kubectl taint nodes node1 key=value:Noschedult-

effect支持如下选项:

  • Noschedule:表示k8s将不会将Pod调度到具有taints的node上

  • PreferNoschedule:表示k8s将尽量避免将Pod调度到该node上

  • NoExecute:表示k8s将不会将Pod调度到具有该污点的Node上,同时会将Node上已经存在的Pod驱逐出去

Tolerations:设置了taints后node不会被调度,但是可以设置tolerations容忍。在yaml文件中进行设置。示例如下

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    env: test
spec:
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
  tolerations:
  - key: "example-key"
    operator: "Exists"
    effect: "NoSchedule"

A toleration “matches” a taint if the keys are the same and the effects are the same, and:

  • the operator is Exists (in which case no value should be specified),
    or
  • the operator is Equal and the values are equal

两种特殊情况

#不指定key值时,将容忍所有的taints-key(key代表什么?)
tolerations
- operator: "Exists"  

#不指定effect值时,容忍所有的taints作用(effect的作用)
tolerations:
- key: "key"
  operator: "Exists"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值