码迷,mamicode.com
首页 > 其他好文 > 详细

LVS NAT模型实现

时间:2014-08-21 19:36:15      阅读:333      评论:0      收藏:0      [点我收藏+]

标签:lvs ipvs ipvsadm

LVS负载均衡集群通过内核ipvs模块实现,而ipvs只是内核的一个框架本身并不能实现load banlancing 功能,要通过ipvsadm这个用户空间的工具编写规则来实现,类似于netfilter/iptables的关系。


yum install -y ipvsadm 安装

ipvsadm命令基本用法

  服务操作

    -A --add-service 添加一个服务

    -E --edit-service 修改服务

    -D --delete-service 删除服务

    -C --clear 清空服务规则

    -t --tcp-service tcp的服务

    -u --udp-service udp的服务

    -f --fwmark-service firewall mark防火墙标记的服务

    -p --persistent timeout 长连接超时时间,默认单位s,也可以指定ms

    -s --schedular 指定调度算法

    -g --gatewaying 网关即DR模型

    -i --ipip 封装ip隧道协议,即TUN模型

    -m --masquerading 地址伪装即NAT模型


  realserver操作:

    -a --add-server

    -r --real-server 指定realserver

    -w --weight

    -e --edit-server

    -d --delete-server

    

  查看操作

    -L --list

    --stats 显示统计信息

    --rate 显示速率等信息

    -c --connection 显示连接信息

    -n --numberic 数字显示,不反解IP地址和端口号


以Web服务为例配置LVS工作在NAT模型:


切记:在配置任何集群之前一定要保证所有主机时间同步!

# 后端两台realserver每3分钟和时间服务器同步时间
[root@node1 ~]# crontab -l
#Ansible: sync time
*/3 * * * * /usr/sbin/ntpdate console > /dev/null
[root@node2 ~]# crontab -l
#Ansible: sync time
*/3 * * * * /usr/sbin/ntpdate console > /dev/null

# 为方便查看负载效果,分别为两台realserver提供不同页面
[root@node1 ~]# echo ‘172.16.100.11‘ > /var/www/html/index.html 
[root@node1 ~]# curl 172.16.100.11
172.16.100.11
[root@node2 ~]# echo ‘172.16.100.12‘ > /var/www/html/index.html 
[root@node2 ~]# curl 172.16.100.12
172.16.100.12

# 添加web服务高度算法wrr,并添加两台realserver
[root@console ~]# ipvsadm -A -t 172.16.100.10:80 -s wrr
[root@console ~]# ipvsadm -a -t 172.16.100.10:80 -r 172.16.100.11 -m -w 1
[root@console ~]# ipvsadm -a -t 172.16.100.10:80 -r 172.16.100.12 -m -w 2
[root@console ~]# ipvsadm -Ln --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
  -> RemoteAddress:Port
TCP  172.16.100.10:80                    0        0        0        0        0
  -> 172.16.100.11:80                    0        0        0        0        0
  -> 172.16.100.12:80                    0        0        0        0        0
  
# 当前console上没有监听在80端口的服务
[root@console ~]# netstat -tanlp | grep 80
[root@console ~]# 

# 效果如下
[root@console ~]# curl console
172.16.100.11
[root@console ~]# curl console
172.16.100.12
[root@console ~]# curl console
172.16.100.12
[root@console ~]# curl console
172.16.100.11
[root@console ~]# curl console
172.16.100.12
[root@console ~]# curl console
172.16.100.12
[root@console ~]# curl console
172.16.100.11
[root@console ~]# curl console
172.16.100.12
[root@console ~]# curl console
172.16.100.12
[root@console ~]# ipvsadm -Ln --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
  -> RemoteAddress:Port
TCP  172.16.100.10:80                 1135     5645     5547   410925   608700
  -> 172.16.100.11:80                  378     1879     1853   136894   203204
  -> 172.16.100.12:80                  757     3766     3694   274031   405496

    

    

    

本文出自 “不懂IT的中医不是好IT” 博客,请务必保留此出处http://zhishen.blog.51cto.com/1612050/1543211

LVS NAT模型实现,布布扣,bubuko.com

LVS NAT模型实现

标签:lvs ipvs ipvsadm

原文地址:http://zhishen.blog.51cto.com/1612050/1543211

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!