快速安装 Nginx双服务器高可用(keepalived)
nginx服务器安装不在此说明了,下面记录的是keepalived的快速安装部署,由于编译安装keepalived2.0.12时出现openssl错误(系统里使用的openssl 1.1.1a),所以采用rpm安装方式:
虚拟ip: 192.168.1.200
主服务器ip:192.168.1.201
备服务器ip:192.168.1.202
1、安装keepalived
yum install keepalived
关闭防火墙或者开启vrrp 协议,防止脑裂
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --protocol vrrp -j ACCEPT
2、修改主备服务器keepalived配置文件
① 主服务器(192.168.1.201)配置文件
[root@proxy ~]# 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.200.1 smtp_connect_timeout 30 router_id LVS_01 # 设置nginx master的id,网络内唯一 #vrrp_skip_check_adv_addr #vrrp_strict #vrrp_garp_interval 0 #vrrp_gna_interval 0 } vrrp_script check { script "/opt/check-nginx.sh" # 配置nginx检测脚本 interval 5 # 检测脚本执行的间隔,单位是秒 weight -30 # 当脚本执行结果不为0的时候,priority=120-30=90,priority小于备用服务器(priority 100),将虚拟ip切换到备用服务器上,检测到执行结果为0时,priority=120不变 fall 2 # 尝试两次都成功才成功 rise 2 # 尝试两次都失败才失败 } vrrp_instance VI_1 { state MASTER # 指定keepalived的角色,MASTER为主,BACKUP为备 interface ens160 # 当前进行vrrp通讯的网络接口卡 virtual_router_id 51 # 虚拟路由编号,主从要一致 priority 120 # 优先级,数值越大,获取处理请求的优先级越高 advert_int 1 # 检查间隔,默认为1s(vrrp组播周期秒数) authentication { auth_type PASS auth_pass 1111 } track_script { check # 调用上面的脚本 } virtual_ipaddress { 192.168.1.200 #虚IP 一行一个 } }
② 备用服务器(192.168.1.202)配置文件
! 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_02 #vrrp_skip_check_adv_addr #vrrp_strict #vrrp_garp_interval 0 #vrrp_gna_interval 0 } vrrp_script check { script "/opt/check-nginx.sh" interval 5 weight -30 fall 2 rise 2 } vrrp_instance VI_2 { state BACKUP interface ens160 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { check } virtual_ipaddress { 192.168.1.200 } }
3、nginx检查脚本(nginx编译安装,目录在/opt/nginx下面)
[root@proxy opt]# vim check-nginx.sh #!/bin/bash nginx=`ps -C nginx --no-header |wc -l` if [ $nginx -eq 0 ];then /opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf #重启nginx if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then #nginx重启失败 exit 1 else exit 0 fi else exit 0 fi [root@proxy opt]# chmod +x check-nginx.sh
4、启动keepalived服务
[root@proxy ~]# systemctl enable keepalived [root@proxy ~]# systemctl start keepalived
目录 返回
首页
- 评论列表