Chinaunix首页 | 论坛 | 博客
  • 博客访问: 97884
  • 博文数量: 6
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 65
  • 用 户 组: 普通用户
  • 注册时间: 2014-10-12 22:15
个人简介

简简单单

文章分类
文章存档

2015年(6)

分类: LINUX

2015-09-24 17:35:02

一、前言

      Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器。keepalived是集群管理中保证集群高可用的一个服务软件,其功能类似于heartbeat,用来防止单点故障。

二、准备工作

1.系统参数

  Master:centos5.8(32)     Ip192.168.18.88

  Slave:centos5.8(32)        Ip192.168.18.89

   VIP:192.168.18.90

2.keepalived安装包
 keepalived-1.2.11.tar.gz

三、编译安装

#tar zxvf keepalived-1.2.11.tar.gz

#cd keepalived-1.2.11

#./configure --prefix=/usr/local/keepalived

#make

#make install

 

注:说明一下,所设keepalived安装到/usr/local/keepalived, 则所有配置文件均位于此目录之下。Linux默认总是将程序安装到/usr/local目录下,所有程序共享了/usr/local/sbin目录,这对一些人的操作习惯是有影响的。

 

四、设置keepalived服务启动脚本

1. 建立服务启动脚本,以便使用service命令控制

#cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived

#chmod +x /etc/init.d/keepalived

因为我们使用非默认路径(/usr/local)安装keepalived, 故需要修改几处路径,以保证keepalived能正常启动, 需要修改的文件如下:

(1)修改/etc/init.d/keepalived,

寻找大约15行左右的. /etc/sysconfig/keepalived,

修改为:. /usr/local/keepalived/etc/sysconfig/keepalived, 即指向正确的文件位置

同时在上述行下添加以下内容(将keepavlied主程序所在路径导入到环境变量PATH中):

PATH="$PATH:/usr/local/keepalived/sbin"

export PATH

(2)修改/usr/local/keepalived/etc/sysconfig/keepalived文件,设置正确的服务启动参数 KEEPALIVED_OPTIONS="-D -f /usr/local/keepalived/etc/keepalived/keepalived.conf"

经过以上修改,keepalived基本安装即可完成,启动测试:

#service keepalived restart

(3) 切勿忘记将此服务设置为开机启动

#chkconfig keepalived on

 

 

五、配置keepalived参数

1. Master服务器:

#vim /usr/local/keepalived/etc/keepalived/keepalived.conf

注:可以把以前的配置的文件删掉或者重新建一个。把以下参数加进去复制进去。

 


  1. ! Configuration File for keepalived
  2. global_defs {
  3. router_id NodeA #节点A
  4. }
  5. vrrp_instance VI_1 {
  6. state MASTER #设置为主服务器名
  7. interface eth0 #监测网络接口
  8. virtual_router_id 51 #主、备必须一样
  9. priority 100 #(主、备机取不同的优先级,主机值较大,备机值较小,值越大优先级越高)
  10. advert_int 1 #VRRP Multicast广播周期秒数
  11. authentication {
  12. auth_type PASS #VRRP认证方式,主备必须一致
  13. auth_pass 1111 #(密码)
  14. }
  15. virtual_ipaddress {
  16. 192.168.18.90 #VRRP HA虚拟地址(可以为多个)
  17. }
  18. }


2.Slave服务器:

 #vim /usr/local/keepalived/etc/keepalived/keepalived.conf

注:可以把以前的配置的文件删掉或者重新建一个。把以下参数加进去复制进去。

 

  1. ! Configuration File for keepalived
  2. global_defs {
  3. router_id NodeB #节点B
  4. }
  5. vrrp_instance VI_1 {
  6. state BACKUP #设置为备服务器名
  7. interface eth0 #监测网络接口
  8. virtual_router_id 51 #主、备必须一样
  9. priority 90 #(主、备机取不同的优先级,主机值较大,备机值较小,值越大优先级越高)
  10. advert_int 1 #VRRP Multicast广播周期秒数
  11. authentication {
  12. auth_type PASS #VRRP认证方式,主备必须一致
  13. auth_pass 1111 #(密码)
  14. }
  15. virtual_ipaddress {
  16. 192.168.18.90 #VRRP HA虚拟地址(可以为多个)
  17. }
  18. }


