提供后端检测,防止真实服务器掉线后,在调度器中分配任务时,也会发送到掉线的RS中。
一、haproxy的基本配置
1.1实验环境
主机名 | IP |
---|---|
client | VMware-nat 172.25.254.102; GW:172.25.254.108 |
haproxy | NAT-eth0:172.25.254.107 ; 仅主机-eth1:192.168.0.107 |
server1 | 仅主机 192.168.0.13 ; GW:192.168.0.107 |
server2 | 仅主机 192.168.0.23 ; GW:192.168.0.107 |
1.2网络配置
(1)client测试机
[root@client ~]# vim /etc/NetworkManager/system-connections/eth0.nmconnection
[connection]
id=eth0
type=ethernet
interface-name=eth0
[ipv4]
address1=172.25.254.102/24,172.25.254.107 -- 此网关为harpoxy的外网接口IP
method=manual
[root@client ~]# nmcli connection reload # 重新加载网络配置
[root@client ~]# nmcli connection up eth0 # 重启网卡
# 关闭防火墙
[root@client ~]# systemctl stop firewalld # 暂时关闭防火墙
[root@client ~]# systemctl mask firewalld # 将防火墙写入null,永久关闭
[root@client ~]# setenforce 0 # 设置selinux为宽容模式
(2)haproxy
# NAT-eth0网卡,连接外网
[root@client ~]# vim /etc/NetworkManager/system-connections/eth0.nmconnection
[connection]
id=eth0
type=ethernet
interface-name=eth0
[ipv4]
address1=172.25.254.107/24,172.25.254.2
method=manual
dns=114.114.114.114;
# 仅主机-eth1网卡,连接内网
[root@haproxy ~]# vim /etc/NetworkManager/system-connections/eth1.nmconnection
[connection]
id=eth1
type=ethernet
interface-name=eth1
[ipv4]
address1=192.168.0.107/24
method=manual
[root@haproxy ~]# nmcli connection reload # 重新加载网络配置
[root@haproxy ~]# nmcli connection up eth0 # 重启网卡
[root@haproxy ~]# nmcli connection up eth1
# 关闭防火墙
[root@haproxy ~]# systemctl stop firewalld # 暂时关闭防火墙
[root@haproxy ~]# systemctl mask firewalld # 将防火墙写入null,永久关闭
[root@haproxy ~]# setenforce 0 # 设置selinux为宽容模式
(3)server端
# server1
[root@server1 ~]# vim /etc/NetworkManager/system-connections/eth0.nmconnection
[connection]
id=eth0
type=ethernet
interface-name=eth0
[ipv4]
address1=192.168.0.13/24,192.168.0.107
method=manual
# server2
[root@server2 ~]# vim /etc/NetworkManager/system-connections/eth0.nmconnection
[connection]
id=eth0
type=ethernet
interface-name=eth0
[ipv4]
address1=192.168.0.23/24,192.168.0.107
method=manual
# 两台server主机重新加载网卡,并且关闭防火墙
[root@server ~]# nmcli connection reload # 重新加载网络配置
[root@server ~]# nmcli connection up eth0 # 重启网卡
# 关闭防火墙
[root@server ~]# systemctl stop firewalld # 暂时关闭防火墙
[root@server ~]# systemctl mask firewalld # 将防火墙写入null,永久关闭
[root@server ~]# setenforce 0 # 设置selinux为宽容模式
1.3server安装nginx服务
# server1
[root@server1 ~]# yum install nginx -y # 安装nginx服务
[root@server1 ~]# systemctl enable --now nginx # 立即启动nginx服务并设置开机启动
[root@server1 ~]# echo server1-192.168.0.13 > /usr/share/nginx/html/index.html # 写入内容到nginx的发布目录的index.html便于测试
# server2
[root@server1 ~]# yum install nginx -y
[root@server1 ~]# systemctl enable --now nginx
[root@server1 ~]# echo server1-192.168.0.13 > /usr/share/nginx/html/index.html
1.4 haproxy的安装和服务配置
-
安装haproxy服务
[root@haproxy ~]# yum install haproxy -y [root@haproxy ~]# rpm -qc haproxy # 查找haproxy的配置文件 /etc/haproxy/haproxy.cfg
-
haprox的基本配置
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg # 编译haproxy的配置文件 # 编译代理设定,设置前端(前后端分开编写) 68 #--------------------------------------------------------------------- 69 # main frontend which proxys to the backends 70 #--------------------------------------------------------------------- 71 72 frontend webcluster -- 名字唯一 73 bind *:80 -- 监听所有端口 74 mode http -- 75 use_backend webcluster-host -- 使用的后端 76 77 backend webcluster-host -- 定义后端 78 balance roundrobin -- 设置调度规则 79 server web1 192.168.0.13:80 -- 后端服务器 80 server web2