haproxy安装
//解压haproxy安装包
[root@DR ~]# tar xf haproxy-2.1.3.tar.gz
[root@DR ~]# ls
anaconda-ks.cfg haproxy-2.1.3 haproxy-2.1.3.tar.gz
//安装依赖包
[root@DR ~]# yum -y install gcc gcc-c++ pcre-devel openssl-devel systemd-devel
//编译安装haproxy
[root@DR haproxy-2.1.3]# make -j $(nproc) TARGET=linux-glibc USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE=1 USE_SYSTEMD=1
[root@DR haproxy-2.1.3]# make install
[root@DR ~]# which haproxy
/usr/local/sbin/haproxy
[root@DR ~]#
//创建haproxy用户
[root@DR ~]# useradd -r -M -s /sbin/nologin haproxy
//开启转发功能
[root@DR ~]# vi /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind = 1 //添加这2行
net.ipv4.ip_forward = 1
[root@DR ~]# vi /etc/sysctl.conf
[root@DR ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
[root@DR ~]#
//提供配置文件
[root@DR ~]# mkdir /etc/haproxy
[root@DR ~]# cat > /etc/haproxy/haproxy.cfg <<EOF
> #--------------全局配置----------------
> global
> log 127.0.0.1 local0 info
> #log loghost local0 info
> maxconn 20480
> #chroot /usr/local/haproxy
> pidfile /var/run/haproxy.pid
> #maxconn 4000
> user haproxy
> group haproxy
> daemon
> #---------------------------------------------------------------------
> #common defaults that all the 'listen' and 'backend' sections will
> #use if not designated in their block
> #---------------------------------------------------------------------
> defaults
> mode http
> log global
> option dontlognull
> option httpclose
> option httplog
> #option forwardfor
> option redispatch
> balance roundrobin
> timeout connect 10s
> timeout client 10s
> timeout server 10s
> timeout check 10s
> maxconn 60000> retries 3
> #--------------统计页面配置------------------
> listen admin_stats
> bind 0.0.0.0:8189
> stats enable
> mode http
> log global
> stats uri /haproxy_stats
> stats realm Haproxy\ Statistics
> stats auth admin:admin
> #stats hide-version
> stats admin if TRUE
> stats refresh 30s
> #---------------web设置-----------------------
> listen webcluster
> bind 0.0.0.0:80
> mode http
> #option httpchk GET /index.html
> log global
> maxconn 3000
> balance roundrobin
> cookie SESSION_COOKIE insert indirect nocache
> server web01 192.168.20.100:80 check inter 2000 fall 5
> server web02 192.168.20.120:80 check inter 2000 fall 5
> EOF
//haproxy.service文件编写
[root@DR ~]# cat > /usr/lib/systemd/system/haproxy.service <<EOF
> [Unit]
> Description=HAProxy Load Balancer
> After=syslog.target network.target
>
> [Service]
> ExecStartPre=/usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
> ExecStart=/usr/local/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
> ExecReload=/bin/kill -USR2 $MAINPID
>
> [Install]
> WantedBy=multi-user.target
> EOF
[root@DR ~]# systemctl daemon-reload
[root@DR ~]# systemctl enable --now haproxy
[root@DR ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 *:8189 *:*
LISTEN 0 128 :::22 :::*
[root@RS1 ~]# yum -y install httpd
[root@RS1 ~]# cd /var/www/html/
[root@RS1 html]# echo "RS1" > index.html
[root@RS1 html]# cat index.html
RS1
[root@RS1 html]# systemctl restart httpd
[root@RS2 ~]# yum -y install httpd
[root@RS2 ~]# cd /var/www/html/
[root@RS2 html]# echo 'RS2' > index.html
[root@RS2 html]# cat index.html
RS2
[root@RS2 html]# systemctl restart httpd
stats uri /haproxy_stats
stats auth admin:admin //用户名和密码