1.准备mysql和xtrabackup镜像

下载mysql官方镜像并上传到本地harbor

docker pull mysql:5.7
docker tag m ysql:5.7 192.168.1.110/base/mysql:5.7
docker push 192.168.1.110/base/mysql:5.7

下载xtrabackup镜像并上传到本地harbor

docker pull registry.cn-hangzhou.aliyuncs.com/hxpdocker/xtrabackup:1.0
docker tag registry.cn-hangzhou.aliyuncs.com/hxpdocker/xtrabackup:1.0 192.168.1.110/base/xtrabackup:1.0
docker push 192.168.1.110/base/xtrabackup:1.0

2.为mysql数据目录准备PV

nfs创建目录

# mkdir /data/k8s-data/mysql/mysql-datadir-1 -p
# mkdir /data/k8s-data/mysql/mysql-datadir-2 -p
# mkdir /data/k8s-data/mysql/mysql-datadir-3 -p
# mkdir /data/k8s-data/mysql/mysql-datadir-4 -p
# mkdir /data/k8s-data/mysql/mysql-datadir-5 -p

编写pv的yaml

root@k8-deploy:~/k8s-yaml/mysql# cat pv/mysql-datadir-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-datadir-1
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
nfs:
server: 192.168.2.10
path: /data/k8s-data/mysql/mysql-datadir-1
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-datadir-2
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
nfs:
server: 192.168.2.10
path: /data/k8s-data/mysql/mysql-datadir-2
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-datadir-3
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
nfs:
server: 192.168.2.10
path: /data/k8s-data/mysql/mysql-datadir-3
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-datadir-4
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
nfs:
server: 192.168.2.10
path: /data/k8s-data/mysql/mysql-datadir-4
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-datadir-5
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
nfs:
server: 192.168.2.10
path: /data/k8s-data/mysql/mysql-datadir-5

创建PV

root@k8-deploy:~/k8s-yaml/mysql# kubectl apply -f mysql-datadir-pv.yaml
persistentvolume/mysql-datadir-1 created
persistentvolume/mysql-datadir-2 created
persistentvolume/mysql-datadir-3 created
persistentvolume/mysql-datadir-4 created
persistentvolume/mysql-datadir-5 created root@k8-deploy:~/k8s-yaml/mysql# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
mysql-datadir-1 10Gi RWO Retain Available 3s
mysql-datadir-2 10Gi RWO Retain Available 3s
mysql-datadir-3 10Gi RWO Retain Available 3s
mysql-datadir-4 10Gi RWO Retain Available 3s
mysql-datadir-5 10Gi RWO Retain Available 3s

3.部署StatefulSet的mysql集群

3.1 编写yaml文件

configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
name: mysql
labels:
app: mysql
data:
master.cnf: |
# Apply this config only on the master.
[mysqld]
log-bin
slave.cnf: |
# Apply this config only on slaves.
[mysqld]
super-read-only

mysql-services.yaml

 Headless service for stable DNS entries of StatefulSet members.
apiVersion: v1
kind: Service
metadata:
name: mysql
labels:
app: mysql
spec:
ports:
- name: mysql
port: 3306
clusterIP: None
selector:
app: mysql
---
# Client service for connecting to any MySQL instance for reads.
# For writes, you must instead connect to the master: mysql-0.mysql.
apiVersion: v1
kind: Service
metadata:
name: mysql-read
labels:
app: mysql
spec:
ports:
- name: mysql
port: 3306
selector:
app: mysql

statefulset.yaml

