专业负载均衡工具—haproxy

haproxy安装与配置

1.配置网站服务

环境说明

主机名ip配置服务
yanlei192.168.30.100haproxy
vm2192.168.30.130httpd
vm3192.168.30.150httpd
1.1在vm2,vm3上配置httpd服务

vm2

[root@vm2 ~]# yum -y install httpd
[root@vm2 html]# systemctl start httpd
[root@vm2 ~]# cd /var/www/html/
[root@vm2 html]# ls
[root@vm2 html]# echo "vm2">index.html
[root@vm2 html]# ls
index.html

在这里插入图片描述

vm3

[root@vm3 html]# yum -y install httpd
[root@vm3 html]# systemctl start httpd
[root@vm3 ~]# cd /var/www/html/
[root@vm3 html]# ls
[root@vm3 html]# echo "vm3">index.html
[root@vm3 html]# ls
index.html

在这里插入图片描述

2.配置 haproxy

2.1haproxy安装
//安装依赖包
yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel
//创建haproxy用户
[root@yanlei ~]# useradd -r -M -s /sbin/nologin haproxy
[root@yanlei ~]# id haproxy
uid=997(haproxy) gid=995(haproxy) groups=995(haproxy)
//解压安装包
[root@yanlei ~]# ls
anaconda-ks.cfg  haproxy-2.1.3.tar.gz
[root@yanlei ~]# tar xf haproxy-2.1.3.tar.gz 
[root@yanlei ~]# ls
anaconda-ks.cfg  haproxy-2.1.3  haproxy-2.1.3.tar.gz
//编译安装

[root@yanlei haproxy-2.1.3]# make -j $(nproc) TARGET=linux-glibc USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE=1 USE_SYSTEMD=1
[root@yanlei haproxy-2.1.3]# make install
‘haproxy’ -> ‘/usr/local/sbin/haproxy’
‘doc/haproxy.1’ -> ‘/usr/local/share/man/man1/haproxy.1’
install: creating directory ‘/usr/local/doc’
install: creating directory ‘/usr/local/doc/haproxy’

[root@yanlei haproxy-2.1.3]# which haproxy 
/usr/local/sbin/haproxy

2.2配置haproxy
2.2.1配置各个负载的内核参数
[root@yanlei ~]# echo 'net.ipv4.ip_nonlocal_bind = 1' >>  /etc/sysctl.conf
[root@yanlei ~]# echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
[root@yanlei ~]# sysctl  -p 
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
2.2.2提供配置文件
//配置日志文件位置
[root@yanlei ~]# vim /etc/rsyslog.conf 
# Save boot messages also to boot.log
local0.*        /var/log/haproxy.log

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

2.2.3 haproxy.service文件编写
[root@yanlei ~]# cat  /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

[root@yanlei ~]# systemctl daemon-reload

2.2.4启动haproxy
[root@yanlei ~]# systemctl enable --now haproxy
Created symlink from /etc/systemd/system/multi-user.target.wants/haproxy.service to /usr/lib/systemd/system/haproxy.service.
[root@yanlei ~]# systemctl status haproxy
● haproxy.service - HAProxy Load Balancer
   Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2020-07-26 22:59:22 EDT; 15s ago
  Process: 22026 ExecStartPre=/usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q (code=exited, status=0/SUCCESS)
 Main PID: 22027 (haproxy)
   CGroup: /system.slice/haproxy.service
           ├─22027 /usr/local/sbin/haproxy -Ws -f /etc/haproxy/ha...
           └─22030 /usr/local/sbin/haproxy -Ws -f /etc/haproxy/ha...

Jul 26 22:59:22 yanlei systemd[1]: Starting HAProxy Load Balancer...
Jul 26 22:59:22 yanlei systemd[1]: Started HAProxy Load Balancer.
Jul 26 22:59:22 yanlei haproxy[22027]: [NOTICE] 207/225922 (22027...
Hint: Some lines were ellipsized, use -l to show in full.
[root@yanlei ~]# ss -tanl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128     *:8189                *:*                  
LISTEN     0      128     *:80                  *:*                  
LISTEN     0      128     *:22                  *:*                  
LISTEN     0      100    127.0.0.1:25                  *:*                  
LISTEN     0      128    :::22                 :::*                  
LISTEN     0      100       ::1:25                 :::*     

//启动日志
systemctl restart rsyslog

2.2.5 验证

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值