一、Mysql StatefulSet应用文件预览
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: mysql:5.7
imagePullPolicy: IfNotPresent
command:
- bash
- "-c"
- |
set -ex
# 从hostname中获取索引,比如(mysql-1)会获取(1)
[[ `hostname` =~ -([0-9]+)$ ]] || exit 1
ordinal=${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 ]]; 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: twoeo/gcr.io-google-samples-xtrabackup:latest
imagePullPolicy: IfNotPresent
command:
- bash
- "-c"
- |
set -ex
# 如果有数据不必克隆数据,直接退出
[[ -d /var/lib/mysql/mysql ]] && exit 0
# 如果是master数据也不必克隆
[[ `hostname` =~ -([0-9]+)$ ]] || exit 1
ordinal=${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/mysql
volumeMounts:
- name: data
mountPath: /var/lib/mysql
subPath: mysql
- name: conf
mountPath: /etc/mysql/conf.d
containers:
- name: mysql
image: mysql:5.7
imagePullPolicy: IfNotPresent
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: 50m
memory: 50Mi
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: twoeo/gcr.io-google-samples-xtrabackup:latest
imagePullPolicy: IfNotPresent
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 ]]; then
# XtraBackup already generated a partial "CHANGE MASTER TO" query
# because we're cloning from an existing slave.
mv xtrabackup_slave_info change_master_to.sql.in
# Ignore xtrabackup_binlog_info in this case (it's useless).
rm -f 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 xtrabackup_binlog_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"
# In case of container restart, attempt this at-most-once.
mv change_master_to.sql.in change_master_to.sql.orig
mysql -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;
EOF
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: 10m
memory: 10Mi
volumes:
- name: conf
emptyDir: {}
- name: config-map
configMap:
name: mysql
volumeClaimTemplates:
- metadata:
name: data
spec:
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主从复制+读写分离
转载:https://blog.csdn.net/qq_38900565/article/details/102482782
查看评论