目录
本文记录了Nginx+Keepalived实现高可用的全部过程,希望能够帮到大家。
资源列表
操作系统 | 配置 | 主机名 | IP |
---|---|---|---|
CentOS7.3.1611 | 2C4G | nginx01 | 192.168.207.131 |
CentOS7.3.1611 | 2C4G | nginx02 | 192.168.207.165 |
VIP | 192.168.207.200 |
基础环境
关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
关闭内核安全机制
sed -i "s/.*SELINUX=.*/SELINUX=disabled/g" /etc/selinux/config
reboot
修改主机名
hostnamectl set-hostname nginx01
hostnamectl set-hostname nginx02
添加hosts解析
cat >> /etc/hosts << EOF
192.168.207.131 nginx01
192.168.207.165 nginx02
EOF
一、安装Nginx
安装Nginx
sudo yum -y install yum-utils
cat > /etc/yum.repos.d/nginx.repo << 'EOF'
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
sudo yum -y install nginx
启动Nginx
systemctl start nginx
systemctl enable nginx
二、安装Keepalived
安装keepalived
yum -y install keepalived
修改配置文件
主节点配置文件
# 备份配置文件
cp /etc/keepalived/keepalived.conf{,_bak}
# 主节点keepalived的配置文件,注意修改interface以及virtual_ipaddress
! 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 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script check_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 1 # 1秒检查一次
weight -2 # 如果脚本失败则priority -2
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_nginx
}
virtual_ipaddress {
192.168.207.200/24
}
}
备节点配置文件
# 备份配置文件
cp /etc/keepalived/keepalived.conf{,_bak}
# 备节点keepalived的配置文件,注意修改interface以及virtual_ipaddress
! 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 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script check_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 1
weight -2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_nginx
}
virtual_ipaddress {
192.168.207.200/24
}
}
创建nginx服务检查脚本
cat > /etc/keepalived/nginx_check.sh << 'EOF'
#!/bin/bash
# 获取nginx进程的数量
num=$(ps -ef | grep nginx | grep process | grep -v grep | wc -l)
if [ "$num" -eq 0 ]
then
systemctl stop keepalived
fi
EOF
chmod +x /etc/keepalived/nginx_check.sh
启动keepalived
systemctl start keepalived
systemctl enable keepalived