Linux系统高可用集群软件之Keepalived-阿里云开发者社区

开发者社区> 开发与运维> 正文

Linux系统高可用集群软件之Keepalived

简介:

     Keepalived 集群软件是一个基于VRRP协议来实现的LVS(四层协议)服务高可用方案,可以利用避免单节点故障.LVS服务需要有2台服务器运行Keepalived服务,一台为主服务器(MASTER),一台为备份服务器(BACKUP),但是对外只有一个虚拟IP,主服务器会发送特定的消息给备份服务器,当备份服务器收不到这个消息的时候,备份服务器认为主服务器宕机并会接管虚拟IP提供服务,从而保证了服务的高可用性.

 

1.环境说明

系统:Centos 6.5 64位

软件:Keepalived ipvsadm

服务:apache

网络:

node1:192.168.1.100 

node2:192.168.1.102  

vip:192.168.1.105

 

2.配置本地时间和网络

(1)两台服务器时间必须一至

[root@node2 ~]# date                       --节点2的时间
Thu Nov 13 15:11:09 CST 2014
[root@node2 ~]#

[root@node1 ~]# date                       --节点1的时间
Thu Nov 13 15:11:13 CST 2014
[root@node1 ~]#

 

(2)配置本地网络

node1节点:

[root@node1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0
HWADDR=08:00:27:EE:3D:F6
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1

[root@node1 ~]# vim /etc/hosts                 --两台服务器相互解析

192.168.1.100 node1
192.168.1.102 node2

[root@node1 ~]#

 

node2节点:

[root@node2 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0
HWADDR=08:00:27:A5:94:4C
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.1.102
NETMASK=255.255.255.0
GATEWAY=192.168.1.1

[root@node2 ~]# vim /etc/hosts

192.168.1.100 node1
192.168.1.102 node2

[root@node2 ~]#

 

3.安装所需要的软件包,配置httpd服务

node1节点:

[root@node1 ~]# yum install httpd keepalived ipvsadm -y          --安装所需的软件

[root@node1 ~]# vim /etc/httpd/conf/httpd.conf

ServerName 127.0.0.1

[root@node1 ~]# echo "node1" > /var/www/html/index.html           --定义web首页

[root@node1 ~]# /etc/init.d/httpd restart
Stopping httpd:                                            [FAILED]
Starting httpd:                                            [  OK  ]

[root@node1 ~]# chkconfig httpd on                    --重启web服务

[root@node1 ~]#

wKioL1RkXUnAvu3yAADW1E1HN_w092.jpg

 

node2节点:

[root@node2 ~]# yum install httpd keepalived ipvsadm -y

[root@node2 ~]# vim /etc/httpd/conf/httpd.conf

ServerName 127.0.0.1

[root@node2 ~]# echo "node2" > /var/www/html/index.html

[root@node2 ~]# /etc/init.d/httpd restart
Stopping httpd:                                            [FAILED]
Starting httpd:                                            [  OK  ]

[root@node2 ~]# chkconfig httpd on

wKioL1RkXciwUB7bAADsqqM5rKI229.jpg

 

4.配置keepalived集群服务

node1节点:

[root@node1 ~]# echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore 
[root@node1 ~]# echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore 
[root@node1 ~]# echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce

[root@node1 ~]# echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce

[root@node1 ~]# ifconfig eth0:1 192.168.1.105 broadcast 192.168.1.105 netmask 255.255.255.255 up
[root@node1 ~]# route add -host 192.168.1.105 dev eth0:1

[root@node1 ~]# ifconfig 
eth0      Link encap:Ethernet  HWaddr 08:00:27:EE:3D:F6  
          inet addr:192.168.1.100  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:feee:3df6/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1600615 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1574022 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:106099219 (101.1 MiB)  TX bytes:102403193 (97.6 MiB)

eth0:1    Link encap:Ethernet  HWaddr 08:00:27:EE:3D:F6  
          inet addr:192.168.1.105  Bcast:192.168.1.105  Mask:255.255.255.255
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:5087 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5087 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:403339 (393.8 KiB)  TX bytes:403339 (393.8 KiB)

[root@node1 ~]# route  -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.105   0.0.0.0         255.255.255.255 UH    0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0

[root@node1 ~]# cd /etc/keepalived/
[root@node1 keepalived]# vim keepalived.conf          --配置keepalived文件


! Configuration File for keepalived

global_defs {
   notification_email {
     
z597011036@qq.com            --宕机后邮件报警
   }
   notification_email_from root       --从哪个用户发出
   smtp_server localhost                --邮件服务器
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER                   --主节点
    interface eth1                     --监听的网卡
    virtual_router_id 51            --虚拟路由ID号
    priority 100                         --配置优先级
    advert_int 1
    authentication {
        auth_type PASS         --认证类型
        auth_pass keep          --认证密码
    }
    virtual_ipaddress {

        192.168.1.105           --虚拟IP地址
    }
}

virtual_server 192.168.1.105 80 {        --虚拟IP和端口设置
    delay_loop 6
    lb_algo rr        
    lb_kind DR
    nat_mask 255.255.255.0
    protocol TCP

    real_server 192.168.1.100 80 {        --主服务器的IP和端口
        weight 1                   --权重
        HTTP_GET {           --启用HTTP服务
            url {
              path /
              status_code 200          --监控状态码为200,如果返回不是200表示宕机
            }
            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3
         }
    }
    real_server 192.168.1.102 80 {              --从服务器的IP和端口
         weight 1                        --权重
         HTTP_GET {
             url {
               path /
               status_code 200
             }
             connect_timeout 3
             nb_get_retry 3
             delay_before_retry 3
         }
     }

}
[root@node2 keepalived]#  

 

node2节点:

[root@node2 ~]# echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore 
[root@node2 ~]# echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore 
[root@node2 ~]# echo "2" > /proc/sys/net/ipv4/conf/all/arp_ignore 
[root@node2 ~]# echo "2" > /proc/sys/net/ipv4/conf/lo/arp_ignore

[root@node2 ~]# ifconfig eth0:1 192.168.1.105 broadcast 192.168.1.105 netmask 255.255.255.255 up
[root@node2 ~]# route add -host 192.168.1.105 dev eth0:1

[root@node2 ~]# ifconfig 
eth0      Link encap:Ethernet  HWaddr 08:00:27:A5:94:4C  
          inet addr:192.168.1.102  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fea5:944c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1601817 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1574326 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:104324952 (99.4 MiB)  TX bytes:104338539 (99.5 MiB)

eth0:1    Link encap:Ethernet  HWaddr 08:00:27:A5:94:4C  
          inet addr:192.168.1.105  Bcast:192.168.1.105  Mask:255.255.255.255
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:6824 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6824 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:2407997 (2.2 MiB)  TX bytes:2407997 (2.2 MiB)

[root@node2 ~]# cd /etc/keepalived/
[root@node2 keepalived]# vim keepalived.conf


! Configuration File for keepalived

global_defs {
   notification_email {
     
z597011036@qq.com
   }
   notification_email_from root
   smtp_server localhost
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BACKUP                --备用节点
    interface eth1
    virtual_router_id 51
    priority 101                  
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass keep
    }
    virtual_ipaddress {

        192.168.1.105
    }
}

virtual_server 192.168.1.105 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.255.0
    protocol TCP

    real_server 192.168.1.100 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3
         }
    }
   real_server 192.168.1.102 80 {
         weight 1
         HTTP_GET {
             url {
               path /
               status_code 200
             }
             connect_timeout 3
             nb_get_retry 3
             delay_before_retry 3
         }
     }

}
[root@node2 keepalived]#  

 

5.启动keepalived服务和ipvsadm的状态

node1和node2启动服务:

[root@node1 keepalived]# /etc/init.d/keepalived restart
Stopping keepalived:                                       [  OK  ]
Starting keepalived:                                       [  OK  ]
[root@node1 keepalived]#

[root@node2 ~]# /etc/init.d/keepalived restart
Stopping keepalived:                                       [  OK  ]
Starting keepalived:                                       [  OK  ]
[root@node2 ~]#

[root@node1 keepalived]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.1.105:80 rr
  -> 192.168.1.100:80             Local   1      0          0         
  -> 192.168.1.102:80             Route   1      0          0         
[root@node1 keepalived]#


6.测试是否正常

wKiom1RkZazwSJdDAAD-oi-4V3M802.jpg

wKiom1RkaCOjc0NIAADqCqSHFWk784.jpg










本文转自 z597011036 51CTO博客,原文链接:http://blog.51cto.com/tongcheng/1576111,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章