Mysql主从
准备环境
一,准备软件
官方docker_image :Mysql5.7.28
Docker Version: 19.03.4
K8s api-version: apps/v1
二,具体制作流程
1根据docker-image编写Dockerfile制作mysql-master镜像2.1根据docker-image编写Dockerfile制作mysql-salve镜像
From mysql:latest #基础镜像
ENV MYSQL_ALLOW_EMPTY_PASSWORD yes #设置无密码登录
COPY conf.sh /mysql/conf.sh #复制配置文件
COPY set.sql /mysql/set.sql #复制配置文件
CMD ["sh", "/mysql/conf.sh"] #容器启动时执行脚本
2.1配置文件conf.sh
#! /bin/bash
set -e
echo "1,设置mysql_server_ID....."
sed -i '/\[mysqld\]/a server-id=1\nlog-bin=/var/lib/mysql/mysql-bin\ngtid-mode=ON\nenforce-gtid-consistency=ON' /etc/mysql/mysql.conf.d/mysqld.cnf
echo "2.启动MySQL 。。。"
service mysql start
echo "3,设置密码。。。"
sed -i 's/MYSQLROOTPASSWOED/'$MYSQL_ROOT_PASSWORD'/' mysql/set.sql
sed -i 's/MYSQLREPLICATIONPASSWORD/'$MYSQL_REPLICATION_PASSWORD'/' mysql/set.sql
sed -i 's/MYSQLREPLICATIONUSER/'$MYSQL_REPLICATION_USER'/' mysql/set.sql
mysql < /mysql/set.sql
echo "4 service mysql status"
tail -f /dev/null
2.2配置文件set.sql 设置mysql——root密码 创建复制用户及密码
use mysql;
set password for root@'localhost' = password("MYSQLROOTPASSWORD");
grant all on *.* to "MYSQLREPLICATIONUSER"@'%' identified by "MYSQLREPLICATIONPASSWORD" with grant option;
grant all on *.* to 'root'@'%' identified by "MYSQLREPLICATIONPASSWORD" with grant option;
flush privileges;
1.3将三个文件放在同级目录构建mysql-master-image
执行命令
#docker build -t mysql5.7-master:001 .
创建完image后在本地docker环境配置容器看是否有无复制用户
#docker run --name mysql-master -e MYSQL_ROOT_PASSWORD="123456" -e MYSQL_REPLICATION_USER="rep1" -e MYSQL_REPLICATION_PASSWORD="123456" -d mysql5.7-master:01
进入容器>进去mysql>查看用户
#docker exec -it mysql-master bash
2.1根据docker-image编写Dockerfile制作mysql-salve镜像
From mysql:latest #基础镜像
ENV MYSQL_ALLOW_EMPTY_PASSWORD yes #设置无密码登录
COPY conf.sh /mysql/conf.sh #复制配置文件
COPY set.sql /mysql/set.sql #复制配置文件
CMD ["sh", "/mysql/conf.sh"] #容器启动时执行脚本
2.1配置文件conf.sh
#! /bin/bash
set -e
echo "1,设置mysql_server_ID....."
sed -i '/\[mysqld\]/a server-id=1\nlog-bin=/var/lib/mysql/mysql-bin\ngtid-mode=ON\nenforce-gtid-consistency=ON' /etc/mysql/mysql.conf.d/mysqld.cnf
echo "2.启动MySQL 。。。"
service mysql start
echo "3,设置密码。。。"
sed -i 's/MYSQLROOTPASSWOED/'$MYSQL_ROOT_PASSWORD'/' mysql/set.sql
sed -i 's/MYSQLREPLICATIONPASSWORD/'$MYSQL_REPLICATION_PASSWORD'/' mysql/set.sql
sed -i 's/MYSQLREPLICATIONUSER/'$MYSQL_REPLICATION_USER'/' mysql/set.sql
mysql < /mysql/set.sql
echo "4 service mysql status"
tail -f /dev/null
2.2配置文件set.sql 设置mysql——root密码 创建复制用户及密码
use mysql;
set password for root@'localhost' = password("MYSQLROOTPASSWORD");
grant all on *.* to 'root'@'%' identified by "MYSQLREPLICATIONPASSWORD" with grant option;
flush privileges;
2.3将三个文件放在同级目录构建mysql-slave-image
执行命令
#docker build -t mysql5.7-slave:001 .
2.4创建完image后在配置slave容器
2.4.1先查看master容器IP
# docker inspect mysql-master | grep IPAddr
2.5创建slave容器
docker run --name mysql-slave -e MYSQL_ROOT_PASSWORD="123456" -e MYSQL_MASTER_SERVICE_HOST="172.17.0.10" -e MYSQL_REPLICATION_USER="rep1" -e MYSQL_REPLICATION_PASSWORD="123456" -d mysql5.7-slave:001
2.6进入容器>进去mysql>slave状态
#docker exec -it mysql-slave bash
三,将所创建mysql-images转入k8s镜像库作为底层pod基础
1 在本地环境docker保存刚刚创建的两个mysql镜像
#docker save -o mysql5.7-master.tar mysql5.7-master:001
#docker save -o mysql5.7-slave.tar mysql5.7-slave:001
2导入至k8s环境
# docker load -i mysql5.7-master.tar
# docker load -i mysql5.7-slave.tar
3编写mysql-master-deploy.yaml
apiVersion: apps/v1 #版本号
kind: Deployment #创建的资源类型
metadata: #该资源的元数据,name必选项
name: mysql-master #name描述
spec: #deployment的规格说明
replicas: 1 #副本数量
selector:
matchLabels:
name: mysql-master
template: #定义pod的模板,配置文件的重要部分
metadata: #定义pod的元数据,至少定义一个label
labels:
name: mysql-master
track: stable
spec: #描述pod的规格,定义pod中每个容器的属性,name和image必须
containers: #容器描述
- image: mysql5.7-master:001 #镜像来源名字
name: mysql-master
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "123456"
- name: MYSQL_REPLICATION_USER
value: "rep1"
- name: MYSQL_REPLICATION_PASSWORD
value: "123456"
4编写mysql-mater-svc.yaml 映射端口为集群:31111
apiVersion: v1
kind: Service
metadata:
name: mysql-master
labels:
name: mysql-master
spec:
type: NodePort
ports:
- port: 3306
targetPort: 3306
nodePort: 31111
protocol: TCP
selector:
name: mysql-master
5编写mysql-slave-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-slave
spec:
replicas: 3
selector:
matchLabels:
name: mysql-slave
template:
metadata:
labels:
name: mysql-slave
spec:
# hostNetwork: true
# endpoint-pod-names: mysql-master
containers:
- name: mysql-slave
image: mysql5.7-slave:001
imagePullPolicy: IfNotPresent
env:
- name: MYSQL_ROOT_PASSWORD
value: "123456"
- name: MYSQL_REPLICATION_USER
value: "rep1"
- name: MYSQL_REPLICATION_PASSWORD
value: "123456"
- name: MYSQL_MASTER_SERVICE_HOST
value: 192.168.226.183
ports:
- name:
containerPort: 3306
查看pods状态
#kubectl get pods -o wide
验证 再master创建一张表看slave是否同步