安装依赖
yum install popt-devel openssl openssl-devel libssl-dev libnl-devel popt-devel gcc gcc+ gcc-c++
安装内核
yum -y install kernel kernel-devel
当前kernel代码建立连接 ln -s /usr/src/kerners/2.6……/ /usr/src/linux
安装keepalived
cd /usr/local
wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
tar -zxvf keepalived-1.2.2.tar.gz
cd keepalived-1.2.2
./configure
make
make install
拷贝相应的文件
cp /usr/local/sbin/keepalived /usr/sbin/
cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
cp -r /usr/local/etc/keepalived/ /etc/
设置vip
vi /etc/sysconfig/network-scripts/ifcfg-eth2:0
DEVICE=eth2:0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
DNS1=192.168.232.2
IPADDR=192.168.232.16
NETMASK=255.255.255.0
GETWAY=192.168.232.2
主机配置
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
#acassen@firewall.loc
#failover@firewall.loc
#sysadmin@firewall.loc
}
#notification_email_from Alexandre.Cassen@firewall.loc
#smtp_server 192.168.200.1
#smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "</dev/tcp/127.0.0.1/8088"
interval 1
weight -2
}
vrrp_instance VI_1 {
state MASTER
interface eth2
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.232.16
}
track_script {
chk_http_port
}
}
备份机
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
#acassen@firewall.loc
#failover@firewall.loc
#sysadmin@firewall.loc
}
#notification_email_from Alexandre.Cassen@firewall.loc
#smtp_server 192.168.200.1
#smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "</dev/tcp/127.0.0.1/8088"
interval 1
weight -2
}
vrrp_instance VI_1 {
state BACKUP
interface eth2
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.232.16
}
track_script {
chk_http_port
}
}
Tips:
state 参数值:主的是MASTER、备用的是BACKUP
priority 参数值: MASTER > BACKUP
virtual_router_id: 参数值要一样
virtual_ipaddress的值是vip的虚拟ip
interface eth2 是网卡配置 根据需要的修改
配置vip 两台机器公用一个vip 例如 166.111.69.100命令如下,eth0 是主网卡配置
例子
ifconfig eth0:0 166.111.69.100 netmask 255.255.255.0 up
检查防火墙是否安装(重要,否则可能会无法访问)
在此说一下关于启动和关闭防火墙的命令:
1) 重启后生效
开启: chkconfig iptables on
关闭: chkconfig iptables off
2) 即时生效,重启后失效
开启: service iptables start
关闭: service iptables stop
这里可能会出现错误码 检查错误码 是否是检查防火墙是否安装 非常重要
启动
service keepalived restart
查看日志 tail -f /var/log/messages
这样就可以可以了。测试一下分别kill掉主机和备份机,发现依旧可以访问
参考文章
http://blog.csdn.net/e421083458/article/details/30086413