操作系统平台:RedHat6.4  x86_64

软件:LVS+keepalived LVS+Keepalived

介绍 LVS LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统。本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一。目前有三种IP负载均衡技术(VS/NAT、VS/TUN和VS/DR); 十种调度算法(rrr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq)。 Keepalvied Keepalived在这里主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现

      IP配置信息:

LVS-DR-Master          192.168.20.135

LVS-DR-BACKUP          192.168.20.136

LVS-DR-VIP             192.168.20.160

WEB1-Realserver        192.168.20.121

WEB2-Realserver        192.168.20.123

GateWay                192.168.20.253

安装LVS和Keepalvied软件包

1. 下载相关软件包 #mkdir /usr/local/src/lvs #cd /usr/local/src/lvs #wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz #wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz

2. 安装LVS和Keepalived

#lsmod |grep ip_vs

#uname -r

#ln -s /usr/src/kernels/2.6.32-358.el6.x86_64/  /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. LVS-DR,配置LVS脚本实现负载均衡

vi /usr/local/sbin/lvs-dr.sh #!/bin/bash

# description: start LVS of DirectorServer

#Written by :NetSeek

#http://www.linuxtone.org

GW=192.168.20.253

# website director vip.

WEB_VIP=192.168.20.160

WEB_RIP1=192.168.20.155

WEB_RIP2=192.168.20.156

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

logger $0 called with $1

case "$1" in

start)

# Clear all iptables rules.

/sbin/iptables -F

# Reset iptables counters.

/sbin/iptables -Z

# Clear all ipvsadm rules/services.

/sbin/ipvsadm -C

#set lvs vip for dr

/sbin/ipvsadm --set 30 5 60

/sbin/ifconfig eth0:0 $WEB_VIP broadcast $WEB_VIP netmask 255.255.255.255 up

/sbin/route add -host $WEB_VIP dev eth0:0

/sbin/ipvsadm -A -t $WEB_VIP:22 -s wrr -p 3

/sbin/ipvsadm -a -t $WEB_VIP:22 -r $WEB_RIP1:22 -g -w 1

/sbin/ipvsadm -a -t $WEB_VIP:22 -r $WEB_RIP2:22 -g -w 1

/sbin/ipvsadm -A -t $WEB_VIP:3389 -s wrr -p 3

/sbin/ipvsadm -a -t $WEB_VIP:3389 -r $WEB_RIP1:3389 -g -w 1

/sbin/ipvsadm -a -t $WEB_VIP:3389 -r $WEB_RIP2:3389 -g -w 1

/sbin/ipvsadm -A -t $WEB_VIP:33389 -s wrr -p 3

/sbin/ipvsadm -a -t $WEB_VIP:33389 -r $WEB_RIP1:33389 -g -w 1

/sbin/ipvsadm -a -t $WEB_VIP:33389 -r $WEB_RIP2:33389 -g -w 1

/sbin/ipvsadm -A -t $WEB_VIP:62000 -s wrr -p 3

/sbin/ipvsadm -a -t $WEB_VIP:62000 -r $WEB_RIP1:62000 -g -w 1

/sbin/ipvsadm -a -t $WEB_VIP:62000 -r $WEB_RIP2:62000 -g -w 1

/sbin/ipvsadm -A -t $WEB_VIP:21 -s wrr -p 3

/sbin/ipvsadm -a -t $WEB_VIP:21 -r $WEB_RIP1:21 -g -w 1

/sbin/ipvsadm -a -t $WEB_VIP:21 -r $WEB_RIP2:21 -g -w 1

/sbin/ipvsadm -A -t $WEB_VIP:55555 -s wrr -p 3

/sbin/ipvsadm -a -t $WEB_VIP:55555 -r $WEB_RIP1:55555 -g -w 1

/sbin/ipvsadm -a -t $WEB_VIP:55555 -r $WEB_RIP2:55555 -g -w 1

/sbin/ipvsadm -A -t $WEB_VIP:8080 -s wrr -p 3

/sbin/ipvsadm -a -t $WEB_VIP:8080 -r $WEB_RIP1:8080 -g -w 1

/sbin/ipvsadm -a -t $WEB_VIP:8080 -r $WEB_RIP2:8080 -g -w 1

/sbin/ipvsadm -A -t $WEB_VIP:8081 -s wrr -p 3

/sbin/ipvsadm -a -t $WEB_VIP:8081 -r $WEB_RIP1:8081 -g -w 1

/sbin/ipvsadm -a -t $WEB_VIP:8081 -r $WEB_RIP2:8081 -g -w 1

touch /var/lock/subsys/ipvsadm >/dev/null 2>&1

# set Arp

/sbin/arping -I eth0 -c 5 -s $WEB_VIP $GW >/dev/null 2>&1

