群集的含义 --Cluster,集群、群集 --由多台主机构成,但对外只表现为一个整体 . 群集的类型

  • 负载均衡群集:主要的功能将来自客户机的访问请求分流给多台服务器,从而缓单台服务器的负载压力,例如京东淘宝的购物节的时候,当天的并发量是分常大的,单台服务器是无法承载的。
  • 高可用群集:高可用群集和hsrp原理基本一样,服务器有主从之分,实现故障切换,当一台服务器发生故障的时候,另一台服务器马上提供工作。
  • 高性能运算群集:这种群集主要用在“云计算”中,就是将多台服务器的硬件整合到一起,实现高性能运算能力。 . 负载均衡的分层结构
  • 第一层:负载调度器,是群集系统的唯一入口,对外使用所有服务器共有的虚拟ip地址,通常会配置主从两台调度器实现热备份,确保高可用性。
  • 第二层:服务器池,也就是提供各种服务的服务器,例如web服务器,ftp服务器,数据库服务器等,处理调度器发来的请求。
  • 第三层:共享存储,主要存储服务器池中应用程序的数据,一般会采用nas或者san设备,我们今天就使用NFS搭建一台nas服务器,工作中也可以购买硬件nas和san。

负载均衡的工作模式

  • 地址转换,简称nat模式,负载均衡调度器作为网关,服务器和负载调度器在同一个私有网络,安全性较好。 .
  • Ip隧道,简称tun模式,负载调度器仅作为客户机的访问入口,各节点通过各自的internet连接直接回应客户机,不在经过负载调度器,服务器的节点分散在互联网的不同位置,具有独立的共有ip地址,通过专用的ip隧道与负载调度器相互通信。 .
  • 直接路由,简称DR模式,与TUN模式类似,但各节点不是分散在各地,而是与调度器位于同一个物理网络,负载调度器与各节点服务器通过本地网络连接,不需要建立专用的ip隧道。 . 以上三种模式中,nat方式只需要一个公网地址,从而成为最容易的一种负载均衡模式,安全性也比较好,许多硬件负载均衡设备就是采用这种方式,性比较而言,DR模式和TUN模式的负载能力更强大,使用范围更广,但节点的安全性要稍差一些。 。 LVS虚拟服务器 Lvs是linux内核的一部分,由我国的章文嵩博士在1998年创建,也是极少数由中国人开发的优秀软件之一,我们可以直接手动加载ip_vs模块,并查看当前系统中ip_vs模块的版本信息,命令如下:

[root@localhost ~]# modprobe ip_vs

[root@localhost ~]# cat /proc/net/ip_vs IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn

. lvs的负载调度算法

  • 轮询:将受到的访问请求按顺序轮流分配给群集中的各节点,不管服务器的连接数和系统负载。 .
  • 加权轮询:也是轮流分配,但是可以调整权重,让处理性能强的服务器承担更多的访问流量。 .
  • 最少连接:根据连接数分配,分配给连接数少的节点。 .
  • 加权最少连接:权重高的节点将承担更大比例的负载 . 使用ipvsadm管理工具 Ipvsadm是在负载调度器上使用的lvs群集管理工具,通过调用ip_vs模块来添加、删除服务器节点。需要手动安装。

. [root@localhost ~]# yum -y install ipvsadm .

. 负载均衡的结构 . . . . 搭建LVS群集(NAT模式)

搭建lvs群集,lvs负载调度器有两块网卡,是所有内部web服务器的网关服务器,需要为负载调度器配置SNAT,以便内部的服务器可以访问internet,所有的节点服务器、共享存储位于私有网络,网关指向负载调度器的192.168.7.254 . 具体环境如下; NFS:192.168.7.250/24 web节点A:192.168.7.21/24 web节点B:192.168.7.22/24 LVS:eth0 192.168.7.254/24 :eth1 172.16.16.172/24 客户机:172.16.16.1 指向lvs . 搭建NFS服务器(网关指向lvs) 安装nfs-utils(用来发布共享和访问)和rpcbind(用于RPC支持) 挂载光盘,先删除原有yum文件不然无法生效 .

[root@centos1 /]# mount /dev/cdrom /media/ [root@centos1 /]# rm -rf /etc/yum.repos.d/* [root@centos1 /]# vim /etc/yum.repos.d/index.repo [root@centos1 /]# yum -y install nfs-utils rpcbind .

[root@centos1 /]# vim /etc/yum.repos.d/index.repo

[local] name=lijialiang baseurl=file:///media enabled=1 gpgcheck=0

