Cronjob
apiVersion: batch/v1
kind: CronJob
metadata:
name: hello
spec:
schedule: "*/1 * * * *"
concurrencyPolicy: Allow
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 1
suspend: false
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
# 查看
[root@k8s-master01 pod]# kubectl get cj
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello */1 * * * * False 0 <none> 4s
# 查看job
[root@k8s-master01 pod]# kubectl get jobs
NAME COMPLETIONS DURATION AGE
hello-28362680 1/1 42s 50s
# 查看pod
[root@k8s-master01 pod]# kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-28362680-ns4mg 0/1 Completed 0 91s
[root@k8s-master01 pod]# kubectl logs -f hello-28362680-ns4mg
Tue Dec 5 07:20:37 UTC 2023
Hello from the Kubernetes cluster
关键字说明:
concurrencyPolicy: # 并发调度策略
- Allow 允许同时运行多个任务
- Forbid 不允许并发运行,如果之前的任务尚未完成,新的任务不会被创建
- Replace 如果之前的任务尚未王成,新的任务会替换之间的任务
successfulJobsHistoryLimit: 3 # 保留多少已完成的任务
failedJobsHistoryLimit: 1 # 保留多少失败的任务
suspend: false # 是否暂停
通过将可选的 .spec.suspend 字段设置为 true,可以挂起针对 CronJob 执行的任务。
这个设置不会影响 CronJob 已经开始的任务。
如果你将此字段设置为 true,后续发生的执行都会被挂起 (这些任务仍然在调度中,但 CronJob 控制器不会启动这些 Job 来运行任务),直到你取消挂起 CronJob 为止。
注意:
在调度时间内挂起的执行都会被统计为错过的任务。当现有的 CronJob 将 .spec.suspend 从 true 改为 false 时, 且没有开始的最后期限,错过的任务会被立即调度。
初始化容器Init containers
用途:
在主应用容器启动之前,做一些初始化的操作,比如创建文件、修改内核参数、等待依赖程序启动或其他需要在主程序启动之前需要做的工作
Init容器相关操作执行完成以后即退出,不会给业务容器带来安全隐患。
比如你想在容器中使用wget或者git命令,但是又不想在镜像里封装,就可以使用初始化容器
与普通容器的区别
如果有多个初始化容器,是依次执行的,如果第一个Init容器失败,k8s不断重启该Pod,直到Init容器成功为止,但是Pod对应策略为Never,则不会重启Pod
第一个初始化容器执行完成后 才会执行第二个初始化容器
Init容器不支持探针
# 初始化容器案例
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: dp-cm
name: dp-cm
spec:
replicas: 1
selector:
matchLabels:
app: dp-cm
template:
metadata:
labels:
app: dp-cm
spec:
initContainers: # 初始化容器,和containers同级
- command:
- sh
- -c
- | # 执行多个cmd的格式
touch /mnt/test-init.txt # 初始化在mnt下创建文件
echo init > /mnt/test-init.txt
image: registry.cn-beijing.aliyuncs.com/dotbalo/nginx:1.15.12-alpine
name: init-touch
volumeMounts: #挂载使用mnt
- name: data
mountPath: /mnt
containers:
- image: registry.cn-beijing.aliyuncs.com/dotbalo/nginx:1.15.12-alpine
name: nginx
volumeMounts:
- mountPath: /mnt
name: data
volumes:
- name: data
emptyDir: {}
# pod状态先是init状态还有才是runing
# 查看执行结果
使用describe
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 13m default-scheduler Successfully assigned default/dp-cm-5686c47868-qc69g to k8s-master01
Normal Pulled 13m kubelet Container image "registry.cn-beijing.aliyuncs.com/dotbalo/nginx:1.15.12-alpine" already present on machine
Normal Created 13m kubelet Created container init-touch
Normal Started 13m kubelet Started container init-touch
Normal Pulled 13m kubelet Container image "registry.cn-beijing.aliyuncs.com/dotbalo/nginx:1.15.12-alpine" already present on machine
Normal Created 13m kubelet Created container nginx
Normal Started 13m kubelet Started container nginx
# 容器中验证
[root@k8s-master01 pod]# kubectl exec -it dp-cm-7c457f9594-n7p8s -- cat /mnt/test-init.txt
Defaulted container "nginx" out of: nginx, init-touch (init)
init
临时容器EphemeralContainers
- 1.16版本之后提供的功能
- 使用场景 : 业务容器没有/bin/bash 无法执行命令和排错
为什么临时容器能看到同Pod下的进程?
利用shareProcessNamespace 技术、它是 Kubernetes Pod 的一个属性,用于配置容器是否共享进程命名空间,默认是True,Pod 中的所有容器将共享相同的进程命名空间。这意味着它们能够看到彼此的进程,并能够相互通信。
# 临时容器使用
1.24版本以下需要手动编辑配置文件进行开启
# 尝试进入容器排查,但是镜像没有sh
[root@k8s-master01 pod]# kubectl exec -it coredns-6d86b45487-phx6j -n kube-system -- sh
Defaulted container "coredns" out of: coredns, debugger-h9mzc (ephem)
error: Internal error occurred: error executing command in container: failed to exec in container: failed to start exec "3df37fbb33b6103016035ed25278745675495482832246a7ff47df4162115329": OCI runtime exec failed: exec failed: unable to start container process: exec: "sh": executable file not found in $PATH: unknown
# 使用临时容器进行debug 执行命令
kubectl debug coredns-6d86b45487-phx6j -it --image=registry.cn-beijing.aliyuncs.com/dotbalo/debug-tools -n kube-system
Defaulting debug container name to debugger-h9mzc.
If you don't see a command prompt, try pressing enter.
(08:55 coredns-6d86b45487-phx6j:/)
(08:55 coredns-6d86b45487-phx6j:/)
这种方式生成的临时容器,其生命周期和会话相关、如果你退出会话容器就结束了
# 除了可以对容器使用debug,还支持对节点执行debug
kubectl debug node/k8s-master01 -it --image=registry.cn-beijing.aliyuncs.com/dotbalo/debug-tools