RHEL8 Haproxy的读写分离
接上篇做双主https://www.cnblogs.com/tingxin/p/14290434.html
下载安装HAProxy
mkdir /HAProxy /usr/local/haproxy wget http://www.haproxy.org/download/2.3/src/haproxy-2.3.4.tar.gz tar -zxvf /mnt/hgfs/HAProxy/haproxy-2.3.4.tar.gz -C /HAProxy make TARGET=generic PREFIX=/usr/local/haproxy make install PREFIX=/usr/local/haproxy useradd haproxy
初始化启动参数文件
cp -p /HAProxy/haproxy-2.3.4/examples/haproxy.init /etc/init.d/haproxy.init chown haproxy:haproxy /etc/init.d/haproxy.init mkdir /usr/local/haproxy/{run,etc} chown -R haproxy:haproxy /usr/local/haproxy/
happroxy,init
[root@win87 examples]# cat /etc/init.d/haproxy.init |grep -v ^$|grep -v ^# config: /usr/local/haproxy/etc/haproxy.cfg pidfile: /var/run/haproxy.pid if [ -f /etc/init.d/functions ]; then . /etc/init.d/functions elif [ -f /etc/rc.d/init.d/functions ] ; then . /etc/rc.d/init.d/functions else exit 0 fi . /etc/sysconfig/network [ ${NETWORKING} = "no" ] && exit 0 BASENAME=`basename $0` if [ -L $0 ]; then BASENAME=`find $0 -name $BASENAME -printf %l` BASENAME=`basename $BASENAME` fi BIN=/usr/local/haproxy/sbin/$BASENAME CFG=/etc/$BASENAME/$BASENAME.cfg [ -f $CFG ] || exit 1 PIDFILE=/var/run/$BASENAME.pid LOCKFILE=/var/lock/subsys/$BASENAME RETVAL=0 start() { quiet_check if [ $? -ne 0 ]; then echo "Errors found in configuration file, check it with '$BASENAME check'." return 1 fi echo -n "Starting $BASENAME: " daemon $BIN -D -f $CFG -p $PIDFILE RETVAL=$? echo [ $RETVAL -eq 0 ] && touch $LOCKFILE return $RETVAL } stop() { echo -n "Shutting down $BASENAME: " killproc $BASENAME -USR1 RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -f $LOCKFILE [ $RETVAL -eq 0 ] && rm -f $PIDFILE return $RETVAL } restart() { quiet_check if [ $? -ne 0 ]; then echo "Errors found in configuration file, check it with '$BASENAME check'." return 1 fi stop start } reload() { if ! [ -s $PIDFILE ]; then return 0 fi quiet_check if [ $? -ne 0 ]; then echo "Errors found in configuration file, check it with '$BASENAME check'." return 1 fi $BIN -D -f $CFG -p $PIDFILE -sf $(cat $PIDFILE) } check() { $BIN -c -q -V -f $CFG } quiet_check() { $BIN -c -q -f $CFG } rhstatus() { status $BASENAME } condrestart() { [ -e $LOCKFILE ] && restart || : } case "$1" in start) start ;; stop) stop ;; restart) restart ;; reload) reload ;; condrestart) condrestart ;; status) rhstatus ;; check) check ;; *) echo $"Usage: $BASENAME {start|stop|restart|reload|condrestart|status|check}" exit 1 esac exit $?
haproxy 参数文件
cat /usr/local/haproxy/etc/haproxy.cfg global log 127.0.0.1 local2 # 日志定义级别 chroot /usr/local/haproxy # 当前工作目录 pidfile /var/run/haproxy.pid # 进程id maxconn 4000 # 最大连接数 user haproxy # 运行改程序的用户 group haproxy daemon # 后台形式运行 stats socket /usr/local/haproxy/stats defaults mode tcp # haproxy运行模式(http | tcp | health) 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 600 # 最大连接数 listen stats # 配置haproxy状态页(用来查看的页面) mode http bind :8888 stats enable stats hide-version # 隐藏haproxy版本号 stats uri /haproxyadmin?stats # 一会用于打开状态页的uri stats realm Haproxy\ Statistics # 输入账户密码时的提示文字 stats auth admin:admin # 用户名:密码 frontend read bind *:3307 # 使用3307端口。监听前端端口(表示任何ip访问3307端口都会将数据轮番转发到mysql服务器群组中) default_backend mysql_read # 后端服务器组名 backend mysql_read balance roundrobin # 使用轮询方式调度 server win89 win89.inno.com:3308 check port 3308 maxconn 300 server win90 win90.inno.com:3390 check port 3390 maxconn 300 frontend write bind *:3308 # 使用3308端口。监听前端端口(表示任何ip访问3308端口都会将数据轮番转发到mysql服务器群组中) default_backend mysql_write # 后端服务器组名 backend mysql_write server win88 win88.inno.com:3308 check port 3308 maxconn 300
配置日志级别
[root@win87 etc]# cat /etc/rsyslog.conf |grep -v ^#|grep -v ^$ module(load="imuxsock" # provides support for local system logging (e.g. via logger command) SysSock.Use="off") # Turn off message reception via local log socket; # local messages are retrieved through imjournal now. module(load="imjournal" # provides access to the systemd journal StateFile="imjournal.state") # File to store the position in the journal module(load="imudp") # needs to be done just once input(type="imudp" port="514") global(workDirectory="/var/lib/rsyslog") module(load="builtin:omfile" Template="RSYSLOG_TraditionalFileFormat") include(file="/etc/rsyslog.d/*.conf" mode="optional") *.info;mail.none;authpriv.none;cron.none /var/log/messages authpriv.* /var/log/secure mail.* -/var/log/maillog cron.* /var/log/cron *.emerg :omusrmsg:* uucp,news.crit /var/log/spooler local7.* /var/log/boot.log local3.* /var/log/haproxy.log local0.* /var/log/haproxy.log
配置开机自启
echo "PATH=$PATH:/usr/local/haproxy/sbin" > /etc/profile.d/haproxy.sh chmod +x /etc/profile.d/haproxy.sh cp -p /etc/init.d/haproxy.init /etc/init.d/haproxy chkconfig --add haproxy
检查开机自启
[root@win87 etc]# chkconfig --list Note: This output shows SysV services only and does not include native systemd services. SysV configuration data might be overridden by native systemd configuration. If you want to list systemd services use 'systemctl list-unit-files'. To see services enabled on particular target use 'systemctl list-dependencies [target]'. haproxy 0:off 1:off 2:off 3:off 4:off 5:off 6:off vmware-tools 0:off 1:off 2:on 3:on 4:on 5:on 6:off
启动haproxy
. /etc/profile.d/haproxy.sh haproxy -f /usr/local/haproxy/etc/haproxy.cfg
测试读
[root@win87 etc]# mysql -uroot -p'12345678' -h'192.168.68.87' -P3307 -e "show variables like 'server_id'" mysql: [Warning] Using a password on the command line interface can be insecure. +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 3390 | +---------------+-------+ [root@win87 etc]# mysql -uroot -p'12345678' -h'192.168.68.87' -P3307 -e "show variables like 'server_id'" mysql: [Warning] Using a password on the command line interface can be insecure. +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 3328 | +---------------+-------+ [root@win87 etc]# mysql -uroot -p'12345678' -h'192.168.68.87' -P3307 -e "show variables like 'server_id'" mysql: [Warning] Using a password on the command line interface can be insecure. +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 3390 | +---------------+-------+ [root@win87 etc]# mysql -uroot -p'12345678' -h'192.168.68.87' -P3307 -e "show variables like 'server_id'" mysql: [Warning] Using a password on the command line interface can be insecure. +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 3328 | +---------------+-------+
测试写
[root@win87 etc]# mysql -uroot -p'12345678' -h'192.168.68.87' -P3308 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 3308 |
+---------------+-------+
[root@win87 etc]# mysql -uroot -p'12345678' -h'192.168.68.87' -P3308 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 3308 |
+---------------+-------+
[root@win87 etc]# mysql -uroot -p'12345678' -h'192.168.68.87' -P3308 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 3308 |
+---------------+-------+
[root@win87 etc]# mysql -uroot -p'12345678' -h'192.168.68.87' -P3308 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 3308 |
+---------------+-------+
后台监控
http://192.168.68.87:8888/haproxyadmin?stats