|
			 br0
			router <--宿主机模拟路由
			 virbr6
			  |
	|--------|---------|-----------------|
	Dr	Bdr	   R1		     R2


piranha可以实现调度器的HA集群,解决调度器单点故障
	还可以检测后端节点的健康

整个网络都模拟成公网
	客户端http://www.upl.com ---> 192.168.29.128

vip: 192.168.29.128   <---调度接受客户端请求的网卡的IP 

router
	br0	10.1.1.21
	virbr6	192.168.29.1


Dr
	eth0	192.168.29.13   
	gw	192.168.29.1

BDr
	eth0	192.168.29.14  
	gw	192.168.29.1

R1
	eth0	192.168.29.11
	gw	192.168.29.1
	lo:0	192.168.29.128/32
R2
	eth0	192.168.29.12
	gw	192.168.29.1
	lo:0	192.168.29.128/32


分析原理:
客户端IP是10.1.1.82 ,访问 http://192.168.29.13



一、部署调度工具

1、两个调度服务器都安装piranha

# yum install piranha -y

2、给piranha的web界面设定登录密码
# piranha-passwd 
New Password: 
Verify: 
Updating password for user piranha

3、在主Dr上启动web界面,并且进行配置
# /etc/rc.d/init.d/piranha-gui start

登录
http://192.168.29.13:3636/

配置过程:略


同步配置文件到另外一个节点:
# rsync  -alvR /etc/sysconfig/ha/lvs.cf 192.168.29.14:/



先启动Dr的服务  ,过一会采取启动bdr的服务
# service pulse start



二、部署后端节点


网络、网关

lo:0 --> 192.168.29.128/32

修改内核参数

service httpd start



测试:
	找宿主机 http://192.168.29.128
	找同学去访问:
		确保它的电脑没有192.168.29.0/24网络
		却把把网关指向宿主机10网段的IP
		再去访问 http://192.168.29.128


三、部署memcached
	把机器的物理内存通过网络服务共享给其他节点使用
	在宿主机上部署

# yum install memcached -y

# vim /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="512"
OPTIONS=""
# service memcached start


四、部署后端节点,让其支持连接memcached服务器存放数据


1、让后端php节点把session存放在memcached服务器
	必须让后端节点支持连接memcached的功能

	如果是源码版本的php,可以安装源码的memcache模块(客户端),安装方法可以参考lnmp.txt文档

	如果rhel6的系统自带php,可以直接安装rpm包客户端
# yum install 	php-pecl-memcache -y

;session.save_handler = files
session.save_handler = memcache

;session.save_path = "/var/lib/php/session"
session.save_path = tcp://192.168.29.1:11211

# service httpd restart 
# php -m | grep memcache
memcache


2、登录phpwind论坛的后台,让论坛把一些访问频率高但是修改频率低的数据保存到memcached服务器,而不用经常访问数据库,降低数据库的压力








设定完毕之后,多刷几次论坛的页面
查看memcached的数据
宿主机 # memcached-tool 192.168.29.1:11211 dump

=====================================================================


Haproxy 负载均衡集群

	Haproxy 支持4层,7层调度。特色是7层调度,能够支持对http协议的分析。



例子:使用harpoxy实现简单的负载均衡

			   | 
			  br0  <---生产网络,面向客户端
			Haproxy
			   |
		|-------------------|
		web1		  web2

Haproxy
	br0	10.1.1.21  <----www.upl.com
	virbr6	192.168.29.1

web1
	192.168.29.11
web2
	192.168.29.12


一、安装

# yum install pcre pcre-devel -y


根据架构选择适当的编译参数
# tar xvf haproxy-1.4.15.tar.gz -C /usr/src
# cd /usr/src/haproxy-1.4.15
# make TARGET=linux26 CPU=i686 USE_STATIC_PCRE=1 PREFIX=/usr/local/haproxy
如果是64位:
# make PREFIX=/usr/local/haproxy TARGET=linux26 ARCH=x86_64 USE_STATIC_PCRE=1


# make install PREFIX=/usr/local/haproxy



# groupadd -g 98 haproxy
# useradd -u 98 -g 98 haproxy
# id haproxy

二、配置

# vim /usr/local/haproxy/haproxy.cfg

global
	log 127.0.0.1	local0 info
	user 	haproxy
	group 	haproxy
	daemon
	pidfile /usr/local/haproxy/haproxy.pid
