Nginx+Keepalived(雙主模式)

1.主主模式,需要兩個VIP,互爲主備,需要修改之前主從模式keepalived配置文件

主從模式

node1 192.168.3.15    vip 192.168.3.11
node2 192.168.3.8     vip 192.168.3.12
2.修改keepalived的配置文件,採用MASTER-BACKUP、BACKUP-MASTER模式

virtual_router_id需要做修改
node1的keepalived配置文件

! Configuration File for keepalived
global_defs {
    router_id master_node
}
vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"
    interval 2
    weight 20
}
vrrp_instance VI_1 {
    state MASTER                          # master模式
    interface enp0s3                      # 綁定網卡
    virtual_router_id 51                  # 需要修改
    mcast_src_ip 192.168.3.15             #地址
    priority 100
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx
    }
    virtual_ipaddress {
        192.168.3.11/24                    #虛IP
    }
}
vrrp_instance VI_2 {
    state BACKUP
    interface enp0s3
    virtual_router_id 52
    mcast_src_ip 192.168.3.8
    priority 90
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx
    }
    virtual_ipaddress {
        192.168.3.12/24
    }
}

node2的配置

! Configuration File for keepalived
global_defs {
    router_id slave_node
}
vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"
    interval 2
    weight 20
}
vrrp_instance VI_1 {
    state BACKUP
    interface enp0s3
    virtual_router_id 51
    mcast_src_ip 192.168.3.15
    priority 90
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx
    }
    virtual_ipaddress {
        192.168.3.11/24
    }
}
vrrp_instance VI_2 {
    state MASTER
    interface enp0s3
    virtual_router_id 52
    mcast_src_ip 192.168.3.8
    priority 100
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx
    }
    virtual_ipaddress {
        192.168.3.12/24
    }
}

這裏寫圖片描述

現在利用3.11和3.12是訪問的不同的nginx。
停掉3.11對應的主機3.15上的keepalived後,
這裏寫圖片描述

  • 3.11和3.12已經到漂移到3.8這臺機器上。
  • 現在仍然可以利用3.11訪問,互爲主備。
  • nginx的主主模式基本搭建完畢。
    這裏寫圖片描述
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章