一、概述
2.配置需求
两个或多个主机以构成冗余主机组;
一个共享的虚拟ip地址,用以对外提供可靠的服务,冗余主机组中的某个主机会对会对这个ip上的服务进行应答;
对于每个主机,需要配置一个真实的ip地址;
一个1-255之间的共享标识符;
一个共享的密码(使得网络间传输的消息都为密文);
当冗余组中某个主机成为MASTER时执行的脚本程序;
当冗余组中某个主机不再是MASTER是执行的脚本程序;
3 Ucap具体参数详解
--inter (-i <if>: bind interface <if> (网络接口绑定网络接口)
--srcip=<ip> (-s <ip>: source (real) IP address of that host (源地址真实ip)
--vhid=<id> (-v <id>: virtual IP identifier (1-255) (虚拟ip标识 (1-255))
--pass=<pass> (-p <pass>: password (密码)
--preempt (-P): becomes a master as soon as possible (以最快的速度成为主服务器的角色)
--neutral (-n): don't run downscript at start if backup (如果是备份主机,启动的时候不运行downscript.)
--addr=<ip> (-a <ip>: virtual shared IP address( 共享虚拟ip地址)
--help (-h): summary of command-line options (帮助)
--advbase=<seconds> (-b <seconds>: advertisement frequency (广播的频率(秒))
--advskew=<skew> (-k <skew>: advertisement skew (0-255) (不广播)用来设置公告间隔时间, 公式(以秒计)为advskew/256+advbase。advbase可以减少网络流量或设置更长的主力机"轮循"时间(直至备份机来顶替它);advskew设置哪台热备份计算机在故障转移时优先成为主力机(这是必需的)
--upscript=<file> (-u <file>: run <file> to become a master (运行一个脚本文件,使本服务器成为主服务器)
--downscript=<file> (-d <file>: run <file> to become a backup (运行一个脚本文件,使本服务器成为从服务器)
--deadratio=<ratio> (-r <ratio>: ratio to consider a host as dead (认定主机已经死掉的比率(阀值))
--shutdown (-z): call shutdown script at exit (在退出的时候,执行关闭的脚本)
--daemonize (-B): run in background (运行在后台)
--facility=<facility> (-f): set syslog facility (default=daemon) (设置syslog工具,默认在后台)
Ucarp参数简述:
-v vip标识 –p 密码 –a vip地址 –u 当机器为升为master时运行的脚本 –d 当机器降为slave时运行的脚本 –s心跳ip地址 –P与-k结合最小的为master机器 –B以daemon方式运行
4.实验环境
Role hostname real_ip vip gateway OS
master node2 192.168.32.32/24 192.168.32.22/24 192.168.32.254 rhel5.5
slave node3 192.168.32.33/24 192.168.32.22/24 192.168.32.254 rhel5.5
二、安装配置
1、安装
[root@node2 ~]# tar -zxf ucarp-1.5.tar.gz #ucarp最好选择1.2以上版本,1.1以下含有bug
[root@node2 ~]# cd ucarp-1.5
[root@node2 ucarp-1.5]# which gcc #确定gcc
/usr/bin/gcc
[root@node2 ucarp-1.5]# ./configure CC=/usr/bin/gcc --prefix=/usr/local/ucarp
[root@node2 ucarp-1.5]# make && make install
2、配置主服务器(node2)
2.1 编写机器升为master时运行的脚本
[root@node2 ~]# vim /etc/master-up.sh
#!/bin/bash
GATEWAY=192.168.32.254
/sbin/ip addr add 192.168.32.22/24 dev eth0
/bin/hostname nodevir2
/sbin/route add default gw $GATEWAY
service httpd start
2.2 编写机器降为slave时运行的脚本
[root@node2 ~]# vim /etc/master-down.sh
#!/bin/bash
GATEWAY=192.168.32.254
/sbin/ip addr del 192.168.32.22/24 dev eth0
/bin/hostname node2
/sbin/route add default gw $GATEWAY
service httpd stop
2.3 编写ucarp运行脚本
[root@node2 ~]# vim /etc/master.sh
#!/bin/bash
/usr/local/ucarp/sbin/ucarp -i eth0 -v 40 -p gw22 -a 192.168.32.22 -u /etc/master-up.sh -d /etc/master-down.sh -s 192.168.32.32 -P -B
2.4 启动服务
[root@node2 ~]# . /etc/master.sh
[root@node2 ~]# ps -ef | grep ucarp
root 11695 1 0 11:33 ? 00:00:00 /usr/local/ucarp/sbin/ucarp -i eth0 -v 40 -p gw22 -a 192.168.32.22 -u /root/master-up.sh -d /root/master-down.sh -s 192.168.32.32 -P -B
[root@node2 ~]# hostname #hostname切换成功
nodevir2
[root@node2 ~]# ip addr show | grep 22 #master-up.sh脚本生效
inet 192.168.32.22/24 scope global secondary eth0
[root@nodevir2 ~]# ps -ef | grep httpd #httpd服务启动
root 11726 1 0 11:33 ? 00:00:00 /usr/sbin/httpd
apache 11727 11726 0 11:33 ? 00:00:00 /usr/sbin/httpd
apache 11729 11726 0 11:33 ? 00:00:00 /usr/sbin/httpd
apache 11730 11726 0 11:33 ? 00:00:00 /usr/sbin/httpd
apache 11731 11726 0 11:33 ? 00:00:00 /usr/sbin/httpd
apache 11732 11726 0 11:33 ? 00:00:00 /usr/sbin/httpd
apache 11733 11726 0 11:33 ? 00:00:00 /usr/sbin/httpd
apache 11734 11726 0 11:33 ? 00:00:00 /usr/sbin/httpd
apache 11735 11726 0 11:33 ? 00:00:00 /usr/sbin/httpd
root 11897 6167 0 11:44 pts/1 00:00:00 grep httpd
3.配置备服务器(node3)
3.1 编写机器升为master时运行的脚本
[root@node3 ~]# vim /etc/master-up.sh
#!/bin/bash
GATEWAY=192.168.32.254
/sbin/ip addr add 192.168.32.22/24 dev eth0
/bin/hostname nodevir2
/sbin/route add default gw $GATEWAY
service httpd start
3.2 编写机器降为slave时运行的脚本
[root@node2 ~]# vim /etc/master-down.sh
#!/bin/bash
GATEWAY=192.168.32.254
/sbin/ip addr del 192.168.32.22/24 dev eth0
/bin/hostname node3
/sbin/route add default gw $GATEWAY
service httpd stop
3.3 编写ucarp运行脚本
[root@node2 ~]# vim /etc/master.sh
#!/bin/bash
/usr/local/ucarp/sbin/ucarp -i eth0 -v 40 -p gw22 -a 192.168.32.22 -u /etc/master-up.sh -d /etc/master-down.sh -s 192.168.32.33 -P -k 15 -B
3.4 启动服务
[root@node2 ~]# . /etc/master.sh
root 5678 1 0 11:40 ? 00:00:00 /usr/local/ucarp/sbin/ucarp -v 40 -i eth0 -p gw22 -a 192.168.32.22 -u /root/master-up.sh -d /root/master-down.sh -s 192.168.32.33 -k 15 -B
[root@node3 ~]# ps -ef | grep httpd #httpd服务为启动
root 5788 32767 0 11:46 pts/1 00:00:00 grep httpd
三. 故障切换
[root@node3 ~]# hostname #hostname切换成功
nodevir2
[root@node3 ~]# ip addr show | grep 22 #虚拟IP切换成功
inet 192.168.32.22/24 scope global secondary eth0
[root@node3 ~]# ps -ef | grep httpd #服务切换成功
root 5824 1 0 11:47 ? 00:00:00 /usr/sbin/httpd
apache 5826 5824 0 11:47 ? 00:00:00 /usr/sbin/httpd
apache 5827 5824 0 11:47 ? 00:00:00 /usr/sbin/httpd
apache 5828 5824 0 11:47 ? 00:00:00 /usr/sbin/httpd
apache 5829 5824 0 11:47 ? 00:00:00 /usr/sbin/httpd
apache 5830 5824 0 11:47 ? 00:00:00 /usr/sbin/httpd
apache 5831 5824 0 11:47 ? 00:00:00 /usr/sbin/httpd
apache 5832 5824 0 11:47 ? 00:00:00 /usr/sbin/httpd
apache 5833 5824 0 11:47 ? 00:00:00 /
2.主恢复
[root@node2 ~]# . /etc/master.sh
[root@nodevir2 ~]# hostname
nodevir2
[root@node2 ~]# ps -ef | grep httpd
root 11969 1 0 11:46 ? 00:00:00 /usr/sbin/httpd
apache 11970 11969 0 11:46 ? 00:00:00 /usr/sbin/httpd
apache 11972 11969 0 11:46 ? 00:00:00 /usr/sbin/httpd
apache 11973 11969 0 11:46 ? 00:00:00 /usr/sbin/httpd
apache 11974 11969 0 11:46 ? 00:00:00 /usr/sbin/httpd
apache 11975 11969 0 11:46 ? 00:00:00 /usr/sbin/httpd
apache 11976 11969 0 11:46 ? 00:00:00 /usr/sbin/httpd
apache 11977 11969 0 11:46 ? 00:00:00 /usr/sbin/httpd
apache 11978 11969 0 11:46 ? 00:00:00 /usr/sbin/httpd
[root@node2 ~]# ip addr show | grep 22
inet 192.168.32.22/24 scope global secondary eth0
[root@node3 ~]# hostname
node3
[root@node3 ~]# ps -ef | grep httpd
root 6148 32767 0 12:02 pts/1 00:00:00 grep httpd
[root@node3 ~]# ip addr show | grep 22 #成功切换
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。