第三十三天 LVS基础原理调度算法NAT和DR的实现 LXC虚拟化

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第三十三天 LVS基础原理调度算法NAT和DR的实现 LXC虚拟化相关的知识,希望对你有一定的参考价值。

大规模站点构建框架
LVS的基础原理
LVS调度方法及NAT模型的实现
LVS  dr模型及lxc虚拟化


一、大规模站点架构框架

    http: stateless

keep-alive

cookie: session


系统的:可扩展性、高可用性:99%, 99.9%, 99.999% 、性能:


可扩展性:

容量:在一定时间内能完成的工作量

scale up: 向上扩展

scale out: 向外扩展

Cluster: 集群

构建高可扩展性系统的重要原则:在系统内部尽量避免串行化和交互

调度器:director, dispatcher, balancer

HA:High Availability

LB:Load Balancing

memcached

key-value

redis: NoSQL

CDN: Content Delivery Network

分层:应用层-->服务层-->数据层

分割:

分布式:

分布式应用

分布式静态资源

分布式数据和存储

分布式计算

二、lvs基础原理

    集群的类型:

LB: 扩展、伸缩

HA:高可用

HP:High Performance  (向量机、并行处理集群)

LB: (应用层、传输层)

软件:lvs (传输层)、haproxy, nginx (应用层)

硬件:F5 BIG-IP、Citrix Netscaler、A10 A10、Array、Redware

        HA:

heartbeat 

corosync + pacemaker

RHCS: cman + rgmanager

cman + pacemaker

keepalived

HP:

hadoop


    lvs: Linux Virtual Server   阿里---章文嵩

    四层路由,四层交换

根据目标地址和目标端口实现请求转发;

netfiler: PREROUTING --> INPUT --> FORWARD --> OUTPUT --> POSTROUTING

iptables/netfilter

lvs: ipvsadm/ipvs:ipvs附着在netfilter上工作(在内核中的框架)


集群服务:tcp、udp、IP:PORT

     注:lvs的后端可以采用轮询机制、也可以指定条件判断后端服务器的忙闲来选择分配后端服务器,而后端服务器down机(或开机重新上线),lvs是否还会向故障服务器分配任务,这就需要lvs实时监测后端服务器的健康状况,而lvs本身不具备监测后端健康状况的功能,故一般lvs都搭配keepalive的使用,keepalived有监测后端服务器健康状况的功能弥补了lvs的缺陷,也提供lvs的高可用服务。


lvs其实是由2段代码组成,一段工作在内核空间叫ipvs,是真正调度的代码,另一段工作在用户空间叫ipvsadm,负责为lpvs内核框架来编写规则,定义谁是集群服务、有哪些real server,所以我们用ipvsadm编写规则定义集群服务告诉lvs用哪些规则去调度real server。


    LVS的术语:

director/real server

        IP:

            VIP:Virtual IP

    DIP: Director IP

    RIP: Real Server IP

    CIP:Client IP

    ipvs: ip virtual server


    LVS的类型:

    lvs-nat:Network Address Translation

    lvs-dr:Direct Routing

    lvs-tun:Tunneling


    nat类型的特性:

1、RS应用使用私有地址;RS的网关必须指向DIP;

2、请求和响应都要经过Director;高负载场景中,Director易成为性能瓶颈;

3、支持端口映射;

4、RS可以使用任意OS;


    dr类型的特性:(推荐)

1、保证前端路由将目标地址为VIP的报文统统发往Directory,而不能是RS;

解决方案:

(1) 静态地址绑定:在前端路由器上操作;问题:未必有路由操作权限

(2) aprtables

(3) 修改RS上内核参数,将RS上的VIP配置在lo接口的别名上,并限制其不能响应对VIP地址解析请求;

2、RS可以使用私有地址;但也可以使用公网地址,此时可通过互联网通过RIP对其直接访问;

3、RS跟Directory必须在同一物理网络中;

4、请求报文经由Director,但响应报文必须不能经过Director;

5、不支持端口映射;

6、RS可以是大多数常见的OS;

7、RS的网关绝不允许指向DIP;

8、IP报文的基础上封装了MAC地址来实现的 cip:vip--mac


tun类型的特性:(很少用)

1、RIP、VIP、DIP全部是公网地址(2个real server分别在上海和北京的场景);

2、RS的网关不会也不可能指向DIP;

3、请求报文经由Director,但响应报文必须不能经过Director;

4、不支持端口映射;

5、RS的OS必须支持隧道功能;

6、IP报文的基础上有封装了IP报文来实现的  cip:vip--dip:rip


三、lvs调度算法及NAT实现

lvs scheduler(调度):

查看调度算法# grep -i ‘VS‘ /boot/config-VERSION  (10种)