;;

stop)

/sbin/ipvsadm -C

/sbin/ipvsadm -Z

ifconfig eth0:0 down

route del $WEB_VIP  >/dev/null 2>&1

rm -rf /var/lock/subsys/ipvsadm >/dev/null 2>&1

/sbin/arping -I eth0 -c 5 -s $WEB_VIP $GW

echo "ipvsadm stoped"

;;

status)

if [ ! -e /var/lock/subsys/ipvsadm ];then

echo "ipvsadm is stoped"

exit 1

else

ipvsadm -ln

echo "..........ipvsadm is OK."

fi

;;

*)

echo "Usage: $0 {start|stop|status}"

exit 1

esac

exit 0

2. 配置Realserver脚本.

在web1和web2上配置realserver脚本:

cat /etc/rc.d/init.d/realserver.sh

#!/bin/bash

# Written by NetSeek

# description: Config realserver lo and apply noarp

WEB_VIP=192.168.20.160

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

case "$1" in

start)

ifconfig lo:0 $WEB_VIP netmask 255.255.255.255 broadcast $WEB_VIP

/sbin/route add -host $WEB_VIP dev lo:0

echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore

echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce

echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore

echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce

sysctl -p >/dev/null 2>&1

echo "RealServer Start OK"

;;

stop)

ifconfig lo:0 down

route del $WEB_VIP >/dev/null 2>&1

echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore

echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce

echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore

echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce

echo "RealServer Stoped"

;;

status)

# Status of LVS-DR real server.

islothere=`/sbin/ifconfig lo:0 | grep $WEB_VIP`

isrothere=`netstat -rn | grep "lo:0" | grep $web_VIP`

if [ ! "$islothere" -o ! "isrothere" ];then

# Either the route or the lo:0 device

# not found.

echo "LVS-DR real server Stopped."

else

echo "LVS-DR Running."

fi

;;

*)

# Invalid entry.

echo "$0: Usage: $0 {start|status|stop}"

exit 1

;;

esac

exit 0

附上realserver机上的/etc/sysctl.conf :

# Kernel sysctl configuration file for Red Hat Linux

# # For binary values, 0 is disabled, 1 is enabled.  See sysctl(8) and # sysctl.conf(5) for more details.

# Controls IP packet forwarding

net.ipv4.ip_forward = 1

# Controls source route verification

net.ipv4.conf.default.rp_filter = 1

# Do not accept source routing

net.ipv4.conf.default.accept_source_route = 0

# Controls the System Request debugging functionality of the kernel

kernel.sysrq = 0

# Controls whether core dumps will append the PID to the core filename.

# Useful for debugging multi-threaded applications.

kernel.core_uses_pid = 1

net.ipv4.conf.lo.arp_ignore = 1

net.ipv4.conf.lo.arp_announce = 2

net.ipv4.conf.all.arp_ignore = 1

net.ipv4.conf.all.arp_announce = 2

或者采用secondary ip address方式配置

# vi /etc/sysctl.conf

添加以下内容如上所示:

net.ipv4.conf.lo.arp_ignore = 1

net.ipv4.conf.lo.arp_announce = 2

net.ipv4.conf.all.arp_ignore = 1

net.ipv4.conf.all.arp_announce = 2

#sysctl –p

#ip addr 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 {

notification_email {

acassen@firewall.loc

failover@firewall.loc

sysadmin@firewall.loc

}

notification_email_from Alexandre.Cassen@firewall.loc

smtp_server 192.168.200.1

smtp_connect_timeout 30

router_id LVS_DEVEL

}

vrrp_instance VI_1 {

state MASTER   # 状态实际MASTER

interface eth0      # 监听网卡切换

virtual_router_id 51

priority 100        # 优先级(越大优先级越高)

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {         # 虚拟IP地址列表,即VIP

192.168.20.160

}

}

