【全网最全最详细】Kubernetes部署Mysql主从复制+读写分离
1.前提:起码得有个已经可以部署简单pod的k8s单机或集群PS: 本文使用静态存储卷实现,非使用存储类2.制作拉取gcr.io镜像源脚本并拉取镜像[root@k8s-m~ ]# cat /usr/local/bin/pull-google.com.shimage=$1echo $1img=`echo $image | sed 's/k8s\.gcr\.io/anjia0532\/go...
·
1.前提:起码得有个已经可以部署简单pod的k8s单机或集群
PS: 本文使用静态存储卷实现,非使用存储类
2.制作拉取gcr.io镜像源脚本并拉取镜像
[root@k8s-m~ ]# cat /usr/local/bin/pull-google.com.sh
image=$1
echo $1
img=`echo $image | sed 's/k8s\.gcr\.io/anjia0532\/google-containers/g;s/gcr\.io/anjia0532/g;s/\//\./g;s/ /\n/g;s/_/-/g;s/anjia0532\./anjia0532\//g' | uniq | awk '{print ""$1""}'`
echo "docker pull $img"
docker pull $img
echo "docker tag $img $image"
docker tag $img $image
[root@k8s-m~ ]# chmod +x /usr/local/bin/pull-google.com.sh
[root@k8s-m~ ]# pull-google.com.sh gcr.io/google-samples/xtrabackup:1.0
3.准备NFS服务,查看NFS服务器IP为192.168.1.11,准备三个持久化磁盘
[root@m-nfs~]# yum install nfs-utils
[root@m-nfs~]# mkdir -p /net/mysql-0 /net/mysql-1 /net/mysql-2
[root@m-nfs~]# ifconfig | head -2| tail -1|awk '{print $2}'
192.168.1.11 # 查看nfs服务器IP
[root@m-nfs~]# echo '/net/mysql-0 *(rw,no_root_squash)' >> /etc/exports
[root@m-nfs~]# echo '/net/mysql-1 *(rw,no_root_squash)' >> /etc/exports
[root@m-nfs~]# echo '/net/mysql-2 *(rw,no_root_squash)' >> /etc/exports
[root@m-nfs~]# systemctl restart nfs-server
[root@m-nfs~]# showmount -e 本机IP # 验证
4.创建三个持久卷(mysql-0、mysql-1、mysql-2)
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-a|pv-b|pv-c
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
- ReadOnlyMany
#persistentVolumeReclaimPolicy: Retain # 当声明被释放,pv将保留(不清理和删除)
persistentVolumeReclaimPolicy: Recycle # 当声明被释放,空间将回收再利用
nfs:
server: 192.168.1.11
path: /net/mysql-0 | /net/mysql-1 | /net/mysql-2
5.创建configMap配置字典
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
6.部署headless服务,有状态服务都需要,让服务旗下的Pod彼此发现
apiVersion: v1
kind: Service
metadata:
name: mysql-headless
labels:
app: mysql
spec:
ports:
- name: mysql
port: 3306
clusterIP: None
selector:
app: mysql
7. 部署SatefulSet应用
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql-ss
spec:
selector:
matchLabels:
app: mysql
serviceName: mysql-headless
replicas: 3
template:
metadata:
labels:
app: mysql
spec:
initContainers:
- name: init-mysql
image: mysql:5.7
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相同而增加偏移量
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: gcr.io/google-samples/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-headless 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
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:
command: ["mysql", "-h", "127.0.0.1", "-e", "SELECT 1"]
initialDelaySeconds: 5
periodSeconds: 2
timeoutSeconds: 1
- name: xtrabackup
image: gcr.io/google-samples/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-headless',
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: 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
8.查看状态
9.测试数据同步 更多详情->简书
(1).往mysql-0创建表,并插入数据
CREATE TABLE demo.js(name VARCHAR(250));
INSERT INTO demo.js(name) VALUES ('hello');
(3).进入三个数据库进行读服务
select * from demo.js;
10. 部署对外只读服务(Only-Read)
apiVersion: v1
kind: Service
metadata:
name: mysql-Read
labels:
app: mysql
spec:
ports:
- name: mysql
port: 3306
targetPort: 3306
nodePort: 30036
type: NodePort
selector:
app: mysql
只读连接测试(使用集群任意节点IP:nodePort进行连接)
11.部署对外读写服务
[root@k8s-master ~]# kubectl get pods --show-labels | grep mysql-ss-0 | awk '{print $6}' | awk -F, '{print $3}'
statefulset.kubernetes.io/pod-name=mysql-ss-0
得到该标签后写入如下selector字段
apiVersion: v1
kind: Service
metadata:
name: mysql-readwrite
labels:
app: mysql
spec:
ports:
- name: mysql
port: 3306
targetPort: 3306
nodePort: 30006
selector:
statefulset.kubernetes.io/pod-name: mysql-ss-0
type: NodePort
读写连接测试(使用集群任意节点IP:nodePort进行连接)
更多推荐
已为社区贡献10条内容
所有评论(0)