StorageClass
ConfigMap
新建文件: configmap.yml
,内容如下:
apiVersion: v1
data:
master.cnf: |
# Apply this config only on the master.
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
log-bin
binlog_expire_logs_seconds=2592000
max_connections=10000
default-time-zone='+8:00'
character-set-client-handshake=FALSE
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci'
slave.cnf: |
# Apply this config only on slaves.
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
super-read-only
max_connections=10000
default-time-zone='+8:00'
character-set-client-handshake=FALSE
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci'
kind: ConfigMap
metadata:
labels:
app: mysql
name: mysql
Headless Service
Headless Service 是有状态服务都需要,让其服务下的Pod能够彼此发现
新建文件: service.yml
,内容如下:
apiVersion: v1
kind: Service
metadata:
name: mysql-svc-master
labels:
app: mysql
spec:
selector:
app: mysql
ports:
- port: 3306
name: mysql
clusterIP: None
SatefulSet
配置文件
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql-ss
spec:
selector:
matchLabels:
app: mysql
serviceName: mysql-svc-master
replicas: 3
template:
metadata:
labels:
app: mysql
spec:
initContainers:
- name: init-mysql
image: mysql:8.0.19
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: mzmuer/xtrabackup:1.0
command:
- 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 1
ordinal=${BASH_REMATCH[1]}
[[ $ordinal -eq 0 ]] && exit 0
# 从序列号比自己小一的数据库克隆数据,比如mysql-2会从mysql-1处克隆数据
ncat --recv-only mysql-ss-$(($ordinal-1)).mysql-svc-master 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:8.0.19
args: ["--default-authentication-plugin=mysql_native_password"]
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: 250m
memory: 256Mi
limits:
cpu: 500m
memory: 512Mi
livenessProbe:
exec:
command: ["mysqladmin", "ping"]
initialDelaySeconds: 30
periodSeconds: 10
timeoutSeconds: 5
readinessProbe:
exec:
command: ["mysql", "-h", "127.0.0.1", "-e", "SELECT 1"]
initialDelaySeconds: 5
periodSeconds: 2
timeoutSeconds: 1
- name: xtrabackup
image: mzmuer/xtrabackup:1.0
ports:
- name: xtrabackup
containerPort: 3307
command:
- bash
- "-c"
- |
set -ex
# 确定binlog 克隆数据位置(如果binlog存在的话).
cd /var/lib/mysql
# 如果存在该文件,则该xrabackup是从现有的从节点克隆出来的。
if [[ -s xtrabackup_slave_info ]]; then
mv xtrabackup_slave_info change_master_to.sql.in
rm -f xtrabackup_binlog_info
elif [[ -f xtrabackup_binlog_info ]]; then
[[ `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
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"
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-ss-0.mysql-svc-master',
MASTER_USER='root',
MASTER_PASSWORD='',
MASTER_CONNECT_RETRY=10;
START SLAVE;
EOF
fi
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
limits:
cpu: 200m
memory: 200Mi
volumes:
- name: conf
emptyDir: {}
- name: config-map
configMap:
name: mysql
volumeClaimTemplates:
- metadata:
name: data
spec:
storageClassName: mysql-sc
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
1.initContainers作用:
- 主数据库无需同步自己数据,从数据库需要被同步数据
判断当前启动的容器是主还是slave,并向pod的conf卷写入如下数据
cat /mnt/conf.d/server-id.cnf
[mysqld]
server-id=10?
ls /mnt/conf.d/
master[?salve].cnf # 从configmap处拷贝而来
当执行完command命令后生命终止,但是pod的conf卷的数据依然存在,因为初始容器和接下来的container容器使用共同的volumes卷,不同的是conf被initContainer挂载到/mnt/conf.d,而在container被挂载在/etc/mysql/conf.d/
2.Container作用:
提供mysql服务
- 运行xtrabakup应用容器提供数据同步
mysql-svc-read 服务
apiVersion: v1
kind: Service
metadata:
name: mysql-svc-read
labels:
app: mysql
spec:
ports:
- name: mysql
port: 3306
selector:
app: mysql
测试 mysql-svc-read 服务
kubectl run mysql-client-loop --image=mysql:8.0.19 -i -t -n test --rm --restart=Never --\
bash -ic "while sleep 1; do mysql -h mysql-svc-read -e 'SELECT @@server_id,NOW()'; done"
每秒查询一次数据库,可以观察到,调度到不同的 server_id,即 pod 节点
删除部署
kubectl delete statefulset mysql-ss -n test
kubectl delete configmap,service,pvc -l app=mysql -n test
参考资料
按照K8s官方文档部署Mysql高可用+读写分离的坑
【深入分析】K8s部署Mysql主从复制+读写分离
【全网最全最详细】Kubernetes部署Mysql主从复制+读写分离
kubernetes中 mysql集群出现Access denied for user ‘root’@’localhost’ (using password: NO)
Kubernetes-部署高可用的MySQL