千家信息网

job的简单配置和简易测试

发表于:2025-01-24 作者:千家信息网编辑
千家信息网最后更新 2025年01月24日,下文主要给大家带来job的简单配置和简易测试,希望这些文字能够带给大家实际用处,这也是我编辑job的简单配置和简易测试这篇文章的主要目的。好了,废话不多说,大家直接看下文吧。1.job简单介绍job也
千家信息网最后更新 2025年01月24日job的简单配置和简易测试

下文主要给大家带来job的简单配置和简易测试,希望这些文字能够带给大家实际用处,这也是我编辑job的简单配置和简易测试这篇文章的主要目的。好了,废话不多说,大家直接看下文吧。

1.job简单介绍

job也是种控制器,k8s有两种类型的控制器,一种是服务类控制器,比如deployment,deamonset,replicaset等等.一种是工作任务类控制器,job和cronjon就是工作任务类控制器.

job的简易参数介绍

spec.template格式同PodRestartPolicy仅支持Never或OnFailure单个Pod时,默认Pod成功运行后Job即结束.spec.completions标志Job结束需要成功运行的Pod个数,默认为1,可以理解pod运行的总数.spec.parallelism标志并行运行的Pod的个数,默认为1,可以理解为同时运行的Pod数量.spec.activeDeadlineSeconds标志失败Pod的重试最大时间,超过这个时间不会继续重试.spec.backoffLimit: 指定job失败后进行重试的次数.

2.job简易实例

[root@k8s-node1 job]#cat job1.yaml apiVersion: batch/v1kind: Jobmetadata:  name: pispec:  template:    metadata:      name: pi    spec:      containers:      - name: pi        image: perl        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]      restartPolicy: Never
[root@k8s-node1 job]# kubectl get jobNAME   COMPLETIONS   DURATION   AGEpi     1/1           8m37s      15m[root@k8s-node1 job]# kubectl get podNAME                             READY   STATUS      RESTARTS   AGEbusybox                          1/1     Running     76         28dmysql-7c9cbfcdf8-hxlrm           1/1     Running     7          22dmysql-t-54666b579c-7m5rv         1/1     Running     11         26dmysql-test-647b8db96b-qdxw6      1/1     Running     10         23dpi-5gtpz                         0/1     Completed   0          15mwordpress-pod-74c47cd8dd-dlzvc   1/1     Running     7          22d[root@k8s-node1 job]# kubectl logs pi-5gtpz

修改配置文件,加上参数测试

[root@k8s-node1 job]# cat job1.yaml apiVersion: batch/v1kind: Jobmetadata:  name: pispec:  completions: 3  parallelism: 2  template:    metadata:      name: pi    spec:      containers:      - name: pi        image: perl        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]      restartPolicy: Never

spec.parallelism参数设置了2,因此开始就启动了两个pod

[root@k8s-node1 job]# kubectl get podNAME                             READY   STATUS              RESTARTS   AGEbusybox                          1/1     Running             76         28dmysql-7c9cbfcdf8-hxlrm           1/1     Running             7          22dmysql-t-54666b579c-7m5rv         1/1     Running             11         26dmysql-test-647b8db96b-qdxw6      1/1     Running             10         23dpi-jjpm4                         0/1     ContainerCreating   0          4spi-xffsz                         0/1     ContainerCreating   0          4swordpress-pod-74c47cd8dd-dlzvc   1/1     Running             7          22d
[root@k8s-node1 job]# kubectl get podNAME                             READY   STATUS      RESTARTS   AGEbusybox                          1/1     Running     76         28dmysql-7c9cbfcdf8-hxlrm           1/1     Running     7          22dmysql-t-54666b579c-7m5rv         1/1     Running     11         26dmysql-test-647b8db96b-qdxw6      1/1     Running     10         23dpi-c4qxs                         0/1     Completed   0          43spi-jjpm4                         0/1     Completed   0          55spi-xffsz                         0/1     Completed   0          55swordpress-pod-74c47cd8dd-dlzvc   1/1     Running     7          22d

3.job备份mysql容器数据库

job在什么场景使用呢?

比如数据库备份

最简单的示例,参考见下:

思路是启用一个job,使用的镜像是mysql:5.7(注意密码必须设置),然后执行命令mysqldump --host=mysql-test -uroot -ppassword --databases t1 >t1.sql.备份MySQL容器pod主机名为mysql-test的数据库(注意mysql-test是svc的名字).

