SlideShare a Scribd company logo
利用 LVS+Keepalived 实现高性能高可用负载均衡
         ByPat博客专注于linux资源分享            http://blog.bypat.com
                       作者:Redhat

                日期:20 -1-27
  背景:
    随着你的网站业务量的增长你网站的服务器压力越来越大?需要负载均衡方案!        商
业的硬件如 F5 又太贵,你们又是创业型互联公司如何有效节约成本,节省不必要的浪费?
同时实现商业硬件一样的高性能高可用的功能?有什么好的负载均衡可伸张可扩展的方案
吗?答案是肯定的!有!我们利用 LVS+Keepalived 基于完整开源软件的架构可以为你提供
一个负载均衡及高可用的服务器。

一. LVS+Keepalived 介绍
  1. LVS
      LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器
      集群系统。本项目在 1998 年 5 月由章文嵩博士成立,是中国国内最早出现的自由
      软件项目之一。目前有三种IP负载均衡技术(VS/NAT、VS/TUN和VS/DR)      ;
      八种调度算法(rr,wrr,lc,wlc,lblc,lblcr,dh,sh)。

   2. Keepalvied
      Keepali ved 在这里主要用作 RealServer 的健康状态检查以及 LoadBalance 主机和
      BackUP 主机之间 failover 的实现
二. 网站负载均衡拓朴图

.




     IP 信息列表:
    名称                             IP
    L VS-DR-Master                 61.164.122.6
    L VS-DR-BACKUP                 61.164.122.7
    L VS-DR-VIP                    61.164.122.8
    WEB1-R ealserver               61.164.122.9
    WEB2-R ealserver               61.164.122.10
    G ateWay                       61.164.122.1
ByPat博客专注于linux资源分享                                http://blog.bypat.com




三. 安装 LVS 和 Keepalvied 软件包
  1. 下载相关软件包
    #m kdir /usr/local/src/lvs
    #c d /usr/local/src/lvs
    #w get http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
    #w get http://www.keepalived.org/software/keepalived-1.1.15.tar.gz
  2. 安装 LVS 和 Keepalived
    #lsm od |grep ip_vs
     #uname -r
      2.6.18-53.el5PAE
     #ln -s /usr/src/kernels/2.6.18-53.el5PAE-i686/ /usr/src/linux

      #tar zxvf ipvsadm-1.24.tar.gz
      #cd ipvsadm-1.24
      #make && make install
      #find / -name ipvsadm # 查看 ipvsadm 的位置

      #tar zxvf keepalived-1.1.15.tar.gz
      #cd keepalived-1.1.15
      #./configure && make && make install
      #find / -name keepalived # 查看 keepalived 位置

      #cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
      #cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
      #mkdir /etc/keepalived
      #cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
      #cp /usr/local/sbin/keepalived /usr/sbin/
      #service keepalived start|stop       # 做成系统启动服务方便管理.



四. 配置 LVS 实现负载均衡
 1. LV S-DR,配置 LVS 脚本实现负载均衡
     #vi /usr/local/sbin/lvs-dr.sh
     #!/bin/bash
     # description: start LVS of DirectorServer



      GW=61.164.122.1
      # website director vip.
      SNS_VIP=61.164.122.8
      SNS_RIP1=61.164.122.9
      SNS_RIP2=61.164.122.10
ByPat博客专注于linux资源分享                                     http://blog.bypat.com



     ./e tc/rc.d/init.d/functions

     logger $0 called with $1

     case "$1" in

     start)
             # set squid vip
            / sbin/ipvsadm --set 30 5 60
            / sbin/ifconfig eth0:0 $SNS_VIP broadcast $SNS_VIP netmask 255.255.255.255
broadcast $SNS_VIP up
            / sbin/route add -host $SNS_VIP dev eth0:0
            / sbin/ipvsadm -A -t $SNS_VIP:80 -s wrr -p 3
            / sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP1:80 -g -w 1
            / sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP2:80 -g -w 1
            t ouch /var/lock/subsys/ipvsadm >/dev/null 2>&1

        ;;
     stop)
                /sbin/ipvsadm -C
                /sbin/ipvsadm -Z
               i fconfig eth0:0 down
               i fconfig eth0:1 down
               rout e del $SNS_VIP
                route del $SS_VIP
               rm -rf /var/lock/subsys/ipvsadm >/dev/null 2>&1
              ech o "ipvsadm stoped"
                ;;

     status)

               i f [ ! -e /var/lock/subsys/ipvsadm ];then
                        echo "ipvsadm stoped"
                        exit 1
                else
                        echo "ipvsadm OK"
                fi
                ;;

     *)
              echo "Usage: $0 {start|stop|status}"
               exit 1
     esac
