介绍
所谓高可用集群,就是当一台nginx代理宕机后,将该nginx代理的服务,资源,IP转移到另一台服务器上,进行nginx代理的工作,从而避免单点故障,满足业务需求。高可用集群最少两台或多台。
keepalived最早是为LVS负载均衡软件设计开发的,用来管理高可用集群的状态,后来在keepalived中加入VRRP功能,使它除了可以管理LVS外,还可以管理nginx,haproxy。高可用简称HA。
高可用的衡量
描述 | 通俗叫法 | 可用性级别 | 停机时间/年 |
---|---|---|---|
基本可用性 | 2个9 | 99% | 87.6h |
较高可用性 | 3个9 | 99.9% | 8.8h |
具有故障自动修复能力的可用性 | 4个9 | 99.99% | 53m |
极高可用性 | 5个9 | 99.999% | 5m |
实验nginx+keepalived集群(一主一从)
实验环境
节点 | IP | 服务 |
---|---|---|
keepalived1 | 10.0.0.11 | keepalived(master)+nginx |
keepalived2 | 10.0.0.12 | keepalived(backup)+nginx |
web1 | 10.0.0.13 | nginx |
web2 | 10.0.0.14 | nginx |
keepalived1(主)部署
#安装nginx,在nginx.conf中http段添加:
upstream web {
server 10.0.0.13:80;
server 10.0.0.14:80;
}
server {
location / {
proxy_pass http://web;
}
}
#安装keepalived
yum -y install keepalived
#备份配置文件
cp -a /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
#修改配置文件
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 10.0.0.11
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
}
vrrp_script chk_nginx {
script /root/chk_nginx.sh
interval 3
}
vrrp_instance gky1 {
state MASTER
interface ens33
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.20
}
track_script {
chk_nginx
}
}
#注意
1.smtp_server邮件可有可无
2.state 主为master,从为backup
3.virtual_router_id keepalived通过virtual_router_id号来查找
4.priority 优先级,100最高,master必须高于backup
#自行添加脚本
vim /root/chk_nginx.sh
#!/bin/bash
d=`date --date today +%Y%m%d_%H:%M:%S`
n=`ps -C nginx --no-heading|wc -l`
if [ $n -eq "0" ]; then
systemctl start nginx
n2=`ps -C nginx --no-heading|wc -l`
if [ $n2 -eq "0" ]; then
echo "$d nginx,keepalived will stop" >> /var/log/check_ng.log
systemctl stop keepalived
fi
fi
#启动
systemctl start nginx
systemctl start keepalived
keepalived2(从)部署
#安装nginx,在nginx.conf中http段添加:
upstream web {
server 10.0.0.13:80;
server 10.0.0.14:80;
}
server {
location / {
proxy_pass http://web;
}
}
#安装keepalived并配置
yum -y install keepalived
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 10.0.0.12
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
}
vrrp_script chk_nginx {
script /root/chk_nginx.sh
interval 3
}
vrrp_instance gky1 {
state BACKUP
interface ens33
virtual_router_id 52
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.20
}
track_script {
chk_nginx
}
}
#注意
1.id号需要与主keepalived相同
2.state为backup
#自行添加脚本
#启动
systemctl start nginx
systemctl start keepalived
web{1..2}
#安装nginx,写入不同内容
web1:
echo "web1 10.0.0.13" > /apps/nginx/html/index.html
systemctl start nginx
web2:
echo "web2 10.0.0.14" > /apps/nginx/html/index.html
systemctl start nginx