# cat mysql-statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
serviceName: mysql
replicas: 3
template:
metadata:
labels:
app: mysql
spec:
initContainers:
- name: init-mysql
image: 192.168.1.110/base/mysql:5.7
command:
- bash
- "-c"
- |
set -ex
# Generate mysql server-id from pod ordinal index.
[[ `hostname` =~ -([0-9]+)$ ]] || exit 1
ordinal=${BASH_REMATCH[1]}
echo [mysqld] > /mnt/conf.d/server-id.cnf
# Add an offset to avoid reserved server-id=0 value.
echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
# Copy appropriate conf.d files from config-map to emptyDir.
if [[ $ordinal -eq 0 ]]; then
cp /mnt/config-map/master.cnf /mnt/conf.d/
else
cp /mnt/config-map/slave.cnf /mnt/conf.d/
fi
volumeMounts:
- name: conf
mountPath: /mnt/conf.d
- name: config-map
mountPath: /mnt/config-map
- name: clone-mysql
image: 192.168.1.110/base/xtrabackup:1.0
command:
- bash
- "-c"
- |
set -ex
# Skip the clone if data already exists.
[[ -d /var/lib/mysql/mysql ]] && exit 0
# Skip the clone on master (ordinal index 0).
[[ `hostname` =~ -([0-9]+)$ ]] || exit 1
ordinal=${BASH_REMATCH[1]}
[[ $ordinal -eq 0 ]] && exit 0
# Clone data from previous peer.
ncat --recv-only mysql-$(($ordinal-1)).mysql 3307 | xbstream -x -C /var/lib/mysql
# Prepare the backup.
xtrabackup --prepare --target-dir=/var/lib/mysql
volumeMounts:
- name: data
mountPath: /var/lib/mysql
subPath: mysql
- name: conf
mountPath: /etc/mysql/conf.d
containers:
- name: mysql
image: 192.168.1.110/base/mysql:5.7
env:
- name: MYSQL_ALLOW_EMPTY_PASSWORD
value: "1"
ports:
- name: mysql
containerPort: 3306
volumeMounts:
- name: data
mountPath: /var/lib/mysql
subPath: mysql
- name: conf
mountPath: /etc/mysql/conf.d
resources:
requests:
cpu: 500m
memory: 1Gi
livenessProbe:
exec:
command: ["mysqladmin", "ping"]
initialDelaySeconds: 30
periodSeconds: 10
timeoutSeconds: 5
readinessProbe:
exec:
# Check we can execute queries over TCP (skip-networking is off).
command: ["mysql", "-h", "127.0.0.1", "-e", "SELECT 1"]
initialDelaySeconds: 5
periodSeconds: 2
timeoutSeconds: 1
- name: xtrabackup
image: 192.168.1.110/base/xtrabackup:1.0
ports:
- name: xtrabackup
containerPort: 3307
command:
- bash
- "-c"
- |
set -ex
cd /var/lib/mysql # Determine binlog position of cloned data, if any.
if [[ -f xtrabackup_slave_info && "x$(<xtrabackup_slave_info)" != "x" ]]; then
# XtraBackup already generated a partial "CHANGE MASTER TO" query
# because we're cloning from an existing slave. (Need to remove the tailing semicolon!)
cat xtrabackup_slave_info | sed -E 's/;$//g' > change_master_to.sql.in
# Ignore xtrabackup_binlog_info in this case (it's useless).
rm -f xtrabackup_slave_info xtrabackup_binlog_info
elif [[ -f xtrabackup_binlog_info ]]; then
# We're cloning directly from master. Parse binlog position.
[[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
rm -f xtrabackup_binlog_info xtrabackup_slave_info
echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\
MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in
fi # Check if we need to complete a clone by starting replication.
if [[ -f change_master_to.sql.in ]]; then
echo "Waiting for mysqld to be ready (accepting connections)"
until mysql -h 127.0.0.1 -e "SELECT 1"; do sleep 1; done echo "Initializing replication from clone position"
mysql -h 127.0.0.1 \
-e "$(<change_master_to.sql.in), \
MASTER_HOST='mysql-0.mysql', \
MASTER_USER='root', \
MASTER_PASSWORD='', \
MASTER_CONNECT_RETRY=10; \
START SLAVE;" || exit 1
# In case of container restart, attempt this at-most-once.
mv change_master_to.sql.in change_master_to.sql.orig
fi # Start a server to send backups when requested by peers.
exec ncat --listen --keep-open --send-only --max-conns=1 3307 -c \
"xtrabackup --backup --slave-info --stream=xbstream --host=127.0.0.1 --user=root"
volumeMounts:
- name: data
mountPath: /var/lib/mysql
subPath: mysql
- name: conf
mountPath: /etc/mysql/conf.d
resources:
requests:
cpu: 100m
memory: 100Mi
volumes:
- name: conf
emptyDir: {}
- name: config-map
configMap:
name: mysql
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 10Gi

3.2 创建集群

kubectl apply -f mysql-configmap.yaml
kubectl apply -f mysql-services.yaml
kubectl apply -f mysql-statefulset.yaml

3.3 测试集群

查看pod是否创建成功

root@k8-deploy:~/k8s-yaml/mysql# kubectl get pod
NAME READY STATUS RESTARTS AGE
mysql-0 2/2 Running 0 3m13s
mysql-1 2/2 Running 1 2m44s
mysql-2 2/2 Running 1 99s

进入pod查看挂载pv

root@k8-deploy:~/k8s-yaml/mysql# kubectl exec mysql-0 -it -- bash
root@mysql-0:/# df -h
Filesystem Size Used Avail Use% Mounted on
...
192.168.2.10:/data/k8s-data/mysql/mysql-datadir-4/mysql 98G 20G 74G 21% /var/lib/mysql
...

进入mysql-0 pod(mysql主)查看mysql状态,并创建测试数据库

root@mysql-0:/# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 148
Server version: 5.7.36-log MySQL Community Server (GPL) Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-0-bin.000003
Position: 154
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec) ERROR:
No query specified mysql> shwo databases;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'shwo databases' at line 1
mysql> show databases;
+------------------------+
| Database |
+------------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| xtrabackup_backupfiles |
+------------------------+
5 rows in set (0.04 sec) mysql> create database yan_test;
Query OK, 1 row affected (0.02 sec) mysql> show databases;
+------------------------+
| Database |
+------------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| xtrabackup_backupfiles |
| yan_test |
+------------------------+
6 rows in set (0.01 sec) mysql> exit

依次查看其他从库的主从同步状态

root@mysql-1:/# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 140
Server version: 5.7.36 MySQL Community Server (GPL) Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: mysql-0.mysql
Master_User: root
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-0-bin.000003
Read_Master_Log_Pos: 325
Relay_Log_File: mysql-1-relay-bin.000002
Relay_Log_Pos: 493
Relay_Master_Log_File: mysql-0-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
... mysql> show databases;
+------------------------+
| Database |
+------------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| xtrabackup_backupfiles |
| yan_test |
+------------------------+
6 rows in set (0.02 sec)

查看mysql的数据是否写入nfs

root@k8-deploy:~/k8s-yaml/mysql# tree -L 2 /data/k8s-data/mysql/
/data/k8s-data/mysql/
├── mysql-datadir-1
│   └── mysql
├── mysql-datadir-2
│   └── mysql
├── mysql-datadir-3
├── mysql-datadir-4
│   └── mysql
└── mysql-datadir-5

4.扩缩容测试

4.1 将集群副本增加到4个

root@k8-deploy:~/k8s-yaml/mysql# kubectl scale -n default statefulset mysql --replicas=4
statefulset.apps/mysql scaled root@k8-deploy:~/k8s-yaml/mysql# kubectl get pod
NAME READY STATUS RESTARTS AGE
mysql-0 2/2 Running 0 13m
mysql-1 2/2 Running 1 12m
mysql-2 2/2 Running 1 11m
mysql-3 1/2 Running 1 24s

4.2 查看增加的mysql-3的主从状态

root@mysql-3:/# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.7.36 MySQL Community Server (GPL) Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: mysql-0.mysql
Master_User: root
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-0-bin.000003
Read_Master_Log_Pos: 325
Relay_Log_File: mysql-3-relay-bin.000002
Relay_Log_Pos: 322
Relay_Master_Log_File: mysql-0-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
... mysql> show databases;
+------------------------+
| Database |
+------------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| xtrabackup_backupfiles |
| yan_test |
+------------------------+
6 rows in set (0.02 sec)

4.3 查看nfs mysql数据目录

root@k8-deploy:~/k8s-yaml/mysql# tree -L 2 /data/k8s-data/mysql/
/data/k8s-data/mysql/
├── mysql-datadir-1
│   └── mysql
├── mysql-datadir-2
│   └── mysql
├── mysql-datadir-3
│   └── mysql
├── mysql-datadir-4
│   └── mysql
└── mysql-datadir-5 9 directories, 0 files

4.4 将副本数缩容至2个

root@k8-deploy:~/k8s-yaml/mysql# kubectl scale -n default statefulset mysql --replicas=2
statefulset.apps/mysql scaled

4.4 查看pod缩容的过程(-w 参数)

root@k8-deploy:~/k8s-yaml/mysql# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
mysql-0 2/2 Running 0 47m
mysql-1 2/2 Running 1 46m
mysql-2 2/2 Running 1 45m
mysql-3 2/2 Terminating 1 34m
mysql-3 0/2 Terminating 1 34m
mysql-3 0/2 Terminating 1 34m
mysql-3 0/2 Terminating 1 34m
mysql-2 2/2 Terminating 1 46m root@k8-deploy:~/k8s-yaml/mysql# kubectl get pod
NAME READY STATUS RESTARTS AGE
mysql-0 2/2 Running 0 48m
mysql-1 2/2 Running 1 47m

4.4 再次查看mysql-1的主从状态是否正常。

# kubectl exec mysql-1 -it -- bash
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: mysql-0.mysql
Master_User: root
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-0-bin.000003
Read_Master_Log_Pos: 325
Relay_Log_File: mysql-1-relay-bin.000002
Relay_Log_Pos: 493
Relay_Master_Log_File: mysql-0-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

4.4 缩容后再次查看nfs上mysql的数据目录(k8s并不会自动删除清理,需要手动操作)

root@k8-deploy:~/k8s-yaml/mysql# tree -L 2 /data/k8s-data/mysql/
/data/k8s-data/mysql/
├── mysql-datadir-1
│   └── mysql
├── mysql-datadir-2
│   └── mysql
├── mysql-datadir-3
│   └── mysql
├── mysql-datadir-4
│   └── mysql
└── mysql-datadir-5 9 directories, 0 files

8.2 k8s 基于StatefulSet运行mysql 一主多从 ,数据通过pv/pvc结合NFS服务器持久化的更多相关文章

  1. 8.3 k8s部署jenkins,通过pv/pvc结合NFS服务器持久化

    1.制作jenkins docker镜像 1.1 下载jenkins wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/war-stable/2.30 ...

  2. ABP框架使用Mysql数据库,以及基于SQLServer创建Mysql数据库的架构和数据

    ABP默认的数据库是SQLServer,不过ABP框架底层是EF框架,因此也是很容易支持其他类型的数据库的,本篇随笔介绍在ABP框架使用Mysql数据库,以及基于SQLServer创建MySql数据库 ...

  3. 基于keepalived搭建mysql双主高可用

    目录 概述 环境准备 keepalived搭建 mysql搭建 mysql双主搭建 mysql双主高可用搭建 概述 传统(不借助中间件)的数据库主从搭建,如果主节点挂掉了,从节点只能读取无法写入,只能 ...

  4. K8S系列第九篇(持久化存储,emptyDir、hostPath、PV/PVC)

    更多k8s内容,请关注威信公众好:新猿技术生态圈 一.数据持久化 Pod是由容器组成的,而容器宕机或停止之后,数据就随之丢了,那么这也就意味着我们在做Kubernetes集群的时候就不得不考虑存储的问 ...

  5. Mysql双主双从高可用集群的搭建且与MyCat进行整合

    1.概述 老话说的好:瞻前顾后.患得患失只会让我们失败,下定决心,干就完了. 言归正传,之前我们聊了Mysql的一主一从读写分离集群的搭建,虽然一主一从或一主多从集群解决了并发读的问题,但由于主节点只 ...

  6. k8s 基于NFS部署storageclass pv自动供给

    在k8s中部署有状态应用时,通常需要做数据持久化存储. 后端存储的方式有以下几种: 1.基于宿主机本地的存储方式: (重启pod时,若pod被调度到其他节点上,尽管原来节点上的数据不会丢失,但是其他节 ...

  7. kubernetes(14):k8s基于NFS部署storageclass实现pv自动供给

    k8s基于NFS部署storageclass实现pv自动供给 https://www.cnblogs.com/Smbands/p/11059843.html https://www.jianshu.c ...

  8. 容器编排系统K8s之StatefulSet控制器

    前文我们聊到了k8s的configmap和secret资源的说明和相关使用示例,回顾请参考:https://www.cnblogs.com/qiuhom-1874/p/14194944.html:今天 ...

  9. 基于amoeba实现mysql数据库的读写分离/负载均衡

    一.Amoeba的简述:[来自百度百科]      Amoeba是一个以MySQL为底层数据存储,并对应用提供MySQL协议接口的proxy.它集中地响应应用的请求,依据用户事先设置的规则,将SQL请 ...

随机推荐

  1. python filter lambda 的使用

    lambda 匿名函数的使用 >>> a=lambda x : x in "1234567890.," >>> a("asd" ...

  2. Vue3学习(六)之使用Vue3进行数据绑定及显示列表数据

    一.写在前面 说来还是比较惭愧的,从周二开始事就比较多,周三还电脑坏了,然后修电脑等等一些杂事,忙的团团转,因为周二.周三自己走的过多了,导致不敢直腰,周四卧床一天. 之前都听说<陈情令> ...

  3. 改善深层神经网络-week1编程题(GradientChecking)

    1. Gradient Checking 你被要求搭建一个Deep Learning model来检测欺诈,每当有人付款,你想知道是否该支付可能是欺诈,例如该用户的账户可能已经被黑客掉. 但是,反向传 ...

  4. 灵光一闪!帮你使用Vue,搞定无法解决的“动态挂载”

    在一些特殊场景下,使用组件的时机无法确定,或者无法在Vue的template中确定要我们要使用的组件,这时就需要动态的挂载组件,或者使用运行时编译动态创建组件并挂载. 今天我们将带大家从实际项目出发, ...

  5. 【二食堂】Alpha - Scrum Meeting 3

    Scrum Meeting 3 例会时间:4.13 12:00 - 12:30 进度情况 组员 昨日进度 今日任务 李健 1. 继续学习前端知识,寻找一些可用的框架.issue 1. 搭建主页html ...

  6. [no_code]OCR表格处理——技术规格说明书

    项目 内容 这个作业属于哪个课程 2020春季计算机学院软件工程(罗杰 任健) 这个作业的要求在哪里 技术规格说明书 我们在这个课程的目标是 远程协同工作,采用最新技术开发软件 这个作业在哪个具体方面 ...

  7. no_code团队介绍和bingduoduo项目采访

    项目 内容 课程:北航-2020-春-软件工程 博客园班级博客 要求 团队作业-团队介绍和采访 成员简介 name avatar intro PM Dev Test UI/Front-End 伦泽标 ...

  8. Spring Cloud Gateway Route Predicate Factory 的使用

    Spring Cloud Gateway的使用 一.需求 二.基本组成 1.简介 2.核型概念 1.Route 路由 2.Predicate 谓语.断言 3.Filter 过滤器 3.工作原理 三.网 ...

  9. HDMI之TMDS通道

    HDMI标准继续沿用了和DVI相同的,由Silicon Image公司发明的TMDS(Time Minimized Differential Signal)最小化传输差分信号传输技术.TMDS是一种微 ...

  10. 在Vue前端界面中,几种数据表格的展示处理,以及表格编辑录入处理操作。

    在Vue前端项目中,我这里主要是基于Vue+Element的开发,大多数情况下,我们利用Element的表格组件就可以满足大多数情况的要求,本篇随笔针对表格的展示和编辑处理,综合性的介绍几款表格组件的 ...