ByPat博客专注于linux资源分享                                   http://blog.bypat.com



   exit 0

2. 配置 Realserver 脚本.
   #vi /usr/local/sbin/realserver.sh
   #!/bin/bash
   # description: Config realserver lo and apply noarp



    SNS_VIP=61.164.122.8

    . /etc/rc.d/init.d/functions

    case "$1" in
    start)
           ifco nfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
            /sbin/route add -host $SNS_VIP dev lo:0
           ec ho "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
           ec ho "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
           ec ho "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
           ec ho "2" >/proc/sys/net/ipv4/conf/all/arp_announce
           sy sctl -p >/dev/null 2>&1
           ec ho "RealServer Start OK"

            ;;
   stop)
            i fconfig lo:0 down
            rou te del $SNS_VIP >/dev/null 2>&1
            ec ho "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
            ec ho "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
            ec ho "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
            ec ho "0" >/proc/sys/net/ipv4/conf/all/arp_announce
            ec ho "RealServer Stoped"
             ;;
   *)
            ec ho "Usage: $0 {start|stop}"
             exit 1
  esac

   exit 0
或者采用 secondary ip address 方式配置
# vi /etc/sysctl.conf
  net.ipv4.conf.lo.arp_ignore = 1
  net.ipv4.conf.lo.arp_announce = 2
ByPat博客专注于linux资源分享                        http://blog.bypat.com


    net.ipv4.conf.all.arp_ignore = 1
    net.ipv4.conf.all.arp_announce = 2
  #sysctl –p
  #ip addr add 61.164.122.8/32 dev lo
  #ip add list 查看是否绑定
  3. 启动 lvs-dr 脚本和 realserver 启本,在 DR 上可以查看 LVS 当前状态:
     #watch ipvsadm –ln

五.利用 Keepalvied 实现负载均衡和和高可用性
 1.配置在主负载均衡服务器上配置 keepalived.conf
 #vi /etc/keepalived/keepalived.conf
 ! Configuration File for keepalived

global_defs {
   n otification_email {
        cnseek@gmail.com
  # fai lover@firewall.loc
  # sy sadmin@firewall.loc
    }
   n otification_email_from sns-lvs@gmail.com
   sm tp_server 127.0.0.1
  # smtp_connect_timeout 30
   ro uter_id LVS_DEVEL
}



# VIP1
vrrp_instance VI_1 {
     state MASTER            #备份服务器上将 MASTER 改为 BACKUP
    i nterface eth0
     virtual_router_id 51
     priority 100     # 备份服务上将 100 改为 99
    ad vert_int 1
    au thentication {
           auth_type PASS
         a uth_pass 1111
     }
     virtual_ipaddress {
          61. 164.122.8
           #(如果有多个 VIP,继续换行填写.)
     }
}

virtual_server 61.164.122.8 80 {
ByPat博客专注于linux资源分享                              http://blog.bypat.com


    delay_loop 6                     #(每隔 10 秒查询 realserver 状态)
    lb_algo wrr                     #(lvs 算法)
    lb_kind DR                      #(Direct Route)
    persistence_timeout 60         #(同一 IP 的连接 60 秒内被分配到同一台 realserver)
    protocol TCP                   #(用 TCP 协议检查 realserver 状态)

    rea l_server 61.164.122.9 80 {
          weight 3                  #(权重)
          TCP_CHECK {
          connect_timeout 10         #(10 秒无响应超时)
          nb_get_retry 3
          delay_before_retry 3
          connect_port 80
          }
     }
    rea l_server 61.164.122.10 80 {
          weight 3
          TCP_CHECK {
         c onnect_timeout 10
          nb_get_retry 3
          delay_before_retry 3
          connect_port 80
          }
       }
}

2. BACKUP        服 务 器 同 上 配 置 , 先 安 装 lvs 再 按 装 keepalived, 仍 后 配 置
    /etc/keepalived/keepalived.conf,只需将红色标示的部分改一下即可.

3. vi /etc/rc.local
   # /usr/local/sbin/lvs-dr.sh 将 lvs-dr.sh 这个脚本注释掉。步
   # /usr/local/sbin/lvs-dr.sh stop 停止 lvs-dr 脚本
   #/ etc/init.d/keepalived start 启动 keepalived 服务,keepalived 就能利用 keepalived.conf 配
    置文件,实现负载均衡和高可用.

