什么是高可用
一般是指2台机器启动着完全相同的业务系统,当有一台机器down机了,另外一台服务器就能快速的接管,对于访问的用户是无感知的。
高可用通常使用什么软件?
硬件通常使用 F5软件通常使用 keepalived
keepalived是如何实现高可用的?
keepalived软件是基于VRRP协议实现的,VRRP虚拟路由冗余协议,主要用于解决单点故障问题
Keepalived高可用安装配置 {#p-2}
|------| | 环境准备 |
|--------------| | 安装keepalived |
[root@lb01 ~]# yum install -y keepalived
[root@lb02 ~]# yum install -y keepalived
1-配置master
#找到配置文件
[root@lb02 ~]# rpm -qc keepalived
/etc/keepalived/keepalived.conf
/etc/sysconfig/keepalived
#编辑配置文件
[root@lb01 ~]# cat /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地址
}
}
209上的配置:
global_defs {
router_id lb01
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 50
priority 150
unicast_src_ip 192.168.1.209 #本机ip
unicast_peer {
192.168.1.210 #对端ip
}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.200
}
}
2- 配置 BACKUP
[root@lb02 ~]# cat /etc/keepalived/keepalived.conf
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 50
priority 100
unicast_src_ip 192.168.1.210 #本机ip
unicast_peer {
192.168.1.209 #对端ip
}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.200
}
}
对比master与Backup的keepalived配置区别
| Keepalived配置区别 | Master节点配置 | Backup节点配置 | |-----------------|----------------|----------------| | route_id(唯一标识) | router_id lb01 | router_id lb02 | | state(角色状态) | state MASTER | state BACKUP | | priority(竞选优先级) | priority 150 | priority 100 |
启动Master和Backup节点的keepalived
#Master节点
[root@lb01 ~]# systemctl start keepalived
[root@lb01 ~]# systemctl enable keepalived
#Backup节点
[root@lb02 ~]# systemctl start keepalived
[root@lb02 ~]# systemctl enable keepalived
验证:
ip a
systemctl status keepalived.service
209:in MASTER adver
210:Entering BACKUP STATE
出现双vip 解决方法
keepalived两台机器同时出现vip问题 - linhaifeng - 博客园 (cnblogs.com)
检查网卡 上的 流量:
tcpdump -i eth0 vrrp -n
210:
{#more-7995}
配置keealived检测nginx脚本
1.脚本内容:
cat check_web.sh
#!/bin/sh
nginxpid=$(ps -C nginx --no-header|wc -l)
#1.判断Nginx是否存活,如果不存活则尝试启动Nginx
if [ $nginxpid -eq 0 ];then
systemctl start nginx &>/dev/null
sleep 2
#2.等待2秒后再次获取一次Nginx状态
nginxpid=$(ps -C nginx --no-header|wc -l)
#3.再次进行判断, 如Nginx还不存活则停止Keepalived,让地址进行漂移,并退出脚本
if [ $nginxpid -eq 0 ];then
systemctl stop keepalived
fi
fi
给脚本执行权限、
chmod +x check_web.sh
2.脚本集成到keepalived软件
[root@lb01 ~]# cat /etc/keepalived/keepalived.conf
global_defs {
router_id lb01
}
#定义脚本
vrrp_script check_web {
script "/root/check_web.sh"
interval 4
}
vrrp_instance VI_1 {
state MASTER
#nopreempt
interface eth0
virtual_router_id 50
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3
}
# 调用脚本
track_script {
check_web
}
}