8月 16

RHEL5中的Cluster组件是基于章文嵩先生创立的LVS(Linux Virtual Server) 制作而成,

关于lvs的工作原理请参照《Linux 服务器集群系统》

Virtual Server via IP Tunneling(VS/TUN)
采用NAT技术时,由于请求和响应报文都必须经过调度器地址重写,当客户请求越来越多时,调度器的处理能力将成为瓶颈。为了解决这个问题,调度器把请求报文通过IP隧道转发至真实服务器,而真实服务器将响应直接返回给客户,所以调度器只处理请求报文。由于一般网络服务应答比请求报文大许多,采用VS/TUN技术后,集群系统的最大吞吐量可以提高10倍。

Cluster(TUN部分)实验

TUN类型的httpd负载均衡集群.网络拓扑为

eth0   192.168.1.100  eth0   192.168.1.200
tunl0  10.0.0.50      tunl0  10.0.0.50
gw     192.168.1.10   gw     192.168.1.10
 ______________      ______________
|              |    |              |
| realserver 1 |    | realserver 2 |
|______________|    |______________|
        |                   |
        |___________________|
                 |
           eth0 192.168.1.10
             ________
            |        |
            |   gw   |
            |________|
           eth1  10.0.0.10         
                |               
                |             
                |             
     VIP(eth0:1)=10.0.0.50     
            _ _ _ _ _       
           |          |
             director 
           |_ _ _ _ _ |
                |     
                |
                |
        ----------------------------------------
       |                 |                     |
       |                 |                     |
  eth0 10.0.0.1   eth0 10.0.0.2     eth0 10.0.0.3
  gw   10.0.0.10  gw   10.0.0.10    gw   10.0.0.10
 ______________    ______________    ______________
|              |  |              |  |              |
|    router    |  | router backup|  |     clinet   |
|______________|  |______________|  |______________|
[client]   
os:windwows xp

[gw]       
os:rhel5   
hostname:gw

[router/router backup]
os:rhel5   
hostname:vs/vs_bk   
software: ipvsadm piranha httpd php


[realserver1/realserver2]
os:rhel5   
hostname:rs1/rs2   
software:httpd

[director]
    为router或者router backup中的一台虚拟出来

配置如下

[gw]

#打开路由转发
[root@gw ~]# echo 1 > /proc/sys/net/ipv4/ip_forward

[router]

#设置piranha密码
piranha-passwd
#开启服务
service piranha-gui start
#访问配置页面
http://10.0.0.1:3636/
#配置以后的文件
[root@vs ~]# cat /etc/sysconfig/ha/lvs.cf
serial_no = 53
primary = 10.0.0.1
service = lvs
backup_active = 1
backup = 10.0.0.2
heartbeat = 1
heartbeat_port = 539
keepalive = 6
deadtime = 18
network = tunnel
nat_nmask = 255.255.255.0
debug_level = NONE
monitor_links = 0
virtual HTTP {
     active = 1
     address = 10.0.0.50 eth0:1
     vip_nmask = 255.255.0.0
     port = 80
     send = "GET / HTTP/1.0\r\n\r\n"
     expect = "HTTP"
     use_regex = 0
     load_monitor = none
     scheduler = wlc
     protocol = tcp
     timeout = 6
     reentry = 15
     quiesce_server = 0
     server rs1.yubo.com {
         address = 192.168.1.100
         active = 1
         weight = 1
     }
     server rs2.yubo.com {
         address = 192.168.1.200
         active = 1
         weight = 1
     }
}
#重启服务
service pulse restart

[router backup]