4. 查看 lvs 服务是否正常
   #watch ipvsadm –ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
 -> RemoteAddress:Port                 F orward Weight ActiveConn InActConn
TCP 61.164.122.8:80 wrr persistent 60
   -> 61.164.122.10:80                 Route 3       0           0
   -> 61.164.122.9:80                  Route 3       0           0
ByPat博客专注于linux资源分享                            http://blog.bypat.com


#t ail –f /var/log/message 监听日志,查看状态。

5.停 Master 服务器的 keepalived 服务,查看 BAKCUP 服务器是否能正常接管.



四.相关参考


    Keepalived 相关参考资料。


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

More Related Content

What's hot

nouka inventry manager
nouka inventry managernouka inventry manager
nouka inventry manager
Toshiaki Baba
 
도커 없이 컨테이너 만들기 4편 네트워크네임스페이스 (2)
도커 없이 컨테이너 만들기 4편 네트워크네임스페이스 (2)도커 없이 컨테이너 만들기 4편 네트워크네임스페이스 (2)
도커 없이 컨테이너 만들기 4편 네트워크네임스페이스 (2)
Sam Kim
 
Ubuntu安装SVN总结
Ubuntu安装SVN总结Ubuntu安装SVN总结
Ubuntu安装SVN总结wensheng wei
 
Make container without_docker_7
Make container without_docker_7Make container without_docker_7
Make container without_docker_7
Sam Kim
 
Cacti安装手册
Cacti安装手册Cacti安装手册
Cacti安装手册Yiwei Ma
 
Squid Server
Squid ServerSquid Server
Squid Server
Sumant Garg
 
Samba 4 - debian instalacao
Samba 4 - debian instalacaoSamba 4 - debian instalacao
Samba 4 - debian instalacao
Eduardo Charquero
 
[Hello world 오픈세미나]varnish로 웹서버성능 향상시키기
[Hello world 오픈세미나]varnish로 웹서버성능 향상시키기[Hello world 오픈세미나]varnish로 웹서버성능 향상시키기
[Hello world 오픈세미나]varnish로 웹서버성능 향상시키기NAVER D2
 
Ceph issue 해결 사례
Ceph issue 해결 사례Ceph issue 해결 사례
Ceph issue 해결 사례
Open Source Consulting
 
Puppet
PuppetPuppet
kubernetes practice
kubernetes practicekubernetes practice
kubernetes practice
wonyong hwang
 
Ex200
Ex200Ex200
CoreOSによるDockerコンテナのクラスタリング
CoreOSによるDockerコンテナのクラスタリングCoreOSによるDockerコンテナのクラスタリング
CoreOSによるDockerコンテナのクラスタリング
Yuji ODA
 
Docker container management
Docker container managementDocker container management
Docker container management
Karol Kreft
 
A little systemtap
A little systemtapA little systemtap
A little systemtap
yang bingwu
 
Squid proxy-configuration-guide
Squid proxy-configuration-guideSquid proxy-configuration-guide
Squid proxy-configuration-guidejasembo
 
Docker and friends at Linux Days 2014 in Prague
Docker and friends at Linux Days 2014 in PragueDocker and friends at Linux Days 2014 in Prague
Docker and friends at Linux Days 2014 in Prague
tomasbart
 
JDO 2019: Tips and Tricks from Docker Captain - Łukasz Lach
JDO 2019: Tips and Tricks from Docker Captain - Łukasz LachJDO 2019: Tips and Tricks from Docker Captain - Łukasz Lach
JDO 2019: Tips and Tricks from Docker Captain - Łukasz Lach
PROIDEA
 
Docker Setting for Static IP allocation
Docker Setting for Static IP allocationDocker Setting for Static IP allocation
Docker Setting for Static IP allocation
Ji-Woong Choi
 

What's hot (20)

nouka inventry manager
nouka inventry managernouka inventry manager
nouka inventry manager
 
도커 없이 컨테이너 만들기 4편 네트워크네임스페이스 (2)
도커 없이 컨테이너 만들기 4편 네트워크네임스페이스 (2)도커 없이 컨테이너 만들기 4편 네트워크네임스페이스 (2)
도커 없이 컨테이너 만들기 4편 네트워크네임스페이스 (2)
 
Ubuntu安装SVN总结
Ubuntu安装SVN总结Ubuntu安装SVN总结
Ubuntu安装SVN总结
 
Make container without_docker_7
Make container without_docker_7Make container without_docker_7
Make container without_docker_7
 
Dev ops
Dev opsDev ops
Dev ops
 
