您好,登录后才能下订单哦!
下文主要给大家带来job的简单配置和简易测试,希望这些文字能够带给大家实际用处,这也是我编辑job的简单配置和简易测试这篇文章的主要目的。好了,废话不多说,大家直接看下文吧。
1.job简单介绍
job也是种控制器,k8s有两种类型的控制器,一种是服务类控制器,比如deployment,deamonset,replicaset等等.一种是工作任务类控制器,job和cronjon就是工作任务类控制器.
job的简易参数介绍
spec.template格式同Pod RestartPolicy仅支持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/v1 kind: Job metadata: name: pi spec: 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 job NAME COMPLETIONS DURATION AGE pi 1/1 8m37s 15m [root@k8s-node1 job]# kubectl get pod NAME READY STATUS RESTARTS AGE busybox 1/1 Running 76 28d mysql-7c9cbfcdf8-hxlrm 1/1 Running 7 22d mysql-t-54666b579c-7m5rv 1/1 Running 11 26d mysql-test-647b8db96b-qdxw6 1/1 Running 10 23d pi-5gtpz 0/1 Completed 0 15m wordpress-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/v1 kind: Job metadata: name: pi spec: 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 pod NAME READY STATUS RESTARTS AGE busybox 1/1 Running 76 28d mysql-7c9cbfcdf8-hxlrm 1/1 Running 7 22d mysql-t-54666b579c-7m5rv 1/1 Running 11 26d mysql-test-647b8db96b-qdxw6 1/1 Running 10 23d pi-jjpm4 0/1 ContainerCreating 0 4s pi-xffsz 0/1 ContainerCreating 0 4s wordpress-pod-74c47cd8dd-dlzvc 1/1 Running 7 22d
[root@k8s-node1 job]# kubectl get pod NAME READY STATUS RESTARTS AGE busybox 1/1 Running 76 28d mysql-7c9cbfcdf8-hxlrm 1/1 Running 7 22d mysql-t-54666b579c-7m5rv 1/1 Running 11 26d mysql-test-647b8db96b-qdxw6 1/1 Running 10 23d pi-c4qxs 0/1 Completed 0 43s pi-jjpm4 0/1 Completed 0 55s pi-xffsz 0/1 Completed 0 55s wordpress-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,pod NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/httpd-svc NodePort 10.254.125.1 <none> 80:8400/TCP 31d service/kubernetes ClusterIP 10.254.0.1 <none> 443/TCP 45d service/mysql ClusterIP 10.254.209.23 <none> 3306/TCP 23d service/mysql-t ClusterIP 10.254.177.63 <none> 3306/TCP 27d service/mysql-test ClusterIP 10.254.177.188 <none> 3306/TCP 24d service/wordpress NodePort 10.254.88.118 <none> 8080:8425/TCP 23d NAME READY STATUS RESTARTS AGE pod/busybox 1/1 Running 92 29d pod/mysql-7c9cbfcdf8-hxlrm 1/1 Running 17 23d pod/mysql-dump-jw5zh 0/1 Completed 0 80s pod/mysql-t-54666b579c-7m5rv 1/1 Running 21 27d pod/mysql-test-647b8db96b-qdxw6 1/1 Running 19 24d pod/t1-55f6c78557-6xxwd 1/1 Running 0 17m pod/t2-7f459d454c-wk2zb 1/1 Running 0 17m pod/wordpress-pod-74c47cd8dd-dlzvc 1/1 Running 12 23d
[root@k8s-node1 job]# cat job2.yaml apiVersion: batch/v1 kind: Job metadata: name: mysql-dump spec: 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 job NAME COMPLETIONS DURATION AGE mysql-dump 1/1 2s 28m
[root@k8s-node1 storage]# kubectl get pod NAME READY STATUS RESTARTS AGE busybox 1/1 Running 81 29d mysql-7c9cbfcdf8-hxlrm 1/1 Running 7 22d mysql-dump-p4wn7 0/1 Completed 0 28m mysql-t-54666b579c-7m5rv 1/1 Running 11 27d mysql-test-647b8db96b-qdxw6 1/1 Running 10 23d nfs-client-provisioner-5bd47b7669-lzv6j 0/1 ContainerCreating 0 6m38s wordpress-pod-74c47cd8dd-dlzvc 1/1 Running 7 22d
完美点的示例,把备份的数据解耦出来,加上pvc.
配置pv和pvc,参考见下:
pv
[root@k8s-node1 storage]# cat job-pv1.yaml apiVersion: v1 kind: PersistentVolume metadata: name: jobpv1 spec: 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 pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE jobpv1 100Mi RWX Retain Available nfs 7s
pvc
[root@k8s-node1 storage]# cat job-pvc1.yaml apiVersion: v1 kind: PersistentVolumeClaim metadata: name: jobpvc1 spec: 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 pvc NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE jobpvc1 Bound jobpv1 100Mi RWX nfs 4s
把pvc使用到job上
[root@k8s-node1 job]# cat job2.yaml apiVersion: batch/v1 kind: Job metadata: name: mysql-dump spec: 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 pod NAME READY STATUS RESTARTS AGE busybox 1/1 Running 92 29d mysql-7c9cbfcdf8-hxlrm 1/1 Running 17 23d mysql-dump-jw5zh 0/1 Completed 0 3s mysql-t-54666b579c-7m5rv 1/1 Running 21 27d mysql-test-647b8db96b-qdxw6 1/1 Running 19 24d t1-55f6c78557-6xxwd 1/1 Running 0 16m t2-7f459d454c-wk2zb 1/1 Running 0 15m wordpress-pod-74c47cd8dd-dlzvc 1/1 Running 12 23d
可以去保存pvc的目录下查看,数据已经备份成功,见下
[root@k8s-node3 jobpv1]# pwd /mnt/data/jobpv1 [root@k8s-node3 jobpv1]# ls t1.sql
4.使用cronjob备份
上面用job备份只是一次性执行结束,对于很多备份是需要持续性备份操作的,持续性备份操作,k8s有cronjob.
就用上面的示例修改
注意cronjob的api和job的api不一样,可用expalin命令检索,见下
[root@k8s-node1 job]# kubectl explain cronjob KIND: CronJob VERSION: batch/v1beta1
修改好的cronjob示例见下
[root@k8s-node1 job]# cat cronjob1.yaml apiVersion: batch/v1beta1 kind: CronJob metadata: name: mysql-dump spec: 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 cronjob NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE mysql-dump */1 * * * * False 1 5s 10s
[root@k8s-node1 job]# kubectl get pod NAME READY STATUS RESTARTS AGE busybox 1/1 Running 103 32d mysql-7c9cbfcdf8-hxlrm 1/1 Running 21 26d mysql-dump-1577082660-d58mx 0/1 Completed 0 9s
检索pvc存储目录,可见每分钟保存一个sql文件,见下:
[root@k8s-node3 jobpv1]# pwd /mnt/data/jobpv1 [root@k8s-node3 jobpv1]# ll total 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的简单配置和简易测试,大家是不是觉得非常有帮助。如果需要了解更多内容,请继续关注我们的行业资讯,相信你会喜欢上这些内容的。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。