使用 keepalived 实现前端HA双机热备,供大家学习参考。
具体的配置步骤如下:
复制代码 代码如下:
# tar zxvf keepalived-1.1.19.tar.gz
# cd keepalived-1.1.19
# ./configure --prefix=/opt/app/keepalived
# make
# make install
# cp /opt/app/keepalived/sbin/keepalived /usr/sbin/
# cp /opt/app/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# cp /opt/app/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
# mkdir /etc/keepalived
# cd /etc/keepalived/# vi keepalived.conf! Configuration File for keepalived
global_defs {
notification_email {
shenliyang163@163.com
}
notification_email shenliyang163@163.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface
eth0
virtual_router_id 51
mcast_src_ip 192.168.2.20 <==辅
nginx的
IP地址
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass chtopnet
}
virtual_ipaddress {
192.168.2.88 <==VIP地址
}
}
# service keepalived start
通过命令看一下# ip a
1: lo: <LOOPBACK,UP> mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 brd 127.255.255.255 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:af:08:1d brd ff:ff:ff:ff:ff:ff
inet 192.168.10.61/24 brd 192.168.10.255 scope global eth0
inet 192.168.10.60/32 scope global eth0
inet6 fe80::20c:29ff:feaf:81d/64 scope link
valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0
说明vip已经启动,这样主服务器就配置好了,辅机的配置大致一样,除了配置文件有少部分的变化。
以下是辅机的配置文件:
复制代码 代码如下:
! Configuration File for keepalived
global_defs {
notification_email {
shenliyang@163.com
}
notification_email_from shenliyang163@163.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
mcast_src_ip 192.168.2.10
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass yahunet
}
virtual_ipaddress {
192.168.2.88
}
备注:
两台服务器都需要配置keepalived。
手动切换,只需要在主机上停止keepalived服务,自动会切到备机上。
使用命令ip a查看虚拟IP地址。