Ubuntu 14服务器上的HAProxy负载均衡器
时间:2020-01-09 10:38:48 来源:igfitidea点击:
说明
HAProxy是一种轻量级的负载平衡器,可以快速轻松地进行设置。尽管它缺少F5和Citrix等公司的企业平衡器中提供的许多功能,但它仍然是几乎可以在任何Linux发行版上免费使用的强大服务器。我在许多预算较低的项目中使用了需要负载平衡的项目。
顾名思义,所有平衡的流量都通过HAProxy代理。
创建虚拟IP
虚拟IP是用户将用于连接到负载平衡应用程序的IP地址。这是我们将在DNS中注册的地址。在我们的示例中,虚拟IP将分配给HAProxy服务器上唯一的网络接口ETH0。在生产中,将平衡的流量和服务器管理流量分离到不同的网络接口上是明智的。
- 在文本编辑器中打开网络配置文件。
sudo nano /etc/network/interfaces
- 在现有的网络接口下,添加一个使用相同名称的新接口,并在其末尾添加一个冒号(:)。例如,我们将虚拟IP添加到的接口名为ETH0。因此,我们的新虚拟接口将被命名为ETH0:1.
# This file describes the network interfaces available on your system
and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet static
address 172.30.0.100
netmask 255.255.255.0
network 172.30.0.0
broadcast 172.30.0.255
gateway 172.30.0.1
# HAProxy Virtual IP 1
auto eth0:1
iface eth0:1 inet static
address 172.30.0.101
netmask 255.255.255.0
- 保存更改并退出文本编辑器。
- 打开新界面。
ifup eth0:1
- 使用ifconfig eth0:1验证接口是否已启动。输出应类似于以下示例。
eth0:1 Link encap:Ethernet HWaddr 00:0c:29:b6:f3:e8
inet addr:172.30.0.101 Bcast:172.30.0.255 Mask: 255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
安装HAProxy
HAProxy软件包在Ubuntu 14存储库中可用。可以使用aptitude安装它。
- 使用具有root特权的帐户登录到服务器。
- 安装HAProxy。
sudo apt-get install haproxy
- 要启用HAProxy服务守护程序,请在文本编辑器中打开/ etc / defaults / haproxy。
sudo nano /etc/defaults/haproxy
- 将"启用"值设置为1.
# Set ENABLED to 1 if you want the init script to start haproxy ENABLED=1 # Add extra flags here #EXTRAOPTS="-de -m 16"
- 保存更改并退出文本编辑器。
第4层平衡(高性能)
第4层负载平衡是平衡Web应用程序的更快方法。简单来说,它接收提交给虚拟IP的请求,并将其转发到支持的服务器(例如,其中一个Web服务器处于平衡状态)。由于仅转发了请求,因此很少进行任何处理。
使用第4层进行平衡的限制是所有后端服务器必须配置相同。否则,用户体验将取决于将用户转发到哪个服务器而有所不同。
- 在文本编辑器中打开HAProxy配置文件。
sudo nano /etc/haproxy/haproxy.cfg
- 修改默认值部分下的某些值。更具体地说,我们需要确保将模式设置为tcp。
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
user haproxy
group haproxy
daemon
defaults
log global
mode tcp
option tcplog
option dontlognull
contimeout 5000
clitimeout 50000
srvtimeout 50000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http
- 为应用程序创建一个新的前端部分。这是我们定义群集的虚拟IP以及要使用的后端服务器群集(在下面定义)的位置。我们可以根据需要为前端命名,只要每个前端都有唯一的名称即可。
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
user haproxy
group haproxy
daemon
defaults
log global
mode tcp
option tcplog
option dontlognull
contimeout 5000
clitimeout 50000
srvtimeout 50000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http
::HL::frontend webapp1
::HL:: bind 172.30.0.101:80
::HL:: default_backend webapp1-servers
- 为刚创建的前端创建一个后端。这是我们定义集群中使用的服务器的地方。我们还在此处设置了平衡算法,以及一些其他可选选项。
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
user haproxy
group haproxy
daemon
defaults
log global
mode tcp
option tcplog
option dontlognull
contimeout 5000
clitimeout 50000
srvtimeout 50000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http
frontend webapp1
bind 172.30.0.101:80
default_backend webapp1-servers
::HL::backend webapp1-servers
::HL:: balance roundrobin
::HL:: mode tcp
::HL:: server webserver1 172.30.0.102
::HL:: server webserver2 172.30.0.103
::HL:: server webserver3 172.30.0.104
::HL:: server webserver4 172.30.0.105
- 保存更改并退出文本编辑器。
- 启动或者重新启动HAProxy使更改生效。
sudo service haproxy restart

