一、环境说明
安装环境:
主机名 | 主机IP地址 |
集群角色 |
集群服务 |
虚拟IP地址 |
keepalived-master |
192.168.1.1 |
MASTER(主节点) |
HTTPD |
192.168.1.3 |
keepalived-backup |
192.168.1.2 | BACKUP(备用节点) |
HTTPD |
官方地址:http://www.keepalived.org/
一、软件部署
分别在keepalived-master 与 keepalived-backup 安装keepalived软件
yum install openssl-devel -y
tar zxvf keepalived-1.2.20.tar.gz
./configure --sysconf=/etc --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64
make && make install
ln -s /usr/local/sbin/keepalived /sbin/
chkconfig --add keepalived
chkconfig --level 35 keepalived on
二、创建配置文件
Keepalived-master 配置文件:
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script check_httpd {
script "killall -0 httpd"
interval 2
}
vrrp_instance HA_1 {
state MASTER
interface eth0
virtual_router_id 80
priority 100
advert_int 2
authentication {
auth_type PASS
auth_pass qweasdzxc
}
notify_master "/etc/keepalived/master.sh"
notify_backup "/etc/keepalived/backup.sh"
notify_fault "/etc/keepalived/fault.sh"
track_script {
check_httpd
}
virtual_ipaddress {
192.168.1.3/24 dev eth0
}
}
其中,master.sh文件内容为:
#!/bin/bash
LOGFILE=/var/log/keepalived-mysql-state.log
echo "[Master]" >> $LOGFILE
date >> $LOGFILE
其中,backup.sh文件内容为:
#!/bin/bash
LOGFILE=/var/log/keepalived-mysql-state.log
echo "[Backup]" >> $LOGFILE
date >> $LOGFILE
其中,fault.sh文件内容为:
#!/bin/bash
LOGFILE=/var/log/keepalived-mysql-state.log
echo "[Fault]" >> $LOGFILE
date >> $LOGFILE
Keepalived-backup 配置文件:
Keepalived-backup节点上的keepalived.conf配置文件与keepalived-master节点上基本相同,需要修改的地方有两个:
将“state MASTER”更改为 “state BACKUP”。
将priority 100 更改为一个较小的值,这里改为“priority 80”。
三、启动服务
分别在keepalived-master与 keepalived-backup节点启动keepalived服务
chkconfig --level 35 httpd on
/etc/init.d/httpd start
/etc/init.d/keepalived start
四、通过日志可查看启动信息
tail -f /var/log/keepalived-mysql-state.log
tail -f /var/log/messages
命令查看:ip add
五、模拟故障切换
停掉keepalived-master 节点的 HTTPD服务,发现192.168.1.3 的IP正确转移至 keepalived-backup节点上。
报错:
[root@nfs-server ~]# /etc/init.d/keepalived start
正在启动 keepalived:/bin/bash: keepalived: command not found
[失败]
解决:
ln -s /usr/local/sbin/keepalived /usr/sbin/