目录

Haproxy安装

haproxy安装

1. 获取安装包

  • 在https://www.haproxy.org/获取最新稳定版,目前是haproxy-2.6.2版本
  • 在https://www.lua.org/获取最新稳定版lua,目前是lua-5.4.4版本

2. 安装各依赖软件

1
yum -y install gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools vim iotop bc zip unzip zlib-devel lrzsz tree screen lsof tcpdump wget ntpdate

3. lua环境安装

1
2
3
4
5
6
yum -y install libevent-devel.x86_64  ncurses-devel.x86_64  readline-devel.x86_64    libtermcap-devel
cd /usr/local/
tar -xvf lua-5.4.4.tar.gz
cd lua-5.4.4/
make linux test
lua -v

4. 安装haproxy

  1. 创建工作目录
1
2
3
mkdir /var/lib/haproxy
mkdir /usr/local/haproxy
mkdir /etc/haproxy
  1. 编译安装
1
2
3
4
5
6
cd /usr/local
tar xf haproxy-2.6.2.tar
cd haproxy-2.6.2
make ARCH=x86_64 TARGET=linux-glibc  USE_PCRE=1  USE_OPENSSL=1  USE_ZLIB=1  USE_SYSTEMD=1  USE_CPU_AFFINITY=1  USE_LUA=1  LUA_INC=/usr/local/lua-5.4.4/src/  LUA_LIB=/usr/lua-5.4.4/src/ PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
ln -sv /usr/local/haproxy/sbin/haproxy  /usr/sbin/
  1. 创建配置文件
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
    ## vim /etc/haproxy/haproxy.cfg
    global
    maxconn 100000                                                      //最大连接数
    chroot /usr/local/haproxy                                         //锁定工作目录(安全)
    stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin   //sock文件
    uid 188                                                //指定用户uid
    gid 188                             //指定用户gid
    daemon                       //以守护进程方式运行
    pidfile /var/lib/haproxy/haproxy.pid
    log 127.0.0.1 local3 info                //日志收集,最多设置2个
     spread-checks 5                  //后端server状态检测提前或延迟(百分比)

    defaults
        option http-keep-alive    //开启与客户端会话保持
        option forwardfor     //透传客户端真实IP到后端服务器
        option redispatch        //当server id对应的服务器挂掉,强制定向到其他健康的服务器,重新派发
        maxconn 100000            //单进程最大连接数
        mode http                                //工作类型
        timeout  http-keep-alive 120s   //session 会话保持时间,范围内会转发到相同的后端服务器
        timeout connect 1000ms    //客户请求从haproxy到后端server的最长连接等待时间 (TCP握手前)
        timeout client 600ms       //客户请求从haproxy到后端server的请求处理超时时长(TCP握手后)
        timeout server 600ms     //haproxy与客户端的最长非活动时间
        timeout check 5s    //对后端服务器的默认检测超时时间
    
    listen stats
        mode http
        bind 0.0.0.0:9999
        stats enable
        log global
        stats uri /haproxy-status
        stats auth haadmin:q1w2e3r4ys
    
    listen web_port
        bind 192.168.64.100:1006  //监听地址
        mode http
        log global
        server web1 192.168.64.101:80 check inter 3000 fall 2 rise 5   //间隔3s,失败次数2次确认失败,探测连续成功次数为5次确认为成功,共耗时15s
  1. 编写启动脚本
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
## vim /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/lib/haproxy/haproxy.pid
 ExecReload=/bin/kill -USR2 $MAINPID
 
 [Install]
 WantedBy=multi-user.target
  1. 测试启动脚本
1
2
3
systemctl daemon-reload
systemctl start haproxy
ps -ef | grep haproxy