. 搭建NFS服务器 安装nfs-utils(用来发布共享和访问)和rpcbind(用于RPC支持) [root@centos1 /]# yum -y install nfs-utils rpcbind

[root@centos1 /]# chkconfig nfs on

[root@centos1 /]# chkconfig rpcbind on

. 设置共享目录 将文件夹/opt/wwwroot共享给内部的web服务器节点使用,命令如下: [root@centos1 /]# mkdir -p /opt/wwwroot

[root@centos1 /]# vi /etc/exports

/opt/wwwroot 192.168.7.0/24(rw,sync,no_root_squash) rw:读写,sync:允许同步写入,no_root_squash:客户机以root身份访问时赋予本地root权限。 . 启动NFS服务程序 [root@centos1 /]# service rpcbind start

[root@centos1 /]# service nfs start

[root@centos1 /]# netstat -anpt | grep rpcbind

. 查看本机发布的NFS共享目录 [root@centos1 /]# showmount -e 192.168.7.250

开防火墙例外

[root@centos1 /]# iptables -I INPUT -p tcp -j ACCEPT

[root@centos1 /]# iptables -I INPUT -p udp -j ACCEPT

.

.

** 在webA挂载光盘。配置yum** [root@centos1 /]# mount /dev/cdrom /media [root@centos1 /]# rm -rf /etc/yum.repos.d/*

[root@centos1 /]# vim /etc/yum.repos.d/index.repo

. .

在web节点A上访问NFS共享资源(网关指向lvs) 若要正常访问NFS共享资源,客户机中也需要安装rpcbind软件包,并启动服务器,如果想使用showmount查询共享,也需要安装nfs-utils软件包,总之和服务器一样都装上吧。
[root@centos1 /]# yum -y install nfs-utils rpcbind

. 启动rpcbind和nfs [root@centos1 /]# service rpcbind start

[root@centos1 /]# service nfs start

[root@centos1 /]# showmount -e 192.168.7.250

. 在两台节点服务器上安装nginx,并开启nginx服务 [root@centos1 /]# yum -y install pcre-devel zlib-devel 切换光盘挂在nginx

[root@centos1 /]# umount /dev/cdrom /media/

[root@centos1 /]# mount /dev/cdrom /media/

[root@centos1 /]# tar zxf nginx-1.6.2.tar.gz -C /usr/src/

[root@centos1 /]# cd /usr/src/nginx-1.6.2/

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

[root@centos1 /]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install

[root@centos1 /]# /usr/local/nginx/sbin/nginx

. 在web节点A上挂载共享目录到本地的nginx网站主目录
[root@centos1 /]# mount -t nfs 192.168.7.250:/opt/wwwroot /usr/local/nginx/html/

[root@centos1 /]# cd /usr/local/nginx/html/

. 查看发现有index.html文件说明已同步 [root@centos1 /]# ls

. 设置自动挂载 [root@centos1 /]# vi /etc/fstab

192.168.7.250:/opt/wwwroot /usr/local/nginx/html nfs defaults,_netdev 0 0 .

[root@centos1 /]# iptables -I INPUT -p tcp -j ACCEPT

[root@centos1 /]# iptables -I INPUT -p udp -j ACCEPT

为了方便测试效果,web节点B的就不挂载NFS了,保持默认的nginx主页,然后在客户端上测试,访问两次,网页都不一样,实现了负载均衡群集。注意测试的时候把两台web节点的网关指向调度器,防火墙开启80端口或者关闭,然后把调度器的防火墙开启FORWARD和INPUT允许80端口。 . webB和A操作相同但是为了测试不做挂载 . . **在(lvs)上配置 挂载配置yum ** [root@centos1 /]# mount /dev/cdrom /media/

[root@centos1 /]# rm -rf /etc/yum.repos.d/*

[root@centos1 /]# vim /etc/yum.repos.d/index.repo

. 配置负载调度器 在调度器上安装ipvsadm软件包 [root@centos1 /]# yum -y install ipvsadm

. 配置SNAT转发规则 [root@centos1 /]# vim /etc/sysctl.conf

net.ipv4.ip_forward = 1
[root@centos1 /]# sysctl -p

.

[root@centos1 /]# iptables -t nat -I POSTROUTING -s 192.168.7.0/24 -o eth1 -j SNAT --to-source 172.16.16.172

. 配置负载分配策略,主要目的是把web节点添加到调度器里面 [root@centos1 /]# service ipvsadm stop

[root@centos1 /]# ipvsadm -A -t 172.16.16.172:80 -s rr

[root@centos1 /]# ipvsadm -a -t 172.16.16.172:80 -r 192.168.7.21:80 -m -w 1

