接上一章,搞下Keepalived的东东,那么它到底是干嘛的呢?
以下是引自百度简介:
Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器。
用Keepalived的主要作用其实上面说的那些基本上不会出现,现在的硬件较为发达,所以很少会出现服务器挂掉的时候,主要还是对外提供一个稳定的虚拟ip,用于对外访问使用。
不废话,Come On!
前提交代:
1、假设,所有的安装包默认被拷贝到 /tmp/ 目录下;
2、至少两台(虚拟)主机,每台虚拟机需要1个静态IP地址,还需要至少1个IP地址作为VIP(虚IP),即最少需要3个静态IP地址。
安装步骤:
1、安装Keepalived:
cd /tmp
tar -zxvf keepalived-1.2.12.tar.gz -C /usr/src/
#等待解压完毕…
cd /usr/src/keepalived-1.2.12/
./configure
#等待配置完毕…
make && make install
配置步骤:
1、文件目录的调整:
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/
2、设置开机启动:
chkconfig --add keepalived
chkconfig keepalived on
3、Keepalived Master配置文件:
! Configuration File for keepalived
global_defs {
notification_email {
test@163.com
}
#邮箱配置
notification_email_from test@163.com
smtp_server stmp.163.com
smtp_connect_timeout 30
router_id ACLOME_DEV
}
vrrp_script chk_nginx {
script "killall -0 nginx"
interval 1
weight -2
fall 2
rise 1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
#虚拟ip
virtual_ipaddress {
192.168.1.100
}
track_script {
chk_nginx
}
}
4、Keepalived Backup配置文件:
! Configuration File for keepalived
global_defs {
notification_email {
test@163.com
}
#邮箱配置
notification_email_from test@163.com
smtp_server stmp.163.com
smtp_connect_timeout 30
router_id ACLOME_DEV
}
vrrp_script chk_nginx {
script "killall -0 nginx"
interval 1
weight -2
fall 2
rise 1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
#虚拟ip
virtual_ipaddress {
192.168.1.100
}
track_script {
chk_nginx
}
}
5、配置防火墙:
service iptables stop
chkconfig iptables off
6、启动Keepalived:
service keepalived start
7、执行ip addr命令可以查看虚拟IP地址已经被配置到网卡
[root@lb1 /]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether f2:2f:03:a1:5d:81 brd ff:ff:ff:ff:ff:ff
inet 10.4.55.6/26 brd 10.4.55.63 scope global eth0
inet 10.4.55.23/32 scope global eth0
inet6 fe80::f02f:3ff:fea1:5d81/64 scope link
valid_lft forever preferred_lft forever
至此就完成了Keepalived的安装配置,相对来说比较简单。