什么是DaemonSet?
DaemonSet 确保全部(或者某些)节点上运行一个Pod的副本。当有节点加入集群时,也会为他们新增一个Pod。当有节点从集群移除时,这些Pod也会被回收。删除DaemonSet将会删除它创建的所有 Pod。
创建daemonset
[root@k8smaster ~]# kubectl apply -f https://k8s.io/examples/controllers/daemonset.yaml
daemonset.apps/fluentd-elasticsearch created
[root@k8smaster ~]# kubectl get ds -n kube-system
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
calico-node 3 3 3 3 3 kubernetes.io/os=linux 3d17h
fluentd-elasticsearch 3 3 3 3 3 <none> 73s
kube-proxy 3 3 3 3 3 kubernetes.io/os=linux 3d17h
[root@k8smaster ~]# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
fluentd-elasticsearch-fvz4z 1/1 Running 0 80s
fluentd-elasticsearch-fwqjx 1/1 Running 0 80s
fluentd-elasticsearch-mgb2n 1/1 Running 0 80s
什么是CronJob?
CronJob 用于执行排期操作,例如备份、生成报告等。一个 CronJob 对象就像Unix系统上的crontab(cron table)文件中的一行。它用Cron格式进行编写, 并周期性地在给定的调度时间执行Job。
创建CronJob
1.编写 cronjob.yaml
[root@k8smaster ~]# vim cronjob.yaml
[root@k8smaster ~]# cat cronjob.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
name: hello
spec:
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
2.运行
[root@k8smaster ~]# kubectl apply -f cronjob.yaml
error: unable to recognize "cronjob.yaml": no matches for kind "CronJob" in version "batch/v1"
这里遇到一个error。解决办法:使用kubectl explain CronJob 去查看原因。发现version不一致,所以修改cronjob.yaml。
[root@k8smaster ~]# kubectl explain CronJob
KIND: CronJob
VERSION: batch/v1beta1
[root@k8smaster ~]# cat cronjob.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: hello
spec:
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
[root@k8smaster ~]# kubectl get cronjob hello
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello * * * * * False 0 42s 2m22s
[root@k8smaster ~]# kubectl get jobs --watch
NAME COMPLETIONS DURATION AGE
hello-1679889060 1/1 20s 2m3s
hello-1679889120 1/1 2s 63s
hello-1679889180 1/1 1s 2s
^C[root@k8smaster ~]# kubectl get cronjob hello
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello * * * * * False 0 37s 3m17s