nginx的高可用---Keepalived
目录
环境
一、centos安装keepalived
1、下载keepalived压缩文件
2、解压
3、进入解压后的keepalived目录并构建环境,然后进行编译安装
4、进入配置目录并进行编辑配置文件
5、编辑主机的keepalived.conf核心配置文件,如下
6、创建Nginx的重启脚本
7、给脚本赋予执行权限
8、编辑脚本
9、由于安装keepalived时,是自定义的安装位置,因此需要拷贝一些文件在系统目录中
10、将keepalived加入系统服务并设置开启自启动
11、测试VIP是否生效
二、在ubuntu上安装keepalived
1、安装一些依赖包
2、下载keepalived压缩文件
3、解压
4、进入解压后的keepalived目录并构建环境,然后进行编译安装
5、进入配置目录并进行编辑配置文件
6、编辑从机的keepalived.conf核心配置文件,如下
7、创建Nginx的重启脚本
8、给脚本赋予执行权限
9、编辑脚本
10、由于安装keepalived时,是自定义的安装位置,因此需要拷贝一些文件在系统目录中
12、将keepalived加入系统服务并设置开启自启动
13、只有当主设备宕机后,从设备才能启用,此时主设备已经宕机,检测VIP是否生效
环境
centos作为主设备,ubuntu作为从设备
一、centos安装keepalivedKeepalived官方下载链接:https://www.keepalived.org/download.html
1、下载keepalived压缩文件 # wget https://www.keepalived.org/software/keepalived-2.2.7.tar.gz 2、解压 # tar -zxvf keepalived-2.2.7.tar.gz 3、进入解压后的keepalived目录并构建环境,然后进行编译安装 # cd keepalived-2.2.7 # ./configure # make && make install 4、进入配置目录并进行编辑配置文件 # cd /root/keepalived/keepalived/etc/keepalived/ # vim keepalived.conf 5、编辑主机的keepalived.conf核心配置文件,如下 global_defs { # 自带的邮件提醒服务,建议用独立的监控或第三方SMTP,也可选择配置邮件发送。 notification_email { root@localhost } notification_email_from root@localhost smtp_server localhost smtp_connect_timeout 30 # 高可用集群主机身份标识(集群中主机身份标识名称不能重复,建议配置成本机IP) router_id 192.168.75.132 } # 定时运行的脚本文件配置 vrrp_script check_nginx_pid_restart { # 之前编写的nginx重启脚本的所在位置 script "check_nginx_pid.sh" # 每间隔3秒执行一次 interval 3 # 如果脚本中的条件成立,重启一次则权重-20 weight -20 } # 定义虚拟路由,VI_1为虚拟路由的标示符(可自定义名称) vrrp_instance VI_1 { # 当前节点的身份标识:用来决定主从(MASTER为主机,BACKUP为从机) state BACKUP # 绑定虚拟IP的网络接口,根据自己的机器的网卡配置 interface ens33 # 虚拟路由的ID号,主从两个节点设置必须一样 virtual_router_id 240 # 填写本机IP mcast_src_ip 192.168.75.132 # 节点权重优先级,主节点要比从节点优先级高 priority 90 # 优先级高的设置nopreempt,解决异常恢复后再次抢占造成的脑裂问题 nopreempt # 组播信息发送间隔,两个节点设置必须一样,默认1s(类似于心跳检测) advert_int 1 authentication { auth_type PASS auth_pass root } # 将track_script块加入instance配置块 track_script { # 执行Nginx监控的脚本 check_nginx_pid_restart } virtual_ipaddress { # 虚拟IP(VIP),也可扩展,可配置多个。 192.168.75.240 } } 6、创建Nginx的重启脚本 # vim check_nginx_pid.sh 7、给脚本赋予执行权限 # chmod +x /root/keepalived/keepalived/etc/keepalived/check_nginx_pid.sh 8、编辑脚本 #!/bin/sh # 通过ps指令查询后台的nginx进程数,并将其保存在变量nginx_number中 nginx_number=`ps -C nginx --no-header | wc -l` # 判断后台是否还有Nginx进程在运行 if [ $nginx_number -eq 0 ];then # 如果后台查询不到`Nginx`进程存在,则执行重启指令 /usr/local/nginx/sbin/nginx -c /usr/local/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.service fi fi 9、由于安装keepalived时,是自定义的安装位置,因此需要拷贝一些文件在系统目录中 # mkdir /etc/keepalived/ # cp /root/keepalived/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ # cp /root/keepalived/keepalived/etc/init.d/keepalived /etc/init.d/ # cp /root/keepalived/keepalived/etc/sysconfig/keepalived /etc/sysconfig 10、将keepalived加入系统服务并设置开启自启动 # systemctl enable keepalived.service # systemctl daemon-reload # systemctl start keepalived.service 11、测试VIP是否生效 二、在ubuntu上安装keepalived 1、安装一些依赖包 # apt-get install libssl-dev # apt-get install openssl # apt-get install libpopt-dev # apt-get install libnl-3-dev # apt-get install libnl-genl-3-dev 2、下载keepalived压缩文件 # wget https://www.keepalived.org/software/keepalived-2.2.7.tar.gz 3、解压 # tar -zxvf keepalived-2.2.7.tar.gz 4、进入解压后的keepalived目录并构建环境,然后进行编译安装 # cd keepalived-2.2.7 # ./configure --profix=/usr/local/keepalived # make && make install 5、进入配置目录并进行编辑配置文件 # /usr/local/keepalived/etc/keepalived # vim keepalived.conf 6、编辑从机的keepalived.conf核心配置文件,如下 global_defs { # 自带的邮件提醒服务,建议用独立的监控或第三方SMTP,也可选择配置邮件发送。 notification_email { root@localhost } notification_email_from root@localhost smtp_server localhost smtp_connect_timeout 30 # 高可用集群主机身份标识(集群中主机身份标识名称不能重复,建议配置成本机IP) router_id 192.168.75.132 } # 定时运行的脚本文件配置 vrrp_script check_nginx_pid_restart { # 之前编写的nginx重启脚本的所在位置 script "/usr/local/keepalived/etc/keepalived/check_nginx_pid.sh" # 每间隔3秒执行一次 interval 3 # 如果脚本中的条件成立,重启一次则权重-20 weight -20 } # 定义虚拟路由,VI_1为虚拟路由的标示符(可自定义名称) vrrp_instance VI_1 { # 当前节点的身份标识:用来决定主从(MASTER为主机,BACKUP为从机) state BACKUP # 绑定虚拟IP的网络接口,根据自己的机器的网卡配置 interface ens33 # 虚拟路由的ID号,主从两个节点设置必须一样 virtual_router_id 240 # 填写本机IP mcast_src_ip 192.168.75.132 # 节点权重优先级,主节点要比从节点优先级高 priority 90 # 优先级高的设置nopreempt,解决异常恢复后再次抢占造成的脑裂问题 nopreempt # 组播信息发送间隔,两个节点设置必须一样,默认1s(类似于心跳检测) advert_int 1 authentication { auth_type PASS auth_pass root } # 将track_script块加入instance配置块 track_script { # 执行Nginx监控的脚本 check_nginx_pid_restart } virtual_ipaddress { # 虚拟IP(VIP),也可扩展,可配置多个。 192.168.75.240 } } 7、创建Nginx的重启脚本 # /usr/local/keepalived/etc/keepalived # vim check_nginx_pid.sh 8、给脚本赋予执行权限 # chmod +x /usr/local/keepalived/etc/keepalived/check_nginx_pid.sh 9、编辑脚本 #!/bin/sh # 通过ps指令查询后台的nginx进程数,并将其保存在变量nginx_number中 nginx_number=`ps -C nginx --no-header | wc -l` # 判断后台是否还有Nginx进程在运行 if [ $nginx_number -eq 0 ];then # 如果后台查询不到`Nginx`进程存在,则执行重启指令 /usr/local/nginx/sbin/nginx -c /usr/local/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.service fi fi 10、由于安装keepalived时,是自定义的安装位置,因此需要拷贝一些文件在系统目录中 # mkdir /etc/sysconfig # mkdir /etc/keepalived # cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/init.d/ # cp /usr/local/keepalived/sbin/keepalived /sbin/ # cp /usr/local/keepalived/etc/keepalived/..keepalived.conf /etc/keepalived/ # cp /root/keepalived/keepalived/keepalived.service /lib/systemd/system/ 12、将keepalived加入系统服务并设置开启自启动 # systemctl enable keepalived # systemctl start keepalived 13、只有当主设备宕机后,从设备才能启用,此时主设备已经宕机,检测VIP是否生效主设备未宕机时
当主设备宕机后