如何使用CentOS的Haproxy LoadBalancer设置Percona群集

时间:2020-03-05 15:31:15  来源:igfitidea点击:

我们之前显示了如何使用Haproxy设置MariaDB Galera Cluster,今天我们将使用Percona的MySQL分发进行类似的设置。
Percona是与MySQL和MongoDB具有深入专业知识的,他们为这两个数据库的存储引擎进行了自己的分布式。
今天,我们只会专注于MySQL,而不是在MongoDB提供这家。

设置主机,防火墙和存储库

首先通过设置主机文件开始。
我们有三个Percona群集节点和一个用于HAProxy的节点。
所有4个服务器上的我的主机文件都有四条线:

10.17.0.8 centos-percona01
10.17.0.9 centos-percona02
10.17.0.10 centos-percona03
10.17.0.11 centos-haproxy

接下来,允许在除HAProxy One之外的所有主机上设置防火墙。
那个将需要不同的设置,我们会这样做后者。
首先让我们在所有三个Percona节点上启动防火墙。

systemctl start firewalld

然后我们允许MySQL服务。
Pecona是MySQL发行版,所以它使用与MySQL相同的端口。

firewall-cmd --zone=public --add-service=mysql --permanent

接下来我们添加其他所需端口:

firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --zone=public --add-port=4567/tcp --permanent
firewall-cmd --zone=public --add-port=4568/tcp --permanent
firewall-cmd --zone=public --add-port=4444/tcp --permanent
firewall-cmd --zone=public --add-port=4567/udp --permanent
firewall-cmd --zone=public --add-port=9200/tcp --permanent

并重新加载防火墙

firewall-cmd --reload

完成后,接下来我们需要安装EPEL版本

yum安装涡旋释放

接下来,我们将SoCAT从EPEL存储库安装

yum install socat

然后我们删除MariaDB-LIBS,因为它与Percona冲突

yum remove mariadb-libs

安装和设置percona

我们需要添加包含Percona的存储库

yum install https://www.percona.com/redir/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm

现在我们可以安装percona集群和所有其他依赖

yum install Percona-XtraDB-Cluster-server-56 Percona-XtraDB-Cluster-client-56 Percona-XtraDB-Cluster-shared-56 percona-toolkit percona-xtrabackup Percona-XtraDB-Cluster-galera-3 rsync nc

并开始mysql.

systemctl start mysql

我们在所有MySQL服务器上执行的第一件事是运行MySQL_Secure_Installation脚本。
所以让我们这样做。

mysql_secure_installation

我们需要输入新的root密码并回答所有问题。

完成后,登录root帐户

mysql -u root -p
enter password

并创建sstuser群集

mysql> create user sstuser@'%' identified by 'strongpassword';
Query OK, 0 rows affected (0.01 sec)
mysql> grant all on *.* to sstuser@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

在此之后,我们必须停止MySQL编辑配置文件。

systemctl stop mysql
nano /etc/my.cnf

在配置中找到这些行并使它们完全相同,只需更改自己的密码并将节点名称和节点地址更改为每个服务器的主机名

wsrep_cluster_address = gcomm://centos-percona01,centos-percona02,centos-percona03
wsrep_provider = /usr/lib64/galera3/libgalera_smm.so
wsrep_slave_threads = 8
wsrep_cluster_name = Cluster Percona XtraDB
wsrep_node_name = centos-percona01
wsrep_node_address = centos-percona01
wsrep_sst_method = xtrabackup-v2
wsrep_sst_auth = sstuser:strongpassword

请注意,第一服务器(CentOS-Percona01)上的第一行可以是空的,如下所示:

wsrep_cluster_address = gcomm://

配置完成后,我们需要引导第一个节点,然后常开第二和第三个节点。

在第一台服务器上运行

systemctl start mysql@bootstrap

第二和第三次运行

systemctl start mysql

接下来,我们需要测试群集是否正常工作。

mysql -u root -p

输入密码

然后运行此命令:

SHOW STATUS LIKE 'wsrep_local_state_comment';
show global status like 'wsrep_cluster_size';

他们应该让你输出这样的输出:

使用此群集设置结束。

haproxy设置

首先,我们需要在所有群集节点上安装群集检查,以便群集可使用HAProxy提供可维护。
让我们用wget获取脚本

wget https://raw.githubusercontent.com/olafz/percona-clustercheck/master/clustercheck

需要将脚本进行可执行文件并将其移动到$Path目录之一。

chmod +x clustercheck
mv clustercheck /usr/bin/

现在我们还需要包含在xinetd包中的mysqlchk:

yum install xinetd

接下来,我们将在数据库上移动ClusterCheck用户。
我们只能在第一个节点上键入此功能

mysql -u root -p
mysql> GRANT PROCESS ON *.* TO 'clustercheckuser'@'localhost' IDENTIFIED BY 'clustercheckpassword!';
exit;

接下来,我们可以测试群集检查是否按预期工作:

[root@centos-percona01 ~]# clustercheck
HTTP/1.1 200 OK
Content-Type: text/plain
Connection: close
Content-Length: 40
Percona XtraDB Cluster Node is synced.

接下来,我们将移动到xinetd的配置,xinetd需要添加到服务列表中。

nano /etc/services

我们使用Ctrl-W寻找端口9200的部分,然后我们表示使用该端口的服务,而是添加新行。
它需要如下所示:

mysqlchk 9200/tcp # mysqlchk
#wap-wsp 9200/tcp # WAP connectionless session service
#wap-wsp 9200/udp # WAP connectionless session service

我们完成后我们保存。
请注意,除HAProxy之外的所有群集节点都需要完成此操作。

现在是时候登录我们的haporoxy服务器。
首先,我们需要备份HAProxy配置。

mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bk

然后我们将从Clean Slate中创建新的

nano /etc/haproxy/haproxy.cfg

我们可以从此复制HAProxy配置,但需要更改这三行:

server centos-percona01 10.132.84.186:3306 check port 9200 inter 12000 rise 3 fall 3
server centos-percona02 10.132.84.141:3306 check port 9200 inter 12000 rise 3 fall 3
server centos-percona03 10.132.84.67:3306 check port 9200 inter 12000 rise 3 fall 3

突出显示的部件需要使用主机名和地址更改。
接下来,我们需要在HAProxy服务器上启动防火墙,并允许我们需要使用的端口

systemctl start firewalld
firewall-cmd --permanent --add-port=9000/tcp
firewall-cmd --permanent --add-port=3030/tcp

之后我们需要重新加载防火墙

firewall-cmd --reload

最后,开始haproxy

systemctl start haproxy

设置完成了我们现在需要测试。

测试Haproxy.

让我们将浏览器指向端口9000上的Haproxy Server的公共IP地址:

所有节点都在线。
接下来,允许在HAProxy服务器上安装Percona客户端,以便尝试从那里查询群集。

yum install https://www.percona.com/redir/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm
yum install Percona-XtraDB-Cluster-client-56

让我们试着看看我们是否可以从此HAProxy服务器上查询:

mysql -u root -p -h 10.132.83.13 -P 3306 -e "select Host, User, Password from mysql.user"