腾讯云VPC内通过keepalived搭建高可用主备集群

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jackyzhousales/article/details/82108237

准备环境:

ng01  10.32.1.17    keepalived nginx

ng02  10.32.1.18    keepalived nginx

vip  10.32.1.200

1.分别在主机ng01,ng02上安装部署keepalived,nginx,配置如下:

ng01 keepalived配置如下

# cat  /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
#   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script chk_nginx {
    script "/usr/local/keepalived/sbin/check_nginx.sh"
    interval 1              
    weight 2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

    unicast_src_ip 10.32.1.17
    
    unicast_peer {
        10.32.1.18
    }

    virtual_ipaddress {
        10.32.1.200
    }
    garp_master_delay 1
    garp_master_refresh 5
    track_interface {
        eth0
    }
    track_script {
        chk_nginx
    }
}

ng02 keepalived配置如下

# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   #smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script chk_nginx {
    script "/usr/local/keepalived/sbin/check_nginx.sh"
    interval 1
    weight 2
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

    unicast_src_ip 10.32.1.18
    
    unicast_peer {
        10.32.1.17
    }

    virtual_ipaddress {
        10.32.1.200
    }
    garp_master_delay 1
    garp_master_refresh 5
    track_interface {
        eth0
    }
    track_script {
        chk_nginx
    }
}

分别在ng01,ng02主机上创建/usr/local/keepalived/sbin/check_nginx.sh脚本

#!/bin/bash
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ];then
    echo 1
    systemctl start nginx.service
    sleep 2

    if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ];then
        systemctl stop  keepalived.service
        echo 2
    fi
fi

2.腾讯云控制台申请高可用虚拟VIP

控制台》私有网络》IP与网卡》高可用虚拟IP

如果控制台没有高可用虚拟IP,可通过工单后台申请开通高可用虚拟Ip

猜你喜欢

转载自blog.csdn.net/jackyzhousales/article/details/82108237