28 集群

18.1 集群介绍

1. 根据功能划分为两大类:高可用负载均衡
2. 高可用集群通常为两台服务器,一台工作,另外一台作为冗余,当提供服务的机器宕机,冗余将接替继续提供服务;实现高可用的开源软件有:heartbeatkeepalived
3. 负载均衡集群,需要有一台服务器作为分发器,它负责把用户的请求分发给后端的服务器处理,在这个集群里,除了分发器外,就是给用户提供服务的服务器了,这些服务器数量至少为2;负载均衡的开源软件有LVSkeepalivedhaproxynginx,商业的有F5Netscaler

18.2 keepalived介绍

1. 在这里我们使用keepalived来实现高可用集群,因为heartbeat在centos6上有一些问题,影响实验效果
2. keepalived通过VRRP虚拟路由冗余协议(Virtual Router Redundancy Protocl)来实现高可用。
3. 在这个协议里会将多台功能相同的路由器组成一个小组,这个小组里会有1个master角色和N(N>=1)个backup角色。4. master会通过组播的形式向各个backup发送VRRP协议的数据包,当backup收不到master发来的VRRP数据包时,就会认为master宕机了。此时就需要根据各个backup的优先级来决定谁成为新的mater。
5. Keepalived要有三个模块,分别是core、check和vrrp。其中core模块为keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析,check模块负责健康检查,vrrp模块是来实现VRRP协议的。

18.3-18.5用keepalived配置高可用集群(上中下)

1. 准备两台机器hao1hao2 hao1作为masterhao2作为backup

2. 两台机器都安装 :yum install -y keepalived
[root@hao-01 ~]# yum install -y keepalived
[root@hao-02 ~]# yum install -y keepalived

3. 两台机器都安装nginx,可以编译安装nginx 也可以用yum安装nginx : yum install -y nginx
安装epel仓库(源码包,默认centos不带nginx包) :yum install -y epel-release    #第三方源   
yum install -y nginx
yum安装nginx启动方式:service nginx start
编译安装nginx启动方式:/etc/init.d/nginx start

4. 搜索nginx是否启动 ?
[root@hao-01 ~]# ps aux |grep nginx
[root@hao-02 ~]# ps aux |grep nginx

hao1机器上操作(这里hao1是编译安装的nginx)

5. 清空keepalived.conf文件中的内容(hao1机器) :
[root@hao-01 ~]# > /etc/keepalived/keepalived.conf
6. 重新配置keepalived.conf文件(hao1机器) :
[root@hao-01 ~]# vim /etc/keepalived/keepalived.conf
插入内容:

