Keepalived+重启脚本+双机热备搭建
①首先创建一个对应的目录并下载keepalived
安装包(提取码:s6aq)到Linux
中并解压:
[root@localhost]# mkdir /soft/keepalived && cd /soft/keepalived
[root@localhost]# wget https://www.keepalived.org/software/keepalived-2.2.4.tar.gz
[root@localhost]# tar -zxvf keepalived-2.2.4.tar.gz
②进入解压后的keepalived
目录并构建安装环境,然后编译并安装:
[root@localhost]# cd keepalived-2.2.4
[root@localhost]# ./configure --prefix=/soft/keepalived/
[root@localhost]# make && make install
③进入安装目录的/soft/keepalived/etc/keepalived/
并编辑配置文件:
[root@localhost]# cd /soft/keepalived/etc/keepalived/
[root@localhost]# vi keepalived.conf
④编辑主机的keepalived.conf
核心配置文件,如下:
global_defs {# 自带的邮件提醒服务,建议用独立的监控或第三方SMTP,也可选择配置邮件发送。notification_email {root@localhost}notification_email_from root@localhostsmtp_server localhostsmtp_connect_timeout 30# 高可用集群主机身份标识(集群中主机身份标识名称不能重复,建议配置成本机IP)router_id 192.168.12.129
}# 定时运行的脚本文件配置
vrrp_script check_nginx_pid_restart {# 之前编写的nginx重启脚本的所在位置script "/soft/scripts/keepalived/check_nginx_pid_restart.sh" # 每间隔3秒执行一次interval 3# 如果脚本中的条件成立,重启一次则权重-20weight -20
}# 定义虚拟路由,VI_1为虚拟路由的标示符(可自定义名称)
vrrp_instance VI_1 {# 当前节点的身份标识:用来决定主从(MASTER为主机,BACKUP为从机)state MASTER# 绑定虚拟IP的网络接口,根据自己的机器的网卡配置interface ens33 # 虚拟路由的ID号,主从两个节点设置必须一样virtual_router_id 121# 填写本机IPmcast_src_ip 192.168.12.129# 节点权重优先级,主节点要比从节点优先级高priority 100# 优先级高的设置nopreempt,解决异常恢复后再次抢占造成的脑裂问题nopreempt# 组播信息发送间隔,两个节点设置必须一样,默认1s(类似于心跳检测)advert_int 1authentication {auth_type PASSauth_pass 1111}# 将track_script块加入instance配置块track_script {# 执行Nginx监控的脚本check_nginx_pid_restart}virtual_ipaddress {# 虚拟IP(VIP),也可扩展,可配置多个。192.168.12.111}
}
⑤克隆一台之前的虚拟机作为从(备)机,编辑从机的keepalived.conf
文件,如下:
global_defs {# 自带的邮件提醒服务,建议用独立的监控或第三方SMTP,也可选择配置邮件发送。notification_email {root@localhost}notification_email_from root@localhostsmtp_server localhostsmtp_connect_timeout 30# 高可用集群主机身份标识(集群中主机身份标识名称不能重复,建议配置成本机IP)router_id 192.168.12.130
}# 定时运行的脚本文件配置
vrrp_script check_nginx_pid_restart {# 之前编写的nginx重启脚本的所在位置script "/soft/scripts/keepalived/check_nginx_pid_restart.sh" # 每间隔3秒执行一次interval 3# 如果脚本中的条件成立,重启一次则权重-20weight -20
}# 定义虚拟路由,VI_1为虚拟路由的标示符(可自定义名称)
vrrp_instance VI_1 {# 当前节点的身份标识:用来决定主从(MASTER为主机,BACKUP为从机)state BACKUP# 绑定虚拟IP的网络接口,根据自己的机器的网卡配置interface ens33 # 虚拟路由的ID号,主从两个节点设置必须一样virtual_router_id 121# 填写本机IPmcast_src_ip 192.168.12.130# 节点权重优先级,主节点要比从节点优先级高priority 90# 优先级高的设置nopreempt,解决异常恢复后再次抢占造成的脑裂问题nopreempt# 组播信息发送间隔,两个节点设置必须一样,默认1s(类似于心跳检测)advert_int 1authentication {auth_type PASSauth_pass 1111}# 将track_script块加入instance配置块track_script {# 执行Nginx监控的脚本check_nginx_pid_restart}virtual_ipaddress {# 虚拟IP(VIP),也可扩展,可配置多个。192.168.12.111}
}
⑥新建scripts
目录并编写Nginx
的重启脚本,check_nginx_pid_restart.sh
:
[root@localhost]# mkdir /soft/scripts /soft/scripts/keepalived
[root@localhost]# touch /soft/scripts/keepalived/check_nginx_pid_restart.sh
[root@localhost]# vi /soft/scripts/keepalived/check_nginx_pid_restart.sh#!/bin/sh
# 通过ps指令查询后台的nginx进程数,并将其保存在变量nginx_number中
nginx_number=`ps -C nginx --no-header | wc -l`
# 判断后台是否还有Nginx进程在运行
if [ $nginx_number -eq 0 ];then# 如果后台查询不到`Nginx`进程存在,则执行重启指令/soft/nginx/sbin/nginx -c /soft/nginx/conf/nginx.conf# 重启后等待1s后,再次查询后台进程数sleep 1# 如果重启后依旧无法查询到nginx进程if [ `ps -C nginx --no-header | wc -l` -eq 0 ];then# 将keepalived主机下线,将虚拟IP漂移给从机,从机上线接管Nginx服务systemctl stop keepalived.servicefi
fi
⑦编写的脚本文件需要更改编码格式,并赋予执行权限,否则可能执行失败:
[root@localhost]# vi /soft/scripts/keepalived/check_nginx_pid_restart.sh:set fileformat=unix # 在vi命令里面执行,修改编码格式
:set ff # 查看修改后的编码格式[root@localhost]# chmod +x /soft/scripts/keepalived/check_nginx_pid_restart.sh
⑧由于安装keepalived
时,是自定义的安装位置,因此需要拷贝一些文件到系统目录中:
[root@localhost]# mkdir /etc/keepalived/
[root@localhost]# cp /soft/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@localhost]# cp /soft/keepalived/keepalived-2.2.4/keepalived/etc/init.d/keepalived /etc/init.d/
[root@localhost]# cp /soft/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
⑨将keepalived
加入系统服务并设置开启自启动,然后测试启动是否正常:
[root@localhost]# chkconfig keepalived on
[root@localhost]# systemctl daemon-reload
[root@localhost]# systemctl enable keepalived.service
[root@localhost]# systemctl start keepalived.service其他命令:
systemctl disable keepalived.service # 禁止开机自动启动
systemctl restart keepalived.service # 重启keepalived
systemctl stop keepalived.service # 停止keepalived
tail -f /var/log/messages # 查看keepalived运行时日志
⑩最后测试一下VIP
是否生效,通过查看本机是否成功挂载虚拟IP
:
[root@localhost]# ip addr
从上图中可以明显看见虚拟IP
已经成功挂载,但另外一台机器192.168.12.130
并不会挂载这个虚拟IP
,只有当主机下线后,作为从机的192.168.12.130
才会上线,接替VIP
。最后测试一下外网是否可以正常与VIP
通信,即在Windows
中直接ping VIP
:
外部通过VIP
通信时,也可以正常Ping
通,代表虚拟IP
配置成功。
经过上述步骤后,keepalived
的VIP
机制已经搭建成功,在上个阶段中主要做了几件事:
- 一、为部署
Nginx
的机器挂载了VIP
。 - 二、通过
keepalived
搭建了主从双机热备。 - 三、通过
keepalived
实现了Nginx
宕机重启。