你的位置:首页 > 操作系统

[操作系统]lvs + keepalived 介绍及安装


LVS介绍

      lvs 核心ipvs
      Ipvs(IP Virtual Server)是整个负载均衡的基础,如果没有这个基础,故障隔离与失败切换就毫无意义了。Ipvs 具体实现是由ipvsadm 这个程序来完成,因此判断一个系统是否具备ipvs 功能,只需要察看ipvsadm 程序是否被安装。察看ipvsadm 程序最简单的办法就是在任意路径执行命令ipvsadm。

1 [root@LVS-DR-Backup ~]# ipvsadm(已安装)2 IP Virtual Server version 1.2.1 (size=4096)3 Prot LocalAddress:Port Scheduler Flags4  -> RemoteAddress:Port      Forward Weight ActiveConn InActConn5 [root@LVS-DR-Backup ~]# ipvsadm (未安装)6 -bash: ipvsadm: command not found

       安装lvs

       1、下载ipvsadm,wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz

       2、 创建一个连接文件,其命令为: ln -s /usr/src/kernels/2.6.32-573.el6.x86_64/  /usr/src/linux注意一定要与当前的运行的内核相一致,因为/usr/src/kernels目录下可多个目录。如果不创建这个连接文件,在编译时会出错,从而不能继续进行安装。

       3、 解包。 tar zxf ipvsadm-1.26.tar.gz

       4、 编译并安装。 cd ipvsadm-1.26; make;make install

       5、检验ipvsadm 是否被正确安装:a.执行ipvsadm,看是否有如上的输出。b. 检查当前加载的内核模块,看是否存在ip_vs 模块。

1 [root@LVS-DR-Backup ~]# lsmod|grep ip_vs2 ip_vs         126534 0 3 libcrc32c        1246 1 ip_vs4 ipv6         335589 137 ip_vs (注:只有执行ipvsadm 以后,才会在内核加载ip_vs 模块,也不能以查进程的方式判断ipvs 是否运行。)

       lvs客户端

       不需要安装软件,只运行一个脚本就可以,脚本如下:

 1 #!/bin/bash 2  3 VIP=此处设置你的vip地址 4  5 . /etc/rc.d/init.d/functions 6  7 case "$1" in 8   start) 9     action " start LVS of REALServer" /bin/true10     /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up11     echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore12     echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce13     echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore14     echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce15     ;;16   stop)17     /sbin/ifconfig lo:0 down18     action "close LVS Directorserver" /bin/true19     echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore20     echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce21     echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore22     echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce23     ;;24  *)25     echo "Usage: $0 {start|stop}"26     exit 127 esac

keepalived介绍

      1、 keepalived 是lvs 的扩展项目,因此它们之间具备良好的兼容性。

      2、 通过对服务器池对象的健康检查,实现对失效机器/服务的故障隔离。

      3、 负载均衡器之间的失败切换failover,是通过VRRPv2(Virtual Router Redundancy Protocol)stack 实现的。

keepalived内部结构图

      在这个结构图里,处于下端的是内核空间,它包括ipvs 和NETLINK 两个部分。Ipvs 的作用在前面的章节已经做过描述,不再重复叙述;netlink 提供高级路由及其他相关的网络功能,如果我们在负载均衡器上启用netfilter/iptable,将会直接影响它的性能。出于图形上方的组件为用户空间,由它来实现具体的功能,下面选取几个重要的来做说明:

     1、 WatchDog 负责监控checkers 和VRRP 进程的状况。

     2、 Checkers 负责真实服务器的健康检查healthchecking,是keepalived 最主要的功能。换句话说—可以没有VRRP Stack,但健康检查healthchecking 是一定要有的。

     3、 VRRP Stack 负责负载均衡器之间的失败切换FailOver.如果只用一个负载均衡器,则VRRP 不是必须的。

     4、 IPVS wrapper 用来发送设定的规则到内核ipvs 代码。

     5、 Netlink Reflector 用来设定 vrrp 的vip 地址等。

     安装keepalived(因为比较简单,不做详细介绍)

     1、 下载最新稳定版 http://www.keepalived.org/software/keepalived-1.2.23.tar.gz

     2、 解包 tar zxf keepalived-1.2.23.tar.gz

     3、 切换目录 cd keepalived-1.2.23

     4、 安装依赖包 yum install -y libnfnetlink-devel openssl-devel

     5、 配置并编译安装./configure ; make ; make install

     6、 拷贝相应文件到/etc下,命令如下:

