容器化应用我爱编程

Kubernetes 入门:运行不同类型的 Job

2018-05-11  本文已影响183人  Anoyi

本文以 Mongo 数据备份为例,演示在 Kubernetes 集群中运行 Job,以及相关命令和概念。

部署 MongoDB

1、创建 Mongo 部署文件 mongo.yaml

apiVersion: v1
kind: Service
metadata:
  name: mongo
spec:
  selector:
    app: mongo
  ports:
    - port: 27017
  type: NodePort
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mongo-pv-claim
  labels:
    app: mongo
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongo
  labels:
    app: mongo
spec:
  replicas: 1
  template:
    metadata:
      name: mongo
      labels:
        app: mongo
    spec:
      containers:
      - name: mongo
        image: registry.docker-cn.com/library/mongo
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 27017
          name: mongo
        volumeMounts:
        - name: mongo-persistent-storage
          mountPath: /data/db
      restartPolicy: Always
      volumes:
        - name: mongo-persistent-storage
          persistentVolumeClaim:
            claimName: mongo-pv-claim
  selector:
    matchLabels:
      app: mongo

2、部署 Mongo

kubectl create -f mongo.yaml

3、在 Dashboard 中查看 Mongo 服务相关信息,访问 http://192.168.99.100:30000 ,一般为 Minikube VM IP(192.168.99.100) + Port (30000)

Mongo 持久化卷 Mongo Pod

部署 MongoDB 数据备份任务

1、创建备份任务的配置文件 backup.yaml

apiVersion: batch/v1
kind: Job
metadata:
  name: mongo-backup
spec:
  template:
    spec:
      containers:
      - name: mongo-backup
        image: registry.docker-cn.com/library/busybox
        command: ["tar", "cvf", "/backup/backup.tar", "/data/db"]
        volumeMounts:
          - mountPath: /backup
            name: mongo-backup-persistent-storage
          - mountPath: /data/db
            name: mongo-persistent-storage
      restartPolicy: Never
      volumes:
        - name: mongo-backup-persistent-storage
          hostPath:
            path: /tmp/mongo
        - name: mongo-persistent-storage
          persistentVolumeClaim:
            claimName: mongo-pv-claim
  backoffLimit: 4

其中用到了 2 个数据卷,mongo-backup-persistent-storage 绑定主机的 /tmp/mongo 路径, mongo-persistent-storage 对应 MongoDB 的数据卷,并同时挂载到 busybox 容器,这样就能将 MongoDB 的数据备份到 /tmp/mongo 目录下。

2、启动备份 Job

kubectl create -f backup.yaml

3、查看 Job 执行情况

4、确认备份文件存在,在 Minikube VM 中查看 /tmp/mongo 文件

移除备份任务

直接在 Dashboard 面板上删除任务,包括任务和容器组。

创建 Cron 任务

1、每分钟备份一次 MongoDB 数据 backup-cron.yaml

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: mongo-backup-cron
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: mongo-backup
            image: registry.docker-cn.com/library/busybox
            command:
              - /bin/sh
              - -c
              - tar cvf /backup/mongo-data.tar /data/db
            volumeMounts:
              - mountPath: /backup
                name: mongo-backup-persistent-storage
              - mountPath: /data/db
                name: mongo-persistent-storage
          restartPolicy: Never
          volumes:
            - name: mongo-backup-persistent-storage
              hostPath:
                path: /tmp/mongo
            - name: mongo-persistent-storage
              persistentVolumeClaim:
                claimName: mongo-pv-claim

2、启动 Cron 任务

kubectl create -f backup-cron.yaml

3、查看任务调度

上一篇下一篇

猜你喜欢

热点阅读