• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
悬溺
博客园    首页    新随笔    联系   管理    订阅  订阅
haproxy

haproxy

安装haproxy

主机 IP
RS1 192.168.78.144
RS2 192.168.78.145
haproxy 192.168.78.146

RS1配置

//修改主机名
[root@localhost ~]# hostnamectl set-hostname RS1
[root@localhost ~]# bash
//关闭防火墙
[root@RS1 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RS1 ~]# setenforce 0
[root@RS1 ~]# vim /etc/selinux/config 
SELINUX=disabled
//配置httpd
[root@RS1 ~]# yum -y install httpd    //安装过程省略
[root@RS1 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@RS1 ~]# echo 'RS1' > /var/www/html/index.html
[root@RS1 ~]# cat /var/www/html/index.html 
RS1

网页测试

RS2配置

//修改主机名
[root@localhost ~]# hostnamectl set-hostname RS2
[root@localhost ~]# bash
//关闭防火墙
[root@RS2 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RS2 ~]# setenforce 0
[root@RS2 ~]# vim /etc/selinux/config 
SELINUX=disabled
//配置httpd
[root@RS2 ~]# yum -y install httpd     //安装过程省略
[root@RS2 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@RS2 ~]# echo 'RS2' > /var/www/html/index.html
[root@RS2 ~]# cat /var/www/html/index.html 
RS2

网页测试

部署haproxy

//关闭防火墙
[root@haproxy ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@haproxy ~]# setenforce 0
[root@haproxy ~]# vim /etc/selinux/config 
SELINUX=disabled 
//下载软件包
[root@haproxy ~]# wget https://github.com/haproxy/haproxy/archive/refs/tags/v2.6.0.tar.gz
[root@haproxy ~]# ls
anaconda-ks.cfg  v2.6.0.tar.gz
//安装依赖包
[root@haproxy ~]# yum install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel -y
[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  v2.6.0.tar.gz
 //编译安装
[root@haproxy haproxy-2.6.0]# 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 haproxy-2.6.0]# cd /usr/local/
[root@haproxy local]# ls
bin  etc  games  haproxy  include  lib  lib64  libexec  sbin  share  src
[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
//-p 读取
[root@haproxy ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1

修改配置文件

[root@haproxy ~]# cd /etc/haproxy/
[root@haproxy haproxy]# ls
[root@haproxy haproxy]# vim haproxy.cfg
[root@haproxy haproxy]# ls
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.78.144:80 check inter 2000 fall 5   //RS1
    server web02 192.168.78.145:80 check inter 2000 fall 5   //RS2
    #server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5

//配置haproxy.service文件
[root@haproxy ~]# vim /usr/lib/systemd/system/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 haproxy-2.6.0]# vim /etc/rsyslog.conf 
# Save boot messages also to boot.log 
local0.*        /var/log/haproxy.log    //加入这一行
local7.*        /var/log/boot.log
    
[root@haproxy ~]# systemctl enable --now  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 09:41:35 EDT; 28s ago
 Main PID: 14621 (haproxy)
    Tasks: 3 (limit: 23458)
   Memory: 31.9M
   CGroup: /system.slice/haproxy.service
           ├─14621 /usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/ru>
           └─14623 /usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/ru>
//查看端口
[root@haproxy ~]# ss -antl        //能够看到80
State     Recv-Q    Send-Q         Local Address:Port         Peer Address:Port    Process    
LISTEN    0         5                    0.0.0.0:873               0.0.0.0:*                  
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                   [::]:*     

网页测试

posted on 2022-08-16 16:55  悬溺·  阅读(51)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3