热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

开发笔记:k8s实践16:使用job控制器备份Mysql容器pod数据库

篇首语:本文由编程笔记#小编为大家整理,主要介绍了k8s实践16:使用job控制器备份Mysql容器pod数据库相关的知识,希望对你有一定的参考价值。job配置和简易测

篇首语:本文由编程笔记#小编为大家整理,主要介绍了k8s实践16:使用job控制器备份Mysql容器pod数据库相关的知识,希望对你有一定的参考价值。



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
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752834791315155748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035637076601047101819429555961989467678374494482553797747268471040475346462080466842590694912933136770289891521047521620569660240580381501935112533824300355876402474964732639141992726042699227967823547816360093417216412199245863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938000816470600161452491921732172147723501414419735685481613611573525521334757418494684385233239073941433345477624168625189835694855620992192221842725502542568876717904946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886269456042419652850222106611863067442786220391949450471237137869609563643719172874677646575739624138908658326459958133904780275901
[root@k8s-node1 job]#

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


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

测试执行成功.见下:


[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.

就用上面的示例修改,未完待续.



推荐阅读
author-avatar
紫藤老君的八卦炉
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有