day15.3
day15.3
目录
nginx负载均衡高可用
# keepalived概述
Keepalived是一个高可用软件,可以和任何应用配合使用
# 什么是高可用
一般是指2台机器启动着完全相同的业务系统,当有一台机器down机了,另外一台服务器就能快速的接管,对于访问的用户是无感知的。
# 高可用软件
1.硬件 F5
2.软件 keepalived heartbeat
3.Mysql MGR MHA
4.redis Redis-Cluster Sentinel
keepailved实现原理
# keepalived底层协议:VRRP(虚拟路由冗余协议)
keepailved核心概念
1.通过选举投票,决定谁是主节点谁是备节点(选举)
2.如果Master故障,Backup自动接管,那么Master恢复后会夺权吗(抢占试、非抢占式)
3.两台服务器都认为自己是master,那么会出现一个故障(脑裂)
keepailved安装配置
环境准备
主机名 | wanip | lanip | 角色 | 应用 |
---|---|---|---|---|
lb01 | 10.0.0.5 | 172.16.1.5 | Master keepalived主节点 | keepalived |
lb02 | 10.0.0.6 | 172.16.1.6 | Backup keepalived备节点 | keepalived |
部署keepalive
# 1.安装Keepalived
[root@lb01 ~]# yum -install -y keepalived
# 2.查找Keepalived配置文件
[root@lb01 ~]# rpm -ql keepalived
/etc/keepalived/keepalived.conf
# 3.修改Keepalived配置文件
[root@lb01 conf.d]# vim /etc/keepalived/keepalived.conf
global_defs { #全局配置
router_id lb01 #标识身份->名称
}
vrrp_instance VI_1 {
state MASTER #标识角色状态
interface eth0 #网卡绑定接口
virtual_router_id 50 #虚拟路由id
priority 150 #优先级
advert_int 1 #监测间隔时间
authentication { #认证
auth_type PASS #认证方式
auth_pass 1111 #认证密码
}
virtual_ipaddress {
10.0.0.3 #虚拟的VIP地址
}
}
# 4.修改backup配置文件
[root@lb02 ~]# vim /etc/keepalived/keepalived.conf
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 50
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3
}
}
Keepalived配置区别 | master节点配置 | backup节点配置 |
---|---|---|
router_id | lb01 | lb02 |
state | master | backup |
priority | 150 | 100 |
# 1.启动master上的Keepalived
[root@lb01 ~]# systemctl start keepalived
[root@lb01 ~]# systemctl enable keepalived
注意:只要停止掉Keepalived,VIP会漂移到另一个节点
非抢占式配置
# 配置需求
1、两个节点的state都必须配置为BACKUP
2、两个节点都必须加上配置 nopreempt
3、其中一个节点的优先级必须要高于另外一个节点的优先级
# mast而节点配置
[root@lb01 ~]# !vim
vim /etc/keepalived/keepalived.conf
global_defs { #全局配置
router_id lb01 #标识身份->名称
}
vrrp_instance VI_1 {
state BACKUP #标识角色状态
interface eth0 #网卡绑定接口
nopreempt
virtual_router_id 50 #虚拟路由id
priority 150 #优先级
advert_int 1 #监测间隔时间
authentication { #认证
auth_type PASS #认证方式
auth_pass 1111 #认证密码
}
virtual_ipaddress {
10.0.0.3 #虚拟的VIP地址
}
}
# backup节点配置
[root@lb02 ~]# !vim
vim /etc/keepalived/keepalived.conf
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
nopreempt
virtual_router_id 50
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3
}
}
脑裂的原因
1、服务器网线松动等网络故障
2、服务器硬件故障发生损坏现象而崩溃
3、主备都开启rewalld防火墙
# 解决脑裂故障
[root@lb02 ~]# vim check_split_brain.sh
#!/bin/sh
vip=10.0.0.3
lb01_ip=10.0.0.5
while true;do
ping -c 2 $lb01_ip &>/dev/null
if [ $? -eq 0 -a `ip add|grep "$vip"|wc -l` -eq 1 ];then
echo "ha is split brain.warning."
else
echo "ha is ok"
fi
sleep 5
done
Keepalived结合nginx做高可用
环境准备
主机名 | wanip | lanip | 角色 | 应用 |
---|---|---|---|---|
lb01 | 10.0.0.5 | 172.16.1.5 | Master keepalived主节点 | keepalived、nginx |
lb02 | 10.0.0.6 | 172.16.1.6 | Backup keepalived备节点 | keepalived、nginx |
web01 | 10.0.0.7 | 172.16.1.7 | web网站 | nginx、php |
web02 | 10.0.0.8 | 172.16.1.8 | web网站 | nginx、php |
![]() |
关联nginx
# 公司使用脚本
[root@lb01 ~]# vim check_web.sh
#!/bin/sh
nginx_count=$(ps -ef|grep [n]ginx|wc -l)
#1.判断Nginx是否存活,如果不存活则尝试启动Nginx
if [ $nginx_count -eq 0 ];then
systemctl start nginx
sleep 3
#2.等待3秒后再次获取一次Nginx状态
nginx_count=$(ps -ef|grep [n]ginx|wc -l)
#3.再次进行判断, 如Nginx还不存活则停止Keepalived,让地址进行漂移,并退出脚本
if [ $nginx_count -eq 0 ];then
systemctl stop keepalived
fi
fi
# 上课脚本
[root@lb01 ~]# vim check_web.sh
nginx_count=$(ps -ef|grep [n]ginx|wc -l)
if [ $nginx_count -eq 0 ];then
systemctl stop keepalived
fi
# 关闭lb01上的nginx
[root@lb01 ~]# systemctl stop nginx
# 查看lb02