准备三台Linux服务器,两台做Web服务器,一台安装HAProxy,实现如下功能:
使用3台虚拟机,1台作为HAProxy调度器、2台作为Real Server、物理机作为客户端。
一:配置后端Web服务器
1)配置两台Web服务器网络参数
# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0 ONBOOT=yes BOOTPROTO=static IPADDR=192.168.0.1 NETMASK=255.255.255.0 GATEWAY=192.168.0.254 DNS1=202.106.0.10
# service network restart
# iptables -F
# service iptables save
# vim /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 ONBOOT=yes BOOTPROTO=static IPADDR=192.168.0.2 NETMASK=255.255.255.0 GATEWAY=192.168.0.254 DNS1=202.106.0.10
# systemctl restart NetworkManager
2)设置后端Web服务
# yum -y install httpd
# systemctl start httpd
# echo “192.168.0.1” > /var/www/html/index.html
# yum -y install httpd
# systemctl start httpd
# echo “192.168.0.2” > /var/www/html/index.html
二:部署HAProxy服务器
1)配置网络,安装软件
# vim /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 ONBOOT=yes BOOTPROTO=static IPADDR=202.114.106.20
# vim /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth0 ONBOOT=yes BOOTPROTO=static IPADDR=192.168.0.254 NETMASK=255.255.255.0
DNS1=202.106.0.20
# systemctl restart NetworkManager
# sed -i '/ip_forward/s/0/1/' sysctl.conf //开启路由转发
# sysctl -p
# yum -y install haproxy
2)修改配置文件
# vim /etc/haproxy/haproxy.cfgglobal
3)启动服务器并设置开机启动
# systemctl start haproxy
# systemctl enable haproxy
三:客户端验证
客户端配置与HAProxy相同网络的IP地址,并使用火狐浏览器访问http://202.114.106.20,测试调度器是否正常工作,客户端访问http://202.114.106.20/haproxy-admin测试状态监控页面是否正常。