Cacti安装手册
Cacti安装手册Cacti安装手册
Cacti安装手册
 
Squid Server
Squid ServerSquid Server
Squid Server
 
Samba 4 - debian instalacao
Samba 4 - debian instalacaoSamba 4 - debian instalacao
Samba 4 - debian instalacao
 
[Hello world 오픈세미나]varnish로 웹서버성능 향상시키기
[Hello world 오픈세미나]varnish로 웹서버성능 향상시키기[Hello world 오픈세미나]varnish로 웹서버성능 향상시키기
[Hello world 오픈세미나]varnish로 웹서버성능 향상시키기
 
Ceph issue 해결 사례
Ceph issue 해결 사례Ceph issue 해결 사례
Ceph issue 해결 사례
 
Puppet
PuppetPuppet
Puppet
 
kubernetes practice
kubernetes practicekubernetes practice
kubernetes practice
 
Ex200
Ex200Ex200
Ex200
 
CoreOSによるDockerコンテナのクラスタリング
CoreOSによるDockerコンテナのクラスタリングCoreOSによるDockerコンテナのクラスタリング
CoreOSによるDockerコンテナのクラスタリング
 
Docker container management
Docker container managementDocker container management
Docker container management
 
A little systemtap
A little systemtapA little systemtap
A little systemtap
 
Squid proxy-configuration-guide
Squid proxy-configuration-guideSquid proxy-configuration-guide
Squid proxy-configuration-guide
 
Docker and friends at Linux Days 2014 in Prague
Docker and friends at Linux Days 2014 in PragueDocker and friends at Linux Days 2014 in Prague
Docker and friends at Linux Days 2014 in Prague
 
JDO 2019: Tips and Tricks from Docker Captain - Łukasz Lach
JDO 2019: Tips and Tricks from Docker Captain - Łukasz LachJDO 2019: Tips and Tricks from Docker Captain - Łukasz Lach
JDO 2019: Tips and Tricks from Docker Captain - Łukasz Lach
 
Docker Setting for Static IP allocation
Docker Setting for Static IP allocationDocker Setting for Static IP allocation
Docker Setting for Static IP allocation
 

Viewers also liked

Bypat博客出品-服务器运维集群方法总结2
Bypat博客出品-服务器运维集群方法总结2Bypat博客出品-服务器运维集群方法总结2
Bypat博客出品-服务器运维集群方法总结2
redhat9
 
Bypat博客出品-利用cent os快速构建自己的发行版
Bypat博客出品-利用cent os快速构建自己的发行版Bypat博客出品-利用cent os快速构建自己的发行版
Bypat博客出品-利用cent os快速构建自己的发行版redhat9
 
ByPat博客出品-高性能Web服务器nginx及相关新技术的应用
ByPat博客出品-高性能Web服务器nginx及相关新技术的应用ByPat博客出品-高性能Web服务器nginx及相关新技术的应用
ByPat博客出品-高性能Web服务器nginx及相关新技术的应用redhat9
 
高性能Web服务器nginx及相关新技术的应用
高性能Web服务器nginx及相关新技术的应用高性能Web服务器nginx及相关新技术的应用
高性能Web服务器nginx及相关新技术的应用redhat9
 
Bypat博客出品-利用cent os快速构建自己的发行版-进阶版v1.1
Bypat博客出品-利用cent os快速构建自己的发行版-进阶版v1.1Bypat博客出品-利用cent os快速构建自己的发行版-进阶版v1.1
Bypat博客出品-利用cent os快速构建自己的发行版-进阶版v1.1redhat9
 
高性能Web服务器nginx及相关新技术的应用
高性能Web服务器nginx及相关新技术的应用高性能Web服务器nginx及相关新技术的应用
高性能Web服务器nginx及相关新技术的应用redhat9
 
Bypat博客出品-服务器运维集群方法总结3
Bypat博客出品-服务器运维集群方法总结3Bypat博客出品-服务器运维集群方法总结3
Bypat博客出品-服务器运维集群方法总结3redhat9
 
Bypat博客出品-手把手教你如何建立自己的linux系统lfs速成手册
Bypat博客出品-手把手教你如何建立自己的linux系统lfs速成手册Bypat博客出品-手把手教你如何建立自己的linux系统lfs速成手册
Bypat博客出品-手把手教你如何建立自己的linux系统lfs速成手册redhat9
 
Bypat博客出品-linux+apache+mysql+php之linux系统安装
Bypat博客出品-linux+apache+mysql+php之linux系统安装Bypat博客出品-linux+apache+mysql+php之linux系统安装
Bypat博客出品-linux+apache+mysql+php之linux系统安装
redhat9
 

