Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Simple configuration and easy testing of job

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

The following mainly brings you the simple configuration and easy test of job. I hope these words can bring you practical use, which is also the main purpose of this article that I edit the simple configuration and easy test of job. All right, don't talk too much nonsense, let's just read the following.

Brief introduction of 1.job

Job is also a kind of controller. K8s has two types of controllers, one is service controller, such as deployment,deamonset,replicaset and so on. One is the work task controller, job and cronjon are the work task controller.

Introduction of simple parameters of job

When spec.template format is the same as PodRestartPolicy only supports Never or OnFailure single Pod After the default Pod runs successfully, the Job ends. Spec.completions marks the end of the number of Pod that need to run successfully. The default is 1. It can be understood that the total number of pod running in parallel. The default is 1, which can be understood as the number of Pod running at the same time. Spec.activeDeadlineSeconds marks the maximum retry time of the failed Pod. After this time, it will not continue to retry .spec.backoffLimit: specify the number of retries after the job fails.

Simple example of 2.job

[root@k8s-node1 job] # cat job1.yaml apiVersion: batch/v1kind: Jobmetadata: name: pispec: template: metadata: name: pispec: 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 8m37s 15m [root@k8s-node1 job] # kubectl get podNAME READY STATUS RESTARTS AGEbusybox 1 28dmysql-7c9cbfcdf8-hxlrm 1 Running 76 28dmysql-7c9cbfcdf8-hxlrm 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

Modify the configuration file, plus parameter testing

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

The spec.parallelism parameter is set to 2, so two pod are started at the beginning

[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 backs up the mysql container database

In what scenarios is job used?

Such as database backup

For the simplest example, see the following:

The idea is to enable a job, use the mysql:5.7 image (note that the password must be set), and then execute the command mysqldump-- host=mysql-test-uroot-ppassword-- databases T1 > t1.sql. Back up the database of the MySQL container pod hostname mysql-test (note that mysql-test is the name of 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 to 1 Running 17 23dpod/mysql-dump-jw5zh 0 to 1 Completed 0 80spod/mysql-t-54666b579c-7m5rv 1 to 1 Running 21 27dpod/mysql-test-647b8db96b-qdxw6 1 to 1 Running 19 24dpod/t1-55f6c78557-6xxwd 1 to 1 Running 0 17mpod/t2-7f459d454c-wk2zb 1 17mpod/wordpress-pod-74c47cd8dd-dlzvc 1 Running 0 17mpod/wordpress-pod-74c47cd8dd-dlzvc 1 Running 12 23d [root@k8s-node1 job] # cat job2.yaml apiVersion: batch/v1kind: Jobmetadata: name: mysql-dumpspec: template: metadata: name: mysql-dumpspec: 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

The test was executed successfully. See below:

[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

Perfect point of the example, the backup data decoupling, plus pvc.

To configure pv and pvc, refer to the following:

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: 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

Use pvc on job

[root@k8s-node1 job] # cat job2.yaml apiVersion: batch/v1kind: Jobmetadata: name: mysql-dumpspec: template: metadata: name: mysql-dumpspec: 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 to 1 Running 19 24dt1-55f6c78557-6xxwd 1 to 1 Running 0 16mt2-7f459d454c-wk2zb to 1 Running 0 15mwordpress-pod-74c47cd8dd-dlzvc 1 to 1 Running 12 23d

You can view the data in the directory where you saved pvc. The data has been backed up successfully, as shown below.

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

4. Backing up with cronjob

The above backup with job is only an one-time end, for many backups need continuous backup operation, continuous backup operation, K8s has cronjob.

Use the above example to modify

Note that the api of cronjob is different from the api of job. You can retrieve it with the expalin command, as shown below.

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

An example of the modified cronjob is shown below.

[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: "/ mnt" restartPolicy: Never volumes:-name: job-pvc persistentVolumeClaim: claimName: jobpvc1

Note 1:schedule: "/ 1", executed every minute

Note 2:date +% Y% m% d% H% M _ date function command, file name plus date and time, more intuitive *

Read and execute 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

To retrieve the pvc storage directory, you can see that one sql file is saved every minute, as shown below:

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

Cronjob is used to back up data, which is very convenient.

For the above simple configuration and easy testing of job, you do not think it is very helpful. If you need to know more, please continue to follow our industry information. I'm sure you'll like it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report