静态方法:仅根据调度算法本身进行调度,不考虑后端服务器负载。

rr: round robin,轮流,轮询,轮叫

wrr: weighted round robin, 加权轮询

sh: source hashing,源地址hash,表示来源于同一个CIP的请求将始终被定向至同一个RS;能实现SESSION保持;

dh: destination hashing, 目标地址hash,


动态方法:根据算法及各RS当前的负载状况进行调度

lc: least connection,最少连接

Overhead=Active*256+Inactive

wlc: weighted lc

Overhead=(Active*256+Inactive)/weight

A: 1, 

B: 5,

sed: shortest expection delay

Overhead=(Active+1)*256/weight

1: B

2: B

3: B

4: B

nq: Never Queue

lblc: Locality-Based Least Connection(基于本地的最少链接)

lblcr:Replicated lblc(带复制的本地最少链接)


配置director:

    定义集群服务

    为集群添加各RS

例如:1、  2台Real Sserver分别定义网页启动httpd服务,配置RIP(10.0.0.8\10.0.0.9),网关指向DIP(10.0.0.1)

      2、  配置调度器双网卡(172.16.100.7\10.0.0.1)安装 ipvsadm(yum -y install ipvsadm)

ipvsadm:   #ipvsadm -h 查看命令


    (1)添加一个集群服务:

    ipvsadm -A|E -t|u|f service-address [-s scheduler] 

        service-address:

        -t|u: VIP:Port

        -f: #

    说明:-A|E 添加|修改群集服务;-t|u|f :tcp|udp|firewall标记;[-s scheduler]调度算法

        如: ipvsadm -A -t 172.16.100.7:80 -s rr

    (2)向一个已经存在集群服务添加一个RS:

    ipvsadm -a|e -t|u|f service-address -r server-address [options]

        -r RS-ADDR

        -w weight

        --gatewaying   -g       gatewaying (direct routing) (default)

        --ipip         -i       ipip encapsulation (tunneling)

        --masquerading -m       masquerading (NAT)


    说明: -a|e 添加修改real server;-r 指定real server

    如:ipvsadm -a -t 172.16.100.7:80 -r 10.0.0.8 -m

ipvsadm -a -t 172.16.100.7:80 -r 10.0.0.9 -m


好了,客户端172.16.100.8 访问172.16.100.7 测试,访问无响应,打开转发功能命令如下:

#echo 1 > /proc/sys/net/ipv4/ip_forward  再试OK!


     查看已经定义的集群服务及RS:

             ipvsadm -L -n

             -c: 查看各连接

             --stats: 统计数据

             --rate: 速率

             --exact: 精确值

    修改加权轮询

#ipvsadm -E -t 172.16.100.7:80 -s wrr 

#ipvsadm -L -n

#ipvsadm -e -t 172.16.100.7:80 -r 10.0.0.8 -m -w 2

#ipvsadm -L -n


 从集群服务中删除RS:

 ipvsadm -d -t|u|f service-address -r server-address


 删除集群服务:

 ipvsadm -D -t|u|f service-address


 清空所有的集群服务:

 ipvsadm -C 


 保存集群服务定义:

 ipvsadm -S > /path/to/some_rule_file

 ipvsadm-save > /path/to/some_rule_file


 让规则文件中的规则生效:

 ipvsadm -R < /path/from/some_rule_file

 ipvsadm-restore < /path/from/some_rule_file


    例如:ipvsadm -S 保存规则后,重启服务(service ipvsadm save,默认保存至/etc/sysconfig/ipvsadm)

     #ipvsadm -S > /etc/sysconfig/ipvsadm.2 保存

     #ipvsadm -C 删除集群,查看 #ipvsadm -L -n

     #ipvsadm -R < /etc/sysconfig/ipvsadm.2 恢复集群


四、LVS_DR模式及lxc虚拟机


 Session持久机制:

 1、session绑定:始终将来自同一个源IP的请求定向至同一个RS;没有容错能力;有损均衡效果;

 2、session复制:在RS之间同步session,每个RS拥有集群中的所有的session;对规模集群不适用;

 3、session服务器:利用单独部署的服务器来统一管理集群中的session;


 博客:lvs-nat部署discuz;


 LVS DR:

 Director: DIP, VIP

 RS: RIP, VIP


 RS: 配置内核参数

 arp_ignore: 如何响应接收ARP地址请求;默认0;1表示仅在请求的地址配置在请求报文的接口进行响应;

 arp_announce: 如何通告本地地址;默认0;2表示仅通过网络直连的接口的地址;


 RS:首先配置内核参数

 配置VIP时使用:

 ifconfig lo:0 VIP netmask 255.255.255.255 broadcast VIP up

 route add -host VIP dev lo:0


Director脚本:

#!/bin/bash

#