Viewers also liked (9)

Bypat博客出品-服务器运维集群方法总结2
Bypat博客出品-服务器运维集群方法总结2Bypat博客出品-服务器运维集群方法总结2
Bypat博客出品-服务器运维集群方法总结2
 
Bypat博客出品-利用cent os快速构建自己的发行版
Bypat博客出品-利用cent os快速构建自己的发行版Bypat博客出品-利用cent os快速构建自己的发行版
Bypat博客出品-利用cent os快速构建自己的发行版
 
ByPat博客出品-高性能Web服务器nginx及相关新技术的应用
ByPat博客出品-高性能Web服务器nginx及相关新技术的应用ByPat博客出品-高性能Web服务器nginx及相关新技术的应用
ByPat博客出品-高性能Web服务器nginx及相关新技术的应用
 
高性能Web服务器nginx及相关新技术的应用
高性能Web服务器nginx及相关新技术的应用高性能Web服务器nginx及相关新技术的应用
高性能Web服务器nginx及相关新技术的应用
 
Bypat博客出品-利用cent os快速构建自己的发行版-进阶版v1.1
Bypat博客出品-利用cent os快速构建自己的发行版-进阶版v1.1Bypat博客出品-利用cent os快速构建自己的发行版-进阶版v1.1
Bypat博客出品-利用cent os快速构建自己的发行版-进阶版v1.1
 
高性能Web服务器nginx及相关新技术的应用
高性能Web服务器nginx及相关新技术的应用高性能Web服务器nginx及相关新技术的应用
高性能Web服务器nginx及相关新技术的应用
 
Bypat博客出品-服务器运维集群方法总结3
Bypat博客出品-服务器运维集群方法总结3Bypat博客出品-服务器运维集群方法总结3
Bypat博客出品-服务器运维集群方法总结3
 
Bypat博客出品-手把手教你如何建立自己的linux系统lfs速成手册
Bypat博客出品-手把手教你如何建立自己的linux系统lfs速成手册Bypat博客出品-手把手教你如何建立自己的linux系统lfs速成手册
Bypat博客出品-手把手教你如何建立自己的linux系统lfs速成手册
 
Bypat博客出品-linux+apache+mysql+php之linux系统安装
Bypat博客出品-linux+apache+mysql+php之linux系统安装Bypat博客出品-linux+apache+mysql+php之linux系统安装
Bypat博客出品-linux+apache+mysql+php之linux系统安装
 

Similar to ByPat博客出品Lvs+keepalived

Salesforce at Stacki Atlanta Meetup February 2016
Salesforce at Stacki Atlanta Meetup February 2016Salesforce at Stacki Atlanta Meetup February 2016
Salesforce at Stacki Atlanta Meetup February 2016
StackIQ
 
Continuous Delivery: The Next Frontier
Continuous Delivery: The Next FrontierContinuous Delivery: The Next Frontier
Continuous Delivery: The Next Frontier
Carlos Sanchez
 
X64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 newX64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 newYiwei Ma
 
Nginx 0.9.x 安装手册
Nginx 0.9.x 安装手册Nginx 0.9.x 安装手册
Nginx 0.9.x 安装手册Yiwei Ma
 
Nginx 0.8.x 安装手册
Nginx 0.8.x 安装手册Nginx 0.8.x 安装手册
Nginx 0.8.x 安装手册Yiwei Ma
 
Control your deployments with Capistrano
Control your deployments with CapistranoControl your deployments with Capistrano
Control your deployments with Capistrano
Ramazan K
 
Ubic-public
Ubic-publicUbic-public
Ftp configuration in rhel7
Ftp configuration in rhel7Ftp configuration in rhel7
Ftp configuration in rhel7
Balamurugan M
 
Tested install-isp config3-ubuntu-16-04
Tested install-isp config3-ubuntu-16-04Tested install-isp config3-ubuntu-16-04
Tested install-isp config3-ubuntu-16-04
SANTIAGO HERNÁNDEZ
 
Self Created Load Balancer for MTA on AWS
Self Created Load Balancer for MTA on AWSSelf Created Load Balancer for MTA on AWS
Self Created Load Balancer for MTA on AWSsharu1204
 
DPDK in Containers Hands-on Lab
DPDK in Containers Hands-on LabDPDK in Containers Hands-on Lab
DPDK in Containers Hands-on Lab
Michelle Holley
 
