Bài: Thiết lập HAPRoxy - Cân Bằng Tải MySQL 8 Group Replication Mục tiêu + Cài đặt chương trình HAProxy từ mã nguồn + Thiết lập system services HAProxy Server + Thiết lập cân bằng tải cho MySQL Group Replication 1. Cài đặt chương trình HAProxy từ mã nguồn - Kích hoạt PowerTools repo có sẵn ở trong CentOS 8 [root@ha ~]# dnf config-manager --enable PowerTools [root@ha ~]# dnf makecache [root@ha ~]# dnf install gcc openssl-devel readline-devel systemd-devel make pcre-devel tar lua lua-devel wget [root@ha ~]# wget https://www.haproxy.org/download/2.2/src/devel/haproxy-2.2-dev10.tar.gz [root@ha ~]# tar xvf haproxy-2.2-dev10.tar.gz [root@ha ~]# cd haproxy-2.2-dev10 [root@ha haproxy-2.2-dev10]# make USE_NS=1 USE_TFO=1 USE_OPENSSL=1 USE_ZLIB=1 USE_LUA=1 USE_PCRE=1 USE_SYSTEMD=1 USE_LIBCRYPT=1 USE_THREAD=1 TARGET=linux-glibc [root@ha haproxy-2.2-dev10]# make PREFIX=/opt/haproxy-2.2-dev10 install Mô tả thông số + USE_NS: Enable Linux namespace support. + USE_TFO: Enable TCP Fast Open. Kernel must be >= 3.7. + USE_OPENSSL: Self-explanatory. + USE_ZLIB: Use Zlib compression library. + USE_LUA: Self-explanatory. + USE_PCRE: Enable regex pattern library. + USE_SYSTEMD: Enable HAProxy's native systemd support. + USE_LIBCRYPT: Enable HAProxy to use encrypted passwords and hashes using the crypt(3) function. + USE_THREAD: Enable HAProxy to use multithreading model. + TARGET: Self-explanatory. - Tạo tài khoản dịch vụ [root@ha ~]# groupadd haproxy [root@ha ~]# useradd -g haproxy -d /var/lib/haproxy -s /sbin/nologin -c haproxy haproxy 2. Thiết lập System Services HAProxy Server - Tạo file haproxy-2.2-dev10.service để start/stop/restart dịch vụ HAPROXY [root@ha ~]# vim /etc/systemd/system/haproxy-2.2-dev10.service [Unit] Description=HAProxy 2.2 Dev 10 After=syslog.target network.target [Service] Type=notify EnvironmentFile=/etc/sysconfig/haproxy-2.2-dev10.service ExecStart=/opt/haproxy-2.2-dev10/sbin/haproxy -f $CONFIG_FILE -p $PID_FILE $CLI_OPTIONS ExecReload=/bin/kill -USR2 $MAINPID ExecStop=/bin/kill -USR1 $MAINPID [Install] WantedBy=multi-user.target - Đối với CentOS 8 ta cần phải tạo thêm file "/etc/sysconfig/haproxy-2.2-dev10.service" được tham chiếu từ file "/etc/systemd/system/haproxy-2.2-dev10.service" [root@ha ~]# vim /etc/sysconfig/haproxy-2.2-dev10.service #Command line options to pass to HAProxy at startup #The default is: #CLI_OPTIONS="-Ws" CLI_OPTIONS="-Ws" # Specify an alternate configuration file. The default is: #CONFIG_FILE=/etc/haproxy/haproxy-2.2-dev10.conf CONFIG_FILE=/etc/haproxy/haproxy-2.2-dev10.conf # File used to track process IDs. The default is: #PID_FILE=/var/run/haproxy-2.2-dev10.pid PID_FILE=/var/run/haproxy-2.2-dev10.pid [root@ha ~]# systemctl daemon-reload [root@ha ~]# mkdir /etc/haproxy [root@ha ~]# vim /etc/haproxy/haproxy-2.2-dev10.conf global daemon maxconn 256 user haproxy group haproxy chroot /var/lib/haproxy #Frontend mysql_cluster_frontend frontend mysql_cluster_frontend bind *:3306 mode tcp option tcplog default_backend mysql_cluster_backend #Backend mysql_cluster_backend backend mysql_cluster_backend mode tcp option tcpka balance leastconn server mysql-01 192.168.255.170:3306 check weight 1 maxconn 180 server mysql-02 192.168.255.171:3306 check weight 1 maxconn 180 Explanation: balance: This defines the destination selection policy used to select a server to route the incoming connections to. mode tcp: Galera Cluster uses TCP type of connections. option tcpka: Enables the keepalive function to maintain TCP connections. server check weight 1 – Defines the nodes you want HAProxy to use in routing connections. [root@ha ~]# systemctl restart haproxy-2.2-dev10 [root@ha ~]# systemctl stop haproxy-2.2-dev10 [root@ha ~]# systemctl start haproxy-2.2-dev10 [root@ha ~]# netstat -ltunp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 36444/sshd tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 108865/haproxy tcp6 0 0 :::22 :::* LISTEN 36444/sshd tcp6 0 0 :::9090 :::* LISTEN 1/systemd udp 0 0 127.0.0.1:323 0.0.0.0:* 1207/chronyd udp6 0 0 ::1:323 :::* 1207/chronyd [root@ha ~]#