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简易实例

[[email protected] 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
[[email protected] job]# kubectl get job
NAME   COMPLETIONS   DURATION   AGE
pi     1/1           8m37s      15m
[[email protected] 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
[[email protected] job]# kubectl logs pi-5gtpz
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752834791315155748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035637076601047101819429555961989467678374494482553797747268471040475346462080466842590694912933136770289891521047521620569660240580381501935112533824300355876402474964732639141992726042699227967823547816360093417216412199245863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938000816470600161452491921732172147723501414419735685481613611573525521334757418494684385233239073941433345477624168625189835694855620992192221842725502542568876717904946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886269456042419652850222106611863067442786220391949450471237137869609563643719172874677646575739624138908658326459958133904780275901
[[email protected] job]#

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

[[email protected] 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

[[email protected] 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
[[email protected] 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的名字).

[[email protected] 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
[[email protected] 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

测试执行成功.见下:

[[email protected] storage]# kubectl get job
NAME         COMPLETIONS   DURATION   AGE
mysql-dump   1/1           2s         28m
[[email protected] 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

[[email protected] 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
[[email protected] storage]# kubectl apply -f job-pv1.yaml
persistentvolume/jobpv1 created
[[email protected] storage]# kubectl get pv
NAME        CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM                STORAGECLASS   REASON   AGE
jobpv1      100Mi      RWX            Retain           Available                        nfs                     7s

pvc

[[email protected] storage]# cat job-pvc1.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
   name: jobpvc1
spec:
   accessModes:
     - ReadWriteMany
   resources:
     requests:
       storage: 100Mi
   storageClassName: nfs
[[email protected] storage]# kubectl apply -f job-pvc1.yaml
persistentvolumeclaim/jobpvc1 created
[[email protected] storage]# kubectl get pvc
NAME         STATUS   VOLUME      CAPACITY   ACCESS MODES   STORAGECLASS   AGE
jobpvc1      Bound    jobpv1      100Mi      RWX            nfs            4s

把pvc使用到job上

[[email protected] 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
[[email protected] job]# kubectl apply -f job2.yaml
job.batch/mysql-dump created
[[email protected] 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的目录下查看,数据已经备份成功,见下

[[email protected] jobpv1]# pwd
/mnt/data/jobpv1
[[email protected] jobpv1]# ls
t1.sql

4.使用cronjob备份

上面用job备份只是一次性执行结束,对于很多备份是需要持续性备份操作的,持续性备份操作,k8s有cronjob.

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

原文地址:https://blog.51cto.com/goome/2460337

时间: 2024-10-03 18:14:30

k8s实践16:使用job控制器备份Mysql容器pod数据库的相关文章

远程备份MYSQL服务器的数据库

1.实验目标:远程备份mysql服务器的数据库2.实验环境:一共2台mysql服务器A和B,在B上远程备份A上的数据库,在A上建立数据库google和baidu,注意在防火墙上开启3306端口或者关闭防火墙,3.实验拓扑:4.实验步骤1)在A上建立数据库2)在A上建立一个建立一个专用的数据库用户,这里我使用root账户,授予root账户对可以通过远程主机192.168.1.2连接A3)在备份主机B上备份测试查看已经备份成功4)在备份主机B上编写脚本,之前需要创建备份目录 mkdir -p /op

k8s实践16:kubernetes监测探针简单测试

1.两种探针 readiness probe(就绪探针)监测容器是否就绪?只有pod里的容器就绪,kubelet才会认为pod处于就绪状态.就绪探针的作用是控制哪些pod可以作为svc的后端,如果pod不是就绪状态,就把它从svc load balancer中移除. liveness probe(存活探针)监测容器是否存活?如果容器中的应用出现问题,liveness将检测到容器不健康会通知kubelet,kubelet重启该pod容器. 2.使用探针的三种方式 官网介绍了三种,见下:comman

自动备份mysql和mariadb数据库

shell脚本 #!/bin/bash mysql_user="123456" #数据库用户名 mysql_pwd="123456" #数据库密码 mysql_name="123456" #需要备份数据库的名称 BackName="mysql" #数据库保存名称,格式:mysql-2017-12-18.sql BackPath="/mysql-backup" #数据库备份路径,如果为空,备份路径为脚本路径

k8s实践(四):Controller

环境说明: 主机名 操作系统版本 ip docker version kubelet version 配置 备注 master Centos 7.6.1810 172.27.9.131 Docker 18.09.6 V1.14.2 2C2G 备注 node01 Centos 7.6.1810 172.27.9.135 Docker 18.09.6 V1.14.2 2C2G 备注 node02 Centos 7.6.1810 172.27.9.136 Docker 18.09.6 V1.14.2

Percona Xtrabackup备份mysql全库及指定数据库(完整备份与增量备份)

原文地址:http://www.tuicool.com/articles/RZRnq2 Xtrabackup简介 Percona XtraBackup是开源免费的MySQL数据库热备份软件,它能对InnoDB和XtraDB存储引擎的数据库非阻塞地备份(对于MyISAM的备份 同样需要加表锁).XtraBackup支持所有的Percona Server.MySQL.MariaDB和Drizzle. XtraBackup优势 : 1.无需停止数据库进行InnoDB热备 2.增量备份MySQL 3.流

Mysql备份系列(4)--lvm-snapshot备份mysql数据(全量+增量)操作记录

Mysql最常用的三种备份工具分别是mysqldump.Xtrabackup(innobackupex工具).lvm-snapshot快照.前面分别介绍了:Mysql备份系列(1)--备份方案总结性梳理Mysql备份系列(2)--mysqldump备份(全量+增量)方案操作记录Mysql备份系列(3)--innobackupex备份mysql大数据(全量+增量)操作记录 废话不多说,下面即是使用lvm-snapshot快照方式备份mysql的操作记录,仅依据本人实验中使用而述. 操作记录:如下环

在CentOS下自动备份mysql

在CentOS下自动备份mysql数据库,并差异同步到其它网络主机上 1.在/root/mysql_backup/下添加backup.sh:vim /root/mysql_backup/backup.sh,然后在里面添加以下代码: mysqldump  -uroot  -p 'a123456'  mysqltest  > /root/mysql_backup/`date +%Y-%m-%d~%H:%M:%S`.bak // 备份数据库 rsync  -av  /root/mysql_backup

写一个脚本定时自动备份mysql到指定目录

实例: 编写一个shell脚本每天16:30备份mysql数据并压缩打包(打包文件按照当天的日期命名)放到/root/data #cd /root #vim mysqlbackup.sh #!/bin/bash time=`date +%Y-%m-%d` backupdir=/root/data if [ ! -d "$backupdir" ] then mkdir -p $backupdir fi if [ -f "$time"_all_databases.sql

使用XtraBackup 备份MySQL数据库

本次测试使用XtraBackup备份MySQL数据库 版本:XtraBackup2.4.5+MySQL5.7.16 下载地址:https://www.percona.com/downloads/XtraBackup/ 1.安装XtraBackup 本次为了方便,使用解压版本进行安装,直接解压就可以用了. 使用过程中可能会遇到缺少perl依赖包的问题,我的方法是直接操作把镜像包里的perl都安装了,yum install -y perl* 2.简介 XtraBackup主要包括两个备份工具xtra