LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2047|回复: 9

LVS 负载均衡是如何计算活动连接数 ActiveConn 的?

[复制链接]
发表于 2006-3-24 15:33:13 | 显示全部楼层 |阅读模式
使用 LVS-DR 做负载均衡,但是使用 ipvsadm -L -n 所看到的 ActiveConn 值与 netstat -ant 2>&1 | wc -l 得到的值相差很大,前者是后者的数倍,比如 ActiveConn ~= 40000 时,netstat ~= 9000;ActiveConn ~=75000, netstat ~= 15000。

所以我不太明白 LVS-DR 的 ActiveConn 是如何计算的?是不是它自己有一个超时的设定,没有超时就仍然算作 ActiveConn?

前后两天,ActiveConn 的值基本相当(~40000),但网络流量却相差很大,性能表现也不同,后来在 message 日志中看到:
kernel: ip_conntrack: table full, dropping packet.
于是将 /proc/sys/net/ipv4/netfilter/ip_conntrack_max 调整到 81920(原来为 32504--各个 realserver 有些差异),性能得到改善,ActiveConn 的值也上升了,与 ip_conntrack_max 基本相当,但也并不完全一样,而且仍然有丢包,但这时 ActiveConn 并没有达到 ip_conntrack_max(~=60000-70000),而:
# wc -l /proc/net/ip_conntrack
的值显示已经到达上限(81920)了,所以也不能简单的认为 ActiveConn 和 ip_conntrack_max 有必然的联系。

而且这里使用的是 LVS direct routing 方式,返回的报文不会经过 director,所以我觉得是这里我没有完全弄明白其计算的机制,因此想向大家取个经,望不吝赐教。

谢谢
发表于 2006-3-24 16:38:00 | 显示全部楼层
lvs 是有自己的超时设置。
lvs 的并发连接数是在编译内核时确定的,redhat 的 lvs default 值比较小,不适合大并发。
不知道你用的是什么发行版?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-24 17:02:07 | 显示全部楼层
公司用 RHEL4
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-24 18:31:34 | 显示全部楼层
ipvsadm -L --timeout 可以看到这几个 timeout 设置:
Timeout (tcp tcpfin udp): 900 120 300

用 ipvsadm --set 可以进行设置。

使用 ipvsadm -lcn 可以查看 tracking hash table,可以看到 ESTABLISHED, SYN_RECV 等标志。

所以我想,如果设置 real servers 的 /proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_established 的值与 lvs tcp timeout 的值一致,LVS 连接跟踪的统计结果会更接近,但应该是 ActiveConn + InactiveConn 之和。

那么,lvs 又是以什么为标志来判断一个 ESTABLISHED 连接是 Active 还是 Inactive 的呢?
回复 支持 反对

使用道具 举报

发表于 2006-3-25 03:33:20 | 显示全部楼层
具体的机制不太清楚,但应该不只是用的TCP协议的超时机制。

--persistent [timeout] 对超时也有影响,你的lvs配置文件能贴出来么?
回复 支持 反对

使用道具 举报

发表于 2006-3-25 04:01:26 | 显示全部楼层
内核参数 IPVS connection table size 对并发影响较大。

The IPVS connection hash table uses the chaining scheme to handle
hash collisions. Using a big IPVS connection hash table will greatly
reduce conflicts when there are hundreds of thousands of connections
in the hash table.

Note the table size must be power of 2. The table size will be the
value of 2 to the your input number power. The number to choose is
from 8 to 20, the default number is 12, which means the table size
is 4096. Don't input the number too small, otherwise you will lose
performance on it. You can adapt the table size yourself, according
to your virtual server application. It is good to set the table size
not far less than the number of connections per second multiplying
average lasting time of connection in the table. For example, your
virtual server gets 200 connections per second, the connection lasts
for 200 seconds in average in the connection table, the table size
should be not far less than 200x200, it is good to set the table
size 32768 (2**15).

Another note that each connection occupies 128 bytes effectively and
each hash entry uses 8 bytes, so you can estimate how much memory is
needed for your box.
回复 支持 反对

使用道具 举报

发表于 2006-3-25 19:34:27 | 显示全部楼层
我觉得反应很快 我就没去注意= =
一直在弄heartbeat和ldirectord
其实 这个我记得在高文嵩的4篇doc里有提到吧
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-27 09:37:52 | 显示全部楼层
这个确实和 TCP 的设置没有什么关系,IPVS 有自己的内核参数,但我不太清楚如何设置。

另外,不知道 clarkent 兄所说的 "your virtual server gets 200 connections per second" 是如何判断出来的呢?

谢谢
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-27 09:47:27 | 显示全部楼层

  1. primary = aaa.bbb.ccc.dd2
  2. service = lvs
  3. backup_active = 1
  4. backup = aaa.bbb.ccc.dd3
  5. heartbeat = 1
  6. heartbeat_port = 539
  7. keepalive = 20
  8. deadtime = 30
  9. network = direct
  10. debug_level = NONE
  11. virtual server1 {
  12.         active = 1
  13.         address = aaa.bbb.ccc.dd1 eth0:1
  14.         load_monitor = none
  15.         timeout = 40
  16.         reentry = 60
  17.         protocol = tcp
  18.         port = 80
  19.         send = "GET / HTTP/1.0\r\n\r\n"
  20.         expect = "HTTP"
  21.         use_regex = 0
  22.         scheduler = wlc
  23.         server Real2 {
  24.                 address = 192.168.100.233
  25.                 active = 1
  26.                 weight = 1
  27.         }
  28.         server Real3 {
  29.                 address = 192.168.100.229
  30.                 active = 1
  31.                 weight = 4
  32.         }
  33.         server Real4 {
  34.                 address = 192.168.100.230
  35.                 active = 1
  36.                 weight = 4
  37.         }
  38.         server Real5 {
  39.                 address = 192.168.100.228
  40.                 active = 1
  41.                 weight = 4
  42.         }
  43. }
复制代码


connection tracking table 默认有4096个 hash bucket,每个 hash bucket 默认有 16 个 connection tracking record,每个 record 的大小是 128 bytes。 查找方式是:以 hash 方式得到 bucket,然后在每个 bucket 中进行顺序查找。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-31 17:27:25 | 显示全部楼层
想问一下用:
ipvsadm -lcn | grep -c SYN_RECV
得到的值与
ipvsadm -lcn | wc -l
的值在什么样的比例可以考虑是否是 SYN flood?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表