haproxy

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   //用户名和密码

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值