# LVS script for VS/DR

#

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

#

VIP=192.168.0.210

RIP1=192.168.0.221

RIP2=192.168.0.222

PORT=80


#

case "$1" in

start)           


 /sbin/ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.255 up

 /sbin/route add -host $VIP dev eth0:1


# Since this is the Director we must be able to forward packets

 echo 1 > /proc/sys/net/ipv4/ip_forward


# Clear all iptables rules.

 /sbin/iptables -F


# Reset iptables counters.

 /sbin/iptables -Z


# Clear all ipvsadm rules/services.

 /sbin/ipvsadm -C


# Add an IP virtual service for VIP 192.168.0.219 port 80

# In this recipe, we will use the round-robin scheduling method. 

# In production, however, you should use a weighted, dynamic scheduling method. 

 /sbin/ipvsadm -A -t $VIP:80 -s wlc


# Now direct packets for this VIP to

# the real server IP (RIP) inside the cluster

 /sbin/ipvsadm -a -t $VIP:80 -r $RIP1 -g -w 1

 /sbin/ipvsadm -a -t $VIP:80 -r $RIP2 -g -w 2


 /bin/touch /var/lock/subsys/ipvsadm &> /dev/null

;; 


stop)

# Stop forwarding packets

 echo 0 > /proc/sys/net/ipv4/ip_forward


# Reset ipvsadm

 /sbin/ipvsadm -C


# Bring down the VIP interface

 /sbin/ifconfig eth0:1 down

 /sbin/route del $VIP

 

 /bin/rm -f /var/lock/subsys/ipvsadm

 

 echo "ipvs is stopped..."

;;


status)

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

   echo "ipvsadm is stopped ..."

 else

   echo "ipvs is running ..."

   ipvsadm -L -n

 fi

;;

*)

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

;;

esac



RealServer脚本:


#!/bin/bash

#

# Script to start LVS DR real server.

# description: LVS DR real server

#

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


VIP=192.168.0.219

host=`/bin/hostname`


case "$1" in

start)

      # Start LVS-DR real server on this machine.

       /sbin/ifconfig lo down

       /sbin/ifconfig lo up

       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


       /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up

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


;;

stop)


       # Stop LVS-DR real server loopback device(s).

       /sbin/ifconfig lo:0 down

       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


;;

