haproxy

[root@RS1 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@RS1 ~]# setenforce 0
[root@RS1 ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RS1 ~]# dnf -y install httpd
[root@RS1 ~]# echo "RS1" > /var/www/html/index.html
[root@RS1 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@RS2 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@RS2 ~]# setenforce 0
[root@RS2 ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RS2 ~]# dnf -y install httpd
[root@RS2 ~]# echo "RS2" > /var/www/html/index.html
[root@RS2 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

负载均衡服务器配置

[root@shujiaming~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@shujiaming~]# setenforce 0
[root@shujiaming~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@shujiaming~]# useradd -rMs /sbin/nologin haproxy
[root@shujiaming~]# dnf -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel
[root@shujiaming~]# wget https://srcfedoraprojectorg/repo/pkgs/haproxy/haproxy-26targz/sha512/7bb7bfb566bbdac61d712bc51c5e8d5a5126ed8827d699b14a2f4562b3bd57f8f21344d95541cee812c66135cca88278afe2f277ff1399e461ddb7bb/haproxy-26targz
[root@shujiaming~]# tar -xf haproxy-2.6.0.tar.gz
[root@shujiaming~]# cd haproxy-2.6.0/
[root@shujiaminghaproxy-2.6.0]# make -j $(grep 'processor' /proc/cpuinfo |wc -l)  \
> TARGET=linux-glibc  \
> USE_OPENSSL=1  \
> USE_ZLIB=1  \
> USE_PCRE=1  \
> USE_SYSTEMD=1
[root@shujiaminghaproxy-2.6.0]# make install PREFIX=/usr/local/haproxy
[root@shujiaminghaproxy-2.6.0]# ls
addons  BRANCHES   CONTRIBUTING  doc       haproxy  INSTALL  MAINTAINERS  README     scripts  SUBVERS  VERDATE
admin   CHANGELOG  dev           examples  include  LICENSE  Makefile     reg-tests  src      tests    VERSION
[root@shujiaminghaproxy-2.6.0]# cp haproxy /usr/sbin/
[root@shujiaming~]# vim /etc/sysctlconf
[root@shujiaming~]# cat /etc/sysctlconf
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
[root@shujiaming~]# mkdir /etc/haproxy
[root@shujiaming~]# vim /etc/haproxy/haproxycfg
global
    daemon
    maxconn 256

defaults
    mode http
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

frontend http-in
    bind *:80
    default_backend servers

backend servers
    server web01 192.168.159.167:80
    server web02 192.168.159.168:80
[root@shujiaming~]# vim /usr/lib/systemd/system/haproxyservice
[root@shujiaming~]# cat /usr/lib/systemd/system/haproxyservice
[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
[root@shujiaming~]#
[root@shujiaming ~]# systemctl daemon-reload 
[root@shujiaming ~]# systemctl enable --now haproxy.service 

[root@client ~]# curl 192.168.159.129
RS1
[root@client ~]# curl 192.168.159.129
RS2
[root@client ~]# curl 192.168.159.129
RS1
[root@client ~]# curl 192.168.159.129
RS2

网页测试

[root@shujiaming~]# vim /etc/haproxy/haproxy.cfg
[root@shujiaming~]# cat /etc/haproxy/haproxy.cfg
global
    log 127.0.0.1 local0  info
    maxconn 20480
    pidfile /var/run/haproxy.pid
    user haproxy
    group haproxy
    daemon

defaults
    mode http
    log global
    option dontlognull
    option httpclose
    option httplog
    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 /admin        //访问网页后缀URL
    stats realm Haproxy\ Statistics
    stats auth admin:admin              //用户名和密码
    stats admin if TRUE
    stats refresh 30s
#---------------web设置-----------------------
listen webcluster
    bind 0.0.0.0:80
    mode http
    log global
    maxconn 3000
    balance roundrobin
    cookie SESSION_COOKIE insert indirect nocache
    server web01 192.168.159.167:80
    server web02 192.168.159.168:80
[root@shujiaming~]#
[root@shujiaming~]# vim /etc/rsyslog.conf
local0.*        /var/log/haproxy.log
[root@shujiaming~]# systemctl restart rsyslog.service
[root@shujiaming~]# systemctl restart haproxy.service

posted @ 2022-09-28 12:27  舒少`  阅读(37)  评论(0)    收藏  举报