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"