部署haproxy
安装依赖
yum install -y gcc pcre pcre-devel openssl openssl-devel
创建依赖账号,并禁止账号登录
useradd -M -s /sbin/nologin -u 1000 haproxy
编译安装haproxy
cd /usr/local/src/ wget https://www.haproxy.org/download/1.7/src/haproxy-1.7.11.tar.gz tar xf haproxy-1.7.11.tar.gz cd haproxy-1.7.11/ make TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 PREFIX=/usr/local/haproxy make install PREFIX=/usr/local/haproxy /usr/local/haproxy/sbin/haproxy -v
复制haproxy启动脚本到启动目录下
cp /usr/local/src/haproxy-1.7.11/haproxy /usr/sbin/ cp /usr/local/src/haproxy-1.7.11/haproxy-systemd-wrapper /usr/sbin
配置成systemctl可以管理
vim /usr/lib/systemd/system/haproxy.service
[Unit] Description=HAProxy Load Balancer After=syslog.target network.target [Service] EnvironmentFile=/etc/sysconfig/haproxy ExecStart=/usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid $OPTIONS ExecReload=/bin/kill -USR2 $MAINPID [Install] WantedBy=multi-user.target
编辑配置文件
cat /etc/sysconfig/haproxy
# Add extra options to the haproxy daemon here. This can be useful for # specifying multiple configuration files with multiple -f options. # See haproxy(1) for a complete list of options. OPTIONS=""
编辑主配置文件
mkdir /etc/haproxy/
vim /etc/haproxy/haproxy.cfg
global maxconn 100000 chroot /usr/local/haproxy uid 1000 gid 1000 daemon nbproc 1 pidfile /usr/local/haproxy/run/haproxy.pid log 127.0.0.1 local6 info defaults option http-keep-alive maxconn 100000 mode http timeout connect 300000ms timeout client 300000ms timeout server 300000ms listen stats mode http bind 0.0.0.0:8888 # 访问的端口 stats enable log global stats uri /haproxy-status #状态地址 stats auth haproxy:123456 #登录账号和密码 #frontend web_port frontend frontend_www_example_com bind 10.0.0.80:80 mode http option httplog log global default_backend backend_www_example_com backend backend_www_example_com option forwardfor header X-REAL-IP option httpchk HEAD / HTTP/1.0 balance source server web-node1 10.0.0.80:8080 check inter 2000 rise 30 fall 15 server web-node2 10.0.0.81:8080 check inter 2000 rise 30 fall 15
创建PID文件存放路径
mkdir /usr/local/haproxy/run/
启动服务
systemctl enable haproxy.service systemctl start haproxy.service
修改日志存放位置 vim /etc/rsyslog.conf
[root@node-1 haproxy-1.7.11]# sed -n "15,21p" /etc/rsyslog.conf $ModLoad imudp $UDPServerRun 514 # Provides TCP syslog reception $ModLoad imtcp $InputTCPServerRun 514 local6.* /var/log/haproxy/haproxy.log local6.* @@10.0.0.80:5160 [root@node-1 haproxy-1.7.11]#
创建日志存放位置并授权haproxy
mkdir /var/log/haproxy chown -R haproxy.haproxy /var/log/haproxy
重启服务生效
systemctl restart rsyslog systemctl restart haproxy.service