Deploy MySQL Master/Slave in a Kubernetes cluster
This article describes how to deploy a MySQL master-slave cluster in a Kubernetes cluster, and NFS is used for data persistence.
I. Environment Introduction
Mysql version: 5.7
Mysql master node:
Host Name: vm1
IP Address: 192.168.115.5/24
Mysql slave node:
Host Name: vm2
IP Address: 192.168.115.6/24
NFS node:
Host Name: vm2
IP Address: 192.168.115.6/24
Shared Directory:/home/mysql_master,/home/mysql_slave
2. Prepare the mysql master-slave image environment
Dockerfile, The docker-entrypoint.sh file is as follows
Https://github.com/docker-library/mysql/tree/master/5.7
Because we want to configure mysql master-slave, so we need to make some modifications to the dockerfile, docker-entrypoint.sh file, mainly in the mysql master-slave configuration part.
Prepare the master Image
Copy the Dockerfile, a copy of the docker-entrypoint.sh for building the master image file.
Add the following content to Dockerfile and set the server-id of the mysql master to 1.
RUN sed-I '/\ [mysqld \]/a server-id = 1 \ nlog-bin'/etc/mysql. conf. d/mysqld. cnf
Add the following to the docker-entrypoint.sh, create a copy user and grant permissions, refresh the system permission table
Echo "create user '$ MYSQL_REPLICATION_USER' @ '%' identified by '$ MYSQL_REPLICATION_PASSWORD';" | "$ {mysql [@]}"
Echo "grant replication slave on *. * TO '$ MYSQL_REPLICATION_USER' @ '%' identified by '$ MYSQL_REPLICATION_PASSWORD';" | "$ {mysql [@]}"
Echo 'flush PRIVILEGES; '| "$ {mysql [@]}"
Prepare an slave Image
Copy the Dockerfile, a copy of the docker-entrypoint.sh for building the slave image file.
Add the following content to Dockerfile and set the server-id of mysql slave to a random number.
Run rand = "$ (date + % s | rev | cut-c 1-2) $ (echo $ {RANDOM }) "& sed-I '/\ [mysqld \]/a server-id =' $ rand' \ nlog-bin'/etc/mysql. conf. d/mysqld. cnf
Add the following to the docker-entrypoint.sh, configure parameters such as host, user, password to connect to the master host, and start the replication process.
Echo "stop slave;" | "$ {mysql [@]}" echo "change master to master_host = '$ MYSQL_MASTER_SERVICE_HOST', master_user = '$ MYSQL_REPLICATION_USER ', master_password = '$ MYSQL_REPLICATION_PASSWORD'; "|" $ {mysql [@]} "echo" start slave; "|" $ {mysql [@]}"
3. Start using the modified dockerfile to create the mysql master and slave images.
# Cd/root/kubernetes/lnmp/mysql/Dockerfiles/Master
# Docker build-t registry.fjhb.cn/mysql-master:0.1.
# Cd/root/kubernetes/lnmp/mysql/Dockerfiles/Slave
# Docker build-t registry.fjhb.cn/mysql-slave:0.1.
# Docker push registry.fjhb.cn/mysql-master:0.1
# Docker push registry.fjhb.cn/mysql-slave:0.1
4. Create pv and pvc for mysql master-slave storage persistent data
Create a directory on the nfs server
# Cd/home/
# Mkdir mysql_master mysql_slave create two sets of pv and pvc through the yaml File
# Cd/root/kubernetes/lnmp/mysql/Storage
# Cat nfs-pv-master.yaml
ApiVersion: v1
Kind: PersistentVolume
Metadata:
Name: pv-nfs-mysql-master
Spec:
Capacity:
Storage: 5Gi
AccessModes:
-ReadWriteOnce
Nfs:
Path:/home/mysql_master
Server: 192.168.115.6
PersistentVolumeReclaimPolicy: Recycle
# Cat nfs-pvc-master.yaml
Kind: PersistentVolumeClaim
ApiVersion: v1
Metadata:
Name: pv-nfs-mysql-master
Spec:
AccessModes:
-ReadWriteOnce
Resources:
Requests:
Storage: 5Gi
# Cat nfs-pv-slave.yaml
ApiVersion: v1
Kind: PersistentVolume
Metadata:
Name: pv-nfs-mysql-slave
Spec:
Capacity:
Storage: 6Gi
AccessModes:
-ReadWriteOnce
Nfs:
Path:/home/mysql_slave
Server: 192.168.115.6
PersistentVolumeReclaimPolicy: Recycle
# Cat nfs-pvc-slave.yaml
Kind: PersistentVolumeClaim
ApiVersion: v1
Metadata:
Name: pv-nfs-mysql-slave
Spec:
AccessModes:
-ReadWriteOnce
Resources:
Requests:
Storage: 6Gi
# Kubectl create-f nfs-pv-master.yaml
# Kubectl create-f nfs-pvc-master.yaml
# Kubectl create-f nfs-pv-slave.yaml
# Kubectl create-f nfs-pvc-slave.yaml
5. Create mysql master ReplicationController and services based on the yaml File
# Cat mysql-master-rc.yaml
ApiVersion: v1
Kind: ReplicationController
Metadata:
Name: mysql-master
Labels:
Name: mysql-master
Spec:
Replicas: 1
Selector:
Name: mysql-master
Template:
Metadata:
Labels:
Name: mysql-master
Spec:
Containers:
-Name: mysql-master
Image: registry.fjhb.cn/mysql-master:0.1
VolumeMounts:
-MountPath:/var/lib/mysql
Name: mysql-master-data
Ports:
-Maid: 3306
Env:
-Name: MYSQL_ROOT_PASSWORD
Value: "12345678"
-Name: MYSQL_REPLICATION_USER
Value: "repl"
-Name: MYSQL_REPLICAITON_PASSWORD
Value: "12345678"
Volumes:
-Name: mysql-master-data
PersistentVolumeClaim:
ClaimName: pv-nfs-mysql-master
# Cat mysql-master-svc.yaml
ApiVersion: v1
Kind: Service
Metadata:
Name: mysql-master
Labels:
Name: mysql-master
Spec:
Type: NodePort
Ports:
-Port: 3306
TargetPort: 3306
Name: http
NodePort: 30066
Selector:
Name: mysql-master
# Kubectl create-f mysql-master-rc.yaml
# Kubectl create-f mysql-master-svc.yaml
Test the connection to the master using the mysql client
6. Create mysql slave ReplicationController and services based on the yaml File
# Cat mysql-slave-rc.yaml
ApiVersion: v1
Kind: ReplicationController
Metadata:
Name: mysql-slave
Labels:
Name: mysql-slave
Spec:
Replicas: 1
Selector:
Name: mysql-slave
Template:
Metadata:
Labels:
Name: mysql-slave
Spec:
Containers:
-Name: mysql-slave
Image: registry.fjhb.cn/mysql-slave:0.1
VolumeMounts:
-MountPath:/var/lib/mysql
Name: mysql-slave-data
Ports:
-Maid: 3306
Env:
-Name: MYSQL_ROOT_PASSWORD
Value: "12345678"
-Name: MYSQL_REPLICATION_USER
Value: "repl"
-Name: MYSQL_REPLICAITON_PASSWORD
Value: "12345678"
Volumes:
-Name: mysql-slave-data
PersistentVolumeClaim:
ClaimName: pv-nfs-mysql-slave
# Cat mysql-slave-svc.yaml
ApiVersion: v1
Kind: Service
Metadata:
Name: mysql-slave
Labels:
Name: mysql-slave
Spec:
Type: NodePort
Ports:
-Port: 3306
TargetPort: 3306
Name: http
NodePort: 30067
Selector:
Name: mysql-slave
# Kubectl create-f mysql-slave-rc.yaml
# Kubectl create-f mysql-slave-svc.yaml
VII. Test and troubleshooting
Use the mysql command line to connect to the slave to view the copy status, and find that the status is ing
When I use the slave container to connect to the master, I find that the root account can be used for normal connection, but the repl account cannot be used for normal connection, prompting access deny
Modify the repl user password on the master
Mysql> alter user repl @ '%' identified by "12345678 ";
Query OK, 0 rows affected (0.00 sec)
Mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
After completing the preceding modification, verify the repl account on the slave. The repl account can be connected normally.
The change master operation is performed on the slave. Because the GTID parameter is not configured when the image is created, the MASTER_AUTO_POSITION = 1 parameter cannot be used here. To enable this parameter, modify the Dockerfile to regenerate the image.
Mysql> stop slave;
Mysql> set global SQL _slave_skip_counter = 1;
Change master \
MASTER_HOST = 'mysql-Master ',\
MASTER_USER = 'repl ',\
MASTER_PASSWORD = '000000 ';
Mysql> start slave;
This article permanently updates link: https://www.bkjia.com/Linux/2018-03/151282.htm