global_defs {
 bal_defs {
  notification_email {   
#指定keepalived在发生切换时需要发送email到的对象,一行一个
    [email protected]     
  }
  notification_email_from root@
hao.com  
 #指定发件人
  smtp_server 127.0.0.1  #指定smtp服务器地址
  smtp_connect_timeout 30 #指定smtp连接超时时间
  router_id LVS_DEVEL  #运行keepalived机器的一个标识
}
vrrp_script chk_nginx { 

   script "/usr/local/sbin/check_ng.sh"
   interval 3
}
vrrp_instance VI_1 { 
 
   state MASTER   #指定那个为master,那个为backup
   interface ens33 #设置实例绑定的网卡 VRRP心跳包从哪块网卡发出
   virtual_router_id 51 #VPID标记 相同VRID的LVS属于同一组,根据优先级选举出一个主
   priority 100  #优先级,高优先级竞选为master
   advert_int 1  #检查间隔,默认1秒 VRRP心跳包的发送周期,单位为s 组播信息发送间隔,两个节点设置必须一样(实际并不一定完全是10秒,测试结果是小于10秒的随机值)
   authentication {  #设置认证
       auth_type PASS  #认证方式
       auth_pass py07133647262  #认证密码(密码只识别前8位)
   }  
   virtual_ipaddress {  
#设置vip
      192.168.130.100  
   }  
   track_script {
  #在实例中引用脚本 # cat /etc/keepalived/check_nginx.sh
       chk_nginx
   }  

}

7. 创建check_ng.sh脚本,并添加内容 :[root@hao-01 ~]# vim /usr/local/sbin/check_ng.sh

插入内容:

#!/bin/bash  #时间变量,用于记录日志
d=`date --date today +%Y%m%d_%H:%M:%S`#计算nginx进程数量
n=`ps -C nginx --no-heading|wc -l`
#如果进程为0,则启动nginx,并且再次检测nginx进程数量,
#如果继续还是为0,说明nginx无法启动,此时需要关闭keepalived
if [ $n -eq "0" ]; then
       /etc/init.d/nginx start
#yum安装的nginx用:systemctl start nginx  编译安装的用:/etc/init.d/nginx start
       n2=`ps -C nginx --no-heading|wc -l`
       if [ $n2 -eq "0"  ]; then
               echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
               systemctl stop keepalived
       fi
fi

clipboard.png

8. 设定check_ng.sh脚本755权限 [root@hao-01 ~]# chmod 755 /usr/local/sbin/check_ng.sh

9. 启动keepalived [root@hao-01 ~]# systemctl start keepalived

10. 搜索keepalived是否启动 [root@hao-02 ~]# ps aux |grep keepalived

clipboard.png

11. 关闭Nginx,再搜索nginx是否启动 
[root@hao-01 ~]# /etc/init.d/nginx stop
[root@hao-01 ~]# ps aux |grep nginx
临时关闭getenforce防火墙 [root@hao-01 ~]# setenforce 0
关闭firewalld防火墙[root@hao-01 ~]# systemctl stop firewalld

hao2机器上操作(这里hao2是yum安装的nginx)

12. 临时关闭getenforce防火墙 [root@hao-02 ~]# setenforce 0

关闭firewalld防火墙[root@hao-02 ~]# systemctl stop firewalld

13. 清空keepalived.conf文件中的内容(hao2机器) :
[root@hao-02 ~]# > /etc/keepalived/keepalived.conf

14. 重新配置keepalived.conf文件(hao2机器) :
[root@hao-02 ~]# vim /etc/keepalived/keepalived.conf

global_defs {
  notification_email {   
#指定keepalived在发生切换时需要发送email到的对象,一行一个
    [email protected]
  }
  notification_email_from root@hao.com
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
}
vrrp_script chk_nginx {
   script "/usr/local/sbin/check_ng.sh"
   interval 3
}
vrrp_instance VI_1 {
   state BACKUP
   interface ens33
   virtual_router_id 51
   priority 90
   advert_int 1
   authentication {
       auth_type PASS
      auth_pass py07133647262
   }
   virtual_ipaddress {
      192.168.130.100
   }
   track_script {
       chk_nginx
   }
}

15. 创建check_ng.sh脚本,并添加内容 :

[root@hao-02 ~]# vim /usr/local/sbin/check_ng.sh

#!/bin/bash        #时间变量,用于记录日志
d=`date --date today +%Y%m%d_%H:%M:%S`   #计算nginx进程数量
n=`ps -C nginx --no-heading|wc -l`      #如果进程为0,则启动nginx,并且再次检测nginx进程数量,
#如果继续还是为0,说明nginx无法启动,此时需要关闭keepalived
if [ $n -eq "0" ]; then
       systemctl start nginx
#yum安装的nginx用:systemctl start nginx  编译安装的用:/etc/init.d/nginx start
       n2=`ps -C nginx --no-heading|wc -l`
       if [ $n2 -eq "0"  ]; then
               echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
               systemctl stop keepalived
      fi
fi

16. 设定check_ng.sh脚本755权限 [root@hao-02 ~]# chmod 755 /usr/local/sbin/check_ng.sh

17. 启动keepalived [root@hao-02 ~]# systemctl start keepalived

18. 搜索keepalived是否启动 [root@hao-02 ~]# ps aux |grep keepalived

hoa1 hao2机器都打开80端口:打开80端口 :
[root@hao-01 ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[root@hao-02 ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT

游览器访问hao1 hao2 ip 测试高可用

1. 关闭Nginx,再搜索nginx是否启动 
[root@hao-01 ~]# systemctl stop nginx
[root@hao-01 ~]# ps aux |grep nginxclipboard.png

2. ip add查看.100是不是在hao1机器上(master) :[root@hao-01 ~]# ip add

clipboard.png

模仿hao1机器挂了,关闭了 keepalived
[root@hao-01 ~]# systemctl stop keepalived
[root@hao-01 ~]# ip add

clipboard.png

3. hao1机器(master)挂了,hao2机器(backup)上,ip add查看.100自动挂载到了hao2上!

clipboard.png

4. hao1机器(master)再启动keepalived .100又挂在hao1机器 :[root@hao-01 ~]# systemctl start keepalived

clipboard.png

扩展
lvs 三种模式详解 :http://www.it165.net/admin/html/201401/2248.html
lvs几种算法 :http://www.aminglinux.com/bbs/thread-7407-1-1.html
关于arp_ignore和 arp_announce :http://www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
lvs原理相关的 :http://blog.csdn.net/pi9nc/article/details/23380589

18.6 负载均衡集群介绍

1. 主流开源软件LVS、keepalived、haproxy、nginx等
2. 其中LVS属于4层(网络OSI 7层模型),nginx属于7层,haproxy既可以认为是4层,也可以当做7层使用
3. keepalived的负载均衡功能其实就是lvs
4. lvs这种4层的负载均衡是可以分发除80外的其他端口通信的,比如MySQL的,而nginx仅仅支持http,https,mail,haproxy也支持MySQL这种
5. 相比较来说,LVS这种4层的更稳定,能承受更多的请求,而nginx这种7层的更加灵活,能实现更多的个性化需求

18.7 LVS介绍;

1. LVS是由国人章文嵩开发http://zh.linuxvirtualserver.org/
2. 流行度不亚于apache的httpd,基于TCP/IP做的路由和转发,稳定性和效率很高
3. LVS最新版本基于Linux内核2.6,有好多年不更新了
4. LVS有三种常见的模式:NAT、DR、IP Tunnel
5. LVS架构中有一个核心角色叫做分发器(Load balance),它用来分发用户的请求,还有诸多处理用户请求的服务器(Real Server,简称rs)

四种模式的性能比较:

因为DR模式,IP TUNELL模式都是在package in 时经过LVS ;在package out是直接返回给client;所以二者的性能比NAT 模式高;但IP TUNNEL 因为是TUNNEL 模式比较复杂,其性能不如DR模式;FULL NAT模式因为不仅要更换DST IP还更换SOURCE IP所以性能比NAT下降10%
所以,4中模式的性能如下:DR  –> IP TUNNEL  —>NAT —–>FULL NAT

参考:http://blog.51cto.com/lansgg/1229421

clipboard.png

  • 网络地址转换:多目标地址转换,来实现负载均衡

1、LVS(Director)上面需要双网卡:DIP(内网)和VIP(外网)
2、内网的Real Server主机的IP必须和DIP在同一个网络中,并且要求其网关都需要指向DIP的地址
3、RIP都是私有IP地址,仅用于各个节点之间的通信
4、Director位于client和Real Server之间,负载处理所有的进站、出站的通信
5、支持端口映射
6、通常应用在较大规模的应用场景中,但Director易成为整个架构的瓶颈!

nat模式
1   当client主机(cip)访问web服务器(rip)时,因为rip是内网ip,此时的目标ip是负载均衡器的前端ip(vip),当负载均衡器收到cip发来的报文后会根据里面的iptables规则里的地址,将报文里的目标地址改为web服务器的rip并将报文根据规则里的算法发送出去
2   client主机将报文发送到rip后,由于报文的目标地址是自己(cip),所以会响应请求,将响应报文返回给lvs,然后lvs将此报文的sip(rip)修改为vip并发送给client主机的客户端。
fullnat模式
1   client主机(cip)将请求发往前端的负载均衡器(vip),请求报文源地址是CIP,目标地址为VIP。负载均衡器收到报文后,发现请求的是在规则里面存在的地址,那么它将客户端请求报文的源MAC地址改为自己DIP的MAC地址,目标MAC改为了RIP的MAC地址,并将此包发送给RS。
2   RS发现请求报文中的目的MAC是自己,就会将次报文接收下来,处理完请求报文后,将响应报文通过lo接口送给eth0网卡直接发送给client主机(cip)。

相关机器信息;
(Director Server)  LB1   eth0:192.168.244.132 (Vip)  (公网)
                                         eth1:192.168.27.128  (Dip)     (内网)
rs1                 rs1    eth0:192.168.27.130  (Rip) (内网)GATEWAY:192.168.27.128  #GATEWAY 网关
rs2                 rs2    eth0:192.168.27.131  (Rip) (内网)GATEWAY:192.168.27.128

clipboard.png

  • 其实数据转发原理和上图是一样的,不过这个我个人认为主要是位于不同位置(不同机房);LB是通过隧道进行了信息传输,虽然增加了负载,可是因为地理位置不同的优势,还是可以参考的一种方案;

优点:负载均衡器只负责将请求包分发给物理服务器,而物理服务器将应答包直接发给用户。所以,负载均衡器能处理很巨大的请求量,这种方式,一台负载均衡能为超过100台的物理服务器服务,负载均衡器不再是系统的瓶颈。使用VS-TUN方式,如果你的负载均衡器拥有100M的全双工网卡的话,就能使得整个Virtual Server能达到1G的吞吐量。
不足:但是,这种方式需要所有的服务器支持"IP Tunneling"(IP Encapsulation)协议;隧道技术(专有网线) 使用比较复杂

LB1: eth0: 192.168.182.132
    vip(tunl0): 192.168.182.200
RS1: eth0:192.168.27.130
     tunl0(vip)  :192.168.182.200
RS2: eth0:192.168.138.131
     tunl0(vip) :192.168.182.200

clipboard.png

  • DR模型:直接路由模型,每个Real Server上都有两个IP:VIP(公网)和RIP,但是VIP是隐藏的,就是不能提高解析等功能,只是用来做请求回复的源IP的,Director上只需要一个网卡,然后利用别名来配置两个IP:VIP和DIP

1、各个集群节点必须和Director在同一个物理网络中
2、RIP地址不能为私有地址,可以实现便捷的远程管理和监控
3、Director仅仅负责处理入站请求,响应报文则由Real Server直接发往客户端
4、集群节点Real Server 的网关一定不能指向DIP,而是指向外部路由
5、Director不支持端口映射
6、Director能够支持比NAT多很多的Real Server 

原理:

DR模型:直接路由模型,每个Real Server上都有两个IP:VIP和RIP,但是VIP是隐藏的,就是不能提高解析等功能,只是用来做请求回复的源IP的,Director上只需要一个网卡,然后利用别名来配置两个IP:VIP和DIP
VIP:响应客户端请求;DIP:与RIP彼此间实现arp解析,并将客户端的请求转发给Real Server

LB1: eth0: 192.168.182.133
    vip(eth0:0): 192.168.182.200
RS1: eth0:192.168.182.130
     lo:0(vip)  :192.168.182.200
RS2: eth0:192.168.182.129
     lo:0(vip)  192.168.182.200

18.8 LVS的调度算法

前四条算法较为重要!!!

1. 轮询 Round-Robin  rr
2. 加权轮询 Weight Round-Robin wrr
3. 最小连接 Least-Connection lc
4. 加权最小连接 Weight Least-Connection wlc
5. 基于局部性的最小连接 Locality-Based Least Connections lblc
6. 带复制的基于局部性最小连接 Locality-Based Least Connections with Replication  lblcr
7. 目标地址散列调度 Destination Hashing dh
8. 源地址散列调度 Source Hashing  sh

18.9 LVS NAT模式搭建(上)

NAT模式搭建 – 准备工作
准备三台机器:hao1  hao2  hao3
hao1机器操作:
hao1机器作为:分发器,也叫调度器(简写为dir)

1. 添加一个新网卡,选择仅主机模式

ens33网卡内网:192.168.223.142(NAT模式)
ens37网卡外网:192.168.145.128(仅主机模式)

clipboard.png

2. 编辑ens37网卡配置文件 :
[root@hao-01 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens37
设定ip为(仅主机网段): 192.168.145.128

添加内容(不用设置网关) :

NAME=ens37
DEVICE=ens37
ONBOOT=no  #ONBOOT是指明在系统启动时是否激活网卡
IPADDR= 192.168.145.128
PREFIX=24  #prefix是前缀的意思,这里指子网掩码的位数。如255.255.255.0则在ifcfg-eth0的配置文件中:PREFIX=24
而NETMASK与PREFIX的作用是一样的,都是配置子网掩码,只是PREFIX更简洁而已,两个都存在时,PREFIX优先起作用

clipboard.png

3. 重启网络服务命令:(重启network.service网络服务)[root@hao-01 ~]# systemctl restart network.service

4. 激活ens37网卡[root@hao-01 ~]# ifup ens37

5. 在windows系统,ping下ens37(仅主机)外网ip:

clipboard.png

hao1   hao2   hao3机器都要执行下面关闭防火墙命令:

1. 关闭firewalld防火墙 :[root@hao-01 ~]# systemctl stop firewalld

设定开机不启动firewalld防火墙 :[root@hao-01 ~]# systemctl disable firewalld

查看firewalld防火墙是否关闭 ?[root@hao-01 ~]# iptables -nvL

clipboard.png

2. 安装centos 6系统中的iptables防火墙工具:[root@hao-01 ~]# yum install -y iptables-services

如果上面安装慢,临时重命名epel.repo,再yum 安装(记得改回重命名!):mv /etc/yum.repos.d/epel.repo  /etc/yum.repos.d/epel.repo1

启用iptables :[root@hao-01 ~]# systemctl enable iptables

开启iptables :[root@hao-01 ~]# systemctl start iptables

清除防火墙规则:[root@hao-01 ~]# iptables -F

关闭iptables :[root@hao-01 ~]# service iptables save

查看firewalld防火墙是否关闭?[root@hao-01 ~]# iptables -nvL

clipboard.png

3. 临时关闭getenforce防火墙 :[root@hao-01 ~]# setenforce 0

永久关闭getenforce防火墙:[root@hao-01 ~]# vi /etc/selinux/config

更改内容:SELINUX=disabled

clipboard.png

hao2   hao3机器上操作:ens33网卡网关改成ip段为内网段,ip为hao1内网ip(128)

1. 更改hao2机器ens33网卡配置文件 :

[root@hao-02 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33

clipboard.png

重启网卡 :[root@hao-02 ~]# systemctl restart network

2. 更改hao3机器ens33网卡配置文件:[root@hao-03 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33

clipboard.png

重启网卡 :[root@hao-03 ~]# systemctl restart network

18.10 LVS NAT模式搭建(下)

hao1机器(dir)上操作:1. 在hao1机器(dir)上,安装 ipvsadm :

[root@hao-01 ~]# yum install -y ipvsadm2. 在hao1机器(dir)上,编写lvs_nat.sh脚本 :

[root@hao-01 ~]# vim /usr/local/sbin/lvs_nat.sh

添加内容:

#! /bin/bash
# director 服务器上开启路由转发功能
echo 1 > /proc/sys/net/ipv4/ip_forward
# 关闭icmp的重定向
echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects
# 注意区分网卡名字,阿铭的两个网卡分别为ens33和ens37
echo 0 > /proc/sys/net/ipv4/conf/ens33/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/ens37/send_redirects
# director 设置nat防火墙
iptables -t nat -F
iptables -t nat -X
iptables -t nat -A POSTROUTING -s 192.168.223.0/24  -j MASQUERADE
# director设置ipvsadm
IPVSADM='/usr/sbin/ipvsadm'
$IPVSADM -C
$IPVSADM -A -t 192.168.145.128:80 -s rr
$IPVSADM -a -t 192.168.145.128:80 -r 192.168.223.143:80 -m -w 1
$IPVSADM -a -t 192.168.145.128:80 -r 192.168.223.144:80 -m -w 1

clipboard.png

3. 执行lvs_nat.sh脚本:[root@hao-01 ~]# sh /usr/local/sbin/lvs_nat.sh

hao2机器(r)上操作:

1. 启动nginx(yum安装的nginx):[root@hao-02 ~]# systemctl start nginx

2. 搜索nginx是否启动?[root@hao-02 ~]# ps aux |grep nginx

3. 清空index.html内容(yum安装的nginx):[root@hao-02 ~]# > /usr/share/nginx/html/index.html

4. 编辑index.html(yum安装的nginx):[root@hao-02 ~]# vim /usr/share/nginx/html/index.html

添加内容(便于和hao3区分):hao2

5. 查看:[root@hao-02 ~]# curl localhost

clipboard.png

hao3机器(r)上操作:

1. 启动nginx(yum安装的nginx):[root@hao-03 ~]# systemctl start nginx

2. 搜索nginx是否启动?[root@hao-03 ~]# ps aux |grep nginx

3. 清空index.html内容(yum安装的nginx):[root@hao-03 ~]# > /usr/share/nginx/html/index.html

4. 编辑index.html(yum安装的nginx):[root@hao-03 ~]# vim /usr/share/nginx/html/index.html

添加内容(便于和hao2区分):hao3

5. 查看 :[root@hao-03 ~]# curl localhost

clipboard.png

hao1机器(dir)上操作测试:

1. curl访问hao1机器(dir) ens37外网ip :[root@hao-01 ~]# curl 192.168.145.128

clipboard.png

 

 

 

猜你喜欢

转载自blog.csdn.net/xiaoyuerp/article/details/83060126
28
28-