Stacki - The1600+ Server Journey
Stacki - The1600+ Server JourneyStacki - The1600+ Server Journey
Stacki - The1600+ Server Journey
Salesforce Engineering
 
StackiFest16: Stacki 1600+ Server Journey - Dave Peterson, Salesforce
StackiFest16: Stacki 1600+ Server Journey - Dave Peterson, Salesforce StackiFest16: Stacki 1600+ Server Journey - Dave Peterson, Salesforce
StackiFest16: Stacki 1600+ Server Journey - Dave Peterson, Salesforce
StackIQ
 
ONOS SDN Controller - Clustering Tests & Experiments
ONOS SDN Controller - Clustering Tests & Experiments ONOS SDN Controller - Clustering Tests & Experiments
ONOS SDN Controller - Clustering Tests & Experiments
Eueung Mulyana
 
How to install squid proxy on server or how to install squid proxy on centos o
How to install squid proxy on server  or how to install squid proxy on centos oHow to install squid proxy on server  or how to install squid proxy on centos o
How to install squid proxy on server or how to install squid proxy on centos o
Proxiesforrent
 
Installation Openstack Swift
Installation Openstack SwiftInstallation Openstack Swift
Installation Openstack Swift
ymtech
 
Securing the network for VMs or Containers
Securing the network for VMs or ContainersSecuring the network for VMs or Containers
Securing the network for VMs or Containers
Marian Marinov
 

Similar to ByPat博客出品Lvs+keepalived (20)

Salesforce at Stacki Atlanta Meetup February 2016
Salesforce at Stacki Atlanta Meetup February 2016Salesforce at Stacki Atlanta Meetup February 2016
Salesforce at Stacki Atlanta Meetup February 2016
 
Continuous Delivery: The Next Frontier
Continuous Delivery: The Next FrontierContinuous Delivery: The Next Frontier
Continuous Delivery: The Next Frontier
 
X64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 newX64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 new
 
Nginx 0.9.x 安装手册
Nginx 0.9.x 安装手册Nginx 0.9.x 安装手册
Nginx 0.9.x 安装手册
 
Nginx 0.8.x 安装手册
Nginx 0.8.x 安装手册Nginx 0.8.x 安装手册
Nginx 0.8.x 安装手册
 
Control your deployments with Capistrano
Control your deployments with CapistranoControl your deployments with Capistrano
Control your deployments with Capistrano
 
Ubic
UbicUbic
Ubic
 
Ubic-public
Ubic-publicUbic-public
Ubic-public
 
Ftp configuration in rhel7
Ftp configuration in rhel7Ftp configuration in rhel7
Ftp configuration in rhel7
 
Tested install-isp config3-ubuntu-16-04
Tested install-isp config3-ubuntu-16-04Tested install-isp config3-ubuntu-16-04
Tested install-isp config3-ubuntu-16-04
 
Self Created Load Balancer for MTA on AWS
Self Created Load Balancer for MTA on AWSSelf Created Load Balancer for MTA on AWS
Self Created Load Balancer for MTA on AWS
 
Cooking with Chef
Cooking with ChefCooking with Chef
Cooking with Chef
 
DPDK in Containers Hands-on Lab
DPDK in Containers Hands-on LabDPDK in Containers Hands-on Lab
DPDK in Containers Hands-on Lab
 
Stacki - The1600+ Server Journey
Stacki - The1600+ Server JourneyStacki - The1600+ Server Journey
Stacki - The1600+ Server Journey
 
StackiFest16: Stacki 1600+ Server Journey - Dave Peterson, Salesforce
StackiFest16: Stacki 1600+ Server Journey - Dave Peterson, Salesforce StackiFest16: Stacki 1600+ Server Journey - Dave Peterson, Salesforce
StackiFest16: Stacki 1600+ Server Journey - Dave Peterson, Salesforce
 
ONOS SDN Controller - Clustering Tests & Experiments
ONOS SDN Controller - Clustering Tests & Experiments ONOS SDN Controller - Clustering Tests & Experiments
ONOS SDN Controller - Clustering Tests & Experiments
 
How to install squid proxy on server or how to install squid proxy on centos o
How to install squid proxy on server  or how to install squid proxy on centos oHow to install squid proxy on server  or how to install squid proxy on centos o
How to install squid proxy on server or how to install squid proxy on centos o
 
Installation Openstack Swift
Installation Openstack SwiftInstallation Openstack Swift
Installation Openstack Swift
 
EC2
EC2EC2
EC2
 
Securing the network for VMs or Containers
Securing the network for VMs or ContainersSecuring the network for VMs or Containers
Securing the network for VMs or Containers
 

