keepalived是集群管理中保证集群高可用(HA)的一个服务软件,其功能类似于heartbeat,用来防止单点故障。
2.工作原理
keepalived是以VRRP协议为实现基础的,当backup收不到vrrp包时就认为master宕掉了,这时就需要根据VRRP的优先级来选举一个backup当master。这样我们就可以保证集群的高可用
keepalived主要有三个模块,分别是core、check和vrrp。core模块为keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析。check负责健康检查,包括常见的各种检查方式。vrrp模块是来实现VRRP协议的
如何判断谁是master:看vip,vip在谁上谁就是master
什么是脑裂
脑裂(split-brain):指在一个高可用(HA)系统中,当联系着的两个节点断开联系时,本来为一个整体的系统,分裂为两个独立节点,这时两个节点开始争抢共享资源,结果会导致系统混乱,数据损坏
脑裂:backup强资源,master不认为自己会死,他俩抢着为客户端服务
解决方案:爆头 master
二:LVS+Keepalived
web-1
加载缓存
yum -y install httpd //安装阿帕奇
systemctl start httpd
切换到默认发布目录:cd /var/www/html/
写内容 echo
systemctl enable httpd
web-2
加载缓存
yum -y install httpd
systemctl enable httpd
2台负载均衡服务器
Nginx-Master(默认master对外提供服务)
仓库文件
yum -y install nginx
systemctl start nginx
systemctl enable nginx
创建池子
调用地址池
nginx -t 检查语法错误
nginx -s reload
安装keepalived软件
yum -y install keepalived
加配置文件
! Configuration File for keepalived
global_defs {
router_id director1
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 80
priority 100 // master优先级
advert_int 1 //每隔一秒检查一次
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { //VIP
10.0.0.110/24 //注意与IP为同一个网段
}
}
systemctl start keepalived
systemctl enable keepalived
Nginx-Backup
vi /etc/yum.repos.d/nginx.repo
yum -y install keepalived
! Configuration File for keepalived
global_defs {
router_id director2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 80
priority 60
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.142.110/24
}
}
systemctl start keepalived
systemctl enable keepalived
测试:
负载均衡出现问题,keepalived正常
解决问题:keepalived停止,在keepalived做健康检测
健康检测
Nginx-Master
systemctl stop nginx
#!/bin/bash
#+检查nginx进程是否存在 ps查看进程
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
systemctl stop keepalived
fi
vi keepalived.conf
vi keepalived.conf
systemctl restart keepalived
Nginx-Backup
测试:
可以访问
二:LVS+Keepalived
给2lvs做高可用
web-1、web-2环境同上
测试是否可以用
web-1
在回环接口lo绑定VIP
永久开始忽略arp广播(前两条)and永久开始路由转发(最后一条)
web-2
在回环接口lo绑定VIP
永久开始忽略arp广播(前两条)and永久开始路由转发(最后一条)
实现负载均衡
设置VIP10.9.12.110
生成配置文件
创建ipvsadm的文件用来存放lvs的规则
添加规则
访问测试
lvs高可用部署
! Configuration File for keepalived
global_defs {
router_id lvs-keepalived-master
}vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 80
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.142.110/24
}
}virtual_server 192.168.142.110 80 {
delay_loop 3
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
protocol TCP
real_server 192.168.142.145 80 {
weight 1
inhibit_on_failure
TCP_CHECK {
connect_port 80
connect_timeout 3
}
}
real_server 192.168.142.149 80 {
weight 1
inhibit_on_failure
TCP_CHECK {
connect_timeout 3
connect_port 80
}
}
}
nginx-backup
lvs高可用部署
! Configuration File for keepalived
global_defs {
router_id lvs-keepalived-backup
}vrrp_instance VI_1 {
state BACKUP
interface ens33
nopreempt #不抢占资源
virtual_router_id 80
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.142.110/24
}
}
virtual_server 192.168.142.110 80 {
delay_loop 3
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 20
protocol TCP
real_server 192.168.142.145 80 {
weight 1
inhibit_on_failure
TCP_CHECK {
connect_port 80
connect_timeout 3
}
}
real_server 192.168.142.149 80 {
weight 1
inhibit_on_failure
TCP_CHECK {
connect_timeout 3
connect_port 80
}
}
}
最后访问:
扩展1:
keepalived坏了可以继续使用
扩展2
模拟web-1出现故障、
利用监控检测