Keepalived提供了很好的高可用性保障服务,它可以检查服务器的状态,如果有服务器出现问题,Keepalived会将其从系统中移除,当这台服务器可以正常工作后,
Keepalived再将其放入服务器群中,这个过程是Keepalived自动完成的,不需要人工干涉,我们只需要修复出现问题的服务器
高可用HA(High Availability)是分布式系统架构设计中必须考虑的因素之一,它通常是指,通过设计减少系统不能提供服务的时间
假设系统一直能够提供服务,我们说系统的可用性是100%
如果系统每运行100个时间单位,会有1个时间单位无法提供服务,我们说系统的可用性是99%
很多公司的高可用目标是4个9,也就是99.99%,这就意味着,系统的年停机时间为8.76个小时
通过vrrp协议实现,虚拟路由冗余协议
主服务器172.17.1.32
从服务器172.17.1.24
一、主服务器配置
1、查看网卡
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether c4:00:ad:20:93:e6 brd ff:ff:ff:ff:ff:ff
inet 172.17.1.32/16 brd 172.17.255.255 scope global noprefixroute enp2s0
valid_lft forever preferred_lft forever
inet6 fe80::f104:bae1:d237:4d35/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: enp4s15: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
link/ether 08:57:00:f7:a4:64 brd ff:ff:ff:ff:ff:ff
[root@localhost ~]#
2、安装
yum install-y keepalived
3、查看配置文件位置
rpm -qc keepalived
[root@localhost ~]# rpm -qc keepalived
/etc/keepalived/keepalived.conf
/etc/sysconfig/keepalived
[root@localhost ~]#
4、编写配置文件内容
global_defs {
router_id lb01
}
vrrp_instance oldboy {
state MASTER
interface enp2s0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.17.1.34
}
}
5、启动
systemctl start keepalived.service
systemctl stop keepalived.service
systemctl restart keepalived.service
开机自启动
systemctl enable keepalived
6、查看
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether c4:00:ad:20:93:e6 brd ff:ff:ff:ff:ff:ff
inet 172.17.1.32/16 brd 172.17.255.255 scope global noprefixroute enp2s0
valid_lft forever preferred_lft forever
inet 172.17.1.34/32 scope global enp2s0
valid_lft forever preferred_lft forever
inet6 fe80::f104:bae1:d237:4d35/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: enp4s15: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
二、从服务器配置
1、查看网卡
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp7s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
link/ether 40:b0:76:47:f1:39 brd ff:ff:ff:ff:ff:ff
3: enp4s0f0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 2c:53:4a:09:e4:fa brd ff:ff:ff:ff:ff:ff
inet 172.17.1.24/24 brd 172.17.1.255 scope global noprefixroute enp4s0f0
valid_lft forever preferred_lft forever
inet 172.17.1.34/32 scope global enp4s0f0
valid_lft forever preferred_lft forever
inet6 fe80::1903:e1e:a381:6790/64 scope link noprefixroute
valid_lft forever preferred_lft forever
2、安装
yum install-y keepalived
3、查看配置文件位置
rpm -qc keepalived
[root@localhost ~]# rpm -qc keepalived
/etc/keepalived/keepalived.conf
/etc/sysconfig/keepalived
[root@localhost ~]#
4、编写配置文件
global_defs {
router_id lb01
}
vrrp_instance oldboy {
state BACKUP
interface enp4s0f0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.17.1.34
}
}
5、启动
systemctl start keepalived.service
systemctl stop keepalived.service
systemctl restart keepalived.service
开机自启动
systemctl enable keepalived
6、查看
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp7s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
link/ether 40:b0:76:47:f1:39 brd ff:ff:ff:ff:ff:ff
3: enp4s0f0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 2c:53:4a:09:e4:fa brd
ff:ff:ff:ff:ff:ff
inet 172.17.1.24/24 brd 172.17.1.255 scope global noprefixroute enp4s0f0
valid_lft forever preferred_lft forever
inet6 fe80::1903:e1e:a381:6790/64 scope link noprefixroute
valid_lft forever preferred_lft forever
三、测试
1、主服务器
systemctl stop keepalived.service
2、查看VIP是否漂移
查看主服务器
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether c4:00:ad:20:93:e6 brd ff:ff:ff:ff:ff:ff
inet 172.17.1.32/16 brd 172.17.255.255 scope global noprefixroute enp2s0
valid_lft forever preferred_lft forever
inet6 fe80::f104:bae1:d237:4d35/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: enp4s15: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
查看从服务器
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp7s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
link/ether 40:b0:76:47:f1:39 brd ff:ff:ff:ff:ff:ff
3: enp4s0f0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 2c:53:4a:09:e4:fa brd ff:ff:ff:ff:ff:ff
inet 172.17.1.24/24 brd 172.17.1.255 scope global noprefixroute enp4s0f0
valid_lft forever preferred_lft forever
inet 172.17.1.34/32 scope global enp4s0f0
valid_lft forever preferred_lft forever
inet6 fe80::1903:e1e:a381:6790/64 scope link noprefixroute
valid_lft forever preferred_lft forever