Linux集群概述
根据功能划分为两大类:高可用和负载均衡
高可用集群
• 高可用集群通常为两台服务器,一台工作,另外一台作为冗余,当提供服务的机器宕机,冗余将接替继续提供服务
• 实现高可用的开源软件有:heartbeat、keepalived
负载均衡
• 负载均衡集群,需要有一台服务器作为分发器,它负责把用户的请求分发给后端的服务器处理,在这个集群里,除了分发器外,就是给用户提供服务的服务器了,这些服务器数量至少为2
• 实现负载均衡的开源软件有LVS、keepalived、haproxy、nginx,商业的有F5、Netscaler
keepalived
Keepalived通过VRRP(virtual router redundancy protocol)虚拟路由冗余协议来实现高可用。在这个协议里会将多台功能相同的路由器组成一个小组,这个小组会有1个master角色和N(N≥1)个backup角色。
Keepalived有三个模块:core、check、vrrp。其中core模块为Keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析;check模块负责健康检查,vrrp负责实现vrrp协议。
keepalived配置高可用集群
1.首先准备两台机器,都安装keepalived (137和139,137作为master,139作为backup)
A机器,安装keepalived
[root@harry-01 ~]# yum install -y keepalived
B机器,安装keepalived
[root@harry-01 ~]# yum install -y keepalived
这里使用 nginx ,把它作为一个高可用的对象——>使用nginx作为演示对象的原因,因为nginx在工作中,在生产环境中,很多企业把nginx做一个负载均衡器
假设nginx一旦挂掉,那么后端所有的web,即使说是正常的,那也无法访问
若是A、B机器没有装nginx服务,可以直接 yum安装
若是lnmp安装过nginx,则不需要安装了(源码包安装的nginx)直接yum安装的nginx,两者很容易区分(PS:有时直接yum安装不了,需要安装yum扩展源——>yum install -y epel-release)
[root@harry-01 local]# yum -y install nginx
[root@harry-01 local]# system
[root@harry-01 local]# systemctl start nginx
[root@harry-01 local]# ps aux | grep nginx
root 5396 0.0 0.1 125008 2116 ? Ss 08:34 0:00 nginx: master process /usr/sbin/nginx
nginx 5397 0.0 0.1 125396 3152 ? S 08:34 0:00 nginx: worker process
root 5406 0.0 0.0 112724 988 pts/0 S+ 08:35 0:00 grep --color=auto nginx
[root@harry-01 local]#
2.更改keepalived配置文件
默认的配置文件路径在/etc/keepalived/keepalived.conf
清空文件的快捷键方法 > !$
A机器更改配置文件
[root@harry-01~]# ls /etc/keepalived/keepalived.conf
/etc/keepalived/keepalived.conf
[root@harry-01 ~]# > !$ //直接清空文件内容了
> /etc/keepalived/keepalived.conf
[root@harry-01 ~]# cat /etc/keepalived/keepalived.conf
[root@harry-01 ~]# vim /etc/keepalived/keepalived.conf //去文件地址去下载内容
将拷贝的内容复制进去
只需要改网卡名字和漂移IP为192.168.31.137
####################### # 全局配置 #######################
global_defs { //global_defs 全局配置标识
notification_email { //notification_email用于设置报警邮件地址
han1118feng@163.com //可以设置多个,每行一个
}
notification_email_from root@aminglinux.com //设置邮件发送地址
smtp_server 127.0.0.1 //设置邮件的smtp server地址
smtp_connect_timeout 30 //设置连接smtp sever超时时间
router_id LVS_DEVEL
}
###################### # VRRP配置 ######################
vrrp_script chk_nginx {
script "/usr/local/sbin/check_ng.sh" //检查服务是否正常,通过写脚本实现,脚本检查服务健康状态
interval 3 //检查的时间间断是3秒
}
vrrp_instance VI_1 { //VRRP配置标识 VI_1是实例名称
state MASTER //定义master相关
interface eno16777736 //通过vrrp协议去通信、去发广播。配置时,需注意自己的网卡名称
virtual_router_id 51 //定义路由器ID ,配置的时候和从机器一致
priority 100 //权重,主角色和从角色的权重是不同的
advert_int 1 //设定MASTER与BACKUP主机质检同步检查的时间间隔,单位为秒
authentication { //认证相关信息
auth_type PASS //这里认证的类型是PASS
auth_pass aminglinux>com //密码的形式是一个字符串
}
virtual_ipaddress { //设置虚拟IP地址 (VIP),又叫做漂移IP地址
192.168.31.137 //更改为192.168.74.100
}
track_script { //加载脚本
chk_nginx
}
}
保存退出
3.virtual_ipaddress:简称VIP,这个vip,两台机器,一个主,一个从,正常的情况是主在服务,主宕掉了,从起来了,从启动服务,从启动nginx以后,,启动以后,访问那个IP呢?把域名解析到那个IP上呢?假如解析到主上,主宕掉了,所以这个,需要定义一个公有IP(主上用的IP,从上也用的IP);这个IP是随时去配置的
4.定义监控脚本
脚本路径在keepalived配置文件中有定义,路径为/usr/local/sbin/check_ng.sh
A机器定义监控脚本
[root@harry-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
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
保存退出
“脑裂”,在高可用(HA)系统中,当联系2个节点的“心跳线”断开时,本来为一整体、动作协调的HA系统,就分裂成为2个独立的个体。由于相互失去了联系,都以为是对方出了故障。两个节点上的HA软件像“裂脑人”一样,争抢“共享资源”、争起“应用服务”,就会发生严重后果——或者共享资源被瓜分、2边“服务”都起不来了;或者2边“服务”都起来了,但同时读写“共享存储”,导致数据损坏
如何判断脑裂?
分别在两台机查看当前服务器是否拥有虚拟IP,如果两台服务器都拥有,则说明发生了脑裂,证明目前双机通信出现问题,产生此问题的原有在于 两台服务器都探测不到组内其他服务器的状态(心跳请求无法正常响应),私自判定另一台服务器挂起,则抢占虚拟IP,脑裂的出现是不被允许的,解决此问题的方法为检查防火墙设置(关闭防火墙)或者使用串口通信。
5.脚本创建完之后,还需要改变脚本的权限(不更改权限,就无法自动加载脚本,那就无法启动keepalived服务)
[root@harry-01 ~]# chmod 755 /usr/local/sbin/check_ng.sh
[root@harry-01 ~]#
6.启动keepalived服务,并查看是否启动成功(PS:启动不成功,有可能是防火墙未关闭或者规则限制导致的)
-
systemctl stop firewalld 关闭firewalld
-
iptables -nvL
-
setenforce 0 临时关闭selinux
-
getenforce命令查看是否为Permissive
-
这时再来启动keepalived,就会看到keepalived进程服务了
[root@harry-01 ~]# vim /usr/local/sbin/check_ng.sh
[root@harry-01 ~]# chmod 755 /usr/local/sbin/check_ng.sh
[root@harry-01 ~]# systemctl start keepalived
[root@harry-01 ~]# ps aux | grep keepalived
root 5381 0.0 0.0 118588 1392 ? Ss 08:54 0:00 /usr/sbin/keepalived -D
root 5382 0.0 0.1 129516 3300 ? S 08:54 0:00 /usr/sbin/keepalived -D
root 5383 0.4 0.1 129456 2828 ? S 08:54 0:00 /usr/sbin/keepalived -D
root 5443 0.0 0.0 112724 992 pts/0 S+ 08:54 0:00 grep --color=auto keepalived
[root@harry-01 ~]#
7.查看nginx服务进程
[root@harry-01 ~]# ps aux | grep nginx
root 5418 0.0 0.0 45816 1136 ? Ss 08:54 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/ngin/conf/nginx.conf
nobody 5422 0.0 0.2 48304 4028 ? S 08:54 0:00 nginx: worker process
nobody 5423 0.0 0.2 48304 3772 ? S 08:54 0:00 nginx: worker process
root 5584 0.0 0.0 112724 988 pts/0 S+ 08:55 0:00 grep --color=auto nginx
[root@harry-01 ~]#
8.这时停止nginx服务
[root@harry-01 ~]# /etc/init.d/nginx stop
Stopping nginx (via systemctl): [ 确定 ]
9.再来查看nginx服务进程,会看到自动加载了
[root@harry-01 ~]# ps aux | grep nginx
root 5910 0.0 0.0 45816 1128 ? Ss 08:57 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/ngin/conf/nginx.conf
nobody 5912 0.0 0.2 48304 3768 ? S 08:57 0:00 nginx: worker process
nobody 5913 0.0 0.2 48304 3768 ? S 08:57 0:00 nginx: worker process
root 5929 0.0 0.0 112724 988 pts/0 S+ 08:57 0:00 grep --color=auto nginx
[root@harry-01 ~]#
10.keepalived日志文件路径 ------ /var/log/messages
11.查看ip地址,使用 ip addr 命令,而不能使用ifconfig命令,因为 ifconfig命令 是无法查看到vip192.168.133.100这个IP的
[root@harry-01 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:d9:20:3c brd ff:ff:ff:ff:ff:ff
inet 192.168.31.136/24 brd 192.168.31.255 scope global noprefixroute dynamic ens33
valid_lft 1789sec preferred_lft 1789sec
inet 192.168.188.100/32 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.31.137/24 brd 192.168.31.255 scope global secondary ens33
valid_lft forever preferred_lft forever
inet6 fe80::fa79:e339:c3:340/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@harry-01 ~]#
检查A、B 机器下防火墙和selinux是否关闭了,若没有关闭,可能会导致实验失败
- systemctl stop firewalld 关闭firewalld
- iptables -nvL
- setenforce 0 临时关闭selinux
- getenforce命令查看是否为Permissive
backup 机器配置
1.关闭B机器的防火墙和selinux
- iptables -F 清空规则
- setenforce 0 临时关闭selinux
2.自定义B机器keepalived配置文件,更改虚拟IP和主一样的
首先清空B机器keepalived里面自带的配置文件
[root@harry-01~]# > /etc/keepalived/keepalived.conf
[root@harry-01 ~]# cat !$
cat /etc/keepalived/keepalived.conf
[root@harry-01 ~]#
然后复制配置文件并粘贴进去,更改虚拟IP和主一样的
[root@harry-01 ~]# vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
aming@aminglinux.com
}
notification_email_from root@aminglinux.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 //这里 和master不一样的名字
interface eno16777736 //网卡和当前机器一致,否则无法启动keepalived服务
virtual_router_id 51 //和主机器 保持一致
priority 90 //权重,要比主机器小的数值
advert_int 1
authentication {
auth_type PASS
auth_pass aminglinux>com
}
virtual_ipaddress {
192.168.31.139 //这里更改为192.168.31.137
}
track_script {
chk_nginx
}
}
保存退出
3.定义监控脚本,路径再keepalived里面已定义过
这个脚本和主上的脚本有一点区别,启动nginx的命令不同,因为一个是yum安装,一个是源码包安装
#时间变量,用于记录日志
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
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
~
4.改动脚本的权限,设置为755权限
[root@harry-01 local]# chmod 755 /usr/local/sbin/check_ng.sh
[root@harry-01 local]#
5. B机器启动keepalived服务
[root@harry-01 local]# systemctl start keepalived
[root@harry-01 local]# ps aux | grep keep
root 6850 0.0 0.0 118588 1392 ? Ss 09:22 0:00 /usr/sbin/keepalived -D
root 6851 0.0 0.1 129516 3296 ? S 09:22 0:00 /usr/sbin/keepalived -D
root 6852 0.0 0.1 129456 2824 ? S 09:22 0:00 /usr/sbin/keepalived -D
root 6899 0.0 0.0 112724 988 pts/0 R+ 09:22 0:00 grep --color=auto keep
[root@harry-01 local]#
测试高可用
模拟线上生产环境,主机器宕机环境,最简单直接的方法,就是直接关闭keepalived服务
关闭master机器(即A机器)上的keepalived服务关闭
[root@harry-01 ~]# systemctl stop keepalived
[root@harry-01 ~]#
查看A机器上的VIP被已经释放掉了
查看backup机器(即B机器)在监听VIP
查看B机器日志
[root@harry-01 local]# tail /var/log/messages
Dec 31 11:06:55 harry-01 Keepalived_vrrp[14579]: Sending gratuitous ARP on ens33 for 192.168.31.100
Dec 31 11:06:55 harry-01 Keepalived_vrrp[14579]: Sending gratuitous ARP on ens33 for 192.168.31.100
Dec 31 11:06:55 harry-01 Keepalived_vrrp[14579]: Sending gratuitous ARP on ens33 for 192.168.31.100
Dec 31 11:06:55 harry-01 Keepalived_vrrp[14579]: Sending gratuitous ARP on ens33 for 192.168.31.100
Dec 31 11:07:00 harry-01 Keepalived_vrrp[14579]: Sending gratuitous ARP on ens33 for 192.168.31.100
Dec 31 11:07:00 harry-01 Keepalived_vrrp[14579]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.31.100
Dec 31 11:07:00 harry-01 Keepalived_vrrp[14579]: Sending gratuitous ARP on ens33 for 192.168.31.100
Dec 31 11:07:00 harry-01 Keepalived_vrrp[14579]: Sending gratuitous ARP on ens33 for 192.168.31.100
Dec 31 11:07:00 harry-01 Keepalived_vrrp[14579]: Sending gratuitous ARP on ens33 for 192.168.31.100
Dec 31 11:07:00 harry-01 Keepalived_vrrp[14579]: Sending gratuitous ARP on ens33 for 192.168.31.100
在生产环境中,可能会用到2-3台backup角色, vim /etc/keepalived/keepalived.conf 这里面的权重调成不通级别,权重越高优先级越高!除了nginx服务的话,还可以做MySQL的高可用集群服务。(做mysql的高可用,一定要保证两边的数据一致)
负载均衡集群介绍
•主流开源软件LVS、keepalived、haproxy、nginx等
• 其中LVS属于4层(网络OSI 7层模型),nginx属于7层,haproxy既可以认为是4层,也可以当做7层使用
• keepalived的负载均衡功能其实就是lvs
• lvs这种4层的负载均衡是可以分发除80外的其他端口通信的,比如MySQL的,而nginx仅仅支持http,https,mail,haproxy也支持MySQL这种
• 相比较来说,LVS这种4层的更稳定,能承受更多的请求,而nginx这种7层的更加灵活,能实现更多的个性化需求
LVS介绍
LVS是基于tcp/ip做的路由和转发,稳定性和效率很高,LVS最新版本是基于Linux内核2.6。LVS常见的模式有三种:NAT、DR、IP Tunnel。LVS架构中有一个核心角色叫分发器(Load Balance),用于分发客户的各种请求,另外还有诸多服务器(real server,简称rs),用于处理用户各种请求。
NAT模式
•这种模式借助iptables的nat表来实现
• 用户的请求到分发器后,通过预设的iptables规则,把请求的数据包转发到后端的rs上去
• rs需要设定网关为分发器的内网ip
• 用户请求的数据包和返回给用户的数据包全部经过分发器,所以分发器成为瓶颈
• 在nat模式中,只需要分发器有公网ip即可,所以比较节省公网ip资源
在该模式中,load balancer是限制该架构处理用户请求的一个瓶颈,所以该模式比较适合小模式的集群(服务器rs在10台以内);该模式的优势是节省公网资源。
LVS IP Tunnel模式
•这种模式,需要有一个公共的IP配置在分发器和所有rs上,我们把它叫做vip
• 客户端请求的目标IP为vip,分发器接收到请求数据包后,会对数据包做一个加工,会把目标IP改为rs的IP,这样数据包就到了rs上
• rs接收数据包后,会还原原始数据包,这样目标IP为vip,因为所有rs上配置了这个vip,所以它会认为是它自己
LVS DR模式
• 这种模式,也需要有一个公共的IP配置在分发器和所有rs上,也就是vip
• 和IP Tunnel不同的是,它会把数据包的MAC地址修改为rs的MAC地址
• rs接收数据包后,会还原原始数据包,这样目标IP为vip,因为所有rs上配置了这个vip,所以它会认为是它自己
LVS调度算法
•轮询 Round-Robin rr
• 加权轮询 Weight Round-Robin wrr
• 最小连接 Least-Connection lc
• 加权最小连接 Weight Least-Connection wlc
• 基于局部性的最小连接 Locality-Based Least Connections lblc
• 带复制的基于局部性最小连接 Locality-Based Least Connections with Replication lblcr
• 目标地址散列调度 Destination Hashing dh
源地址散列调度 Source Hashing sh
常用的是前面四种算法。
LVS NAT模式搭建
准备工作
网络准备
三台机器
分发器(调度器director)
内网:192.168.8.133 外网:192.168.75.128
real server1(real1)
内网:192.168.8.134 设置网关为: 192.168.8.133
real server2(real2)
内网:192.168.8.135 设置网关为: 192.168.8.133
防火墙( 分别在三台机器进行以下操作)
关闭系统防火墙:
[root@real1 ~]# systemctl stop firewalld
[root@real1 ~]# systemctl disable firewalld
关闭SELinux:
[root@director ~]# setenforce 0
[root@director ~]# vim /etc/selinux/config
SELINUX=disabled
以下操作在real1和real2中进行:
安装iptables服务:
[root@real1 ~]#yum install -y iptables-services
设置iptables服务:
[root@real1 ~]# iptables -F
[root@real1 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ 确定 ]
进行该设置的原因是NAT模式是基于防火墙nat表的一种模式,所以会使用iptables规则。
开始搭建
配置director:
安装ipvsadm工具:
[root@director ~]# yum install -y ipvsadm
配置ipvsadm脚本:
[root@director ~]# 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
# 注意区分网卡名字,adai的两个网卡分别为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.8.0/24 -j MASQUERADE
# director设置ipvsadm规则
IPVSADM='/usr/sbin/ipvsadm'
$IPVSADM -C
#-C=clear,清除规则
$IPVSADM -A -t 192.168.75.128:80 -s rr
#-A:=add,添加规则;-t:=tcp;-s 指定算法;
#在此可以添加-p:指定超时时间(解决session问题:保证同一请求被分发到同一rs上)
#因为添加-p选项后会影响测试效果,所以在此不加该参数(注:时间不能设置为0)
$IPVSADM -a -t 192.168.75.128:80 -r 192.168.8.134:80 -m -w 1
$IPVSADM -a -t 192.168.75.128:80 -r 192.168.8.135:80 -m -w 1
#-a:=add,增加nat架构中的rs;-r:指定rs的IP;-m:指定LVS模式为NAT(masquerade)
#-w:=weight,指定权重
执行该脚本:
[root@director ~]# sh /usr/local/sbin/lvs_nat.sh
#注:执行该脚本时无错误输出说明没问题。
查看ipvsadm规则:
[root@director ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.75.128:80 rr
-> 192.168.8.134:80 Masq 1 1 3
-> 192.168.8.135:80 Masq 1 0 4
配置rs:
在两台rs上安装Nginx,并分别设置其主页用来区分两台机器。
[root@real1 ~]# yum install -y nginx
[root@real1 ~]# systemctl start nginx
编辑rs1主页:
[root@real1 ~]# vim /usr/share/nginx/html/index.html
This is real server 1.
编辑rs2主页:
[root@real2 ~]# vim /usr/share/nginx/html/index.html
This is real server 2.
测试
访问公网IP:192.168.75.128
[root@director ~]# curl 192.168.75.128
This is real server 2.
[root@director ~]# curl 192.168.75.128
This is real server 1.
[root@director ~]# curl 192.168.75.128
This is real server 2.
[root@director ~]# curl 192.168.75.128
This is real server 1.
[root@director ~]# curl 192.168.75.128