使用Haproxy搭建Web群集

来源:互联网 发布:安卓悬浮窗源码 编辑:程序博客网 时间:2024/06/06 11:39

使用Haproxy搭建Web群集

 

一,实验拓扑:



二,实验要求:

使用1台物理机器

虚拟机安装rhel6.564位系统

编译安装Nginx服务器

编译安装Haproxy

配置Haproxy

测试Web集群

 

Nginx-1 服务器地址:192.168.1.20

Nginx-2 服务器地址:192.168.1.30

Haproxy 服务器地址:192.168.1.10

 

Haproxy是目前比较流行的一种集群调度工具

Haproxy LVSNginx的比较:

LVS性能最好,但是搭建相对复杂

Nginxupstream模块支持集群功能,但是对集群节点健康检查功能不强,性能没有Haproxy

 

三,实验配置:

 

Nginx-1安装:

安装nginx服务器需要安装以下安装依赖包:

yum install -y \

pcre-devel \

zlib-devel

 

编译安装nginx


 [root@wan2 ~]# useradd -M -s /sbin/nologin nginx   

[root@wan2 ~]# tar xzvf nginx-1.6.0.tar.gz -C /opt/

[root@wan2 ~]# cd /opt/nginx-1.6.0/

[root@wan2 nginx-1.6.0]# ./configure \

> --prefix=/usr/local/nginx \

> --user=nginx \

> --group=nginx

 

[root@wan2 nginx-1.6.0]# make

[root@wan2 nginx-1.6.0]# make install

 

[root@wan2 nginx-1.6.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/   创建软连接

 

[root@wan2 nginx-1.6.0]# echo "<h1>this is server-01</h1>" > /usr/local/nginx/html/index.htmlecho this is server-01 的测试页 ,方便测试

 

[root@wan2 nginx-1.6.0]# nginx

[root@wan2 nginx-1.6.0]# netstat -anpt | grep 80   查看80端口


[root@wan2 nginx-1.6.0]# service iptables stop   关闭防护墙

[root@wan2 nginx-1.6.0]# setenforce 0         关闭安全Linux

 

访问nginx-1192.168.1.20


Nginx-2 服务器安装:

 

编译安装:

[root@wan3 ~]# useradd -M -s /sbin/nologin nginx

 

[root@wan3 ~]# tar xzvf nginx-1.6.0.tar.gz  -C /opt/

[root@wan3 ~]# cd /opt/nginx-1.6.0/

[root@wan3 nginx-1.6.0]#

[root@wan3 nginx-1.6.0]# ./configure \

> --prefix=/usr/local/nginx \

> --user=nginx \

> --group=nginx

 

[root@wan3 nginx-1.6.0]# make

[root@wan3 nginx-1.6.0]# make install

 

[root@wan3 nginx-1.6.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

[root@wan3 nginx-1.6.0]# echo "<h1>this is server-01</h1>" > /usr/local/nginx/html/index.html

 

[root@wan3 nginx-1.6.0]# nginx

[root@wan3 nginx-1.6.0]#

[root@wan3 nginx-1.6.0]# netstat -anpt | grep 80


[root@wan3 nginx-1.6.0]# service iptables stop

[root@wan3 nginx-1.6.0]# setenforce 0

 

访问: 192.168.1.30




编译安装haproxy

安装依赖包:

yum install -y \

pcre-devel \

bzip2-devel

 

编译安装

[root@wan-1 ~]# tar xzvf haproxy-1.4.24.tar.gz -C /opt/

 

[root@wan-1 ~]# cd /opt/haproxy-1.4.24/

[root@wan-1 haproxy-1.4.24]# make TARGET=linux26   

[root@wan-1 haproxy-1.4.24]# make install

 

[root@wan-1 haproxy-1.4.24]# mkdir /etc/haproxy        创建haproxy文件夹

[root@wan-1 haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/  拷贝配置文件到haproxy文件夹中

 

[root@wan-1 haproxy-1.4.24]# vi /etc/haproxy/haproxy.cfg    编辑配置文件

注释下面两个命令:

       #chroot /usr/share/haproxy

       #redispatch

删除所有的listen ,添加下端集群:


[root@wan-1 haproxy-1.4.24]# cp examples/haproxy.init /etc/init.d/haproxy

[root@wan-1 haproxy-1.4.24]# chmod 755 /etc/init.d/haproxy   添加权限

[root@wan-1 haproxy-1.4.24]# chkconfig --add haproxy         添加服务即 haproxy

 

[root@wan-1 haproxy-1.4.24]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy  软连接

[root@wan-1 haproxy-1.4.24]# service haproxy start      启动 haproxy

[root@wan-1 haproxy-1.4.24]# netstat -anpt | grep 80  查看80端口


[root@wan-1 haproxy-1.4.24]# service iptables stop  关闭防火墙

 

四:测试实验结果

访问调度器的地址:192.18.1.10  有缓存时间,等过段时间就好了