一、Mysql StatefulSet应用文件预览
已用中文进行解释,请看绿色字体
apiVersion: apps/v1
kind: StatefulSet
metadata:name: mysql
spec:selector:matchLabels:app: mysqlserviceName: mysqlreplicas: 3template:metadata:labels:app: mysqlspec:initContainers: - name: init-mysqlimage: mysql:5.7imagePullPolicy: IfNotPresentcommand:- bash- "-c"- |set -ex# 从hostname中获取索引,比如(mysql-1)会获取(1)[[ `hostname` =~ -([0-9]+)$ ]] || exit 1ordinal=${BASH_REMATCH[1]}echo [mysqld] > /mnt/conf.d/server-id.cnf# 为了不让server-id=0而增加偏移量echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf# 拷贝对应的文件到/mnt/conf.d/文件夹中if [[ $ordinal -eq 0 ]]; thencp /mnt/config-map/master.cnf /mnt/conf.d/elsecp /mnt/config-map/slave.cnf /mnt/conf.d/fivolumeMounts:- name: confmountPath: /mnt/conf.d- name: config-mapmountPath: /mnt/config-map- name: clone-mysqlimage: twoeo/gcr.io-google-samples-xtrabackup:latestimagePullPolicy: IfNotPresentcommand:- bash- "-c"- |set -ex# 整体意思:# 1.如果是主mysql中的xtrabackup,就不需要克隆自己了,直接退出# 2.如果是从mysql中的xtrabackup,先判断是否是第一次创建,因为第二次重启本地就有数据库,无需克隆。若是第一次创建(通过/var/lib/mysql/mysql文件是否存在判断),就需要克隆数据库到本地。# 如果有数据不必克隆数据,直接退出()[[ -d /var/lib/mysql/mysql ]] && exit 0# 如果是master数据也不必克隆[[ `hostname` =~ -([0-9]+)$ ]] || exit 1ordinal=${BASH_REMATCH[1]}[[ $ordinal -eq 0 ]] && exit 0# 从序列号比自己小一的数据库克隆数据,比如mysql-2会从mysql-1处克隆数据ncat --recv-only mysql-$(($ordinal-1)).mysql 3307 | xbstream -x -C /var/lib/mysql# 比较数据xtrabackup --prepare --target-dir=/var/lib/mysqlvolumeMounts:- name: datamountPath: /var/lib/mysqlsubPath: mysql- name: confmountPath: /etc/mysql/conf.dcontainers:- name: mysqlimage: mysql:5.7imagePullPolicy: IfNotPresentenv:- name: MYSQL_ALLOW_EMPTY_PASSWORDvalue: "1"ports:- name: mysqlcontainerPort: 3306volumeMounts:- name: datamountPath: /var/lib/mysqlsubPath: mysql- name: confmountPath: /etc/mysql/conf.dresources:requests:cpu: 50mmemory: 50MilivenessProbe:exec:command: ["mysqladmin", "ping"]initialDelaySeconds: 30periodSeconds: 10timeoutSeconds: 5readinessProbe:exec:# Check we can execute queries over TCP (skip-networking is off).command: ["mysql", "-h", "127.0.0.1", "-e", "SELECT 1"]initialDelaySeconds: 5periodSeconds: 2timeoutSeconds: 1- name: xtrabackupimage: twoeo/gcr.io-google-samples-xtrabackup:latestimagePullPolicy: IfNotPresentports:- name: xtrabackupcontainerPort: 3307command:- bash- "-c"- |set -excd /var/lib/mysql# 确定binlog 克隆数据位置(如果binlog存在的话).if [[ -f xtrabackup_slave_info ]]; then# 如果存在该文件,则该xrabackup是从现有的从节点克隆出来的。mv xtrabackup_slave_info change_master_to.sql.in# 在这种情况下,忽略xtrabackup_binlog_info(它是无用的)。rm -f xtrabackup_binlog_infoelif [[ -f xtrabackup_binlog_info ]]; then# 我们直接从主人那里克隆。分析binlog位置。[[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1rm xtrabackup_binlog_infoecho "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.infi# 检查是否需要通过启动复制来完成克隆.if [[ -f change_master_to.sql.in ]]; thenecho "Waiting for mysqld to be ready (accepting connections)"until mysql -h 127.0.0.1 -e "SELECT 1"; do sleep 1; doneecho "Initializing replication from clone position"# 如果容器重新启动,最多尝试一次。mv change_master_to.sql.in change_master_to.sql.origmysql -h 127.0.0.1 <<EOF$(<change_master_to.sql.orig),MASTER_HOST='mysql-0.mysql',MASTER_USER='root',MASTER_PASSWORD='',MASTER_CONNECT_RETRY=10;START SLAVE;EOFfi# 开启监听服务,发送备份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: datamountPath: /var/lib/mysqlsubPath: mysql- name: confmountPath: /etc/mysql/conf.dresources:requests:cpu: 10mmemory: 10Mivolumes:- name: confemptyDir: {
}- name: config-mapconfigMap:name: mysqlvolumeClaimTemplates:- metadata:name: dataspec:accessModes: ["ReadWriteOnce"]resources:requests:storage: 0.1Gi
1.initContainers作用:
(1).主数据库无需同步自己数据,从数据库需要被同步数据
(2).判断当前启动的容器是主还是slave,并向pod的conf卷写入如下数据
cat /mnt/conf.d/server-id.cnf
[mysqld]
server-id=10?
ls /mnt/conf.d/
master[?salve].cnf # 从configmap处拷贝而来
(3)当执行完command命令后生命终止,但是pod的conf卷的数据依然存在,因为初始容器和接下来的container容器使用共同的volumes卷,不同的是conf被initContainer挂载到/mnt/conf.d,而在container被挂载在/etc/mysql/conf.d/
2.Container作用:
(1)提供mysql服务
(2)运行xtrabakup应用容器提供数据同步
二、数据结构分析
{
"apiVersion":"apps/v1","kind":"StatefulSet","metadata":Object{
...},"spec":{
"selector":Object{
...},"serviceName":"mysql","replicas":3,"template":{
"metadata":Object{
...},"spec":{
"initContainers":Array[2],"containers":Array[2],"volumes":Array[2]}},"volumeClaimTemplates":Array[1]}
}
从上图可知知道satefulSet的pvc模板由pv提供存储,voluems作用范围在Pod内所有容器,volumes卷中conf的数据被所有容器共享,/var/lib/mysql映射的data是要被持久化的数据…
了解彼此资源、对象的关系后,看这篇文章实现K8s部署Mysql主从复制+读写分离,利用静态存储卷
【全网最全最详细】Kubernetes部署Mysql主从复制+读写分离