defaults
	log	global
	mode	http
	option	httplog
	option	dontlognull 
	retries	3
	option	redispatch
	timeout client	50000
	timeout server	50000
	timeout connect	60000
        option httpchk HEAD /ok.html HTTP/1.0

listen weblb *:80
        option httpclose
        option forwardfor
        balance roundrobin
	cookie SERVERID insert indirect
        server web1 192.168.29.11:80 weight 1 cookie web1 check inter 2000 rise 2 fall 2
        server web2 192.168.29.12:80 weight 1 cookie web2 check inter 2000 rise 2 fall 2


listen stats *:8888  
       mode http  
       transparent  
       stats uri / haproxy-stats  
       stats realm Haproxy \ statistic  
       stats auth admin:admin



把笔记中提供的启动脚本/haproxy 拷贝 服务脚本目录,并且 755

# service haproxy start


三、部署后端节点

。。。。。。
# touch /var/www/html/ok.html

# service httpd start



测试:
	

=================================================================

例子2:使用haproxy实现动静分离的负载均衡集群

	静态元素: 图片,js,css,swf


			   | 
			  br0  <---生产网络,面向客户端
			Haproxy
			   |
		|-------------------|
		web1		  web2


web1	动态页面的服务器,专注于所有动态页面请求和非静态页面的请求

web2	处理所有指定的静态元素的请求  图片,js,css,swf


http://www.upl.com/index.php
http://www.upl.com/

http://www.upl.com/logo.png

===============================================
acl规则的定义,用于匹配客户端的请求
acl 规则名字	匹配选项	匹配的内容
acl  url_static 		path_end	.gif .png .jpg .css .js .swf
acl  upload_p_w_upload		path_beg	/p_w_upload/
		匹配 http://www.ulp.com /p_w_upload/.....
acl  short_domain   hdr(Host) -i upl.com
		匹配 http://upl.com  
acl right_ref hdr_reg(Referer) -i upl.com
		hdr_reg 使用正则表达式匹配内容  a.upl.com , b.upl.com ,upl.com ...
		Referer 来路,这个请求是从哪个页面点击进来
==================================================

一、部署haproxy
global
	log 127.0.0.1	local0 info
	user 	haproxy
	group 	haproxy
	daemon
	pidfile /usr/local/haproxy/haproxy.pid

defaults
	log	global
	mode	http
	option	httplog
	option	dontlognull
	retries	3
	option	redispatch
	timeout client	50000
	timeout server	50000
	timeout connect	60000
        option httpchk HEAD /ok.html HTTP/1.0


frontend http-in
        bind *:80
        mode http
        option httpclose
        option forwardfor

	acl url_static 		path_end	.gif .png .jpg .css .js .swf
	acl short_domain hdr(Host) -i upl.com
	acl right_ref hdr_reg(Referer) -i upl.com
	acl steal_link path_end no.png 


	redirect prefix http://www.upl.com code 301 if short_domain
	redirect location http://www.upl.com/p_w_picpaths/no.png code 302 if !right_ref  url_static !steal_link


	use_backend imgservs if url_static 
        default_backend orgservs

backend orgservs
        balance roundrobin
	cookie SERVERID insert indirect
        server web1 192.168.29.11:80 weight 1 cookie web1  check inter 2000 rise 2 fall 2
	

backend imgservs
	server img1 192.168.29.12:80 weight 1 check inter 2000 rise 2 fall 2
	


listen stats :8888  
       mode http  
       transparent  
       stats uri / haproxy-stats  
       stats realm Haproxy \ statistic  
       stats auth admin:admin



二、部署后端节点


1、解压代码到其中一个orgserver定义后端节点web1
	如果存在多个orgserv定义的节点,那么仅仅把代码解压到其中一个。




2、把同样的代码解压到所有的静态服务器上(imgservs)
	这些节点存放的是静态文件
	
	这些节点完全没有必要安装支持php的相关功能


3、把其余节点调度都临时暂停
	让harproxy仅仅把除了静态请求之外的所有请求调度web1


4、向导安装论坛,安装完毕把代码同步orgservs下其余节点,并且恢复这些节点调度


测试的时,客户端必须使用域名访问

	http://www.upl.com  --> 10.1.1.21


	验证短域名跳转

	验证防盗链:
		直接访问:由于没有来路所以认为是盗链		
		http://www.upl.com/p_w_picpaths/wind/logo.png

	验证动静分离:
		把imgservs节点关闭掉,再去访问,页面没有图片,变形