Kubernetes部署Mysql主从
环境:
[root@cs100 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k3s-master Ready control-plane,master 26h v1.20.4+k3s1
mysql-master-rc.yaml
cat > /root/k3s_yaml/mysql5.7/mysql-master-rc.yaml <<EOF
apiVersion: v1
kind: ReplicationController
metadata:
name: mysql-master-rc
namespace: mysql-space
labels:
name: mysql-master-rc
spec:
replicas: 1
selector:
name: mysql-master-pod
template:
metadata:
labels:
name: mysql-master-pod #该pod拥有的标签,对应上面的spec.selector.name,否则匹配不上
spec:
containers:
- name: mysql
image: mysql:5.7
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "mysql123"
- name: MYSQL_REPLICATION_USER
value: "repl"
- name: MYSQL_REPLICATION_PASSWORD
value: "repl123"
args:
- --lower_case_table_names=1
- --character_set_server=utf8
- --log-bin=mysql-bin
- --binlog-ignore-db=mysql
- --server-id=1001
- --symbolic-links=0
- --sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
EOF
mysql-master-svc.yaml
cat > /root/k3s_yaml/mysql5.7/mysql-master-svc.yaml <<EOF
apiVersion: v1
kind: Service
metadata:
name: mysql-master-svc
namespace: mysql-space
labels:
name: mysql-master-svc
spec:
type: NodePort
ports:
- port: 3306
protocol: TCP
targetPort: 3306
name: http
nodePort: 32306
selector:
name: mysql-master-pod
EOF
mysql-slave-rc.yaml
cat > /root/k3s_yaml/mysql5.7/mysql-slave-rc.yaml <<EOF
apiVersion: v1
kind: ReplicationController
metadata:
name: mysql-slave-rc
namespace: mysql-space
labels:
name: mysql-slave-rc
spec:
replicas: 1
selector:
name: mysql-slave-pod
template:
metadata:
labels:
name: mysql-slave-pod
spec:
containers:
- name: mysql
image: mysql:5.7
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "mysql123"
- name: MYSQL_REPLICATION_USER
value: "repl"
- name: MYSQL_REPLICATION_PASSWORD
value: "repl123"
args:
- --lower_case_table_names=1
- --character_set_server=utf8
- --server-id=2001
- --symbolic-links=0
- --sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
- --default-authentication-plugin=mysql_native_password
EOF
### mysql-slave-svc.yaml
cat > /root/k3s_yaml/mysql5.7/mysql-slave-svc.yaml <<EOF
apiVersion: v1
kind: Service
metadata:
name: mysql-slave-svc
namespace: mysql-space
labels:
name: mysql-slave-svc
spec:
type: NodePort
ports:
- port: 3306
protocol: TCP
targetPort: 3306
name: http
nodePort: 32307
selector:
name: mysql-slave-pod
EOF
创建namespace
kubectl create namespace mysql-space
分别创建mysql master slave服务
kubectl create -f mysql-master-rc.yaml
kubectl create -f mysql-master-svc.yaml
kubectl create -f mysql-slave-rc.yaml
kubectl create -f mysql-slave-svc.yaml
验证服务是否正常
[root@cs100 mysql5.7]# kubectl get pod -n mysql-space
NAME READY STATUS RESTARTS AGE
mysql-master-rc-fnr98 0/1 ContainerCreating 0 38s
mysql-slave-rc-mrwhd 0/1 ContainerCreating 0 35s
[root@cs100 mysql5.7]# kubectl get pods -n mysql-space -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-master-rc-fnr98 1/1 Running 0 7m19s 10.42.0.29 k3s-master <none> <none>
mysql-slave-rc-mrwhd 1/1 Running 0 7m16s 10.42.0.30 k3s-master <none> <none>
进入master给slave库授权
[root@cs100 mysql5.7]# kubectl exec -ti -n mysql-space mysql-master-rc-fnr98 -- mysql -u root -pmysql123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.33-log MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> GRANT REPLICATION SLAVE ON *.* to 'repl'@'%' IDENTIFIED by 'repl123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 437 | | mysql | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)
mysql> exit
Bye
进入slave库启动同步功能
[root@cs100 ~]# kubectl get svc -A
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 26h
kube-system kube-dns ClusterIP 10.43.0.10 <none> 53/UDP,53/TCP,9153/TCP 26h
kube-system metrics-server ClusterIP 10.43.132.150 <none> 443/TCP 26h
kube-system traefik-prometheus ClusterIP 10.43.14.176 <none> 9100/TCP 26h
default seata-server NodePort 10.43.231.116 <none> 8091:30091/TCP 23h
kube-system traefik LoadBalancer 10.43.217.228 192.168.72.100 80:31649/TCP,443:30378/TCP 26h
mysql-space mysql-master-svc NodePort 10.43.40.10 <none> 3306:32306/TCP 161m
mysql-space mysql-slave-svc NodePort 10.43.65.118 <none> 3306:32307/TCP 160m
[root@cs100 ~]#
启动同步功能
master_host='mysql-master-svc.mysql-space'
:为主mysql的mysql-master-svc.yaml
中
“服务名.命名空间”
[root@cs100 mysql5.7]# kubectl exec -ti -n mysql-space mysql-slave-rc-mrwhd -- mysql -u root -pmysql123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.33 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> change master to master_host='mysql-master-svc.mysql-space',master_user='repl',master_password='repl123',master_log_file='mysql-bin.000003',master_log_pos=1032 ;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql>
查看状态是否正常,如果没有错误信息,那就ok
从库报了这样的一个错误:
Slave_IO_Running: No
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: mysql-master-svc.mysql-space
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 1032
Relay_Log_File: mysql-slave-rc-mrwhd-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: No
Slave_SQL_Running: Yes
...
Last_IO_Errno: 1236
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'
解决方法:
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql> reset slave;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
再次查看:
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: mysql-master-svc.mysql-space
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 1608
Relay_Log_File: mysql-slave-rc-mrwhd-relay-bin.000005
Relay_Log_Pos: 1821
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
验证同步功能实现
可以在master库中创建表,验证slave库中是否有
Pod的扩容
Kubernetes不支持停止/暂停Pod当前状态并在需要时恢复.但是,您仍然可以通过没有有效的部署(将副本数设置为0)来实现,查看帮助:kubectl scale --help
执行后数据就没有
kubectl scale rc -n mysql-space mysql-master-rc --replicas=0
扩容
kubectl scale rc -n mysql-space mysql-master-rc --replicas=1