用keepalived搭建nginx高可用集群
配置master
安装
yum install -y keepalived
备份原始配置文件
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf_bak
配置master的keepalived脚本
> /etc/keepalived/keepalived.conf
清空
vim /etc/keepalived/keepalived.conf
重新编写
写入以下内容:
global_defs {
notification_email {
}
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
监控的时间间隔为3秒一次
}
vrrp_instance VI_1 {
state
MASTER
状态为主服务master
interface
ens33
连接接口为ens33网卡
virtual_router_id 51
虚拟路由ID为 51,须根backup保持一致
priority 100
权重为100,最高权重
advert_int 1
authentication {
auth_type PASS
认证类型为密码
auth_pass 123456
认证密码为123456
}
virtual_ipaddress {
虚拟IP地址,用来和服务器的真实IP绑定,首先绑定master,若master宕机则自动绑定backup
192.168.133.100
须和服务器在同一网段
}
track_script {
chk_nginx
}
}
配置master的监控脚本(监控nginx进程)
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`
计算nginx进程数量
if [ $n -eq "0" ]; then
若进程为0,则
/etc/init.d/nginx start
启动nginx,如果
不是编译安装的,则需要用
systemctl start nginx
n2=`ps -C nginx --no-heading|wc -l`
再次检测nginx进程数量
if [ $n2 -eq "0" ]; then
若进程依然为0,则
echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
记录日志并
systemctl stop keepalived
关闭keepalived,
防止出现“脑裂”
fi
fi
脚本授权
chmod 755 /usr/local/sbin/check_ng.sh
启动服务
systemctl start keepalived
关闭SElinux
getenforce
setenforce 0
清空或更改防火墙规则
查看服务进程
ps aux |grep keepalived
查看网卡是否当定成功
ip addr
[root@test-01 keepalived]# ip addr
1: lo: mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:bb:d5:33 brd ff:ff:ff:ff:ff:ff
inet 192.168.31.128/24 brd 192.168.31.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.31.100/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::38da:ac91:29ef:9092/64 scope link
valid_lft forever preferred_lft forever
配置backup
安装
yum install -y keepalived
备份原始配置文件
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf_bak
配置backup的keepalived脚本
> /etc/keepalived/keepalived.conf
清空
vim /etc/keepalived/keepalived.conf
重新编写脚本
写入以下内容:
global_defs {
notification_email {
}
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
状态是备用服务器,和主服务器master有区别
interface ens33
virtual_router_id
51
须和主服务器保持一致
priority
90
权重,比主服务器master低一些,可以根据硬件承载能力自定义
advert_int 1
authentication {
auth_type
PASS
auth_pass
123456
}
virtual_ipaddress {
192.168.
31.100
须和主服务器及其它服务器保持一致
}
track_script {
chk_nginx
}
}
配置backup的监控脚本(监控nginx进程)
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`
计算nginx进程数量
if [ $n -eq "0" ]; then
若进程为0,则
/etc/init.d/nginx start
启动nginx,如果
不是编译安装的,则需要用
systemctl start nginx
n2=`ps -C nginx --no-heading|wc -l`
再次检测nginx进程数量
if [ $n2 -eq "0" ]; then
若进程依然为0,则
echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
记录日志并
systemctl stop keepalived
关闭keepalived,
防止出现“脑裂”
fi
fi
脚本授权
chmod 755 /usr/local/sbin/check_ng.sh
启动服务
systemctl start keepalived
关闭SElinux
getenforce
setenforce 0
清空或更改防火墙规则
查看服务进程
ps aux |grep keepalived
查看网卡是否当定成功
ip addr
[root@test-02 keepalived]# ip addr
1: lo: mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:54:a7:66 brd ff:ff:ff:ff:ff:ff
inet 192.168.31.129/24 brd 192.168.31.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.31.100/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::95c:95e1:a022:75e9/64 scope link
valid_lft forever preferred_lft forever
测试高可用
确定两台机器上nginx差异,可以通过curl -I(大写i) 来查看nginx版本
测试1:关闭master上的nginx服务,会自动重启,且启动时间有变更。
测试2:关闭backup上的nginx服务,会自动重启,且启动时间有变更。
测试3:在master上nginx默认主页写入this is master,并在浏览器访问192.168.31.128,可以看到this is master。
测试4:在backup上nginx默认主页写入this is backup,并在浏览器访问192.168.31.128,可以看到this is backup。
测试5:并在浏览器访问192.168.31.100,查看显示的是master还是backup。
测试3:关闭master上的keepalived服务,并在浏览器访问192.168.31.100,查看显示的是master还是backup。
测试4:启动master上的keepalived服务,并在浏览器访问192.168.31.100,查看显示的是master还是backup。