haproxy

Posted liping0826

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了haproxy相关的知识,希望对你有一定的参考价值。

1.haproxy安装

[root@node1 ~]# yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel
[root@node1 ~]# useradd -r -M -s /sbin/nologin haproxy
[root@node1 ~]# tar xf haproxy-2.1.3.tar.gz
[root@node1 ~]# cd haproxy-2.1.3
[root@node1 ~]# make clean
[root@node1 ~]# make -j $(grep ‘processor‘ /proc/cpuinfo |wc -l)  TARGET=linux-glibc  USE_OPENSSL=1  USE_ZLIB=1  USE_PCRE=1  USE_SYSTEMD=1
[root@node1 ~]# make install PREFIX=/usr/local/haproxy
[root@node1 ~]# cp haproxy  /usr/sbin/

2.配置各个负载的内核参数

[root@node1 ~]# echo ‘net.ipv4.ip_nonlocal_bind = 1‘ >>  /etc/sysctl.conf
[root@node1 ~]# echo ‘net.ipv4.ip_forward = 1‘ >> /etc/sysctl.conf
[root@node1 ~]# sysctl  -p 
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1

3.提供配置文件

[root@node1 ~]# mkdir /etc/haproxy
[root@node1 ~]# cat /etc/haproxy/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 192.168.249.129: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.249.129:80 check inter 2000 fall 5
    #server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5

4.haproxy.service文件编写

[root@node1 ~]# cat > /usr/lib/systemd/system/haproxy.service <<EOF
[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 $MAINPID

[Install]
WantedBy=multi-user.target
EOF
[root@node1 ~]# systemctl daemon-reload

5.启用日志并启动服务

[root@node1 ~]# vim /etc/rsyslog.conf
local0.*                        /var/log/haproxy.log

[root@node1 ~]# systemctl restart rsyslog
[root@node1 ~]# systemctl restart haproxy
[root@node1 ~]# ss -antl
State      Recv-Q Send-Q    Local Address:Port                   Peer Address:Port              
LISTEN     0      128                   *:80                                *:*                  
LISTEN     0      128                   *:22                                *:*                  
LISTEN     0      128                   *:4505                              *:*                  
LISTEN     0      100           127.0.0.1:25                                *:*                  
LISTEN     0      128                   *:4506                              *:*                  
LISTEN     0      128     192.168.249.129:8189                              *:*                  
LISTEN     0      128                  :::22                               :::*                  
LISTEN     0      100                 ::1:25                               :::*        

6.web界面

技术图片
技术图片


以上是关于haproxy的主要内容,如果未能解决你的问题,请参考以下文章

haproxy代码框架分析

python-haproxy作业讲解视频总结

haproxy-----网关

haproxy

HAproxy指南之haproxy重定向配置(案例篇)

HAProxy