一、keepalived双实例

       keepalived在master/backup工作模式下,会有一个主机处于闲置,所以keepalived可以使用vrrp的特性配置双master模式,使资源最大化。

    1、承接上文配置 --->  keepalived基础配置

    2、双主配置

第一个节点:

[root@Nginx keepalived]# grep -Ev '#|^$' keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
	root@localhost
   }
   notification_email_from kaadmin@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id Nginx
}
vrrp_script chk_maintance {
    script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
    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 51ea2a78
    }
    virtual_ipaddress {
        192.168.0.80/24 label eth0:0
    }
    
    track_script {
	chk_maintance
    }
}
vrrp_instance VI_2 {
    state BACKUP
    interface eth0
    virtual_router_id 52               #id必须修改
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass abcdefg              #认证字符串必须修改为不一致
    }
    virtual_ipaddress {
        192.168.0.90/24 label eth0:1
    }
    
    track_script {
	chk_maintance
    }
}

第二个节点:

[root@node1 keepalived]# !egrep
egrep -v '#|^$' keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
	root@localhost
   }
   notification_email_from kaadmin@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node1
}
vrrp_script chk_maintance {
    script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
    interval 1
    weight -2
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 51ea2a78
    }
    virtual_ipaddress {
        192.168.0.80/24 label eth0:0
    }
    
    track_script {
	chk_maintance
    }
}
vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass abcdefg
    }
    virtual_ipaddress {
        192.168.0.90/24 label eth0:1
    }
    
    track_script {
	chk_maintance
    }
}


配置结果

[root@Nginx keepalived]# service keepalived restart; ssh node1 'service keepalived restart'
[root@Nginx keepalived]# ip addr | grep eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 192.168.0.104/24 brd 192.168.0.255 scope global eth0
    inet 192.168.0.80/24 scope global secondary eth0:0
    
[root@node1 keepalived]# ip addr | grep eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 192.168.0.40/24 brd 192.168.0.255 scope global eth0
    inet 192.168.0.90/24 scope global secondary eth0:1


    3、测试

调度
[root@Nginx keepalived]# service keepalived stop
停止 keepalived:                                          [确定]
[root@Nginx keepalived]# ip add | grep eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 192.168.0.104/24 brd 192.168.0.255 scope global eth0
[root@node1 keepalived]# ip add | grep eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 192.168.0.40/24 brd 192.168.0.255 scope global eth0
    inet 192.168.0.90/24 scope global secondary eth0:1
    inet 192.168.0.80/24 scope global secondary eth0:0
    
抢回
[root@Nginx keepalived]# service keepalived start
正在启动 keepalived:                                      [确定]
[root@Nginx keepalived]# ip add | grep eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 192.168.0.104/24 brd 192.168.0.255 scope global eth0
    inet 192.168.0.80/24 scope global secondary eth0:0