使用Keepalived实现双机热备

来源:互联网 发布:js输出时间格式 编辑:程序博客网 时间:2024/06/07 00:55

Keepalived简介

     Keepalived的作用是检测服务器的状态,如果有一台web服务器宕机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代替该服务器的工作,当服务器工作正常后Keepalived自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的服务器。

keepalived采用的是VRRP(虚拟路由冗余协议)热备份协议,VRRP路由器是指运行VRRP的路由器,是物理实体,虚拟路由器是指VRRP协议创建的,是逻辑概念。一组VRRP路由器协同工作,共同构成一台虚拟路由器。 Vrrp中存在着一种选举机制,用以选出提供服务的路由即主控路由,其他的则成了备份路由。当主控路由失效后,备份路由中会重新选举出一个主控路由,来继续工作,来保障不间断服务。

keepalived官网http://www.keepalived.org/
下面来以keepalived结合Apache来实现一个web服务器的双机热备。
测试环境: 两台服务器和一个虚拟服务器(vip)
Master:192.168.30.132
Backup: 192.168.30.135
VIP:192.168.30.150

一、Keepalived安装
1)安装支持软件
内核开发软件包kernel-devel 以及openssl-devel popt-devel等支持库,nodeA&nodeB都需要安装!
# yum install gcc gcc-c++ -y && yum install -y kernel-devel openssl-devel popt-develLoaded plugins: product-id, refresh-packagekit, security, subscription-managerThis system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.aaa                                                                                                                                                 | 3.9 kB     00:00 ... aaa/primary_db                                                                                                                                      | 3.1 MB     00:00 ... Setting up Install ProcessResolving Dependencies--> Running transaction check---> Package gcc.x86_64 0:4.4.7-4.el6 will be installed--> Processing Dependency: cpp = 4.4.7-4.el6 for package: gcc-4.4.7-4.el6.x86_64
2)编译安装keepalived(nodeA&nodeB同样的操作)
# tar zxf keepalived-1.2.12.tar.gz [root@localhost ~]# cd keepalived-1.2.12[root@localhost keepalived-1.2.12]# ./configure --prefix=/checking for gcc... gccchecking whether the C compiler works... yeschecking for C compiler default output file name... a.outchecking for suffix of executables... checking whether we are cross compiling... nochecking for suffix of object files... ochecking whether we are using the GNU C compiler... yeschecking whether gcc accepts -g... yeschecking for gcc option to accept ISO C89... none neededchecking for a BSD-compatible install... /usr/bin/install -cchecking for strip... stripchecking how to run the C preprocessor... gcc -Echecking for grep that handles long lines and -e... /bin/grepchecking for egrep... /bin/grep -Echecking for ANSI C header files... yeschecking for sys/wait.h that is POSIX.1 compatible... yeschecking for sys/types.h... yeschecking for sys/stat.h... yes.....# make && make install
3)使用keepalived服务
执行完make install操作后,会自动生成/etc/init.d/keepalived/脚本文件,但是需要手动添加为系统服务,这样就可以使用service chkconfig工具来对keepalived进行管理了。
# ls -l /etc/init.d/keepalived -rwxr-xr-x. 1 root root 1308 9月  18 21:33 /etc/init.d/keepalived[root@localhost ~]# chkconfig --add keepalived[root@localhost ~]# chkconfig keepalived on
二、使用keepalived实现双机热备
1)主服务器配置
# cd /etc/keepalived/[root@localhost keepalived]# vi keepalived.conf#global_defs {   router_id HA_TEST_R1                  }    vrrp_instance VI_1 {           state MASTER         interface eth1                      virtual_router_id 51                  priority 100                        advert_int 1                        authentication {        auth_type PASS                      auth_pass 1111                    }    virtual_ipaddress {                    192.168.30.150/24    }}
配置完成之后保存退出,然后启动keepalived服务,实际状态为MASTER的主服务器为eth1接口自动添加vip地址,通过ip addr命令可以查看
#service keepalived start正在启动 keepalived:                                      [确定][root@localhost ~]# ip addr1: 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 forever2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000    link/ether 00:0c:29:c5:68:3e brd ff:ff:ff:ff:ff:ff    inet 192.168.30.132/24 brd 192.168.30.255 scope global eth1    inet 192.168.30.150/24 scope global eth1    inet6 fe80::20c:29ff:fec5:683e/64 scope link        valid_lft forever preferred_lft forever
此时我们的主服务器已经配置完成
2)从服务器的配置
在同一个热备组内,从服务器的配置与主的配置文件几乎相同,不同之处在优先级,路由名称,热备状况等。以下是从的配置文件
#global_defs {        router_id HA_TEST_R2   } vrrp_instance VI_1 {    state BACKUP    interface eth1    virtual_router_id 51    priority 99    advert_int 1    authentication {        auth_type PASS        auth_pass 1111    }    virtual_ipaddress {        192.168.30.250/24    }}
配置完成后启动keepalived服务,此时主服务器仍然在线,vip地址实际上仍然由主服务器控制,其他的服务器都是处于备用状态,因此从服务器上不会为eth1添加vip地址。
# service keepalived start正在启动 keepalived:                                      [确定][root@localhost ~]# ip addr1: 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 forever2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000    link/ether 00:0c:29:cd:18:0c brd ff:ff:ff:ff:ff:ff    inet 192.168.30.135/24 brd 192.168.30.255 scope global eth1    inet6 fe80::20c:29ff:fecd:180c/64 scope link        valid_lft forever preferred_lft forever
配置完成后我们安装httpd服务测试 nodeA&nodeB都需要安装并且重启,直接yum install httpd就好了。导入页面进去
# service httpd start正在启动 httpd:httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName                                                           [确定][root@localhost ~]# echo "This is yankerp" > /var/www/html/index.html[root@localhost ~]# cat /var/www/html/index.html This is yankerp
主和从都需要导入页面,随后我们去访问VIP地址测试。http://IP   

此时我们发现访问成功,接下来测试高可用双机热备,在这里我们先看一下master和backup的ip信息。
master信息

backup信息

这时候我们把master的ip地址干掉,就是故意摧毁掉它,再次查看backup的ip信息

此时master的ip地址已经摧毁,现在重点来了,再看backup从服务器上的ip地址信息

我们可以看到vip地址成功的让backup接管,这就是我们前面说的这句话:

如果有一台web服务器宕机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代替该服务器的工作,当服务器工作正常后Keepalived自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的服务器。这就实现了我们的高可用。然后在次测试



希望对您有所帮助,再见~