用Keepalived搭建高可用集群

时间:2022-12-19 10:33:22

一、集群介绍

1、根据功能划分为两大类:高可用和负载均衡

2、高可用:一台工作,另一台冗余,当一台服务器故障宕机时,另一台服务器提供服务。开源软件有heartbeat,keepalived.

3、负载均衡:有一台服务器做分发器,负责把用户的请求发送给后端处理,其他的服务器给用户提供服务。开源软件有LVS,keepalived,haproxy,nginx.商业的有F5,Netscaler.

二、keepalived介绍

1、keepalived 是采用VRRP协议实现高可用。keepalived 有三个模块,core ,check,vrrp。core模块是keepalived的核心,负责主进程启动,维护和全局模块的加载和解析,check模块负责健康检查,vrrp模块用来实现VRRP协议。

三、用keepalived搭建高可用集群。

1、为两台机器安装keepalived

yum install -y keepalived

2、为两台机器安装nginx,其中IP为130的主机已经在之前安装过,只需为另一台安装

yum install -y nginx

用Keepalived搭建高可用集群

3、编辑130主机的keepalived的配置文件 vim /etc/keepalived/keepalived.conf 

1)首先清空keepalived.conf

>  keepalived.conf

2)加入以下内容:

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 MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass aminglinux>com
}
virtual_ipaddress {
192.168.134.100
}
 
track_script {
chk_nginx
}
 
}
4、定义130主机上的监控脚本 vim /usr/local/sbin/check_ng.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
/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
5、给 check_ng.sh 权限,启动keepalived服务
chmod 755 /usr/local/sbin/check_ng.sh
systemctl start keepalived
用Keepalived搭建高可用集群
6、停止nginx服务,查看nginx是否启动,查看日志  /var/log/message 和 IP  ip add 。
用Keepalived搭建高可用集群
 
用Keepalived搭建高可用集群
 
用Keepalived搭建高可用集群
7、关闭主从机器的防火墙和 SELinux

systemctl stop firewalld

setenforce 0

8、配置IP 为150的从服务器,
1)先清空keepalived.conf文件
>  /etc/keepalived/keepalived.conf
2)添加以下内容:
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
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass aminglinux>com
}
virtual_ipaddress {
192.168.134.100
}
 
track_script {
chk_nginx
}
 
}
9、编辑定义150的监控脚本
#!/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 down,keepalived will stop" >> /var/ log/check_ng.log
systemctl stop keepalived
fi
fi
10、 区分nginx
a)web访问主的IP
用Keepalived搭建高可用集群
b)web访问从的IP
用Keepalived搭建高可用集群
 
用Keepalived搭建高可用集群
c)web访问vip
用Keepalived搭建高可用集群
 
这就说明nginx服务在master 主服务器上,192.168.134.100在master上,和从服务器没有关系。
11、测试高可用
a)在主服务上增加防火墙规则

iptables -I OUTPUT -p vrrp -j DROP

用Keepalived搭建高可用集群用Keepalived搭建高可用集群

此时发现封掉vrrp协议不能实现切换资源的目的。

b)恢复防火墙,停掉keepalived。查看从服务器的ip add 和日志。

iptables -F

systemctl stop keepalived

用Keepalived搭建高可用集群

用Keepalived搭建高可用集群

  用Keepalived搭建高可用集群
 可以发现此时已经切换到从服务器了。
c)在开启主服务器的keepalived,在查看ip add 和web.
 
用Keepalived搭建高可用集群
 
用Keepalived搭建高可用集群
 
可以发现 ip add 马上就切到主机服务器上了,资源立刻就切换了。