赞
踩
实验机说明 | 主机名 | ip地址 | 系统 |
---|---|---|---|
负载调度器 | DR | 192.168.10.130 | CentOS-8 |
后端服务器1 | RS1 | 192.168.10.131 | CentOS-8 |
后端服务器2 | RS2 | 192.168.10.133 | CentOS-8 |
首先在DR调度机上安装依赖包
[root@DR ~]# yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel --allowerasing
创建haproxy用户
[root@DR ~]# useradd -r -M -s /sbin/nologin haproxy
[root@DR ~]# id haproxy
uid=995(haproxy) gid=992(haproxy) groups=992(haproxy)
下载haproxy
[root@DR opt~]# wget https://www.haproxy.org/download/2.7/src/haproxy-2.7.10.tar.gz
解压
[root@DR opt]# tar xf haproxy-2.7.10.tar.gz
编译安装haproxy,详细阅读INSTALL,里面有安装操作
[root@DR haproxy-2.7.10]# 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@DR haproxy-2.7.10]# vim INSTALL
编译
[root@DR opt]# cd haproxy-2.7.10
[root@DR haproxy-2.7.10]# make clean # 清除上次的make命令所产生的object文件
[root@DR haproxy-2.7.10]# make -j $(nproc) \
> TARGET=linux-glibc \
> USE_OPENSSL=1 \
> USE_PCRE=1 \
> USE_SYSTEMD=1
CC src/slz.o
CC src/ev_poll.o
CC src/ev_epoll.o
CC src/cpuset.o
.......
安装
[root@DR haproxy-2.7.10]# make install PREFIX=/usr/local/haproxy
[root@DR haproxy-2.7.10]# ls /usr/local/
bin etc games haproxy include lib lib64 libexec sbin share src
[root@DR haproxy-2.7.10]# cd /usr/local/haproxy/
[root@DR haproxy]# ls
doc sbin share
创建软连接
[root@DR haproxy]# ln -s /usr/local/haproxy/sbin/* /usr/sbin/
查看环境变量中的文件
[root@DR haproxy]# which haproxy
/usr/sbin/haproxy
查看haproxy的版本号
[root@DR haproxy]# haproxy -V
HAProxy version 2.7.10-d796057 2023/08/09 - https://haproxy.org/
Status: stable branch - will stop receiving fixes around Q1 2024.
Known bugs: http://www.haproxy.org/bugs/bugs-2.7.10.html
Running on: Linux 4.18.0-365.el8.x86_64 #1 SMP Thu Feb 10 16:11:23 UTC 2022 x86_64
.....
配置各个负载的内核参数
[root@DR ~]# echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.conf #没有配置的本地ip也能放在配置文件中
[root@DR ~]# echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf # 开启IP转发功能
使配置的参数立即生效
[root@DR ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
[root@DR ~]#
配置加入systemctl管理
[root@DR ~]# vim /usr/lib/systemd/system/haproxy.service
[root@DR ~]# cat /usr/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/run/haproxy.pid
ExecReload=/bin/kill -USR2
[Install]
WantedBy=multi-user.target
[root@DR ~]#
配置man文档
[root@DR haproxy]# cd share/
[root@DR share]# ls #这里有一个man文档但是使用不了
man
[root@DR share]# pwd
/usr/local/haproxy/share
# 编辑/etc/man_db.conf 文件,将当前路径下的man目录写入进去
[root@DR share]# vim /etc/man_db.conf
......
MANDATORY_MANPATH /usr/man
MANDATORY_MANPATH /usr/share/man
MANDATORY_MANPATH /usr/local/share/man
MANDATORY_MANPATH /usr/local/haproxy/share/man #添加这一行
然后就可以用man帮助文档查看haproxy
[root@DR share]# man haproxy
创建存放haproxy配置文件的目录
[root@DR share]# mkdir /etc/haproxy
[root@DR share]# cd /etc/haproxy
[root@DR haproxy]# ls
生成配置文件
#直接复制下面的内容,然后执行 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 RS1 192.168.10.131:80 check inter 2000 fall 5 #在这里添加两台主机的信息 server RS2 192.168.10.133:8080 check inter 2000 fall 5 #server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5 EOF
重启haproxy服务
[root@DR ~]# systemctl restart haproxy.service
创建haproxy文件开机自启
#直接复制下面的内容,然后执行
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@DR haproxy]# systemctl daemon-reload
设置开机自启haproxy
[root@DR haproxy]# systemctl enable --now haproxy
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /usr/lib/systemd/system/haproxy.service.
[root@DR haproxy]#
[root@DR haproxy]# ss -antl
# 80端口和8189端口起来了
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 [::]:*
[root@DR haproxy]#
创建日志并重启rsyslog
[root@DR ~]# vim /etc/rsyslog.conf
#进入配置文件,命令行输入/local7找到这一行
local0.* /var/log/harpoxy.log #在/local7前一行加入这一行
local7.* /var/log/boot.log
重启rsyslog
[root@DR ~]# systemctl restart rsyslog.service
[root@localhost ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]#
[root@localhost ~]# setenforce 0
[root@RS1 ~]# vim /etc/selinux/config
yum -y install httpd
systemctl enable --now httpd
[root@RS2 ~]# vim /etc/httpd/conf/httpd.conf
#Listen 12.34.56.78:80 #命令行输入/80找到这一行
Listen 8080 #添加这一行
[root@RS2 ~]# systemctl restart httpd
RS1
[root@RS1 ~]# echo "RS1" > /var/www/html/index.html
[root@RS1 ~]# cat /var/www/html/index.html
RS1
[root@RS1 ~]#
RS2
[root@RS2 ~]# echo "RS2" > /var/www/html/index.html
[root@RS2 ~]# cat /var/www/html/index.html
RS2
[root@RS2 ~]#
命令行测试
[root@DR ~]# curl http://192.168.10.130
RS1
[root@DR ~]# curl http://192.168.10.130
RS2
[root@DR ~]#
浏览器测试
刷新一下
地址栏加上8189端口号和haproxy_stats
登陆的用户名和密码都是:admin
[root@RS2 ~]# systemctl stop httpd
[root@RS2 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@RS2 ~]#
首先要,配置用于测试的https页面
然后要生成证书,并在所有后端服务器上安装证书
httpd服务端口号是443
RS1
[root@RS1 ~]# systemctl restart httpd.service
[root@RS1 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 *:443 *:*
LISTEN 0 128 *:80 *:*
[root@RS1 ~]#
RS2
[root@RS2 ssl]# systemctl restart httpd.service
[root@RS2 ssl]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 *:443 *:*
[root@RS2 ssl]#
[root@DR ~]# vim /etc/haproxy/haproxy.cfg
#在web设置下将bind后面的80端口改成443,mode后面的http改正tcp,RS1和RS2的端口号改成443
#---------------web设置-----------------------
listen webcluster
bind 0.0.0.0:443
mode tcp
#option httpchk GET /index.html
log global
maxconn 3000
balance roundrobin
cookie SESSION_COOKIE insert indirect nocache
server RS1 192.168.10.131:443 check inter 2000 fall 5
server RS2 192.168.10.133:443 check inter 2000 fall 5
#server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
[root@DR ~]# systemctl restart haproxy
[root@DR ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:443 0.0.0.0:*
LISTEN 0 128 0.0.0.0:8189 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@DR ~]#
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。