k8s || DaemonSet和CronJob

什么是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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韩未零

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值