注:为了保证服务可靠性,我们应该在每个节点服务器上运行shell脚本检测本机的服务是否正常,一旦检测到服务异常时,尝试开启服务;如果开启失败,则停止掉本机的keepalived, 如此虚拟IP自动转移到备用机器之上,如每隔10秒检测一次本机服务状态,如果连接3次检测失败,则停止掉keepalived实例。同时如果本机服务是正常的,但是keepalived没有启动(故障恢复之后),则启动keepalived,以达到故障恢复之目的。

脚本名为check_service.sh。放在每个节点服务器的开机脚本里

#vim /etc/rc.local

或者手动执行测试。


  1. #!/bin/sh
  2. #date: 09/17/2015
  3. maxfails=3 #最大失败数
  4. fails=0 #失败
  5. success=0 #成功
  6. while [ 1 ]
  7. do
  8. echo "尝试连接http"
  9. /usr/bin/wget --timeout=3 --tries=1 -q -O /dev/null #试连接本地http服务http服务
  10. if [ $? -ne 0 ] ; then
  11. service httpd status | grep stopped
  12. echo "http访问失败,正常尝试开启!"
  13. logger -is "http service fails... try to start httpd."
  14. service httpd start 2>&1 | logger
  15. let fails=$[$fails+1]
  16. echo "fails:"$fails
  17. success=0
  18. else
  19. echo "http连接成功!"
  20. sleep 2
  21. echo "尝试连接tomcat"
  22. /usr/bin/wget --timeout=3 --tries=1 -q -O /dev/null
  23. if [ $? -ne 0 ] ; then
  24. service tomcat status | grep stopped
  25. echo "tomcat访问失败,正在尝试开启!"
  26. logger -is "tomcat service fails... try to start tomcat"
  27. echo "loading "
  28. service tomcat start 2>&1 | logger
  29. let fails=$[$fails+1]
  30. echo "fails:"$fails
  31. success=0
  32. else
  33. echo "tomcat连接成功!"
  34. sleep 2
  35. echo "尝试连接mysql服务"
  36. /usr/bin/wget --timeout=3 --tries=1 -q -O /dev/null
  37. if [ $? -ne 0 ] ; then
  38. service mysqld status | grep stopped
  39. echo "mysql访问失败,正在尝试开启!"
  40. logger -is "http service fails...try to start mysql"
  41. service mysqld start 2>&1 | logger
  42. let fails=$[$fails+1]
  43. echo "fails:"$fails
  44. success=0
  45. else
  46. echo "mysql连接成功!"
  47. fails=0
  48. let success=$[$success+1]
  49. echo "success:"$success
  50. fi
  51. fi
  52. fi
  53. if [ $fails -ge $maxfails ] ; then
  54. echo "失败大于等于3"
  55. fails=0
  56. success=0
  57. echo "查看keepalived状态"
  58. service keepalived status | grep running
  59. echo "httpd,tomcat,mysql当中有服务有故障,尝试关闭 keepalived服务 "
  60. if [ $? -eq 0 ] ; then
  61. logger -is "local service fails $maxfails times ... try to stop keepalived."
  62. service keepalived stop 2>&1 | logger
  63. echo "恭喜你,keepalived服务关闭成功!"
  64. fi
  65. fi
  66. if [ $success -gt $maxfails ] ; then
  67. service keepalived status | grep stopped
  68. echo "所有服务正常,尝试开启keepalived服务"
  69. if [ $? -eq 0 ] ; then
  70. logger -is "service changes normal, try to start keepalived ."
  71. service keepalived start 2>&1 | logger
  72. echo "keepalived服务已运行!"
  73. fi
  74. success=0
  75. fi
  76. sleep 25
  77. done
阅读(3636) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~