status)


       # Status of LVS-DR real server.

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

       isrothere=`netstat -rn | grep "lo:0" | grep $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 real server Running."

       fi

;;

*)

           # Invalid entry.

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

           exit 1

;;

esac



 补充资料:linux network bridge


 A bridge is a way to connect two Ethernet segments together in a protocol independent way. Packets are forwarded based on Ethernet address, rather than IP address (like a router). Since forwarding is done at Layer 2, all protocols can go transparently through a bridge.


The Linux bridge code implements a subset of the ANSI/IEEE 802.1d standard. The original Linux bridging was first done in Linux 2.2, then rewritten by Lennert Buytenhek. The code for bridging has been integrated into 2.4 and 2.6 kernel series.


What can be bridged?


Linux bridging is very flexible; the LAN‘s can be either traditional Ethernet device‘s, or pseudo-devices such as PPP, VPN‘s or VLAN‘s. The only restrictions are that the devices:


All devices share the same maximum packet size (MTU). The bridge doesn‘t fragment packets.

Devices must look like Ethernet. i.e have 6 byte source and destination address.

Support promiscuous operation. The bridge needs to be able to receive all network traffic, not just traffic destined for its own address.

Allow source address spoofing. The bridge must be able to send data over network as if it came from another host.


在CentOS 6.5上使用lxc虚拟机(为LVS提供轻量级实验环境):


原理:宿主机网卡变成交换机,并增加一个虚拟网卡和外部通讯


1 解决依赖关系

#service NetworkManager status   #确保NetworkManager服务关闭

#service network status  #保证network服务开启

# yum install libcgroup 

# service cgconfig start


2 提供虚拟网桥接口


在/etc/sysconfig/network-scripts目录中新建名为ifcfg-br0的配置文件,其内容如下:


DEVICE=br0

TYPE=Bridge

BOOTPROTO=static

IPADDR=172.16.100.7

NETMASK=255.255.0.0

GATEWAY=172.16.0.1

ONBOOT=yes

DELAY=0

NM_CONTROLLED=no


接下将桥接的物理网卡(假设为eth0)关联至前面定义的桥接设备,编辑/etc/sysconfig/network-script/ifcfg-eth0为类似如下内容:


DEVICE="eth0"

BOOTPROTO="static"

NM_CONTROLLED="no"

ONBOOT="yes"

TYPE="Ethernet"

BRIDGE=br0


上述步骤无误后重启network服务即可。另外,还有其它简单的方式来实现桥接设备的创建,例如使用brctl或virsh等,这里不再详述。


3 安装lxc


epel源中提供的lxc版本为0.9.0,其未带centos系统模板。因此,这里选择使用目前最新的lxc版本1.0.5。编译安装过程较为简单,按其源码目录中的INSTALL文档中的提示进行即可。我们事先经过测试后已经将lxc-1.0.5制作成了适用于centos 6 x86_64平台的rpm包(通过附件下载),因此,这里将直接使用rpm命令安装。


# yum install lxc-1.0.5-1.el6.x86_64.rpm  lxc-libs-1.0.5-1.el6.x86_64.rpm

# rpm -ql lxc

#yum install libcgroup -y

#service cgconfig stat

#chkconfig cfconfig on


4 检查lxc运行环境


# lxc-checkconfig 

Kernel configuration not found at /proc/config.gz; searching...

Kernel configuration found at /boot/config-2.6.32-431.el6.x86_64

--- Namespaces ---

Namespaces: enabled

Utsname namespace: enabled

Ipc namespace: enabled

Pid namespace: enabled

User namespace: enabled

Network namespace: enabled

Multiple /dev/pts instances: enabled


--- Control groups ---

Cgroup: enabled

Cgroup namespace: enabled

Cgroup device: enabled

Cgroup sched: enabled

Cgroup cpu account: enabled

Cgroup memory controller: enabled

Cgroup cpuset: enabled


--- Misc ---

Veth pair device: enabled

Macvlan: enabled

Vlan: enabled

File capabilities: enabled


Note : Before booting a new kernel, you can check its configuration

usage : CONFIG=/path/to/config /usr/bin/lxc-checkconfig


5 创建centos虚拟机


lxc为创建虚拟机提供了模板文件,它们位于/usr/share/lxc/templates目录中。其中的lxc-centos即为创建lxc centos系统的模板。


另外,lxc为虚拟机提供的默认配置文件为/etc/lxc/default.conf,其中使用的桥接接口名称为virbr0,此与前面的创建的接口名称不一致,因此需要作出修改。当然,也可以将此文件复制之后进行修改,并以为作为接下来的要创建的centos虚拟机的专用配置文件。修改后的default.conf如下所示。


lxc.network.type = veth

lxc.network.link = br0

lxc.network.flags = up


创建虚拟机centos:


# lxc-create -n centos -t /usr/share/lxc/templates/lxc-centos 

Host CPE ID from /etc/system-release-cpe: cpe:/o:centos:linux:6:GA

Checking cache download in /var/cache/lxc/centos/x86_64/6/rootfs ... 

…………

…………

Complete!

Download complete.

Copy /var/cache/lxc/centos/x86_64/6/rootfs to /var/lib/lxc/centos/rootfs ... 

Copying rootfs to /var/lib/lxc/centos/rootfs ...

Storing root password in ‘/var/lib/lxc/centos/tmp_root_pass‘

Expiring password for user root.

passwd: Success


Container rootfs and config have been created.

Edit the config file to check/enable networking setup.


The temporary root password is stored in:


       ‘/var/lib/lxc/centos/tmp_root_pass‘


The root password is set up as expired and will require it to be changed

at first login, which you should do as soon as possible.  If you lose the

root password or wish to change it without starting the container, you

can change it from the host by running the following command (which will

also reset the expired flag):


       chroot /var/lib/lxc/centos/rootfs passwd


上述输出内容表示系统安装已经成功,可由lxc-start命令启动了。另外,目标系统的root用户的默认密码在/var/lib/lxc/centos/tmp_root_pass文件中。


6 启动目标系统centos:


启动lxc虚拟机需要使用lxc-start命令。此命令的常用选项有

-n NAME:要启动的虚拟机的名称

-d: 在后台运行此虚拟机

-o /path/to/somefile: 日志文件的保存位置

-l: 日志级别


简单的启动命令如下所示:

# lxc-start -n centos


启动后可直接连接至目标系统的控制台,并在显示登录提示符后登录系统即可。需要注意的是,root用户的默认密码已经被设置为过期,因此第一次登录时需要修改密码后方可使用。


也可以在lxc-start的命令后面附加-d选项,让系统运行于后台。如果要停止虚拟机,使用lxc-stop命令即可。


以上是关于第三十三天 LVS基础原理调度算法NAT和DR的实现 LXC虚拟化的主要内容,如果未能解决你的问题,请参考以下文章

LVS-NAT和LVS-DR类型的工作原理及实现步骤

LVS-NAT与LVS-DR类型的负载均衡实现原理

LVS原理详解

Linux集群架构LVS介绍LVS的调度算法NAT模式搭建 DR模式keepalive

LVS专题: LVS的工作模型和调度算法介绍

Mysql基础第三十三天,改善性能