#设置piranha密码
piranha-passwd
#开启服务
service piranha-gui start
#访问配置页面
http://10.0.0.2:3636/
#配置以后的文件
[root@vs_bk ~]# cat /etc/sysconfig/ha/lvs.cf
serial_no = 48
primary = 10.0.0.2
service = lvs
backup_active = 1
backup = 10.0.0.1
heartbeat = 1
heartbeat_port = 539
keepalive = 6
deadtime = 18
network = tunnel
nat_nmask = 255.255.0.0
debug_level = NONE
monitor_links = 0
virtual HTTP {
     active = 1
     address = 10.0.0.50 eth0:1
     vip_nmask = 255.255.0.0
     port = 80
     send = "GET / HTTP/1.0\r\n\r\n"
     expect = "HTTP"
     use_regex = 0
     load_monitor = none
     scheduler = wlc
     protocol = tcp
     timeout = 6
     reentry = 15
     quiesce_server = 0
     server rs1.yubo.com {
         address = 192.168.1.100
         active = 1
         weight = 1
     }
     server rs2.yubo.com {
         address = 192.168.1.200
         active = 1
         weight = 1
     }
}
#重启服务
service pulse restart

[realserver1/realserver2]
封装到达realserver的包,目的地址是vip的地址,而不是rs的ip地址,如果不做处理,会被拒绝,加上一个虚拟设备就解决问题了tunl0

ifconfig tunl0 10.0.0.50 netmask 255.255.255.255

之后开启rs1,rs2的httpd服务,为了使得便于观察

[root@rs1 ~]# echo "rs1.yubo.org" > /var/www/html/index.html
[root@rs2 ~]# echo "rs2.yubo.org" > /var/www/html/index.html

client频繁访问http://10.0.0.50时,会发现显示内容在”rs2.yubo.org”和”rs1.yubo.org”之间切换

试验完成以后,不要忘记保存配置,以免启动以后无法使用

[vs/vs_bk]

chkconfig --level 2345 piranha-gui on
chkconfig --level 2345 pulse on

[rs1/rs2]

echo "ifconfig tunl0 10.0.0.50 netmask 255.255.255.255" >> /etc/rc.local

[注意]
route 和 route backup 互为备份,没有主次之分(注意每个配置文件的backup和backup_private)
route backup 是route的备份
route 是route backup的备份
10.0.0.50个虚拟ip地址同一时刻只出现在1个router上,当前router当机以后,这个ip地址会被另一台备份机器接管

[其他]

[root@vs_bk ~]# ipvsadm --save -n
-A -t 10.0.0.50:80 -s wlc
-a -t 10.0.0.50:80 -r 192.168.1.200:80 -i -w 1
-a -t 10.0.0.50:80 -r 192.168.1.100:80 -i -w 1
8月 16

RHEL5中的Cluster组件是基于章文嵩先生创立的LVS(Linux Virtual Server) 制作而成,

关于lvs的工作原理请参照《Linux 服务器集群系统》

Virtual Server via Direct Routing(VS/DR)
VS/DR通过改写请求报文的MAC地址,将请求发送到真实服务器,而真实服务器将响应直接返回给客户。同VS/TUN技术一样,VS/DR技术可极大地提高集群系统的伸缩性。这种方法没有IP隧道的开销,对集群中的真实服务器也没有必须支持IP隧道协议的要求,但是要求调度器与真实服务器都有一块网卡连在同一物理网段上。

Cluster(DR部分)实验

应为DR使基于MAC改写的,为了使试验简单明了,假设client,router,realserver都在一个网段上,
DR类型的httpd负载均衡集群.网络拓扑为
*当然,实际使用中,还需要考虑网关,路由等

#拓扑图
             ________
            |        |
            | client |
            |________|
             10.0.0.10         
                |                eth0 10.0.0.1
                |                 _____________
                |                |             |
     VIP(eth0:1)=10.0.0.50       |    router   |
            _ _ _ _ _            |_____________|
           |          |               |
             director  ---------------+
           |_ _ _ _ _ |               |
                |                eth0 10.0.0.2
                |                 ______________
                |                |              |
        -----------------        | router backup|
       |                 |       |______________|
       |                 |       
  eth0 10.0.0.100   eth0 10.0.0.200
 ______________    ______________
