yxixi 发表于 2018-12-31 09:02:49

LVS负载均衡(5)——LVS+Keepalived


Keepalived介绍
1. Keepalived 介绍
Keepalived 起初是转为 LVS 设计的专门用来监控集群系统中各个服务节点的状态,后来又加入了 VRRP 的功能,VRRP 是 Virtual Router Redundancy Protocol(虚拟路由器冗余协议)的缩写,VRRP 出现的目的是为了解决静态路由出现的单点故障问题,它能保证网络的不间断、稳定的运行,所以 Keepalived 一方面具有 LVS cluster nodes healthchecks 功能,又具有 LVS directors failover 功能
一般安装 Keepalived 是在高可用服务器对之间安装,这个服务器不一定是调度器,也可以是 web 服务器,数据库服务器,存储服务器等。
2. Keepalived 故障切换转移原理
Keepalived Directors 之间的故障切换转移是通过 VRRP 协议来实现的。
在 Keepalived Directors 正常工作时,主 Director 节点会不断的向备节点广播心跳消息,用以告诉节点自己还活着,当主节点发生故障时,备节点就无法继续检测到主节点的心跳,进而调用自身的接管程序,接管主节点的 IP 资源及服务。当主节点恢复故障时,备节点会释放主节点故障时自身接管的 IP 资源及服务,恢复到原来的自身的备用角色。

Keepalived配置
Keepalived 官方站点:http://www.keepalived.org,
官方文档是:      http://www.keepalived.org/documentation.html
1. 安装 Keepalived
# 下载软件并安装
wget http://www.keepalived.org/software/keepalived-1.1.17.tar.gz
ll /usr/usr/linux            # 检查 link,如果没有可能 kernel-devel 包没装
tar zxf keepalived-1.1.17.tar.gz
cd keepalived-1.1.17 && ./configure
make && make install
# 配置规范启动
cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/         # 启动脚本命令
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/      # 配置启动脚本的参数
mkdir /etc/keepalived                              # 创建默认的 keepalived 配置文件路径
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived    # 拷贝配置模板
cp /usr/local/sbin/keepalived /usr/sbin
/etc/init.d/keepalived start
ps -ef | grep keepalived
/etc/init.d/keepalived stop      # 成功后,停止服务,因为是根据默认配置文件启动

2. Keepalived 配置文件
文章:http://18567.blog.运维网.com/8567/655043
  ! Configuration File for keepalived
  global_defs {
     notification_email {
         1160636144@qq.com
     }
     notification_email_from aa@qq
     smtp_server 192.168.100.100
     smtp_connect_timeout 30
     router_id LVS_181
  }
  

  vrrp_instance VI_1 {
      state MASTER
      interface eth1
      virtual_router_id 100
      priority 150
      advert_int 1
      authentication {
        auth_type PASS
        auth_pass 1111
      }
      virtual_ipaddress {
        192.168.100.100
      }
  }
  

  vrrp_instance VI_2 {
      state BACKUP
      interface eth1
      virtual_router_id 200
      priority 100
      advert_int 1
      authentication {
        auth_type PASS
        auth_pass 1111
      }
      virtual_ipaddress {
        192.168.100.200
      }
  }
  ! Configuration File for keepalived

  global_defs {
     notification_email {
         1160636144@qq.com
     }
     notification_email_from aa@qq
     smtp_server 192.168.100.100
     smtp_connect_timeout 30
     router_id LVS_182
  }
  

  vrrp_instance VI_1 {
      state BACKUP
      interface eth1
      virtual_router_id 100
      priority 100
      advert_int 1
      authentication {
        auth_type PASS
        auth_pass 1111
      }
      virtual_ipaddress {
        192.168.100.100
      }
  }
  

  vrrp_instance VI_2 {
      state MASTER
      interface eth1
      virtual_router_id 200
      priority 150
      advert_int 1
      authentication {
        auth_type PASS
        auth_pass 1111
      }
      virtual_ipaddress {
        192.168.100.200
      }
  }
2. LVS DR + Keepalived

  ! Configuration File for keepalived
  

  global_defs {
     notification_email {
         1160636144@qq.com
     }
     notification_email_from aa@qq
     smtp_server 192.168.100.100
     smtp_connect_timeout 30
     router_id LVS_181
  }
  

  vrrp_instance VI_1 {
      state MASTER
      interface eth1
      virtual_router_id 100
      priority 150
      advert_int 1
      authentication {
        auth_type PASS
        auth_pass 1111
      }
      virtual_ipaddress {
        192.168.100.100/24
      }
  }
  

  virtual_server 192.168.100.100 80 {
     delay_loop 6
     lb_algo wrr            
     lb_kind DR            
     nat_mask 255.255.255.0
     persistence_timeout 50   
     protocol TCP            
     real_server 192.168.100.183 80{
         weight 1
         TCP_CHECK {
            connect_timeout 8
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
         }
     }
  

     real_server 192.168.100.184 80{
         weight 1
         TCP_CHECK {
            connect_timeout 8
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
         }
     }
  }
  ! Configuration File for keepalived
  

  global_defs {
     notification_email {
         1160636144@qq.com
     }
     notification_email_from aa@qq
     smtp_server 192.168.100.100
     smtp_connect_timeout 30
     router_id LVS_182
  }
  

  vrrp_instance VI_1 {
      state BACKUP
      interface eth1
      virtual_router_id 100
      priority 100
      advert_int 1
      authentication {
        auth_type PASS
        auth_pass 1111
      }
      virtual_ipaddress {
        192.168.100.100/24
      }
  }
  

  virtual_server 192.168.100.100 80 {
      delay_loop 6
      lb_algo wrr
      lb_kind DR
      nat_mask 255.255.255.0
      persistence_timeout 50
      protocol TCP
      real_server 192.168.100.183 80 {
        weight 1
        TCP_CHECK {
               connect_timeout 8
               nb_get_retry 3
               delay_before_retry 3
               connect_port 80
        }
      }
  

      real_server 192.168.100.184 80 {
        weight 1
        TCP_CHECK {
               connect_timeout 8
               nb_get_retry 3
               delay_before_retry 3
               connect_port 80
        }
      }
  }
名称  接口
  IP
  用途
  MASTER
  eth0
  10.0.0.19
用于 WLAN 数据转发
  eth1
  192.168.1.19
用于 LAN 数据转发
  eth2
  192.168.2.19
用于 LB 间心跳连接
  vip
  10.0.0.29
提供服务  BACKUP
  eth0
  10.0.0.16
用于 WLAN 数据转发
  eth1
  192.168.1.16
  用于 LAN 数据转发


  eth2
  192.168.2.16
  用于 LB 间心跳连接


  vip
  10.0.0.29

  RealServer01
  eth0
  10.0.0.17
用于 WLAN 数据转发
  eth1
  192.168.1.17
用于 LAN 数据转发  BACKUP
  eth0
  10.0.0.18
用于 WLAN 数据转发
  eth1
  192.168.1.18
用于 LAN 数据转发


页: [1]
查看完整版本: LVS负载均衡(5)——LVS+Keepalived