欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

haproxy

程序员文章站 2022-07-09 19:38:25
[root@zyy180 ~]# tar xf haproxy-2.1.3.tar.gz [root@zyy180 haproxy-2.1.3]# yum -y install openssl-devel pcre-devel gcc gcc-c++ systemd-devel [root@zyy180 haproxy-2.1.3]#make -j $(nproc) TARGET=linux-glibc USE_ OPENSSL=1 USE_ ZLIB=1 USE_ PCRE=1 USE_ SYS...
准备环境
[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                            :::*      

haproxy

本文地址:https://blog.csdn.net/zyy130988/article/details/107589738