@llplmlyd
2023-06-28T08:17:11.000000Z
字数 2839
阅读 328
Linux
3.9.1 创建haproxy运行账户和组,不允许haproxy用户直接登录系统
# groupadd haproxy# useradd -g haproxy haproxy -s /bin/false
3.9.2安装依赖包
# yum install -y gcc# yum install -y openssl openssl-devel systemd-devel.x86_64
3.9.3下载编译安装Haproxy
# rz# tar zxvf haproxy-2.0.5.tar.gz# cd haproxy-2.0.5# uname -r3.10.0-957.27.2.el7.x86_64# make TARGET=linux3100 CPU=x86_64 PREFIX=/usr/local/haprpxy USE_OPENSSL=1 USE_SYSTEMD=1 USE_PCRE=1 USE_ZLIB=1# make install PREFIX=/usr/local/haproxy
3.9.4 Haproxy 目录设置
# mkdir -p /usr/local/haproxy/conf #创建配置文件目录# mkdir -p /etc/haproxy #创建配置文件目录# touch /usr/local/haproxy/conf/haproxy.cfg #创建配置文件# ln -s /usr/local/haproxy/conf/haproxy.cfg /etc/haproxy/haproxy.cfg #添加配置文件软连接# cp -r /home/service/app/haproxy-2.0.5/examples/errorfiles /usr/local/haproxy/errorfiles #拷贝错误页面# ln -s /usr/local/haproxy/errorfiles /etc/haproxy/errorfiles #添加软连接# mkdir -p /usr/local/haproxy/log #创建日志文件目录# touch /usr/local/haproxy/log/haproxy.log #创建日志文件# ln -s /usr/local/haproxy/log/haproxy.log /var/log/haproxy.log #添加软连接# ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/haproxy #添加软连接
3.9.5 手动配置haproxy.cfg
# vim /etc/haproxy/haproxy.cfg# Global settingsgloballog 127.0.0.1 local2chroot /usr/local/haproxypidfile /var/run/haproxy.pid ###haproxy的pid存放路径,启动进程的用户必须有权限访问此文件maxconn 400000 ###最大连接数user haproxygroup haproxydaemon ###创建1个进程进入deamon模式运行# turn on stats unix socketstats socket /usr/local/haproxy/stats# defaults settingsdefaultsmode tcplog globalretries 3option redispatchtimeout queue 60mtimeout client 60m #客户端超时timeout server 60m #服务器超时timeout check 120stimeout http-request 10stimeout http-keep-alive 120stimeout connect 60m #连接超时maxconn 30000listen admin_statsbind 0.0.0.0:2222mode httpoption httplog #采用 http 日志格式option dontlognull #禁用空连接记录maxconn 10 #默认的最大连接数stats enablestats refresh 30s #统计页面自动刷新时间stats uri /haproxy #统计页面 urlstats realm Haproxy #统计页面密码框上提示文本stats auth admin:pingcap123 #设置监控页面的用户和密码: admin, 可以设置多个用户名stats hide-version #隐藏统计页面上 HAProxy 的版本信息stats admin if TRUE #设置手工启动 / 禁用,后端服务器 (haproxy-1.4.9 以后版本)# tidb-cluster settingslisten tidb-clusterbind *:33066mode tcpbalance roundrobinserver tidb2-bjht8044 10.37.70.1:4000 check inter 2000 rise 2 fall 3server tidb1-bjht7600 10.37.64.2:4000 check inter 2000 rise 2 fall 3server tidb3-bjht8192 10.37.70.3:4000 check inter 2000 rise 2 fall 3
3.9.6 将haproxy注册到CentOS7的服务,启用systemctl命令控制
# vim /usr/lib/systemd/system/haproxy.service[Unit]Description=HAProxy Load BalancerAfter=syslog.target network.target[Service]ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -qExecStart=/usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pidExecReload=/bin/kill -USR2 $MAINPID[Install]WantedBy=multi-user.target
激活服务
# systemctl daemon-reload# systemctl enable haproxy# systemctl start haproxy
3.9.7 haproxy开启日志
修改/etc/rsyslog.conf:
# vim /etc/rsyslog.conf$ModLoad imudp$UDPServerRun 514local2.* /var/log/haproxy.log
修改/etc/sysconfig/rsyslog:
# vim /etc/sysconfig/rsyslogSYSLOGD_OPTIONS="-r -m 0 -c 2"
重启haproxy服务,并查看日志
# systemctl restart haproxy# systemctl restart rsyslog