案例环境
本案例使用三台服务器模拟搭建一套Web集群,如下所示:
实验步骤如下:
- 编辑安装nginx服务器
安装依赖包下载nginx-01服务器
[[email protected] ~]# yum -y install gcc pcre-devel zlib-devel
[[email protected] ~]# wget https://nginx.org/download/nginx-1.6.3.tar.gz
[[email protected] ~]# tar zxvf nginx-1.6.3.tar.gz
[[email protected] ~]# cd nginx-1.6.3/
[[email protected] nginx-1.6.3]# ./configure --prefix=/usr/local/nginx
[[email protected] nginx-1.6.3]# make && make install
[[email protected] nginx-1.6.3]# cd /usr/local/nginx/html/
建立测试页面
[[email protected] html]# echo "192.168.150.129" >> index.html
启动nginx
[[email protected] html]# /usr/local/nginx/sbin/nginx
建立防火墙机制
[[email protected] html]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
搭建nginx-02服务器
[[email protected] ~]# yum -y install gcc pcre-devel zlib-devel
[[email protected] ~]# wget https://nginx.org/download/nginx-1.6.3.tar.gz
[[email protected] ~]# tar zxvf nginx-1.6.3.tar.gz
[[email protected] ~]# cd nginx-1.6.3/
[[email protected] nginx-1.6.3]# ./configure --prefix=/usr/local/nginx
[[email protected] nginx-1.6.3]# make && make install
[[email protected] nginx-1.6.3]# cd /usr/local/nginx/html/
建立测试页面
[[email protected] html]# echo "192.168.150.133" >> index.html
启动nginx
[[email protected] html]# /usr/local/nginx/sbin/nginx
建立防火墙机制
[[email protected] html]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
安装完成后,在客户端访问nginx进行测试,如下图所示: - 安装配置haproxy
[[email protected] ~]# yum -y install pcre-devel bzip2-devel gcc* -y
[[email protected] ~]# wget http://www.haproxy.org/download/1.4/src/haproxy-1.4.27.tar.gz
[[email protected] ~]# tar xvzf haproxy-1.4.27.tar.gz
[[email protected] ~]# cd haproxy-1.4.27/
[[email protected] haproxy-1.4.27]# make TARGET=linux26
创建配置文件目录
[[email protected] haproxy-1.4.27]# mkdir /etc/haproxy
将样本复制到配置文件目录
[[email protected] haproxy-1.4.27]# cp examples/haproxy.cfg /etc/haproxy/ - 根据目前集群设计,将haproxy.cfg配置文件修改如下:
[[email protected] haproxy-1.4.27]# cat /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
frontend main *:80
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js
default_backend app
backend app
balance roundrobin
server app1 192.168.150.129:80 check
server app2 192.168.150.133:80 check
配置文件因版本而已 请不要复制 参数基本一样
- 创建自启动脚本
[[email protected] haproxy-1.4.27]# cp examples/haproxy.init /etc/init.d/haproxy
[[email protected] haproxy-1.4.27]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[[email protected] haproxy-1.4.27]# chmod +x /etc/init.d/haproxy
[[email protected] haproxy-1.4.27]#systemctl start haproxy
- 测试Web集群
通过上面的步骤,已经搭建完成Haproxy的Web集群,下面验证一下集群是否正常工作,一个集群一般具备两个特性:高性能与高可用
(1)测试高性能
在客户端使用浏览器打开http://192.168.150.131,如下图所示:
(2)测试高可用
现在将nginx1服务停用,在客户端访问代理服务器,浏览器会显示nginx2网页内容,不会影响集群的使用,满足了集群的高可用性
原文地址:https://blog.51cto.com/xia1314520ting/2406983