18.1 集群介绍
18.2 keepalived介绍
18.3/18.4/18.5 用keepalived配置高可用集群linux
一.集群介绍nginx
二.keepalived介绍git
三.用Keepalived配置高可用集群(上)(中)(下)vim
示例一:后端
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
}centos
vrrp_script chk_nginx {
script "/usr/local/sbin/check_ng.sh" 检测服务是否正常
interval 3 检测3秒钟
}bash
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51 定义路由器的id
priority 100 权重
advert_int 1
authentication { 认证相关的信息
auth_type PASS
auth_pass aminglinux>com
}
virtual_ipaddress { 定义VIP的,共有ip。
192.168.188.100
}服务器
track_script {
chk_nginx
}负载均衡
}curl
#!/bin/bash
#时间变量,用于记录日志
d=`date --date today +%Y%m%d_%H:%M:%S`
#计算nginx进程数量
n=`ps -C nginx --no-heading|wc -l`
#若是进程为0,则启动nginx,而且再次检测nginx进程数量,
#若是还为0,说明nginx没法启动,此时须要关闭keepalived
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
配置从
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.133.100
}
track_script {
chk_nginx
}
}
#时间变量,用于记录日志
d=`date --date today +%Y%m%d_%H:%M:%S`
#计算nginx进程数量
n=`ps -C nginx --no-heading|wc -l`
#若是进程为0,则启动nginx,而且再次检测nginx进程数量,
#若是还为0,说明nginx没法启动,此时须要关闭keepalived
if [ $n -eq "0" ]; then
systemctl start nginx 启动命令(跟主不同,由于这个是yum安装的,主是编译安装的)
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
测试1:关闭master上的nginx服务 (很差模拟演示)
测试2:在master上增长iptabls规则 (很差模拟演示)
iptables -I OUTPUT -p vrrp -j DROP
测试3:关闭master上的keepalived服务
测试4:启动master上的keepalived服务(很差模拟演示)