shenzhang 发表于 2018-12-28 13:46:28

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)两台服务器时间必须一至
  # date                     --节点2的时间
Thu Nov 13 15:11:09 CST 2014
#
  # date                     --节点1的时间
Thu Nov 13 15:11:13 CST 2014
#
  
  (2)配置本地网络
  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
  # vim /etc/hosts               --两台服务器相互解析
  192.168.1.100 node1
192.168.1.102 node2
  #
  
  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
  # vim /etc/hosts
  192.168.1.100 node1
192.168.1.102 node2
  #
  
  3.安装所需要的软件包,配置httpd服务
  node1节点:
  # yum install httpd keepalived ipvsadm -y          --安装所需的软件
  # vim /etc/httpd/conf/httpd.conf
  ServerName 127.0.0.1
  # echo "node1" > /var/www/html/index.html         --定义web首页
  # /etc/init.d/httpd restart
Stopping httpd:                                          
Starting httpd:                                          
  # chkconfig httpd on                  --重启web服务
  #
  http://s3.运维网.com/wyfs02/M02/52/66/wKioL1RkXUnAvu3yAADW1E1HN_w092.jpg
  
  node2节点:
  # yum install httpd keepalived ipvsadm -y
  # vim /etc/httpd/conf/httpd.conf
  ServerName 127.0.0.1
  # echo "node2" > /var/www/html/index.html
  # /etc/init.d/httpd restart
Stopping httpd:                                          
Starting httpd:                                          
  # chkconfig httpd on
  http://s3.运维网.com/wyfs02/M00/52/67/wKioL1RkXciwUB7bAADsqqM5rKI229.jpg
  
  4.配置keepalived集群服务
  node1节点:
  # echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
# echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
# echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
  # echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
  # ifconfig eth0:1 192.168.1.105 broadcast 192.168.1.105 netmask 255.255.255.255 up
# route add -host 192.168.1.105 dev eth0:1
  # ifconfig
eth0      Link encap:EthernetHWaddr 08:00:27:EE:3D:F6
          inet addr:192.168.1.100Bcast:192.168.1.255Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:feee:3df6/64 Scope:Link
          UP BROADCAST RUNNING MULTICASTMTU:1500Metric: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:EthernetHWaddr 08:00:27:EE:3D:F6
          inet addr:192.168.1.105Bcast:192.168.1.105Mask:255.255.255.255
          UP BROADCAST RUNNING MULTICASTMTU:1500Metric:1
  lo      Link encap:Local Loopback
          inet addr:127.0.0.1Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNINGMTU:16436Metric: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)
  # 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
  # cd /etc/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
         }
   }
  }
#
  node2节点:
  # echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
# echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
# echo "2" > /proc/sys/net/ipv4/conf/all/arp_ignore
# echo "2" > /proc/sys/net/ipv4/conf/lo/arp_ignore
  # ifconfig eth0:1 192.168.1.105 broadcast 192.168.1.105 netmask 255.255.255.255 up
# route add -host 192.168.1.105 dev eth0:1
  # ifconfig
eth0      Link encap:EthernetHWaddr 08:00:27:A5:94:4C
          inet addr:192.168.1.102Bcast:192.168.1.255Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fea5:944c/64 Scope:Link
          UP BROADCAST RUNNING MULTICASTMTU:1500Metric: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:EthernetHWaddr 08:00:27:A5:94:4C
          inet addr:192.168.1.105Bcast:192.168.1.105Mask:255.255.255.255
          UP BROADCAST RUNNING MULTICASTMTU:1500Metric:1
  lo      Link encap:Local Loopback
          inet addr:127.0.0.1Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNINGMTU:16436Metric: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)
  # cd /etc/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
         }
   }
  }
#
  
  5.启动keepalived服务和ipvsadm的状态
  node1和node2启动服务:
  # /etc/init.d/keepalived restart
Stopping keepalived:                                       
Starting keepalived:                                       
#
  # /etc/init.d/keepalived restart
Stopping keepalived:                                       
Starting keepalived:                                       
#
  # ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port         Forward Weight ActiveConn InActConn
TCP192.168.1.105:80 rr
-> 192.168.1.100:80             Local   1      0          0         
-> 192.168.1.102:80             Route   1      0          0         
#
  
6.测试是否正常
http://s3.运维网.com/wyfs02/M00/52/6E/wKiom1RkZazwSJdDAAD-oi-4V3M802.jpg
http://s3.运维网.com/wyfs02/M00/52/6F/wKiom1RkaCOjc0NIAADqCqSHFWk784.jpg
  
  
  



页: [1]
查看完整版本: Linux系统高可用集群软件之Keepalived