[root@centos1 /]# ipvsadm -a -t 172.16.16.172:80 -r 192.168.7.22:80 -m -w 1

[root@centos1 /]# service ipvsadm save

[root@centos1 /]# chkconfig ipvsadm on

. 测试LVS群集 查看负载调度器 [root@centos1 /]# ipvsadm -ln

[root@centos1 /]# iptables -I FORWARD -p tcp -j ACCEPT

[root@centos1 /]# iptables -I INPUT -p tcp -j ACCEPT

. 测试是否访问成功 . . 搭建LVS群集(路由模式) 这种模式的群集中,lvs负载调度器作为群集的入口,但不做网关使用了,web服务器节点都各自接入internet,发送给客户机的web相应数据包不经过lvs负载调度器。 .

环境如下; NFS:192.168.7.250/24 web节点A:eth0:192.168.7.21/24 :eth1:172.16.16.177/24 web节点A:eth0:192.168.7.22/24 :eth1:172.16.16.178/24 LVS:172.16.16.173/24 客户机:172.16.16.1 . 配置节点服务器 使用DR模式时,节点服务器也需要配置VIP地址,因为客户端请求的是群集IP地址,目标MAC地址是LVS的,节点服务器回应时应该以群集IP回应,否则客户端不能成功接收。还需要调整内核的ARP响应参数阻止更新VIP的MAC地址,因为客户端在发送ARP请求的时候IP地址是群集地址,这是所有的节点都配置了VIP,这时客户端的ARP缓存内就会出现多条重复的IP地址对应的MAC地址确不相同,这回导致客户端无法正确找到调度器。 . 在节点(A,B)上配置虚拟ip地址v1

[root@centos1 /]# vim /etc/sysconfig/network-scripts/ifcfg-lo:0 DEVICE=lo:0 IPADDR=172.16.16.172 NETMASK=255.255.255.255 ONBOOT=yes

[root@centos1 /]# cd /etc/sysconfig/network-scripts/ [root@centos1 /]# ifup lo:0 [root@centos1 /]# ifconfig lo:0 . 添加vip本地访问路由

[root@centos1 /]# vim /etc/rc.local /sbin/route add -host 172.16.16.172 dev lo:0 //永久生效 [root@centos1 /]# route add -host 172.16.16.172 dev eth0 //临时生效 [root@centos1 /]# route add -host 172.16.16.172 dev lo:0 . 调整/proc响应参数 vim /etc/sysctl.conf,添加6行 [root@centos1 /]# vim /etc/sysctl.conf net.ipv4.ip_forward = 0 net.ipv4.conf.all.arp_ignore = 1 net.ipv4.conf.all.arp_announce = 2 net.ipv4.conf.default.arp_ignore = 1 net.ipv4.conf.default.arp_announce = 2 net.ipv4.conf.lo.arp_ignore = 1 net.ipv4.conf.lo.arp_announce = 2 . [root@centos1 /]# sysctl -p

. . . 配置调度器v8 配置虚拟ip地址(vip) [root@centos1 /]# cd /etc/sysconfig/network-scripts/ [root@centos1 /]# cp ifcfg-eth0 ifcfg-eth0:0 [root@centos1 /]# vi ifcfg-eth0:0 DEVICE=eth0:0 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=none IPADDR=172.16.16.172 NETMASK=255.255.255.0 . 执行:ifup eth0:0 [root@centos1 /]# service network restart . 调整/proc响应参数 因为lvs负载调度器和各节点需要共用vip地址,应该关闭linux内核重定向参数响应,打开vi /etc/sysctl.conf,增加三行。 [root@centos1 /]# vim /etc/sysctl.conf

net.ipv4.conf.all.send_redirects = 0 net.ipv4.conf.default.send_redirects = 0 net.ipv4.conf.eth0.send_redirects = 0 . 执行Sysctl -p 配置负载分配策略

[root@centos1 /]# sysctl -p [root@centos1 /]# service ipvsadm stop [root@centos1 /]# ipvsadm -A -t 172.16.16.172:80 -s rr [root@centos1 /]# ipvsadm -a -t 172.16.16.172:80 -r 172.16.16.177:80 -g -w 1 [root@centos1 /]# ipvsadm -a -t 172.16.16.172:80 -r 172.16.16.178:80 -g -w 1 [root@centos1 /]# service ipvsadm save [root@centos1 /]# chkconfig ipvsadm on

. 在客户端上测试,每次打开浏览器访问的页面在两台web服务器之间切换就对了。在实际工作中两个web节点上的网页要保持一致,这样就能始终访问一个相同的网站,从而能实现负载均衡。

.