• nginx+keepalived(双主)


    一、环境

    nginx1   192.168.40.211  

    nginx2   192.168.40.132 

    vip1     192.168.40.223  主为keep1,从为keep2

    vip2     192.168.40.222  主为keep2,从为keep1

    (此处安装过程皆采用yum方式,不作累述)

    二、keepalived配置文件

    nginx1

    vi /etc/keepalived/keepalived.conf

    global_defs {

        notification_email {

            root@localhost

        }

        notification_email_from keepalived@localhost

        smtp_server 127.0.0.1

        smtp_connect_timeout 30

        route_id LVS_DEVEL

    }

    vrrp_script_chk_nginx {

        script "/etc/keepalived/ck_ng.sh"

        interval 2

        weight 2

    }

    #VIP1

    vrrp_instance VI_1 {

        state MASTER

        interface ens33

        lvs_sync_daemon_interface ens33

        virtual_router_id 151

        priority 100

        advert_int 5

        nopreempt

        authentication {

            auth_type PASS

            auth_pass 1111

        }

        virtual_ipaddress {

            192.168.40.223

        }

        track_script {

            chk_nginx

        }

    }

    #VIP2

    vrrp_instance VI_2 {

        state BACKUP

        interface ens33

        lvs_sync_daemon_interface ens33

        virtual_router_id 152

        priority 90

        advert_int 5

        nopreempt

        authentication {

            auth_type PASS

            auth_pass 2222

        }

        virtual_ipaddress {

            192.168.40.222

        }

        track_script {

            chk_nginx

        }

    }


    nginx2

    #vi /etc/keepalived/keepalived.conf

    global_defs {

        notification_email {

            root@192.168.40.132

        }

        notification_email_from keepalived@192.168.40.132

        smtp_server 127.0.0.1

        smtp_connect_timeout 30

        route_id LVS_DEVEL

    }

    vrrp_script_chk_nginx {

        script "/etc/keepalived/ck_ng.sh"

        interval 2

        weight 2

    }

    #VIP1

    vrrp_instance VI_1 {

        state BACKUP

        interface ens33

        lvs_sync_daemon_interface ens33

        virtual_router_id 151

        priority 90

        advert_int 5

        nopreempt

        authentication {

            auth_type PASS

            auth_pass 1111

        }

        virtual_ipaddress {

            192.168.40.223

        }

        track_script {

            chk_nginx

        }

    }

    #VIP2

    vrrp_instance VI_2 {

        state MASTER

        interface ens33

        lvs_sync_daemon_interface ens33

        virtual_router_id 152

        priority 100

        advert_int 5

        nopreempt

        authentication {

            auth_type PASS

            auth_pass 2222

        }

        virtual_ipaddress {

            192.168.40.222

        }

        track_script {

            chk_nginx

        }

    }

    三、nginx检测脚本(两台机子一样)

    #vi /etc/keepalived/ck_ng.sh

    #!/bin/bash

    #检查nginx进程是否存在

    counter=$(ps -C nginx --no-heading|wc -l)

    if [ "${counter}" = "0" ]; then

    #尝试启动一次nginx,停止5秒后再次检测

        service nginx start

        sleep 5

        counter=$(ps -C nginx --no-heading|wc -l)

        if [ "${counter}" = "0" ]; then

    #如果启动没成功,就杀掉keepalive触发主备切换

            service keepalived stop

        fi

    fi

    #chmod +x /etc/keepalived/ck_ng.sh

    四、测试

    [root@localhost ~]# ip a (nginx1)

    ....

    2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

        link/ether 00:0c:29:17:c0:db brd ff:ff:ff:ff:ff:ff

        inet 192.168.40.211/24 brd 192.168.40.255 scope global ens33

           valid_lft forever preferred_lft forever

        inet 192.168.40.223/32 scope global ens33

           valid_lft forever preferred_lft forever

        inet6 fe80::20c:29ff:fe17:c0db/64 scope link

           valid_lft forever preferred_lft forever

    [root@localhost ~]# ip a  (nginx2)

    ....

    2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

        link/ether 00:0c:29:5e:ba:a3 brd ff:ff:ff:ff:ff:ff

        inet 192.168.40.132/24 brd 192.168.40.255 scope global ens33

           valid_lft forever preferred_lft forever

        inet 192.168.40.222/32 scope global ens33

           valid_lft forever preferred_lft forever

        inet6 fe80::20c:29ff:fe5e:baa3/64 scope link

           valid_lft forever preferred_lft forever

    停掉nginx1的keepalived,此时nginx1的vip转移到nginx2,如下

    [root@localhost ~]# ip a

    ....

    2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

        link/ether 00:0c:29:5e:ba:a3 brd ff:ff:ff:ff:ff:ff

        inet 192.168.40.132/24 brd 192.168.40.255 scope global ens33

           valid_lft forever preferred_lft forever

        inet 192.168.40.222/32 scope global ens33

           valid_lft forever preferred_lft forever

        inet 192.168.40.223/32 scope global ens33

           valid_lft forever preferred_lft forever

        inet6 fe80::20c:29ff:fe5e:baa3/64 scope link

           valid_lft forever preferred_lft forever

    两个虚拟IP跑到一台机子上,实验成功

  • 相关阅读:
    .Net中World转PDF
    asp.net开启多线程异步处理
    win8.1 安装kb2999226 一直提示 搜索更新
    MVC实例应用模式
    MVC模式
    23种设计模式
    Android 的Android Device Monitor 打不开出现 A error has occured
    xxx系统的可用性和易用性分析
    淘宝网的质量属性分析
    《架构漫谈》读后感
  • 原文地址:https://www.cnblogs.com/zjz20/p/11563885.html
Copyright © 2020-2023  润新知