• Nginx与keepalived实现高可用


    主keepalived设置

    #安装keepalived
    [root@localhost ~]# yum -y install keepalived
    #安装nginx
    [root@localhost ~]# yum -y install nginx
    ——————————————————————————————————
    #keepalived配置文件
    [root@localhost ~]# 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_DEVEL
       vrrp_skip_check_adv_addr
       vrrp_strict
       vrrp_garp_interval 0
       vrrp_gna_interval 0
    }
    
    vrrp_script check_nginx {  #引入脚本文件
        script "/shell/nginx_check.sh"
        interval 2
        weight -20
    }
    
    vrrp_instance VI_1 {
        state MASTER   #主
        interface eno16777728 #心跳网卡
        virtual_router_id 51
        priority 100 #优先级
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            192.168.200.201
        }
        track_script {
        check_nginx     #引用脚本
        }
    }
    ______________________________________________________
    #装备测试文件
    [root@localhost ~]# echo "1111111" > /usr/share/nginx/html/index.html

    从keepalived设置

    #安装keepalived
    [root@localhost ~]# yum -y install keepalived
    #安装nginx
    [root@localhost ~]# yum -y install nginx
    ______________________________________________________________
    #keepalived配置文件
    [root@localhost ~]# 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_DEVEL
       vrrp_skip_check_adv_addr
       vrrp_strict
       vrrp_garp_interval 0
       vrrp_gna_interval 0
    }
    
    vrrp_script check_nginx {
        script "/shell/nginx_check.sh"
        interval 2
        weight -20
    }
    
    vrrp_instance VI_1 {
        state BACKUP
        interface eno16777728
        virtual_router_id 51
        priority 90
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            192.168.200.201
        }
        track_script {
        check_nginx    
        }
    }
    
    ———————————————————————————————————
    #准备测试文件
    [root@localhost ~]# echo "22222" > /usr/share/nginx/html/index.html

    三、测试
    主服务器工作时
    在这里插入图片描述
    从服务器工作时
    在这里插入图片描述

    四、nginx_check.sh shell文件,配置为周期性任务

    #!/bin/bash
    count="$(ps -C nginx --no-header|wc -l)"
    
    if [ $count -eq 0 ];then
            systemctl restart nginx
            sleep 2
            if [ ps -c nginx --no-header|wc -l -eq 0 ];then
                    systemctl stop keepalived
            fi
    fi
    #给脚本加执行权限
    chmod +x /shell/nginx_check.sh
  • 相关阅读:
    【c# 学习笔记】使用virtual和override关键字实现方法重写
    【c# 学习笔记】多态
    【c# 学习笔记】子类的初始化顺序
    mybatis入门截图二
    解析xml文件,遍历输出xml文件中的所有节点, 最终模仿实现struts2框架自动封装参数的功能
    mybatis入门截图总结
    springMVC入门截图
    OA项目总结3
    修改struts2自定义标签的源代码,在原有基础上增加功能(用于OA项目权限判断,是否显示某个权限)
    ongl表达式中得到对象,调用对象方法(OA项目权限显示模块)
  • 原文地址:https://www.cnblogs.com/canflyfish/p/11634958.html
Copyright © 2020-2023  润新知