haproxy

Posted 卑微小胡

tags:

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

haproxy

haproxy安装

[root@localhost ~]# yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel
#安装过程省略
[root@localhost ~]# useradd -r -M -s /sbin/nologin haproxy
[root@localhost ~]# tar xf haproxy-v2.3.0.tar.gz
[root@localhost ~]# cd haproxy-2.3.0/
[root@localhost haproxy-2.3.0]# make clean
[root@localhost haproxy-2.3.0]# make -j $(grep 'processor' /proc/cpuinfo |wc -l)  \\
> TARGET=linux-glibc  \\
> USE_OPENSSL=1  \\
> USE_ZLIB=1  \\
> USE_PCRE=1  \\
> USE_SYSTEMD=1
make install PREFIX=/usr/local/haproxy
[root@localhost haproxy-2.3.0]# cp haproxy  /usr/sbin/

配置各个负载的内核参数

[root@localhost haproxy-2.3.0]# echo 'net.ipv4.ip_nonlocal_bind = 1' >>  /etc/sysctl.conf
[root@localhost haproxy-2.3.0]# echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
[root@localhost haproxy-2.3.0]# sysctl  -p 
net.ipv4.ip_forward = 1
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1

提供配置文件

[root@localhost haproxy-2.3.0]# mkdir /etc/haproxy
[root@localhost haproxy-2.3.0]# 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.130:80 check inter 2000 fall 5
>     #server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
> EOF
[root@localhost haproxy-2.3.0]# 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

haproxy.service文件编写

[root@localhost haproxy-2.3.0]# 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@localhost haproxy-2.3.0]# systemctl daemon-reload

启用日志

[root@localhost haproxy-2.3.0]# vim /etc/rsyslog.conf 
local0.*                        /tmp/haproxy.log
[root@localhost haproxy-2.3.0]# systemctl restart rsyslog

启动服务

[root@localhost haproxy-2.3.0]# systemctl enable --now haproxy 
[root@localhost haproxy-2.3.0]# ss -anlt
State   Recv-Q  Send-Q   Local Address:Port    Peer Address:Port  Process  
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              [::]:*              
LISTEN  0       80                   *:3306               *:*   

测试

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

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

haproxy代码框架分析

python-haproxy作业讲解视频总结

haproxy-----网关

haproxy

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

HAProxy