CentOS7,keepalived搭建双机热备

Centos7 keepalived 搭建双机热备

操作环境:
keepalived热备份(双机热备)
node1: Centos 7系统,ip地址: 10.211.55.37 --主服务器
node2: Centos 7系统,ip地址: 10.211.55.16 --从服务器
虚拟IP:10.211.55.26

安装 keepalived

在两台机器上分别安装 keepalived

# yum install keepalived  -y

配置 keepalived

  • node1 上 keepalived 配置
global_defs {
    router_id node1
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        10.211.55.26
    }
}
  • node2 上 keepalived 配置
global_defs {
    router_id node2
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        10.211.55.26
    }
}

配置说明

global_defs {
	router_id node1 #这个id自已改下,不重复即可
	//这里可能有一些发邮件的相关配置,可以直接删除,保留上面那行router_id即可
}
vrrp_instance VI_1 {
    state MASTER             #主服务器就写MASTER,备用的就写BACKUP
    interface eth0       		#网口名,写服务器IP对应的网卡就行
    virtual_router_id 51     #主、备必须一样
    priority 100 				    #主、备机取不同的优先级,主机值较大,备份机值较小,值越大优先级越高
    advert_int 1    			    #VRRP Multicast广播周期秒数
    authentication {
        auth_type PASS     #VRRP认证方式,主备必须一致 
        auth_pass 1111      #密码
    }
    virtual_ipaddress {
         10.211.55.26        #VRRP 虚拟地址
    }
}

启动keepalived

# service keepalived start
# chkconfig keepalived on

###查看IP

  • node1
# ip addr show eth0
2: eth0:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:1c:42:06:c9:04 brd ff:ff:ff:ff:ff:ff
    inet 10.211.55.37/24 brd 10.211.55.255 scope global dynamic eth0
       valid_lft 1269sec preferred_lft 1269sec
    inet 10.211.55.26/32 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fdb2:2c26:f4e4:0:21c:42ff:fe06:c904/64 scope global noprefixroute dynamic 
       valid_lft 2591952sec preferred_lft 604752sec
    inet6 fe80::21c:42ff:fe06:c904/64 scope link 
       valid_lft forever preferred_lft forever
  • node2
# ip addr show eth0
2: eth0:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:1c:42:e1:ec:7f brd ff:ff:ff:ff:ff:ff
    inet 10.211.55.16/24 brd 10.211.55.255 scope global dynamic eth0
       valid_lft 1209sec preferred_lft 1209sec
    inet6 fdb2:2c26:f4e4:0:21c:42ff:fee1:ec7f/64 scope global noprefixroute dynamic 
       valid_lft 2591905sec preferred_lft 604705sec
    inet6 fe80::21c:42ff:fee1:ec7f/64 scope link 
       valid_lft forever preferred_lft forever

此时node1服务器包含虚拟IP地址10.211.55.26,node2服务器不包含虚拟IP地址10.211.55.26。

测试

用另一台电脑ping虚拟IP 10.211.55.26

bing:~ bing$ ping 10.211.55.26
PING 10.211.55.26 (10.211.55.26): 56 data bytes
64 bytes from 10.211.55.26: icmp_seq=0 ttl=64 time=0.252 ms
64 bytes from 10.211.55.26: icmp_seq=1 ttl=64 time=0.423 ms
64 bytes from 10.211.55.26: icmp_seq=2 ttl=64 time=0.286 ms

虚拟IP可以ping通,然后停止node1上的keepalived服务,模拟node1服务器出现异常。

# service keepalived stop
再查看node1 IP 地址
# ip addr show eth0
2: eth0:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:1c:42:06:c9:04 brd ff:ff:ff:ff:ff:ff
    inet 10.211.55.37/24 brd 10.211.55.255 scope global dynamic eth0
       valid_lft 1522sec preferred_lft 1522sec
    inet6 fdb2:2c26:f4e4:0:21c:42ff:fe06:c904/64 scope global noprefixroute dynamic 
       valid_lft 2591720sec preferred_lft 604520sec
    inet6 fe80::21c:42ff:fe06:c904/64 scope link 
       valid_lft forever preferred_lft forever
 此时node1服务器不再包含虚拟IP地址10.211.55.26

然后再ping虚拟IP 10.211.55.26

bing:~ bing$ ping 10.211.55.26
PING 10.211.55.26 (10.211.55.26): 56 data bytes
64 bytes from 10.211.55.26: icmp_seq=0 ttl=64 time=0.412 ms
64 bytes from 10.211.55.26: icmp_seq=1 ttl=64 time=0.285 ms
64 bytes from 10.211.55.26: icmp_seq=2 ttl=64 time=0.340 ms
64 bytes from 10.211.55.26: icmp_seq=3 ttl=64 time=0.355 ms

还可以ping通,查看node2服务器的IP地址

# ip addr show eth0
2: eth0:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:1c:42:e1:ec:7f brd ff:ff:ff:ff:ff:ff
    inet 10.211.55.16/24 brd 10.211.55.255 scope global dynamic eth0
       valid_lft 1396sec preferred_lft 1396sec
    inet 10.211.55.26/32 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fdb2:2c26:f4e4:0:21c:42ff:fee1:ec7f/64 scope global noprefixroute dynamic 
       valid_lft 2591906sec preferred_lft 604706sec
    inet6 fe80::21c:42ff:fee1:ec7f/64 scope link 
       valid_lft forever preferred_lft forever

可以看到虚拟IP地址,说明此时ping通的是备用机node2.

此时如果再把 node1 上的 keepalived 服务启动,会发现虚拟IP “10.211.55.26” 又重新绑定到 node1 上了

你可能感兴趣的:(linux)