负载均衡---haproxy

haproxy简介

HAProxy特别适用于那些负载特大的web站点(中型企业),这些站点通常又需要会话保持或七层处理

HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。


haproxy配置

准备环境
[root@zyy180 haproxy-2.1.3]# yum -y install openssl-devel pcre-devel gcc gcc-c++ systemd-devel 




压缩软件包
[root@zyy180 ~]# tar xf haproxy-2.1.3.tar.gz 





1.创建用户
useradd -r -M -s /sbin/nologin haproxy





2.进入软件包编译安装
[root@zyy180 ~]# cd haproxy-2.1.3
[root@zyy180 haproxy-2.1.3]#make -j $(nproc) TARGET=linux-glibc USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE=1 USE_SYSTEMD=1
[root@zyy180 haproxy-2.1.3]# make install





3.配置各个负载的内核参数
[root@zyy180 haproxy-2.1.3]# echo 'net.ipv4.ip_nonlocal_bind = 1' >>  /etc/sysctl.conf
[root@zyy180 haproxy-2.1.3]# echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf

[root@zyy180 haproxy-2.1.3]# sysctl  -p 
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1





4.创建haproxy的配置文件
[root@zyy180 haproxy-2.1.3]# mkdir /etc/haproxy

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 172.16.103.253:80 check inter 2000 fall 5           ##添加主机
    server web02 172.16.103.254:80 check inter 2000 fall 5
    #server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
EOF






5.创建启动服务脚本
[root@zyy180 haproxy-2.1.3]# vim /usr/lib/systemd/system/haproxy.service

[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

[Install]
WantedBy=multi-user.target






6.启动服务
[root@zyy180 haproxy-2.1.3]# systemctl daemon-reload

[root@zyy180 haproxy-2.1.3]# systemctl enable --now haproxy
[root@zyy180 haproxy-2.1.3]# systemctl start --now haproxy
[root@zyy180 haproxy-2.1.3]# ss -anlt
State       Recv-Q Send-Q Local Address:Port                Peer Address:Port              
LISTEN      0      128                *:80                             *:*                  
LISTEN      0      128                *:22                             *:*                  
LISTEN      0      100        127.0.0.1:25                             *:*                  
LISTEN      0      128                *:8189                           *:*                  
LISTEN      0      128               :::22                            :::*                  
LISTEN      0      100              ::1:25                            :::*      

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值