使用Haproxy搭建Web群集
使用Haproxy搭建Web群集
一,实验拓扑:
二,实验要求:
使用1台物理机器
虚拟机安装rhel6.5的64位系统
编译安装Nginx服务器
编译安装Haproxy
配置Haproxy
测试Web集群
Nginx-1 服务器地址:192.168.1.20
Nginx-2 服务器地址:192.168.1.30
Haproxy 服务器地址:192.168.1.10
Haproxy是目前比较流行的一种集群调度工具
Haproxy 与LVS、Nginx的比较:
LVS性能最好,但是搭建相对复杂
Nginx的upstream模块支持集群功能,但是对集群节点健康检查功能不强,性能没有Haproxy好
三,实验配置:
Nginx-1安装:
安装nginx服务器需要安装以下安装依赖包:
yum install -y \
pcre-devel \
zlib-devel
编译安装nginx:
[[email protected] ~]# useradd -M -s /sbin/nologin nginx
[[email protected] ~]# tar xzvf nginx-1.6.0.tar.gz -C /opt/
[[email protected] ~]# cd /opt/nginx-1.6.0/
[[email protected] nginx-1.6.0]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx
[[email protected] nginx-1.6.0]# make
[[email protected] nginx-1.6.0]# make install
[[email protected] nginx-1.6.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ 创建软连接
[[email protected] nginx-1.6.0]# echo "<h1>this is server-01</h1>" > /usr/local/nginx/html/index.html echo this is server-01 的测试页 ,方便测试
[[email protected] nginx-1.6.0]# nginx
[[email protected] nginx-1.6.0]# netstat -anpt | grep 80 查看80端口
[[email protected] nginx-1.6.0]# service iptables stop 关闭防护墙
[[email protected] nginx-1.6.0]# setenforce 0 关闭安全Linux
访问nginx-1:192.168.1.20
Nginx-2 服务器安装:
编译安装:
[[email protected] ~]# useradd -M -s /sbin/nologin nginx
[[email protected] ~]# tar xzvf nginx-1.6.0.tar.gz -C /opt/
[[email protected] ~]# cd /opt/nginx-1.6.0/
[[email protected] nginx-1.6.0]#
[[email protected] nginx-1.6.0]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx
[[email protected] nginx-1.6.0]# make
[[email protected] nginx-1.6.0]# make install
[[email protected] nginx-1.6.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[[email protected] nginx-1.6.0]# echo "<h1>this is server-01</h1>" > /usr/local/nginx/html/index.html
[[email protected] nginx-1.6.0]# nginx
[[email protected] nginx-1.6.0]#
[[email protected] nginx-1.6.0]# netstat -anpt | grep 80
[[email protected] nginx-1.6.0]# service iptables stop
[[email protected] nginx-1.6.0]# setenforce 0
访问: 192.168.1.30
编译安装haproxy
安装依赖包:
yum install -y \
pcre-devel \
bzip2-devel
编译安装:
[[email protected] ~]# tar xzvf haproxy-1.4.24.tar.gz -C /opt/
[[email protected] ~]# cd /opt/haproxy-1.4.24/
[[email protected] haproxy-1.4.24]# make TARGET=linux26
[[email protected] haproxy-1.4.24]# make install
[[email protected] haproxy-1.4.24]# mkdir /etc/haproxy 创建haproxy文件夹
[[email protected] haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/ 拷贝配置文件到haproxy文件夹中
[[email protected] haproxy-1.4.24]# vi /etc/haproxy/haproxy.cfg 编辑配置文件
注释下面两个命令:
#chroot /usr/share/haproxy
#redispatch
删除所有的listen ,添加下端集群:
[[email protected] haproxy-1.4.24]# cp examples/haproxy.init /etc/init.d/haproxy
[[email protected] haproxy-1.4.24]# chmod 755 /etc/init.d/haproxy 添加权限
[[email protected] haproxy-1.4.24]# chkconfig --add haproxy 添加服务即 haproxy
[[email protected] haproxy-1.4.24]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy 软连接
[[email protected] haproxy-1.4.24]# service haproxy start 启动 haproxy
[[email protected] haproxy-1.4.24]# netstat -anpt | grep 80 查看80端口
[[email protected] haproxy-1.4.24]# service iptables stop 关闭防火墙
四:测试实验结果
访问调度器的地址:192.18.1.10 有缓存时间,等过段时间就好了