Nginx-keepalived双机热备集群
Nginx服务器安装部署详细步骤(多次安装,过程详细)
离线安裝keepalived
Centos7离线安装和配置Keepalived
链接:https://pan.baidu.com/s/1x7ucXNyOt9sB-M6r8x7_bw
提取码:ok3o
在线安装keepalived
# 安装依赖包
yum install -y curl gcc openssl-devel libnl3-devel net-snmp-devel
# 安装keepalived
yum install -y keepalived
卸载keepalived
keepalived 安装与卸载
Linux下卸载keepalived
# 进入自己的安装目录
cd /usr/local/src/keepalived
# 执行卸载命令
make uninstall
# 找到所有的keepalived文件
whereis keepalived
# 执行 rm -rf 命令挨个删除
keepalived基础命令
# 开机⾃启动
systemctl enable keepalived
# 启动
systemctl start keepalived
# 暂停
systemctl stop keepalived
# 重启
systemctl restart keepalived
# 查看状态
systemctl status keepalived
# 查看keepalived日志
tail -f /var/log/messages
keepalived配置虚拟ip
Nginx配置——搭建 Nginx 高可用集群(双机热备)
安装完成之后, 在 /etc/keepalived 目录下有keepalived的配置文件
cat /etc/keepalived/keepalived.conf
global_defs 配置下,删除掉vrrp_strict配置
state :主节点为MASTER,备节点为BACKUP
interface :需要绑定的⽹卡(可以通过ifconfig查看,我的是ens33)
priority :主节点配置成100,备节点配置成99
virtual_ipaddress : 虚拟IP,两个节点需要相同
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 # 通过它,可以访问到主机,在hosts文件中,要做映射关系,类似于 127.0.0.1 LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict # 删除这句
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER # 现在MASTER是主机, 如果是从机要换成 BACKUP
interface eth0 # 把eth0 换成自己服务器的网卡
virtual_router_id 51 # 主、备机的 virtual_router_id 必须相同
priority 100 # 主节点配置成100,备节点配置成99 , 主、备机取不同的优先级,主机值较大,备份机值较小
advert_int 1 # #每隔一秒发送一次心跳,确保从服务器是否还活着
authentication {
auth_type PASS
auth_pass 1111 # 心跳检测需要的密码
}
virtual_ipaddress { # 要虚拟的地址
192.168.200.16
192.168.200.17
192.168.200.18
}
}
获取interface值, (自己服务器的网卡) 可以通过 ip a 命令
ip a
绑定Nginx进程和Keepalived服务
在路径/usr/local/src/ 下新建检测脚本 nginx_check.sh
#! /bin/bash
A=`ps -C nginx -no-header | wc - 1`
if [ $A -eq 0];then
/usr/local/nginx/sbin/nginx #自己nginx的安装目录
sleep 2
if [`ps -C nginx --no-header| wc -1` -eq 0 ];then
killall keepalived
fi
fi
修改keepalived配置文件, 然后重启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 192.168.20.61
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}
# 添加这一段检测脚本配置
vrrp_script chk_http_ port {
script "/usr/local/src/nginx_check.sh"
interval 2 # (检测脚本执行的间隔)2s
weight 2 #权重,如果这个脚本检测为真,服务器权重+2
}
vrrp_instance VI_1 {
state MASTER
interface ens160
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.16
}
}