job和cronjob

job和cronjob

job是在集群中完成执行一次任务的一个特殊的控制器

cronjob是定时管理job

job管理的Pod的重启策略只能够有OnFailure(错误情况下重启),Never(无论什么情况下都不重启),不能够有Always(无论什么情况下都重启)

job

只执行一次

# job
---
apiVersion: batch/v1
kind: Job
metadata:
  name: test-job
spec:
  template:
    metadata:
      labels:
        app: job
        deploy: discuz
    spec:
      restartPolicy: OnFailure # 重启策略
      containers:
        - name: mysql
          image: mysql:5.7
          command:
            - "/bin/bash"
            - "-c"
            - "/usr/bin/mysqldump -uroot -p123456 -hmysql-cluster-svc -B discuz > /opt/`date +%F_%H:%m:%S`.sql" # 数据库备份命令
          volumeMounts: # 挂载
            - mountPath: /opt
              name: job-sql-dump
      volumes:
        - name: job-sql-dump
          persistentVolumeClaim:
            claimName: job-sql-dump
              
---
# 创建pvc
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: job-sql-dump  # 存储卷名字
spec:
#  storageClassName: nfs-client 
  accessModes:  # 访问模式
    - "ReadWriteMany"
  resources:
    requests:
      storage: "5Gi"

cronjob

控制时间执行

# CronJob
---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: cronjob
spec:
  jobTemplate:
    metadata:
      labels:
        app: job
        deploy: cronjob
    spec:
      template:
        metadata:
          labels:
            app: job
            deploy: cronjob-pod
        spec:
          restartPolicy: OnFailure # 重启策略
          containers:
              - name: mysql
                image: mysql:5.7
                command:
                  - "/bin/bash"
                  - "-c"
                  - "/usr/bin/mysqldump -uroot -p123456 -hmysql-cluster-svc -B discuz > /opt/`date +%F_%H:%m:%S`.sql" # 数据库备份命令
                volumeMounts: # 挂载
                  - mountPath: /opt
                    name: job-sql-dump
          volumes:
            - name: job-sql-dump
              persistentVolumeClaim:
                claimName: job-sql-dump
  schedule: "* * */1 * *"  # crontab表达式,每天执行一次



---
# 创建pvc
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: job-sql-dump  # 存储卷名字
spec:
  #  storageClassName: nfs-client 
  accessModes:  # 访问模式
    - "ReadWriteMany"
  resources:
    requests:
      storage: "5Gi"
posted @ 2021-12-10 15:53  Jeff的技术栈  阅读(121)  评论(0编辑  收藏  举报
回顶部