《OpenShift 4.x HOL教程汇总》
说明:本文已经在 OpenShift 4.11 环境中验证。
- 创建 Template
$ oc create -f https://raw.githubusercontent.com/liuxiaoyu-git/mysql-container/master/examples/replica/mysql_replica.json
在OpenShift控制台Developer Catalog中先创建mysql项目,然后找到mysql-replication-example的Template。
根据以下配置创建 MySQL 数据库,其中项目选中 mysql。
完成后查看创建的对象。
$ oc get all -n mysql
NAME READY STATUS RESTARTS AGE
pod/mysql-master-1-deploy 0/1 Completed 0 2m21s
pod/mysql-master-1-nz75p 1/1 Running 0 2m9s
pod/mysql-slave-1-deploy 0/1 Completed 0 2m21s
pod/mysql-slave-1-kpk4r 1/1 Running 0 2m9s
NAME DESIRED CURRENT READY AGE
replicationcontroller/mysql-master-1 1 1 1 2m21s
replicationcontroller/mysql-slave-1 1 1 1 2m21s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/mysql-master ClusterIP None <none> 3306/TCP 2m22s
service/mysql-slave ClusterIP None <none> 3306/TCP 2m22s
NAME REVISION DESIRED CURRENT TRIGGERED BY
deploymentconfig.apps.openshift.io/mysql-master 1 1 1 config
deploymentconfig.apps.openshift.io/mysql-slave 1 1 1 config
- 查看PVC
$ oc get pvc -n mysql
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mysql-master Bound pvc-33fdf122-163e-43e5-8047-c86e38d9281b 1Gi RWO gp2 3m55s
mysql-slave Bound pvc-ef03875c-6eb0-4b0e-8993-84c387a02832 1Gi RWO gp2 3m55s
- 在第一个Terminal中进入运行master的pod。
$ oc rsh dc/mysql-master
- 登录MySQL,然后查看复制的Slave数据库主机地址。注意主机的IP地址是对应MySQL Slave的Pod地址。
sh-4.2$ mysql -u master -ppassword
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.26 Source distribution
mysql> SHOW SLAVE HOSTS;
+------------+-------------+------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID |
+------------+-------------+------+-----------+--------------------------------------+
| 3941291735 | 10.131.0.24 | 3306 | 1 | 850d0c61-c9b2-11ea-b3b1-0a580a83000e |
+------------+-------------+------+-----------+--------------------------------------+
1 row in set (0.00 sec)
mysql> exit
Bye
- 在Master MySQL中的 userdb 数据库中创建一个表,并添加记录。
sh-4.2$ mysql -u user -ppassword
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 8.0.26 Source distribution
mysql> use userdb;
Database changed
mysql> create table test(a char(2));
Query OK, 0 rows affected (0.03 sec)
mysql> insert into test value('1');
Query OK, 1 row affected (0.01 sec)
mysql> insert into test value('2');
Query OK, 1 row affected (0.01 sec)
mysql> select * from test;
+------+
| a |
+------+
| 2 |
+------+
1 row in set (0.00 sec)
- 在第二个Terminal中进入运行 MySQL Slave 的 pod。
$ oc rsh dc/mysql-slave
- 登录MySQL数据库,确认可以在Slave的userdb数据库的test表中看到在master节点实例上添加的记录。
sh-4.2$ mysql -u user -ppassword
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 8.0.26 Source distribution
mysql> use userdb;
Database changed
mysql> select * from test;
+------+
| a |
+------+
| 1 |
+------+
| 2 |
+------+
1 row in set (0.00 sec)
- 在第一个Terminal中执行命令,从MySQL Slave的测试表中删除一个记录。
mysql> delete from test where a='1';
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
+------+
| a |
+------+
| 2 |
+------+
- 在第二个Terminal中执行命令确认MySQL Slave的测试表也删除了对应数据记录。
mysql> select * from test;
+------+
| a |
+------+
| 2 |
+------+
参考
https://docs.openshift.com/online/pro/using_images/db_images/mysql.html
本文含有隐藏内容,请 开通VIP 后查看