|              |  |              |
| realserver1  |  | realserver2  |
|______________|  |______________|
[client]
    os
        windwows xp

[router]
    os
        rhel5
    hostname
        vs
    software
        ipvsadm piranha httpd php

[router backup]
    os
        rhel5
    hostname
        vs_bk
    software
        ipvsadm piranha httpd php

[real server 1]
    os
        rhel5
    hostname
        rs1
    software
        httpd
 
[real server B]
    os
        rhel5
    hostname
        rs2
    software
        httpd

[director]
    为router或者router backup中的一台虚拟出来

配置如下
[router]

#设置piranha密码
piranha-passwd
#开启服务
service piranha-gui start
#访问配置页面
http://10.0.0.1:3636/
#配置以后的文件
[root@vs ~]# cat /etc/sysconfig/ha/lvs.cf
serial_no = 51
primary = 10.0.0.1
service = lvs
backup_active = 1
backup = 10.0.0.2
heartbeat = 1
heartbeat_port = 539
keepalive = 6
deadtime = 18
network = direct
nat_nmask = 255.255.255.0
debug_level = NONE
monitor_links = 0
virtual HTTP {
     active = 1
     address = 10.0.0.50 eth0:1
     vip_nmask = 255.255.0.0
     port = 80
     send = "GET / HTTP/1.0\r\n\r\n"
     expect = "HTTP"
     use_regex = 0
     load_monitor = none
     scheduler = wlc
     protocol = tcp
     timeout = 6
     reentry = 15
     quiesce_server = 0
     server rs1.yubo.com {
         address = 10.0.0.100
         active = 1
         weight = 1
     }
     server rs2.yubo.com {
         address = 10.0.0.200
         active = 1
         weight = 1
     }
}
#重启服务
service pulse restart

[router backup]

#设置piranha密码
piranha-passwd
#开启服务
service piranha-gui start
#访问配置页面
http://10.0.0.2:3636/
#配置以后的文件
[root@vs_bk ~]# cat /etc/sysconfig/ha/lvs.cf
serial_no = 46
primary = 10.0.0.2
service = lvs
backup_active = 1
backup = 10.0.0.1
heartbeat = 1
heartbeat_port = 539
keepalive = 6
deadtime = 18
network = direct
nat_nmask = 255.255.0.0
debug_level = NONE
monitor_links = 0
virtual HTTP {
     active = 1
     address = 10.0.0.50 eth0:1
     vip_nmask = 255.255.0.0
     port = 80
     send = "GET / HTTP/1.0\r\n\r\n"
     expect = "HTTP"
     use_regex = 0
     load_monitor = none
     scheduler = wlc
     protocol = tcp
     timeout = 6
     reentry = 15
     quiesce_server = 0
     server rs1.yubo.com {
         address = 10.0.0.100
         active = 1
         weight = 1
     }
     server rs2.yubo.com {
         address = 10.0.0.200
         active = 1
         weight = 1
     }
}
#重启服务
service pulse restart

[realserver1/realserver2]
修改mac地址,但是到达realserver的包,目的地址使vip的地址,而不是rs的ip地址,如果不做处理,会被拒绝,有arptables_jf/iptables 两种方法,我用的使iptables
2台rs都要做

iptables -t nat -A PREROUTING -p tcp -d 1 --dport 80 -j REDIRECT

之后开启rs1,rs2的httpd服务,为了使得便于观察

[root@rs1 ~]# echo "rs1.yubo.org" > /var/www/html/index.html
[root@rs2 ~]# echo "rs2.yubo.org" > /var/www/html/index.html

client频繁访问http://10.0.0.50时,会发现显示内容在”rs2.yubo.org”和”rs1.yubo.org”之间切换

试验完成以后,不要忘记保存配置,以免启动以后无法使用

[vs/vs_bk]

chkconfig --level 2345 piranha-gui on
chkconfig --level 2345 pulse on

[rs1/rs2]

service iptables save
chkconfig --level 2345 iptables on

