一,准备:

准备两节点:

master:192.168.122.21

backup:192.168.122.22

准备两个vip

vip1:192.168.122.16

vip2:192.168.122.17

关闭所有节点防火墙

关闭所有节点selinux

二,安装:

所有节点yum安装keepalived,nginx

keepalived,nginx设置开机自启

为了方便辨识,修改两节点nginx默认首页

keepalived+nginx高可用_第1张图片

keepalived+nginx高可用_第2张图片

三,配置

1,主备争抢模式配置

主节点配置:

! Configuration File for keepalived

global_defs {
   router_id master
   vrrp_skip_check_adv_addr
   !vrrp_strict
   vrrp_garp_interval 1
   vrrp_gna_interval 1
}


vrrp_instance VI_1 {
    state MASTER
    interface ens3
    virtual_router_id 51
    priority 100
    advert_int 1
    !nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.122.16
    }
    unicast_src_ip 192.168.122.21
    unicast_peer {
        192.168.122.22
    }
}

virtual_server 192.168.122.16 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.122.21 80 {
        weight 1
    }

    real_server 192.168.122.22 80 {
        weight 1
    }
}

备节点配置:

! Configuration File for keepalived

global_defs {
   router_id backup
   vrrp_skip_check_adv_addr
   !vrrp_strict
   vrrp_garp_interval 1
   vrrp_gna_interval 1
}


vrrp_instance VI_1 {
    state BACKUP
    interface ens3
    virtual_router_id 51
    priority 90
    advert_int 1
    !nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.122.16
    }
    unicast_src_ip 192.168.122.22
    unicast_peer {
        192.168.122.21
    }
}

virtual_server 192.168.122.16 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.122.21 80 {
        weight 1
    }

    real_server 192.168.122.22 80 {
        weight 1
    }
}


2,主备非争抢模式:

两节点state都设置为BACKUP

两节点都取消注释nopreempt


3,双主模式

主节点配置:

! Configuration File for keepalived

global_defs {
   router_id master
   vrrp_skip_check_adv_addr
   !vrrp_strict
}


vrrp_instance VI_1 {
    state MASTER
    interface ens3
    virtual_router_id 51
    priority 100
    advert_int 1
    !nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.122.16
    }
    unicast_src_ip 192.168.122.21
    unicast_peer {
        192.168.122.22
    }
}

vrrp_instance VI_2 {
    state BACKUP
    interface ens3
    virtual_router_id 50
    priority 90
    advert_int 1
    !nopreempt
    authentication {
        auth_type PASS
        auth_pass 2222
    }
    virtual_ipaddress {
        192.168.122.17
    }
    unicast_src_ip 192.168.122.21
    unicast_peer {
        192.168.122.22
    }
}

virtual_server 192.168.122.16 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.122.21 80 {
        weight 1
    }

    real_server 192.168.122.22 80 {
        weight 1
    }
}

virtual_server 192.168.122.17 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.122.21 80 {
        weight 1
    }

    real_server 192.168.122.22 80 {
        weight 1
    }
}

备节点配置:

! Configuration File for keepalived

global_defs {
   router_id backup
   vrrp_skip_check_adv_addr
   !vrrp_strict
   vrrp_garp_interval 1
   vrrp_gna_interval 1
}


vrrp_instance VI_1 {
    state BACKUP
    interface ens3
    virtual_router_id 51
    priority 90
    advert_int 1
    !nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.122.16
    }
    unicast_src_ip 192.168.122.22
    unicast_peer {
        192.168.122.21
    }
}

vrrp_instance VI_2 {
    state MASTER
    interface ens3
    virtual_router_id 50
    priority 100
    advert_int 1
    !nopreempt
    authentication {
        auth_type PASS
        auth_pass 2222
    }
    virtual_ipaddress {
        192.168.122.17
    }
    unicast_src_ip 192.168.122.22
    unicast_peer {
        192.168.122.21
    }
}

virtual_server 192.168.122.16 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.122.21 80 {
        weight 1
    }

    real_server 192.168.122.22 80 {
        weight 1
    }
}

virtual_server 192.168.122.17 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.122.21 80 {
        weight 1
    }

    real_server 192.168.122.22 80 {
        weight 1
    }
}

四,说明

1,主备非争抢模式:初始状态一致,都是backup,优先级高者为主节点,主节点挂掉,备节点接替作为主节点,主节点重新上线后作为备节点

2,主备争抢模式:初始状态不一致,主节点为master,备节点为backup,并且主节点优先级高于备节点,主节点挂掉,备节点接替作为主节点,主节点重新上线后会夺回主节点权限,成为主节点,备节点重新成为备节点

3,双主模式:有两个vip,都可以访问,两节点互为主备,一个节点挂掉,两个vip依然都可以访问