Recently uploaded

Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 

Recently uploaded (20)

Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 

ByPat博客出品Lvs+keepalived

  • 1. 利用 LVS+Keepalived 实现高性能高可用负载均衡 ByPat博客专注于linux资源分享 http://blog.bypat.com 作者:Redhat 日期:20 -1-27 背景: 随着你的网站业务量的增长你网站的服务器压力越来越大?需要负载均衡方案! 商 业的硬件如 F5 又太贵,你们又是创业型互联公司如何有效节约成本,节省不必要的浪费? 同时实现商业硬件一样的高性能高可用的功能?有什么好的负载均衡可伸张可扩展的方案 吗?答案是肯定的!有!我们利用 LVS+Keepalived 基于完整开源软件的架构可以为你提供 一个负载均衡及高可用的服务器。 一. LVS+Keepalived 介绍 1. LVS LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器 集群系统。本项目在 1998 年 5 月由章文嵩博士成立,是中国国内最早出现的自由 软件项目之一。目前有三种IP负载均衡技术(VS/NAT、VS/TUN和VS/DR) ; 八种调度算法(rr,wrr,lc,wlc,lblc,lblcr,dh,sh)。 2. Keepalvied Keepali ved 在这里主要用作 RealServer 的健康状态检查以及 LoadBalance 主机和 BackUP 主机之间 failover 的实现 二. 网站负载均衡拓朴图 . IP 信息列表: 名称 IP L VS-DR-Master 61.164.122.6 L VS-DR-BACKUP 61.164.122.7 L VS-DR-VIP 61.164.122.8 WEB1-R ealserver 61.164.122.9 WEB2-R ealserver 61.164.122.10 G ateWay 61.164.122.1
  • 2. ByPat博客专注于linux资源分享 http://blog.bypat.com 三. 安装 LVS 和 Keepalvied 软件包 1. 下载相关软件包 #m kdir /usr/local/src/lvs #c d /usr/local/src/lvs #w get http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz #w get http://www.keepalived.org/software/keepalived-1.1.15.tar.gz 2. 安装 LVS 和 Keepalived #lsm od |grep ip_vs #uname -r 2.6.18-53.el5PAE #ln -s /usr/src/kernels/2.6.18-53.el5PAE-i686/ /usr/src/linux #tar zxvf ipvsadm-1.24.tar.gz #cd ipvsadm-1.24 #make && make install #find / -name ipvsadm # 查看 ipvsadm 的位置 #tar zxvf keepalived-1.1.15.tar.gz #cd keepalived-1.1.15 #./configure && make && make install #find / -name keepalived # 查看 keepalived 位置 #cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ #cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ #mkdir /etc/keepalived #cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ #cp /usr/local/sbin/keepalived /usr/sbin/ #service keepalived start|stop # 做成系统启动服务方便管理. 四. 配置 LVS 实现负载均衡 1. LV S-DR,配置 LVS 脚本实现负载均衡 #vi /usr/local/sbin/lvs-dr.sh #!/bin/bash # description: start LVS of DirectorServer GW=61.164.122.1 # website director vip. SNS_VIP=61.164.122.8 SNS_RIP1=61.164.122.9 SNS_RIP2=61.164.122.10
  • 3. ByPat博客专注于linux资源分享 http://blog.bypat.com ./e tc/rc.d/init.d/functions logger $0 called with $1 case "$1" in start) # set squid vip / sbin/ipvsadm --set 30 5 60 / sbin/ifconfig eth0:0 $SNS_VIP broadcast $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP up / sbin/route add -host $SNS_VIP dev eth0:0 / sbin/ipvsadm -A -t $SNS_VIP:80 -s wrr -p 3 / sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP1:80 -g -w 1 / sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP2:80 -g -w 1 t ouch /var/lock/subsys/ipvsadm >/dev/null 2>&1 ;; stop) /sbin/ipvsadm -C /sbin/ipvsadm -Z i fconfig eth0:0 down i fconfig eth0:1 down rout e del $SNS_VIP route del $SS_VIP rm -rf /var/lock/subsys/ipvsadm >/dev/null 2>&1 ech o "ipvsadm stoped" ;; status) i f [ ! -e /var/lock/subsys/ipvsadm ];then echo "ipvsadm stoped" exit 1 else echo "ipvsadm OK" fi ;; *) echo "Usage: $0 {start|stop|status}" exit 1 esac
  • 4. ByPat博客专注于linux资源分享 http://blog.bypat.com exit 0 2. 配置 Realserver 脚本. #vi /usr/local/sbin/realserver.sh #!/bin/bash # description: Config realserver lo and apply noarp SNS_VIP=61.164.122.8 . /etc/rc.d/init.d/functions case "$1" in start) ifco nfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP /sbin/route add -host $SNS_VIP dev lo:0 ec ho "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore ec ho "2" >/proc/sys/net/ipv4/conf/lo/arp_announce ec ho "1" >/proc/sys/net/ipv4/conf/all/arp_ignore ec ho "2" >/proc/sys/net/ipv4/conf/all/arp_announce sy sctl -p >/dev/null 2>&1 ec ho "RealServer Start OK" ;; stop) i fconfig lo:0 down rou te del $SNS_VIP >/dev/null 2>&1 ec ho "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore ec ho "0" >/proc/sys/net/ipv4/conf/lo/arp_announce ec ho "0" >/proc/sys/net/ipv4/conf/all/arp_ignore ec ho "0" >/proc/sys/net/ipv4/conf/all/arp_announce ec ho "RealServer Stoped" ;; *) ec ho "Usage: $0 {start|stop}" exit 1 esac exit 0 或者采用 secondary ip address 方式配置 # vi /etc/sysctl.conf net.ipv4.conf.lo.arp_ignore = 1 net.ipv4.conf.lo.arp_announce = 2
  • 5. ByPat博客专注于linux资源分享 http://blog.bypat.com net.ipv4.conf.all.arp_ignore = 1 net.ipv4.conf.all.arp_announce = 2 #sysctl –p #ip addr add 61.164.122.8/32 dev lo #ip add list 查看是否绑定 3. 启动 lvs-dr 脚本和 realserver 启本,在 DR 上可以查看 LVS 当前状态: #watch ipvsadm –ln 五.利用 Keepalvied 实现负载均衡和和高可用性 1.配置在主负载均衡服务器上配置 keepalived.conf #vi /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { n otification_email { cnseek@gmail.com # fai lover@firewall.loc # sy sadmin@firewall.loc } n otification_email_from sns-lvs@gmail.com sm tp_server 127.0.0.1 # smtp_connect_timeout 30 ro uter_id LVS_DEVEL } # VIP1 vrrp_instance VI_1 { state MASTER #备份服务器上将 MASTER 改为 BACKUP i nterface eth0 virtual_router_id 51 priority 100 # 备份服务上将 100 改为 99 ad vert_int 1 au thentication { auth_type PASS a uth_pass 1111 } virtual_ipaddress { 61. 164.122.8 #(如果有多个 VIP,继续换行填写.) } } virtual_server 61.164.122.8 80 {
  • 6. ByPat博客专注于linux资源分享 http://blog.bypat.com delay_loop 6 #(每隔 10 秒查询 realserver 状态) lb_algo wrr #(lvs 算法) lb_kind DR #(Direct Route) persistence_timeout 60 #(同一 IP 的连接 60 秒内被分配到同一台 realserver) protocol TCP #(用 TCP 协议检查 realserver 状态) rea l_server 61.164.122.9 80 { weight 3 #(权重) TCP_CHECK { connect_timeout 10 #(10 秒无响应超时) nb_get_retry 3 delay_before_retry 3 connect_port 80 } } rea l_server 61.164.122.10 80 { weight 3 TCP_CHECK { c onnect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } } 2. BACKUP 服 务 器 同 上 配 置 , 先 安 装 lvs 再 按 装 keepalived, 仍 后 配 置 /etc/keepalived/keepalived.conf,只需将红色标示的部分改一下即可. 3. vi /etc/rc.local # /usr/local/sbin/lvs-dr.sh 将 lvs-dr.sh 这个脚本注释掉。步 # /usr/local/sbin/lvs-dr.sh stop 停止 lvs-dr 脚本 #/ etc/init.d/keepalived start 启动 keepalived 服务,keepalived 就能利用 keepalived.conf 配 置文件,实现负载均衡和高可用. 4. 查看 lvs 服务是否正常 #watch ipvsadm –ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port F orward Weight ActiveConn InActConn TCP 61.164.122.8:80 wrr persistent 60 -> 61.164.122.10:80 Route 3 0 0 -> 61.164.122.9:80 Route 3 0 0
  • 7. ByPat博客专注于linux资源分享 http://blog.bypat.com #t ail –f /var/log/message 监听日志,查看状态。 5.停 Master 服务器的 keepalived 服务,查看 BAKCUP 服务器是否能正常接管. 四.相关参考 Keepalived 相关参考资料。 http://www.keepalived.org/documentation.html