haproxy实现HTTP的负载均衡

生成网页

haproxy软件包下载
环境准备:

主机 IP
RS1 192.168.170.128
RS2 192.168.170.129
haproxy 192.168.170.132

配置RS1

//关闭防火墙和selinux
[root@RS1 ~]# systemctl disable --now firewalld
[root@RS1 ~]# vim /etc/selinux/config 
SELINUX=disabled

//部署httpd和网页
[root@RS1 ~]# dnf install httpd -y
[root@RS1 ~]# systemctl enable --now httpd
[root@RS1 ~]# echo 'RS1' > /var/www/html/index.html
[root@RS1 ~]# cat /var/www/html/index.html 
RS1

web网页

配置RS2

//关闭防火墙和selinux
[root@RS2 ~]# systemctl disable --now firewalld
[root@RS2 ~]# vim /etc/selinux/config 
SELINUX=disabled


//部署httpd和网页
[root@RS2 ~]# dnf install httpd -y
[root@RS2 ~]# systemctl enable --now httpd
[root@RS2 ~]# echo 'RS2' > /var/www/html/index.html
[root@RS2 ~]# cat /var/www/html/index.html 
RS2

web网页

部署haproxy

//下载软件包
[root@haproxy ~]# wget https://github.com/haproxy/haproxy/archive/refs/tags/v2.6.0.tar.gz

//安装依赖包
[root@haproxy ~]# dnf -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel

//创建haproxy用户
[root@haproxy ~]# useradd -r -M -s /sbin/nologin haproxy

//解压
[root@haproxy ~]# tar -xf v2.6.0.tar.gz
[root@haproxy ~]# ls
anaconda-ks.cfg  haproxy-2.6.0  initial-setup-ks.cfg  max  v2.6.0.tar.gz

//编译安装
[root@haproxy ~]# cd haproxy-2.6.0/
[root@haproxy ~]# make -j $(grep 'processor' /proc/cpuinfo |wc -l)  \
TARGET=linux-glibc  \
USE_OPENSSL=1  \
USE_ZLIB=1  \
USE_PCRE=1  \
USE_SYSTEMD=1
[root@haproxy haproxy-2.6.0]# make install PREFIX=/usr/local/haproxy 
[root@haproxy ~]# cd /usr/local/
[root@haproxy local]# ls
bin  etc  games  haproxy  include  lib  lib64  libexec  sbin  share  src

//设置软连接 让系统能够找到haproxy命令
[root@haproxy ~]# ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/
[root@haproxy ~]# which haproxy
/usr/sbin/haproxy

配置各个负载的内核参数

[root@haproxy ~]# echo 'net.ipv4.ip_nonlocal_bind = 1' >>  /etc/sysctl.conf
[root@haproxy ~]# echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
[root@haproxy ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1    //ip绑定 
net.ipv4.ip_forward = 1    //ip转发

提供配置文件

//创建haproxy目录
[root@haproxy ~]# mkdir /etc/haproxy
[root@haproxy ~]# cd /etc/haproxy/

//创建haproxy.cfg 文件
[root@haproxy haproxy]# cat 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.170.129:80 check inter 2000 fall 5   //RS1
    server web02 192.168.170.132:80 check inter 2000 fall 5   //RS2

haproxy.service文件编写

[root@haproxy ~]# cat /usr/lib/systemd/system/haproxy.service 
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

[Service]
ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg   -c -q
ExecStart=/usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg  -p /var/run/haproxy.pid
ExecReload=/bin/kill -USR2 

[Install]
WantedBy=multi-user.target

[root@haproxy ~]# systemctl enable --now haproxy   //启动并设置开机自启
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /usr/lib/systemd/system/haproxy.service.
[root@haproxy ~]# systemctl status haproxy.service
● haproxy.service - HAProxy Load Balancer
   Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2022-08-15 22:56:36 CST; 6s ago
  Process: 147820 ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q (code=>
 Main PID: 147822 (haproxy)
    Tasks: 2 (limit: 11070)
   Memory: 23.3M
   CGroup: /system.slice/haproxy.service
           ├─147822 /usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy>
           └─147825 /usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy>

[root@haproxy ~]# vim /etc/rsyslog.conf
local0.*                                                /var/log/haproxy.log   //添加此行
local7.*                                                /var/log/boot.log
[root@haproxy ~]# systemctl enable --now rsyslog

web页面

posted @ 2022-08-16 00:23  夏天的海  阅读(75)  评论(0)    收藏  举报