[root@k8s-node1 job]# kubectl get svc,podNAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)         AGEservice/httpd-svc    NodePort    10.254.125.1             80:8400/TCP     31dservice/kubernetes   ClusterIP   10.254.0.1               443/TCP         45dservice/mysql        ClusterIP   10.254.209.23            3306/TCP        23dservice/mysql-t      ClusterIP   10.254.177.63            3306/TCP        27dservice/mysql-test   ClusterIP   10.254.177.188           3306/TCP        24dservice/wordpress    NodePort    10.254.88.118            8080:8425/TCP   23dNAME                                 READY   STATUS      RESTARTS   AGEpod/busybox                          1/1     Running     92         29dpod/mysql-7c9cbfcdf8-hxlrm           1/1     Running     17         23dpod/mysql-dump-jw5zh                 0/1     Completed   0          80spod/mysql-t-54666b579c-7m5rv         1/1     Running     21         27dpod/mysql-test-647b8db96b-qdxw6      1/1     Running     19         24dpod/t1-55f6c78557-6xxwd              1/1     Running     0          17mpod/t2-7f459d454c-wk2zb              1/1     Running     0          17mpod/wordpress-pod-74c47cd8dd-dlzvc   1/1     Running     12         23d
[root@k8s-node1 job]# cat job2.yaml apiVersion: batch/v1kind: Jobmetadata:  name: mysql-dumpspec:  template:    metadata:      name: mysql-dump    spec:      containers:      - name: mysql-dump        image: mysql:5.7        env:        - name: MYSQL_ROOT_PASSWORD          value: abc123        command: ["/bin/sh","-c","mysqldump --host=mysql-test -uroot -ppassword --databases t1 >t1.sql"]      restartPolicy: Never

测试执行成功.见下:

[root@k8s-node1 storage]# kubectl get jobNAME         COMPLETIONS   DURATION   AGEmysql-dump   1/1           2s         28m
[root@k8s-node1 storage]# kubectl get podNAME                                      READY   STATUS              RESTARTS   AGEbusybox                                   1/1     Running             81         29dmysql-7c9cbfcdf8-hxlrm                    1/1     Running             7          22dmysql-dump-p4wn7                          0/1     Completed           0          28mmysql-t-54666b579c-7m5rv                  1/1     Running             11         27dmysql-test-647b8db96b-qdxw6               1/1     Running             10         23dnfs-client-provisioner-5bd47b7669-lzv6j   0/1     ContainerCreating   0          6m38swordpress-pod-74c47cd8dd-dlzvc            1/1     Running             7          22d

完美点的示例,把备份的数据解耦出来,加上pvc.

配置pv和pvc,参考见下:

pv

[root@k8s-node1 storage]# cat job-pv1.yaml apiVersion: v1kind: PersistentVolumemetadata:  name: jobpv1spec:  capacity:    storage: 100Mi  accessModes:    - ReadWriteMany  persistentVolumeReclaimPolicy: Retain  storageClassName: nfs  nfs:    path: /mnt/data/jobpv1    server: 192.168.174.130[root@k8s-node1 storage]# kubectl apply -f job-pv1.yaml persistentvolume/jobpv1 created[root@k8s-node1 storage]# kubectl get pvNAME        CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM                STORAGECLASS   REASON   AGEjobpv1      100Mi      RWX            Retain           Available                        nfs                     7s

pvc

[root@k8s-node1 storage]# cat job-pvc1.yaml apiVersion: v1kind: PersistentVolumeClaimmetadata:   name: jobpvc1spec:   accessModes:     - ReadWriteMany   resources:     requests:       storage: 100Mi   storageClassName: nfs[root@k8s-node1 storage]# kubectl apply -f job-pvc1.yaml persistentvolumeclaim/jobpvc1 created[root@k8s-node1 storage]# kubectl get pvcNAME         STATUS   VOLUME      CAPACITY   ACCESS MODES   STORAGECLASS   AGEjobpvc1      Bound    jobpv1      100Mi      RWX            nfs            4s

把pvc使用到job上

