《OpenShift 4.x HOL教程汇总》
说明:本文已经在 OpenShift 4.11 环境中验证。
我们可以使用多种方式实现多实例运行 MySQL 数据库,来提升数据的访问性能或面对故障的可用性。
Percona XtraDB Cluster 使用了 Galera Replication 的复制机制,可在多个 MySQL 数据库之间实现同步复制。PXC 可通过分布式事务将对一个 MySQL 数据库实例的操作事务实施于 PXC 所有数据实例,只有所有数据库都操作成功后,数据事务才完成,这样就有效的保障了多数据库实例之间的数据的一致性。
另外,Percona XtraDB Cluster 还提供了 proxysql,它为客户端访问数据库提供负载均衡功能,这样用户访问多实例的 MySQL 集群就完全透明了。
$ oc get node -l node-role.kubernetes.io/worker=''
NAME STATUS ROLES AGE VERSION
ip-10-0-159-73.us-east-2.compute.internal Ready worker 103m v1.23.5+012e945
ip-10-0-210-245.us-east-2.compute.internal Ready worker 103m v1.23.5+012e945
ip-10-0-227-136.us-east-2.compute.internal Ready worker 102m v1.23.5+012e945
$ oc get statefulset -n mysql
NAME READY AGE
mysql-cluster1-haproxy 3/3 28m
mysql-cluster1-pxc 3/3 28m
$ oc get pvc -n mysql
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
datadir-mysql-cluster1-pxc-0 Bound pvc-507bf962-2b60-4595-ad9c-0b2b833d2d51 6Gi RWO gp2 29m
datadir-mysql-cluster1-pxc-1 Bound pvc-e276ef7b-df03-4398-9e96-9999abe355b2 6Gi RWO gp2 28m
datadir-mysql-cluster1-pxc-2 Bound pvc-5e5b243d-9d52-4d6b-933e-e1dcc6f64419 6Gi RWO gp2 27m
$ oc get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-507bf962-2b60-4595-ad9c-0b2b833d2d51 6Gi RWO Delete Bound mysql/datadir-mysql-cluster1-pxc-0 gp2 29m
pvc-5e5b243d-9d52-4d6b-933e-e1dcc6f64419 6Gi RWO Delete Bound mysql/datadir-mysql-cluster1-pxc-2 gp2 27m
pvc-e276ef7b-df03-4398-9e96-9999abe355b2 6Gi RWO Delete Bound mysql/datadir-mysql-cluster1-pxc-1 gp2 28m
$ oc get pod -n mysql
NAME READY STATUS RESTARTS AGE
mysql-cluster1-haproxy-0 2/2 Running 0 21m
mysql-cluster1-haproxy-1 2/2 Running 0 20m
mysql-cluster1-haproxy-2 2/2 Running 0 20m
mysql-cluster1-pxc-0 3/3 Running 0 21m
mysql-cluster1-pxc-1 3/3 Running 0 20m
mysql-cluster1-pxc-2 3/3 Running 0 19m
percona-xtradb-cluster-operator-648cf8bbdf-7v47l 1/1 Running 0 25m
$ oc get svc -n mysql
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql-cluster1-haproxy ClusterIP 172.30.115.92 <none> 3306/TCP,3309/TCP,33062/TCP,33060/TCP 25m
mysql-cluster1-haproxy-replicas ClusterIP 172.30.142.115 <none> 3306/TCP 25m
mysql-cluster1-pxc ClusterIP None <none> 3306/TCP,33062/TCP,33060/TCP 25m
mysql-cluster1-pxc-unready ClusterIP None <none> 3306/TCP,33062/TCP,33060/TCP 25m
percona-xtradb-cluster-operator ClusterIP 172.30.67.39 <none> 443/TCP 28m
$ PASSWORD=$(oc get secret mysql-cluster1-secrets -n mysql --template={{.data.root}} | base64 -d)
$ oc run -i --rm --tty percona-client --image=percona:8.0 --restart=Never -- mysql -h mysql-cluster1-haproxy -uroot -p${PASSWORD}
If you dont see a command prompt, try pressing enter.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3301
Server version: 8.0.27-18.1 Percona XtraDB Cluster (GPL), Release rel18, Revision ac35177, WSREP version 26.4.3
Copyright (c) 2009-2022 Percona LLC and/or its affiliates
Copyright (c) 2000, 2022, 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.
CREATE DATABASE testdb;
USE testdb;
create table test(name VARCHAR(255), PRIMARY KEY (`name`));
insert into test value('1');
insert into test value('2');
insert into test value('3');
insert into test value('4');
$ oc rsh -n mysql -c pxc mysql-cluster1-pxc-0 mysql -uroot -p${PASSWORD}
mysql> select * from testdb.test;
+------+
| name |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
+------+
4 rows in set (0.00 sec)
$ oc rsh -n mysql -c pxc mysql-cluster1-pxc-1 mysql -uroot -p${PASSWORD}
mysql> select * from testdb.test;
+------+
| name |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
+------+
4 rows in set (0.00 sec)
mysql> update testdb.test set name=33 where name=3;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
$ oc rsh -n mysql -c pxc mysql-cluster1-pxc-2 mysql -uroot -p${PASSWORD}
mysql> select * from testdb.test;
+------+
| name |
+------+
| 1 |
| 2 |
| 33 |
| 4 |
+------+
4 rows in set (0.00 sec)
mysql> delete from testdb.test where name=33;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
分别直接访问3个 MySQL 实例,执行以下命令确认 testdb.test 当年数据状态如下:
mysql> select * from testdb.test;
+------+
| name |
+------+
| 1 |
| 2 |
| 4 |
+------+
3 rows in set (0.00 sec)
如果此时 OpenShift 还有空余 worker 节点(即 worker 节点数少于运行 mysql 集群的 pxc 实例数),则可以通过修改 PerconaXtraDBCluster 集群实例中 pxc 部分的 size 参数来增加运行 mysql 的实例。
在向集群增加新实例后,确认当前数据会自动同步到新运行的 MySQL 数据库中。
视频