环境:redhat 7
IP: 192.168.247.140
准备工作:
关闭iptables selinux
安装:
[root@ha01 ~]# yum install -y haproxy# 版本[root@ha01 ~]# haproxy -vHA-Proxy version 1.5.18 2016/05/10Copyright 2000-2016 Willy Tarreau <willy@haproxy.org>
配置:
mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak
vi /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
daemon # 后台运行
defaults
mode tcp
log global
option dontlognull
option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器
retries 3 #三次连接失败就认为是服务器不可用,也可以通过后面设置
timeout http-request 10s
timeout queue 1m
timeout connect 10s #连接超时
timeout client 1m #客户端超时
timeout server 1m #服务器超时
timeout http-keep-alive 10s
timeout check 10s # 心跳检测超时
maxconn 4096 #默认的最大连接数
listen status
mode http
bind :8080
stats enable # 开启监控
stats hide-version ## 隐藏版本号
stats uri /haproxyadmin?stats ## http://ip:8080/haproxyadmin?stats
stats realm Haproxy\ Statistics
stats auth admin:admin # 用户名密码
stats admin if TRUE #是否开启管理功能,必须跟stats auth命令
frontend main
bind *:3306
default_backend mysql
backend mysql
balance leastconn # 表示最少连接者先处理
server s1 192.168.247.122:3306 check port 3306 maxconn 3000 #负载的机器,负载的机器可以有多个,往下排列即可
server s2 192.168.247.123:3306 check port 3306 maxconn 3000
修改日志系统:
vi /etc/rsyslog.conf
# Provides UDP syslog reception//去掉下面两行注释,开启UDP监听
$ModLoad imudp
$UDPServerRun 514
local2.* /var/log/haproxy.log // 结尾添加此行
启动服务:
# 重启
systemctl restart rsyslog
# 启动
systemctl start haproxy
# 查看日志
tailf /var/log/haproxy.log
查看状态:
[root@ha01 ~]# systemctl status haproxy
● haproxy.service - HAProxy Load Balancer
Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled; vendor preset: disabled)
Active: active (running) since 二 2021-07-27 22:21:31 CST; 4s ago
Main PID: 28200 (haproxy-systemd)
CGroup: /system.slice/haproxy.service
├─28200 /usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy....
├─28201 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/hapr...
└─28202 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/hapr...
7月 27 22:21:31 ha01 systemd[1]: Started HAProxy Load Balancer.
7月 27 22:21:31 ha01 systemd[1]: Starting HAProxy Load Balancer...
7月 27 22:21:31 ha01 haproxy-systemd-wrapper[28200]: haproxy-systemd-wrapper...
Hint: Some lines were ellipsized, use -l to show in full.
测试连接:
[root@mysql2 ~]# for (( i=0;i<10;i++ ));
> do
> mysql -uroot -proot1234 -h192.168.247.140 -P3306 -e "select @@server_id"
> done
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| @@server_id |
+-------------+
| 247122 |
+-------------+
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| @@server_id |
+-------------+
| 247123 |
+-------------+
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| @@server_id |
+-------------+
| 247122 |
+-------------+
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| @@server_id |
+-------------+
| 247123 |
+-------------+
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| @@server_id |
+-------------+
| 247122 |
+-------------+
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| @@server_id |
+-------------+
| 247123 |
+-------------+
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| @@server_id |
+-------------+
| 247122 |
+-------------+
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| @@server_id |
+-------------+
| 247123 |
+-------------+
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| @@server_id |
+-------------+
| 247122 |
+-------------+
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| @@server_id |
+-------------+
| 247123 |
+-------------+