virtual_server 192.168.20.160 9080 {

delay_loop 6

lb_algo wrr        #分发算法

lb_kind DR       # DR模式

nat_mask 255.255.255.0

persistence_timeout 50

protocol TCP

real_server 192.168.20.121 9080 {

weight 3    # 权重(权重越高处理的请求越多)

TCP_CHECK {

connect_port    9080

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

real_server 192.168.20.123 9080 {

weight 3

TCP_CHECK {

connect_port    9080

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

}

virtual_server 192.168.20.160 9081 {

delay_loop 6

lb_algo wrr

lb_kind DR

nat_mask 255.255.255.0

persistence_timeout 50

protocol TCP

real_server 192.168.20.121 9081 {

weight 3

TCP_CHECK {

connect_port    9081

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

real_server 192.168.20.123 9081 {

weight 3

TCP_CHECK {

connect_port    9081

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

}

virtual_server 192.168.20.160 22 {

delay_loop 6

lb_algo wrr

lb_kind DR

nat_mask 255.255.255.0

persistence_timeout 50

protocol TCP

real_server 192.168.20.121 22 {

weight 3

TCP_CHECK {

connect_port    22

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

real_server 192.168.20.123 22 {

weight 3

TCP_CHECK {

connect_port    22

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

}

virtual_server 192.168.20.160 3389 {

delay_loop 6

lb_algo wrr

lb_kind DR

nat_mask 255.255.255.0

persistence_timeout 50

protocol TCP

real_server 192.168.20.121 3389 {

weight 3

TCP_CHECK {

connect_port    3389

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

real_server 192.168.20.123 3389 {

weight 3

TCP_CHECK {

connect_port    3389

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

}

virtual_server 192.168.20.160 33389 {

delay_loop 6

lb_algo wrr

lb_kind DR

nat_mask 255.255.255.0

persistence_timeout 50

protocol TCP

real_server 192.168.20.121 33389 {

weight 3

TCP_CHECK {

connect_port    33389

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

real_server 192.168.20.123 33389 {

weight 3

TCP_CHECK {

connect_port    33389

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

}

virtual_server 192.168.20.160 55555 {

delay_loop 6

lb_algo wrr

lb_kind DR

nat_mask 255.255.255.0

persistence_timeout 50

protocol TCP

real_server 192.168.20.121 55555 {

weight 3

TCP_CHECK {

connect_port    55555

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

real_server 192.168.20.123 55555 {

weight 3

TCP_CHECK {

connect_port    55555

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

}

virtual_server 192.168.20.160 62000 {

delay_loop 6

lb_algo wrr

lb_kind DR

nat_mask 255.255.255.0

persistence_timeout 50

protocol TCP

real_server 192.168.20.121 62000 {

weight 3

TCP_CHECK {

connect_port    62000

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

real_server 192.168.20.123 62000 {

weight 3

TCP_CHECK {

connect_port    62000

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

}

virtual_server 192.168.20.160 21 {

delay_loop 6

lb_algo wrr

lb_kind DR

nat_mask 255.255.255.0

persistence_timeout 50

protocol TCP

real_server 192.168.20.121 21 {

weight 3

TCP_CHECK {

connect_port    21

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

real_server 192.168.20.123 21 {

weight 3

TCP_CHECK {

connect_port    21

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

}

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

3. #/etc/init.d/keepalived start  启动keepalived 服务,keepalived就能利用keepalived.conf 配置文件,实现负载均衡和高可用.

4. 查看lvs服务是否正常

Ipvsadm -l  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

TCP  192.168.20.160:33389 wrr persistent 50

-> 192.168.20.121:33389         Route   3  0          0

-> 192.168.20.123:33389         Route   3  0          0

TCP  192.168.20.160:62000 wrr persistent 50

-> 192.168.20.121:62000         Route   3  0          0

-> 192.168.20.123:62000         Route   3  0          0

TCP  192.168.20.160:55555 wrr persistent 50

-> 192.168.20.121:55555         Route   3  0          0

-> 192.168.20.123:55555         Route   3  0          0

TCP  192.168.20.160:3389 wrr persistent 50

-> 192.168.20.121:3389          Route   3  0          0

-> 192.168.20.123:3389          Route   3  0          0

TCP  192.168.20.160:22 wrr persistent 50

-> 192.168.20.121:22            Route   3  0          0

-> 192.168.20.123:22            Route   3  0          0

TCP  192.168.20.160:21 wrr persistent 50

TCP  192.168.20.160:9081 wrr persistent 50

-> 192.168.20.121:9081          Route   3  0          0

-> 192.168.20.123:9081          Route   3  0          0

TCP  192.168.20.160:9080 wrr persistent 50

-> 192.168.20.121:9080          Route   3  0          0

-> 192.168.20.123:9080          Route   3  0          0

#watch ipvsadm –ln

IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddressort Scheduler Flags  -> RemoteAddressort

Forward Weight ActiveConn InActConn

#tail –f /var/log/message  监听日志,查看状态,测试LVS负载均衡及高可用性是否有效。

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

keepalived+lvs+usp安装实施文档的更多相关文章

  1. LVS+Heartbeat安装部署文档

    LVS+Heartbeat安装部署文档 发表回复 所需软件: ipvsadm-1.24-10.x86_64.rpmheartbeat-2.1.3-3.el5.centos.x86_64.rpmhear ...

  2. keepalived双机热备,安装部署文档

    keepalived双击热备,安装部署文档: 下载目录:/apps/keepalived-1.2.7.tar.gz 1:---> yum install -y make wget 2:---&g ...

  3. Oracle 11g 单实例到单实例OGG同步实施文档-OGG initial load

    Oracle 11g 单实例到单实例OGG同步实施文档-OGG initial load 2018-06-07 00:514730原创GoldenGate 作者: leo 本文链接:https://w ...

  4. Oracle 11g 单实例到单实例OGG同步实施文档-RMAN 初始化

    Oracle 11g 单实例到单实例OGG同步实施文档-RMAN 初始化 2018-06-07 13:455170原创GoldenGate 作者: leo 本文链接:https://www.cndba ...

  5. Oracle 11g 单实例到单实例OGG同步实施文档-EXPDP初始化

    Oracle 11g 单实例到单实例OGG同步实施文档-EXPDP初始化 2018-06-07 00:446470原创GoldenGate 作者: leo 本文链接:https://www.cndba ...

  6. DATAGUARD实施文档

    DATAGUARD实施文档 一.前期准备及备机安装: 通过获取到的主机信息规划备机数据库安装基础信息:(注:在安装备机时需要尽量保障与主机库信息一致,以下表格中的备机信息为根据主机信息规划的安装信息. ...

  7. PROD异机全备份恢复验证实施文档

    PROD异机全备份恢复验证实施文档 ******利用10月25日的全量备份.10月26日当天的归档日志及当前redo日志,恢复数据库到故障前数据库状态****** 准备工作:source 源库:PRO ...

  8. Xcode离线安装帮助文档

    Xcode离线安装帮助文档   1.在线查看帮助文件:Xcode下查看帮助文件,菜单Help-Developer Documentation在右上角搜索框中即可检索,但速度很慢,在线查看. 2.下载帮 ...

  9. CM5(Cloudera Manager 5) + CDH5(Cloudera's Distribution Including Apache Hadoop 5)的安装详细文档

    参考 :http://www.aboutyun.com/thread-9219-1-1.html Cloudera Manager5及CDH5在线(cloudera-manager-installer ...

随机推荐

  1. 01.轮播图之一 :scrollView 轮播

    接触的每个项目,都会用到轮播图的部分,轮播图都写了好多次,用过各种各样的方式写: 这篇总结的博客,我将分为几个篇幅写,希望写完这几篇博客之后,我能总结出自己写这个轮播的优缺和不同之处 scrollvi ...

  2. Informix网页数据维护客户端工具

    Informix是IBM公司出品的关系数据库管理系统,目前还有在银行,电信等行业使用,Informix的客户端工具很少,数据维护及可视化比较麻烦,现在TreeSoft数据库管理系统已支持Informi ...

  3. CX ONE 不能全屏

    兼容性  win7 以管理员方式运行 1. 打开CX-Programmer,选择“工具”——“选项”:2. 点击“通用”选项卡,选择“高级”:3. 勾选“当下一次启动CX-Programmer时不显示 ...

  4. IO流学习

    1,流是一组有顺序的,有起点和重点的字节集合,是对数据传输的总称和抽象.即数据在两个设备之间的传输称作流.流的本质就是数据传输,根据数据传输的特性,将流抽象为各种累,方便直观的进行数据操作. 2,根据 ...

  5. Python爬虫入门教程之BeautifulSoup

    模块安装 pip3 install beautifulsoup4 模块导入 from bs4 import BeautifulSoup 示例html内容 RPC是一种比较流行的RPC通信框架,由谷歌公 ...

  6. Z1. 广度优先搜索(BFS)解题思路

    /** BFS 解题思路 特点:从某些特定的节点开始,感染相邻的节点; 被感染的节点,再感染其相邻的节点,以此类推. 题目常见于数据结构包括 二维数组.树.图 **/ /** 1). 二维数组特定节点 ...

  7. 利用Python进行数据分析_Pandas_数据清理、转换、合并、重塑

    1 合并数据集 pandas.merge pandas.merge(left, right, how='inner', on=None, left_on=None, right_on=None, le ...

  8. const关键字的使用——C语言

    一.常规用法 关键字const用来定义只读变量,被const定义的变量它的值是不允许改变的,即不允许给它重新赋值,即使是赋相同的值也不可以.所以说它定义的是只读变量,这也就意味着必须在定义的时候就给它 ...

  9. 原来你是这样的PaaS!

    啥叫PaaS? 许多人身处互联网领域,对PaaS仍然是雾里看花.它看似复杂,其实只要用对看法,人人都可以轻松的认识它. 网络上盛传着用pizza为例子帮助人们了解什么是PaaS,那么编者今天也不举栗子 ...

  10. Kafka集群安装及prometheus监控

    前提 zookeeper安装参考:https://www.cnblogs.com/JustinLau/p/11372782.html 其他安装参考:https://www.cnblogs.com/lu ...