成都創(chuàng)新互聯(lián)公司作為成都網(wǎng)站建設(shè)公司,專注網(wǎng)站建設(shè)公司、網(wǎng)站設(shè)計(jì),有關(guān)企業(yè)網(wǎng)站設(shè)計(jì)方案、改版、費(fèi)用等問題,行業(yè)涉及白烏魚等多個(gè)領(lǐng)域,已為上千家企業(yè)服務(wù),得到了客戶的尊重與認(rèn)可。
下文主要給大家?guī)韏ob的簡單配置和簡易測(cè)試,希望這些文字能夠帶給大家實(shí)際用處,這也是我編輯job的簡單配置和簡易測(cè)試這篇文章的主要目的。好了,廢話不多說,大家直接看下文吧。
1.job簡單介紹
job也是種控制器,k8s有兩種類型的控制器,一種是服務(wù)類控制器,比如deployment,deamonset,replicaset等等.一種是工作任務(wù)類控制器,job和cronjon就是工作任務(wù)類控制器.
job的簡易參數(shù)介紹
spec.template格式同Pod RestartPolicy僅支持Never或OnFailure 單個(gè)Pod時(shí),默認(rèn)Pod成功運(yùn)行后Job即結(jié)束. spec.completions標(biāo)志Job結(jié)束需要成功運(yùn)行的Pod個(gè)數(shù),默認(rèn)為1,可以理解pod運(yùn)行的總數(shù). spec.parallelism標(biāo)志并行運(yùn)行的Pod的個(gè)數(shù),默認(rèn)為1,可以理解為同時(shí)運(yùn)行的Pod數(shù)量. spec.activeDeadlineSeconds標(biāo)志失敗Pod的重試最大時(shí)間,超過這個(gè)時(shí)間不會(huì)繼續(xù)重試. spec.backoffLimit: 指定job失敗后進(jìn)行重試的次數(shù).
2.job簡易實(shí)例
[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
修改配置文件,加上參數(shù)測(cè)試
[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參數(shù)設(shè)置了2,因此開始就啟動(dòng)了兩個(gè)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容器數(shù)據(jù)庫
job在什么場(chǎng)景使用呢?
比如數(shù)據(jù)庫備份
最簡單的示例,參考見下:
思路是啟用一個(gè)job,使用的鏡像是mysql:5.7(注意密碼必須設(shè)置),然后執(zhí)行命令mysqldump --host=mysql-test -uroot -ppassword --databases t1 >t1.sql.備份MySQL容器pod主機(jī)名為mysql-test的數(shù)據(jù)庫(注意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.180:8400/TCP 31d service/kubernetes ClusterIP 10.254.0.1 443/TCP 45d service/mysql ClusterIP 10.254.209.23 3306/TCP 23d service/mysql-t ClusterIP 10.254.177.63 3306/TCP 27d service/mysql-test ClusterIP 10.254.177.188 3306/TCP 24d service/wordpress NodePort 10.254.88.118 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
測(cè)試執(zhí)行成功.見下:
[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
完美點(diǎn)的示例,把備份的數(shù)據(jù)解耦出來,加上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使用到j(luò)ob上
[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的目錄下查看,數(shù)據(jù)已經(jīng)備份成功,見下
[root@k8s-node3 jobpv1]# pwd /mnt/data/jobpv1 [root@k8s-node3 jobpv1]# ls t1.sql
4.使用cronjob備份
上面用job備份只是一次性執(zhí)行結(jié)束,對(duì)于很多備份是需要持續(xù)性備份操作的,持續(xù)性備份操作,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 ",每分鐘執(zhí)行一次
注釋2:date +%Y%m%d%H%M,date函數(shù)命令,文件名加上日期時(shí)間,更能直觀*
讀取執(zhí)行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存儲(chǔ)目錄,可見每分鐘保存一個(gè)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用來備份數(shù)據(jù),非常方便.
對(duì)于以上關(guān)于job的簡單配置和簡易測(cè)試,大家是不是覺得非常有幫助。如果需要了解更多內(nèi)容,請(qǐng)繼續(xù)關(guān)注我們的行業(yè)資訊,相信你會(huì)喜歡上這些內(nèi)容的。