RHEL6.5 + Keepalived 实现双机热备
目录:
一、工作原理................................................................................................................... 2
二、安装环境规划:........................................................................................................ 2
三、操作系统安装.......................................................................................................... 3
3.1 配置/etc/hosts文件........................................................................................ 3
3.2 Master/slaver分别配置防火墙........................................................................ 3
3.3 配置RHEL6.5本地镜像yum源........................................................................... 4
3.3.1 为本地镜像创建挂载目录......................................................................... 4
3.3.2 挂载DVD至本地新建目录......................................................................... 4
3.3.3 备份yum源配置文件................................................................................ 4
3.3.4 修改yum源配置文件................................................................................ 4
3.3.5 清除旧的缓存数据................................................................................... 5
3.3.6 配置本地yum源完成................................................................................ 5
四、keepalived安装:.................................................................................................... 5
4.1 开始安装.......................................................................................................... 5
4.2 配置文件整理:................................................................................................ 5
4.3 修改master配置文件:.................................................................................... 6
4.4 修改slaver配置文件....................................................................................... 7
五、keepalived管理命令................................................................................................. 8
六、测试.......................................................................................................................... 8
6.1 在本机上查看虚拟ip状态................................................................................ 8
6.2 在同一网段的其他机器上查询arp缓存............................................................. 9
6.3 停掉master的master网络/keepalived........................................................... 9
6.4 重启master网络/keepalived服务................................................................. 10
七、测试http服务......................................................................................................... 11
7.1 (master/slave)分别安装http服务:......................................................... 11
7.2 创建测试页面................................................................................................. 11
7.3 Master正常情况访问测试............................................................................... 11
7.4 Master宕机或者keepalived服务异常............................................................ 12
八、总结........................................................................................................................ 12
Keepalived实现双机热备
一、工作原理
首先有一个虚拟ip暴露给客户端,虚拟ip对应的mac地址为一台真实服务器,即用户向虚拟ip发送一个请求,该请求会被分发到真实服务器上。
现在有2台真实服务器,一台master,一台backup,master和backup上都运行着keepalived,当master挂了的时候,backup检测之后,自己成为master,且arp缓存虚拟ip对应的mac地址将变为backup的mac地址,这样请求虚拟ip的报文会被发送到backup。
架构图如下:
二、安装环境规划:
主机:192.168.1.171
备机:192.168.1.172
虚拟ip:192.168.1.173
操作系统:RedHat Enterprise Linux 6.5 x86_64
三、操作系统安装
操作系统安装过程网上安装文档较多,此过程省略。
3.1 配置/etc/hosts文件
分别在master和slaver主机写入
#vim /etc/hosts
192.168.1.171 rhelha01.site
192.168.1.172 rhelha02.site
3.2 Master/slaver分别配置防火墙
关闭防火墙
#serviceiptables stop
#vim/etc/selinux/config
SELINUX=disabled
3.3 配置RHEL6.5本地镜像yum源
利用RHEL6.5操作系统光盘制作一个本地yum源,具体配置步骤如下:
3.3.1 为本地镜像创建挂载目录
#mkdir /media/mnt
3.3.2挂载DVD至本地新建目录
#mount /dev/cdrom /media/mnt
3.3.3 备份yum源配置文件
#cp /etc/yum.repos.d/rhel-source.repo/etc/yum.repos.d/rhel-source.repo.backup
3.3.4 修改yum源配置文件
#vim /etc/yum.repos.d/rhel-source.repo
删除原有内容,添加如下:
[RHEL-Source]
name=local ios
baseurl=file:///media/mnt/
enabled=1
gpgcheck=0
gpgkey=file:///media/mnt/RPM-GPG-KEY-redhat-release
3.3.5清除旧的缓存数据
yum clean all
yum makecache
yum list
3.3.6配置本地yum源完成
四、keepalived安装:
下载:keepalived-1.2.16.tar.gz
http://www.keepalived.org/download.html
4.1 开始安装
# tar zxf keepalived-1.2.16.tar.gz
cd keepalived-1.2.16
#./configure --disable-fwmark--prefix=/usr/local/keepalived
#make && make install
4.2 配置文件整理:
mkdir/etc/keepalived/
cp/usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp/usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived/etc/sysconfig/
ln -s/usr/local/keepalived/sbin/keepalived /sbin/
4.3 修改master配置文件:
vi /etc/keepalived/keepalived.conf
###############################################
Maste! Configuration File forkeepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_typePASS
auth_pass1111
}
virtual_ipaddress {
192.168.1.173
}
}
######################################################
4.4 修改slaver配置文件
vi /etc/keepalived/keepalived.conf
Backup 和master一样,只要修改如下地方:
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99 # 这里改为99,master优先级是100
advert_int 1
authentication {
auth_typePASS
auth_pass1111
}
virtual_ipaddress {
192.168.1.173
}
}
五、keepalived管理命令
停止
service keepalived stop
启动
service keepalived start
重启
service keepalived restart
查看状态
service keepalived status
六、测试
6.1 在本机上查看虚拟ip状态
ip add
6.2 在同一网段的其他机器上查询arp缓存
#arp -a
这里的1.73对应的mac地址6a:c7(即master的MAC地址)。
6.3 停掉master的master网络/keepalived
#servicekeepalived stop
在同一网段的其他机器上查询arp缓存
#arp �Ca
发现1.173的mac地址已经变为5a:7c(即slave的mac地址)。
在同一网段的其他机器上,测试连接1.173,发现成功切换到ha02(1.172)主机。
6.4 重启master网络/keepalived服务
# arp -a
这里的1.73重新对应的mac地址6a:c7(即master的MAC地址)。
在同一网段的其他机器上,测试连接1.173,发现成功切换到ha01(1.171)主机。
那么主备双机热备就完成了!
七、测试http服务
7.1 (master/slave)分别安装http服务:
#cd/media/mnt/Packages/
#yum install -yhttpd*
#service httpdstart
#chkconfighttpd on
7.2 创建测试页面
Master:
#echo"redhat-ha-master-server01">>/var/www/html/index.html
Slaver:
#echo"redhat-ha-master-server02">>/var/www/html/index.html
7.3 Master正常情况访问测试
7.4 Master宕机或者keepalived服务异常
可以看到服务已经由redhat-ha-master-server01自动转换至redhat-ha-master-server02.
八、总结
1.master主机挂了,或者master 上keepalived挂了之后,可以切换到slave
2.在主机宕机或断网后可能需要重启keepalived服务。