DaemonSet
DaemonSet介绍
DaemonSet能够让所有(或者特定)的节点运行同一个pod。 当节点加入到K8S集群中,pod会被(DaemonSet)调度到该节点上运行,当节点从K8S集群中被移除,被DaemonSet调度的pod会被移除 如果删除DaemonSet,所有跟这个DaemonSet相关的pods都会被删除。 如果一个DaemonSet的Pod被杀死、停止、或者崩溃,那么DaemonSet将会重新创建一个新的副本在这台计算节点上。 DaemonSet一般应用于日志收集、监控采集、分布式存储守护进程等
案例演示
环境搭建,一主二从 编写yaml文件
apiVersion : apps/v1
kind : DaemonSet
metadata :
name : daemonset- nginx
spec :
selector :
matchLabels :
app : nginx
template :
metadata :
labels :
app : nginx
spec :
containers :
- name : nginx
imagePullPolicy : IfNotPresent
image : nginx
tolerations :
- key : node- role.kubernetes.io/master
effect : NoSchedule
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
[ node1 ~] $ kubectl apply -f daemonset.yaml
daemonset.apps/daemonset-nginx created
验证:每个节点都会运行一个pod
Job
Job介绍
对于ReplicaSet而言,它希望pod保持预期数目、持久运行下去,除非用户明确删除,否则这些对象一直存在,它们针对的是耐久性任务,如web服务等。 对于非耐久性任务,比如压缩文件,任务完成后,pod需要结束运行,不需要pod继续保持在系统中,这个时候就要用到Job。 Job负责批量处理短暂的一次性任务 (short lived one-off tasks),即仅执行一次的任务,它保证批处理任务的一个或多个Pod成功结束。
案例演示
简单job场景演示
apiVersion : batch/v1
kind : Job
metadata :
name : job- test
spec :
template :
spec :
containers :
- name : nginx
image : nginx
imagePullPolicy : IfNotPresent
command : [ "/bin/bash" , "-c" , "for ((i=1;i<10;i++));do date; sleep 1;done" ]
restartPolicy : Never
验证
创建固定次数job
apiVersion : batch/v1
kind : Job
metadata :
name : busybox- job
spec :
completions : 10
parallelism : 1
template :
metadata :
name : busybox- job- pod
spec :
containers :
- name : busybox
image : busybox
imagePullPolicy : IfNotPresent
command : [ "echo" , "hello" ]
restartPolicy : Never
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
验证
CronJob
CronJob介绍
类似于Linux系统的crontab,在指定的时间周期运行相关的任务 时间格式:分时日月周
CronJob应用案例
apiVersion : batch/v1
kind : CronJob
metadata :
name : cronjob- test
spec :
schedule : "* * * * *"
jobTemplate :
spec :
template :
spec :
containers :
- name : hello
image : busybox
args :
- /bin/sh
- - c
- date; echo hello k8s
imagePullPolicy : IfNotPresent
restartPolicy : OnFailure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
验证:正常创建cronjob类型控制器,同时可以看到每隔1分钟创建了一个pod
zsx: cronJob zsx$ kubectl apply - f cronjob.yaml
cronjob.batch/cronjob- test created
zsx: cronJob zsx$ kubectl get cj
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
cronjob- test * * * * * False 0 > 5s
zsx: cronJob zsx$ kubectl get pod
NAME READY STATUS RESTARTS AGE
cronjob- test- 27690367- 9chtm 0/1 Completed 0 12s
zsx: cronJob zsx$ kubectl get pod
NAME READY STATUS RESTARTS AGE
cronjob- test- 27690367- 9chtm 0/1 Completed 0 2m
cronjob- test- 27690368- c8zjh 0/1 Completed 0 60s
cronjob- test- 27690369- lcz7d 0/1 ContainerCreating 0 0s