[root@k8s-node1 job]# cat job2.yaml apiVersion: batch/v1kind: Jobmetadata:  name: mysql-dumpspec:  template:    metadata:      name: mysql-dump    spec:      containers:      - name: mysql-dump        image: mysql:5.7        env:        - name: MYSQL_ROOT_PASSWORD          value: abc123        command: ["/bin/sh","-c","mysqldump --host=mysql-test -uroot -ppassword --databases t1 >/mnt/t1.sql"]        volumeMounts:          - name: job-pvc            mountPath: "/mnt"      restartPolicy: Never      volumes:        - name: job-pvc          persistentVolumeClaim:            claimName: jobpvc1
[root@k8s-node1 job]# kubectl apply -f job2.yaml job.batch/mysql-dump created[root@k8s-node1 job]# kubectl get podNAME                             READY   STATUS      RESTARTS   AGEbusybox                          1/1     Running     92         29dmysql-7c9cbfcdf8-hxlrm           1/1     Running     17         23dmysql-dump-jw5zh                 0/1     Completed   0          3smysql-t-54666b579c-7m5rv         1/1     Running     21         27dmysql-test-647b8db96b-qdxw6      1/1     Running     19         24dt1-55f6c78557-6xxwd              1/1     Running     0          16mt2-7f459d454c-wk2zb              1/1     Running     0          15mwordpress-pod-74c47cd8dd-dlzvc   1/1     Running     12         23d

可以去保存pvc的目录下查看,数据已经备份成功,见下

[root@k8s-node3 jobpv1]# pwd/mnt/data/jobpv1[root@k8s-node3 jobpv1]# lst1.sql

4.使用cronjob备份

上面用job备份只是一次性执行结束,对于很多备份是需要持续性备份操作的,持续性备份操作,k8s有cronjob.

就用上面的示例修改

注意cronjob的api和job的api不一样,可用expalin命令检索,见下

[root@k8s-node1 job]# kubectl explain cronjobKIND:     CronJobVERSION:  batch/v1beta1

修改好的cronjob示例见下

[root@k8s-node1 job]# cat cronjob1.yaml apiVersion: batch/v1beta1kind: CronJobmetadata:  name: mysql-dumpspec:  schedule: "*/1 * * * *"  jobTemplate:    spec:      template:        spec:          containers:          - name: mysql-dump            image: mysql:5.7            env:            - name: MYSQL_ROOT_PASSWORD              value: abc123            command: ["/bin/sh","-c","mysqldump --host=mysql-test -uroot -ppassword --databases t1 >/mnt/t1`date +%Y%m%d%H%M`.sql"]            volumeMounts:              - name: job-pvc                mountPath: "/mnt"          restartPolicy: Never          volumes:            - name: job-pvc              persistentVolumeClaim:                claimName: jobpvc1

注释1:schedule: "/1 ",每分钟执行一次
注释2:date +%Y%m%d%H%M,date函数命令,文件名加上日期时间,更能直观*

读取执行cronjob

[root@k8s-node1 job]# kubectl apply -f cronjob1.yaml cronjob.batch/mysql-dump created
[root@k8s-node1 job]# kubectl get cronjobNAME         SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGEmysql-dump   */1 * * * *   False     1        5s              10s
[root@k8s-node1 job]# kubectl get podNAME                                      READY   STATUS      RESTARTS   AGEbusybox                                   1/1     Running     103        32dmysql-7c9cbfcdf8-hxlrm                    1/1     Running     21         26dmysql-dump-1577082660-d58mx               0/1     Completed   0          9s

检索pvc存储目录,可见每分钟保存一个sql文件,见下:

[root@k8s-node3 jobpv1]# pwd/mnt/data/jobpv1[root@k8s-node3 jobpv1]# lltotal 32-rw-r--r-- 1 root root 2447 Dec 23 14:31 t1201912230631.sql-rw-r--r-- 1 root root 2447 Dec 23 14:32 t1201912230632.sql-rw-r--r-- 1 root root 2447 Dec 23 14:33 t1201912230633.sql-rw-r--r-- 1 root root 2447 Dec 23 14:34 t1201912230634.sql-rw-r--r-- 1 root root 2447 Dec 23 14:35 t1201912230635.sql-rw-r--r-- 1 root root 2447 Dec 23 14:36 t1201912230636.sql-rw-r--r-- 1 root root 2447 Dec 23 14:37 t1201912230637.sql

cronjob用来备份数据,非常方便.

对于以上关于job的简单配置和简易测试,大家是不是觉得非常有帮助。如果需要了解更多内容,请继续关注我们的行业资讯,相信你会喜欢上这些内容的。

0