[注意]
route 和 route backup 互为备份,没有主次之分(注意每个配置文件的backup和backup_private)
route backup 是route的备份
route 是route backup的备份
10.0.0.50个虚拟ip地址同一时刻只出现在1个router上,当前router当机以后,这个ip地址会被另一台备份机器接管

8月 16

RHEL5中的Cluster组件是基于章文嵩先生创立的LVS(Linux Virtual Server) 制作而成,

关于lvs的工作原理请参照《Linux 服务器集群系统》

Virtual Server via Network Address Translation(VS/NAT)
通过网络地址转换,调度器重写请求报文的目标地址,根据预设的调度算法,将请求分派给后端的真实服务器;真实服务器的响应报文通过调度器时,报文的源地址被重写,再返回给客户,完成整个负载调度过程。

Cluster(NAT部分)实验

用到5台机器,做一个nat类型的httpd负载均衡集群.网络拓扑为

             ________
            |        |
            | client |
            |________|
           192.168.1.28          eth0 192.168.1.5
                |                eth1 10.0.0.1
                |                 _____________
     VIP(eth0:1)=192.168.1.50    |             |
     NIP(eth1:1)=10.0.0.254      |    router   |
            _ _ _ _ _            |_____________|
           |          |               |
             director  ---------------+
           |_ _ _ _ _ |               |
                |                eth0 192.168.1.6
                |                eth1 10.0.0.2
                |                 ______________
        -----------------        |              |
       |                 |       | router backup|
       |                 |       |______________|
  eth0 10.0.0.100   eth0 10.0.0.200
  gw   10.0.0.254   gw   10.0.0.254
 ______________    ______________
|              |  |              |
| realserver1  |  | realserver2  |
|______________|  |______________|


[client]
    os
        windwows xp

[router]
    os
        rhel5
    hostname
        vs
    software
        ipvsadm piranha httpd php

[router backup]
    os
        rhel5
    hostname
        vs_bk
    software
        ipvsadm piranha httpd php

[real server 1]
    os
        rhel5
    hostname
        rs1
    software
        httpd
 
[real server B]
    os
        rhel5
    hostname
        rs2
    software
        httpd

[director]
    为router或者router backup中的一台虚拟出来


配置如下
[router]

#打开路由转发功能
/etc/sysctl.conf
>
net.ipv4.ip_forward = 1

#设置piranha密码
piranha-passwd

#开启服务
service piranha-gui start

#访问配置页面
http://192.168.1.5:3636/

#配置以后的文件
[root@vs ~]# cat /etc/sysconfig/ha/lvs.cf
#序号
serial_no = 41


#公网ip地址
primary = 192.168.1.5

#内部ip地址
primary_private = 10.0.0.1

#服务名称
service = lvs

#是否有备份
backup_active = 1

#备份机器的ip地址
backup = 192.168.1.6

#备份机器的内网ip地址
backup_private = 10.0.0.2

#是否开启心跳
heartbeat = 1

#心跳的udp端口
heartbeat_port = 539

#心跳间隔(秒)
keepalive = 6

#如果主 LVS 节点在deadtime(秒)后没有答复,那么备份 LVS 路由器节点就会发起失效转移。
deadtime = 18

#lvs的类型
network = nat

#浮动ip地址以及对应设备
nat_router = 10.0.0.254 eth1:1

#浮动ip掩码
nat_nmask = 255.255.255.0

#debug信息级别
debug_level = NONE

#是否开启realserver的监视功能,和后面的scheduler(调度算法)以及load_monitor相关
monitor_links = 0

