您好,登錄后才能下訂單哦!
下文主要給大家帶來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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。