1 cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/2 cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/3 cp /usr/local/sbin/keepalived /usr/sbin/4 mkdir /etc/keepalived -p5 cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

      7、启动并查看keepalived

 1 [root@LVS-DR-Backup script]# /etc/init.d/keepalived start 2 正在启动 keepalived:                   [确定] 3 [root@LVS-DR-Backup script]# ipvsadm 4 IP Virtual Server version 1.2.1 (size=4096) 5 Prot LocalAddress:Port Scheduler Flags 6  -> RemoteAddress:Port      Forward Weight ActiveConn InActConn 7 TCP 192.168.200.100:https rr persistent 50 8  -> 192.168.201.100:https    Masq  1   0     0      9 TCP 10.10.10.2:connlcli rr persistent 5010  -> 192.168.200.2:connlcli    Masq  1   0     0     11  -> 192.168.200.3:connlcli    Masq  1   0     0     12 TCP 10.10.10.3:connlcli rr persistent 5013  -> 192.168.200.4:connlcli    Masq  1   0     0     14  -> 192.168.200.5:connlcli    Masq  1   0     0     15 [root@LVS-DR-Backup script]# 

      8、至此安装完成,下一章,keepalived.conf详解。

      参考:http://www.keepalived.org/documentation.html

附:lvs和keepalived一键安装脚本

 1 #!/bin/bash 2 # Filename :Install_lvs.sh 3 # Description : 安装lvs+keepalived软件脚本 4  5 #参数设置 6 Toolsdir=/home/test/tools 7 IPVSADM=ipvsadm-1.26 8 Kernel=uname -r 9 KEEPALIVED=keepalived-1.2.2310 11 12 #定义安装ipvsadm函数13 function IPvsadm(){14 cd ${Toolsdir}15 #下载ipvsadm软件16 [ ! -f ${IPVSADM}.tar.gz ] && wget http://www.linuxvirtualserver.org/software/kernel-2.6/${IPVSADM}.tar.gz17 #安装依赖包18 yum install -y popt libnl-devel popt-static19 20 #解压并安装ipvsadm21 tar zxf ${IPVSADM}.tar.gz22 if [ $? == 0 ];then23   cd ${IPVSADM}24   ln -s /usr/src/kernel/${Kernel}/ /usr/src/linux25   make26   if [ $? == 0 ];then27     make install28     if [ $? == 0 ];then 29        echo "IPVSADM INSTALL SUCCESSFUL."30     else31        echo "make install error."32        eixt 033        sleep 534     fi35   else36     echo "make error."37     exit 138   fi39 else40   echo "tar error."  41   exit 242 fi43 }44 45 #定义安装keepalived函数46 47 function Keepalived(){48 #下载软件49 cd ${Toolsdir}50 [ ! -f ${KEEPALIVED}.tar.gz ] && wget http://www.keepalived.org/software/${KEEPALIVED}.tar.gz51 52 #安装依赖包53 yum install -y libnfnetlink-devel openssl-devel54 55 #安装keepalived56 tar zxf ${KEEPALIVED}.tar.gz57 if [ $? == 0 ];then58   cd ${KEEPALIVED}59   ./configure 60   if [ $? == 0 ];then61     make62     if [ $? == 0 ];then63       make install64       if [ $? == 0 ];then65         echo "Keepalived install successful."66       else67         echo "make install error"68       fi69     else70       echo "make error."71     fi72   else73     echo "configure error."74   fi75 else76   echo "tar error"77 fi78 79 #拷贝配置文件80 cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/81 cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/82 cp /usr/local/sbin/keepalived /usr/sbin/83 mkdir /etc/keepalived -p84 cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/85 86 #开启内核转发87 sed -i 's/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/g' sysctl.conf88 sysctl -p89 }90 91 IPvsadm92 Keepalived

View Code