HAProxy负载均衡

安装haproxy

https://github.com/haproxy/haproxy/archive/refs/tags/v2.3.0.tar.gz

//关闭防火墙selinux
[root@LB ~]# systemctl stop firewalld && setenforce 0

//安装httpd
[root@RS1 ~]# yum -y install httpd
[root@RS1 ~]# systemctl enable --now  httpd
[root@RS1 ~]# echo RS1> /var/www/html/index.html
[root@RS2 ~]# yum -y install httpd
[root@RS2 ~]# systemctl enable --now  httpd
[root@RS2 ~]# echo RS2> /var/www/html/index.html

//解压并安装相关依赖
[root@DR ~]# tar xf haproxy-2.3.0.tar.gz 
[root@DR ~]# cd haproxy-2.3.0/
[root@DR haproxy-2.3.0]# yum -y install gcc pcre-devel bzip2-devel openssl-devel systemd-devel 
[root@DR haproxy-2.3.0]# useradd -r -M -s /sbin/nologin haproxy

安装与配置

查看安装相关配置的文档

[root@DR haproxy-2.3.0]# less INSTALL

编译并安装

[root@DR haproxy-2.3.0]# make clean
//-j 多核加速编译 
[root@DR haproxy-2.3.0]# make -j $(nproc) TARGET=linux-glibc USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE=1 USE_SYSTEMD=1

[root@DR haproxy-2.3.0]# make install PREFIX=/usr/local/haproxy

[root@DR haproxy]# cp sbin/haproxy /usr/sbin/

内核参数配置

[root@DR haproxy]# echo 'net.ipv4.ip_forward = 1'>> /etc/sysctl.conf
[root@DR haproxy]# echo 'net.ipv4.ip_nonlocal_bind = 1'>> /etc/sysctl.conf
[root@DR haproxy]# sysctl -p
net.ipv4.ip_forward = 1
net.ipv4.ip_nonlocal_bind = 1

haproxy生成配置文件

[root@LB ~]# mkdir /etc/haproxy
[root@LB ~]# vim /etc/haproxy/haproxy.cfg
[root@LB ~]# vim /etc/haproxy/haproxy.cfg
#--------------全局配置----------------
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.100.141:80 check inter 2000 fall 5
    server web02 192.168.100.142:80 check inter 2000 fall 5
    #server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
//重启服务
[root@LB ~]# systemctl restart haproxy
[root@LB ~]# ss -antl
State     Recv-Q     Send-Q         Local Address:Port         Peer Address:Port    
LISTEN    0          128                  0.0.0.0:80                0.0.0.0:*       
LISTEN    0          128                  0.0.0.0:22                0.0.0.0:*       
LISTEN    0          128                  0.0.0.0:8189              0.0.0.0:*       
LISTEN    0          128                     [::]:22                   [::]:* 

访问测试

[root@LB ~]# curl 192.168.100.144
RS1
[root@LB ~]# curl 192.168.100.144
RS2

 

posted @ 2021-05-10 00:46  cbcbage  阅读(73)  评论(0)    收藏  举报