#虚拟服务的名称
virtual HTTP {
     #是否激活
     active = 1

     #虚拟服务所绑定的ip(vip)以及设备名
     address = 192.168.1.50 eth0:1

     #vip相对应的掩码
     vip_nmask = 255.255.255.0

     #虚拟服务的端口
     port = 80

     #给realserver发送的验证字符串
     send = "GET / HTTP/1.0\r\n\r\n"

     #服务器正常运行时应该返回的文本答复,用来判断realserver是否工作正常
     expect = "HTTP"

     #expect中是否使用正则表达式
     use_regex = 0

     #LVS 路由器能够使用 rup 或 ruptime 来监视各个真正服务器的载量。如果你从拉下菜单中选择了 rup,每个真正服务器就必须运行 rstatd 服务。如果选择了 ruptime,每个真正服务器就必须运行 rwhod 服务。
     load_monitor = none


     #调度算式,有循环调度/循环调度/最少连接/加权最少连接法(默认)/基于地区的最少连接调度/带有复制调度的基于地区的最少连接调度/目标散列调度/源散列调度
     scheduler = wlc

     #虚拟服务使用的协议类型
     protocol = tcp

     #realserver失效后从lvs路由条目中移除realserver所必须经过的时间(秒)
     timeout = 6

     #移除以后的realserver重新加入lvs路由条目所必须经过的时间(秒)
     reentry = 15

     #当选择了 Quiesce server 单选按钮时,无论何时某个新的真正服务器节点联机,最少连接表都会被重设为零,因此活跃 LVS 路由器就会选路发送所有请求,如同所有真正服务器都被重新加入群集一样。这个选项防止了新服务器在进入群集时对大量连接应接不暇。
     quiesce_server = 0

     #realserver服务器名称空间配置
     server rs1.yubo.org {
         #realserver的ip地址
         address = 10.0.0.100

         #是否激活
         active = 1

         #一个表明和集合内其它主机相比而言的主机能力的整数值。这个值可以是任意的,但是请把它当作和群集中其它真正服务器的比例对待
         weight = 1
     }
     server rs2.yubo.org {
         address = 10.0.0.200
         active = 1
         weight = 1
     }
}

#重启服务
service pulse restart




[router backup]

#打开路由转发功能
/etc/sysctl.conf
>
net.ipv4.ip_forward = 1

#设置piranha密码
piranha-passwd

#开启服务
service piranha-gui start

#访问配置页面
http://192.168.1.6:3636/

#配置以后的文件
[root@vs_bk ~]# cat /etc/sysconfig/ha/lvs.cf
serial_no = 39
primary = 192.168.1.6
primary_private = 10.0.0.2
service = lvs
backup_active = 1
backup = 192.168.1.5
backup_private = 10.0.0.1
heartbeat = 1
heartbeat_port = 539
keepalive = 6
deadtime = 18
network = nat
nat_router = 10.0.0.254 eth1:1
nat_nmask = 255.255.0.0
debug_level = NONE
monitor_links = 0
virtual HTTP {
     active = 1
     address = 192.168.1.50 eth0:1
     vip_nmask = 255.255.255.0
     port = 80
     send = "GET / HTTP/1.0\r\n\r\n"
     expect = "HTTP"
     use_regex = 0
     load_monitor = none
     scheduler = wlc
     protocol = tcp
     timeout = 6
     reentry = 15
     quiesce_server = 0
     server rs1.yubo.org {
         address = 10.0.0.100
         active = 1
         weight = 1
     }
     server rs2.yubo.org {
         address = 10.0.0.200
         active = 1
         weight = 1
     }
}


#重启服务
service pulse restart



之后开启rs1,rs2的httpd服务,为了使得便于观察
[root@rs1 ~]# echo "rs1.yubo.org" > /var/www/html/index.html
[root@rs2 ~]# echo "rs2.yubo.org" > /var/www/html/index.html


client频繁访问http://192.168.1.50时,会发现显示内容在"rs2.yubo.org"和"rs1.yubo.org"之间切换


[注意]
route 和 route backup 互为备份,没有主次之分(注意每个配置文件的backup和backup_private)
route backup 是route的备份
route 是route backup的备份

192.168.1.50和10.0.0.254这2个虚拟ip地址同一时刻只出现在1个router上,当前router当机以后,这2个ip地址会被另一台备份机器接管