squid

squid中cache_peer的sibling用法[原创]

提高squid命中率和使用效率是每个系统管理员都希望的一件事,毕竟在现有资源中榨干每一份的资源是必须滴,使用nginx的按url做定向投递,自然能提高squid的命中和效率,但毕竟公司目前没有这个条件,这个自然没法子实施,我就想到了另外的一个方法,就是今天的主角,cache_peer中的sibling。

先说说我的思路

如果有5台squid,每台的缓存空间是10G,估计每台能缓存静态文件110W个,如果前端放置f5或其他负载均衡设备,把所有请求的url都采用轮询的方式投递给squid,那最终每台squid上缓存住的静态文件几乎都是相同的,对于一个中小型的网站来说110W量的静态文件已经几乎是所有的静态文件了,但对于以在线漫画,或者图片为服务的网站来讲,静态文件将远远不止这个数量。

如果能让每台squid上缓存的都是独立的静态文件,那这组服务器所能缓存的文件将是原来能力的5倍,当然,前面提到了nginx的按url做定向投递可以解决这个问题,但这不是我要说的重点,其实用squid自己的堆叠就可以实现了,即cache_peer里的sibling

sibling是通过icq向同伴squid发送请求,一旦在同伴squid中发现了缓存命中,则由这个缓存命中返回给客户端,如果没有发现则到parent的squid去拿或者到realserver去拿

有了这个概念那就是说如果http://www.aaa.ccc/a.jpg发送过来了,如果a上没有,就会到b上找,如果有就反给客户端,如果没有会再去c上找如果最后都没有由a到后端parent或者realserver上取

下面看一个例子,我用vmware模拟了两个linux,上面分别跑了一个squid,

a上的配置,a的ip:192.168.220.128

icp_port 3130

icp_access allow all

cache_peer 61.152.242.1 parent 80 0 no -query originserver name=www2
cache_peer_domain www2 www.tgbus.com

b上的配置,b的ip:192.168.220.130

icp_port 3130

icp_access allow all

cache_peer 192.168.220.128 sibling 80 3130 proxy -only name=cache
cache_peer 61.152.242.1 parent 80 0 no -query originserver name=www2
cache_peer_domain cache www.tgbus.com
cache_peer_domain www2 www.tgbus.com

测试

修改本机本机hosts,先把www.tgbus.com指向到192.168.220.128上访问一遍,让220.128上有一份静态的缓存
然后把www.tgbus.com指向到192.168.220.130上,然后访问,可以看下两台机器上的日志
a的

1233463229.874 0 192.168.220.130 UDP_MISS/000 42 ICP_QUERY http: / /www.tgbus.com/ - NONE / - -
1233463231.555 0 192.168.220.130 UDP_HIT/000 51 ICP_QUERY http: / /www.tgbus.com/style.css - NONE / - -
1233463231.557 0 192.168.220.130 TCP_IMS_HIT/304 210 GET http: / /www.tgbus.com/style.css - NONE / - text /css
1233463231.596 0 192.168.220.130 UDP_MISS/000 67 ICP_QUERY http: / /www.tgbus.com/index/domain/Images/3.jpg - NONE / - -
1233463231.602 0 192.168.220.130 UDP_HIT/000 59 ICP_QUERY http: / /www.tgbus.com/images/v3/lqs.jpg - NONE / - -
1233463231.603 0 192.168.220.130 UDP_MISS/000 67 ICP_QUERY http: / /www.tgbus.com/index/domain/Images/4.jpg - NONE / - -
1233463231.605 0 192.168.220.130 TCP_IMS_HIT/304 212 GET http: / /www.tgbus.com/images/v3/lqs.jpg - NONE / - image/jpeg
1233463231.609 0 192.168.220.130 UDP_HIT/000 67 ICP_QUERY http: / /www.tgbus.com/index/tvgame/images/1.jpg - NONE / - -
1233463231.612 0 192.168.220.130 TCP_IMS_HIT/304 212 GET http: / /www.tgbus.com/index/tvgame/images/1.jpg - NONE / - image/jpeg
1233463231.615 0 192.168.220.130 UDP_HIT/000 67 ICP_QUERY http: / /www.tgbus.com/index/tvgame/images/2.jpg - NONE / - -
1233463231.618 0 192.168.220.130 TCP_IMS_HIT/304 212 GET http: / /www.tgbus.com/index/tvgame/images/2.jpg - NONE / - image/jpeg
1233463231.619 0 192.168.220.130 UDP_HIT/000 67 ICP_QUERY http: / /www.tgbus.com/index/tvgame/images/4.jpg - NONE / - -
1233463231.619 0 192.168.220.130 UDP_HIT/000 58 ICP_QUERY http: / /www.tgbus.com/images/v3/mc.jpg - NONE / - -
1233463231.623 1 192.168.220.130 TCP_IMS_HIT/304 212 GET http: / /www.tgbus.com/index/tvgame/images/4.jpg - NONE / - image/jpeg
1233463231.623 1 192.168.220.130 TCP_IMS_HIT/304 212 GET http: / /www.tgbus.com/images/v3/mc.jpg - NONE / - image/jpeg

b的

1233490583.540 269 192.168.220.1 TCP_MISS/200 57811 GET http: / /www.tgbus.com/ - FIRST_UP_PARENT/www2 text /html
1233490585.753 3 192.168.220.1 TCP_MISS/304 271 GET http: / /www.tgbus.com/style.css - SIBLING_HIT/cache text /css
1233490585.845 5 192.168.220.1 TCP_MISS/304 273 GET http: / /www.tgbus.com/images/v3/lqs.jpg - SIBLING_HIT/cache image/jpeg
1233490585.857 5 192.168.220.1 TCP_MISS/304 273 GET http: / /www.tgbus.com/index/tvgame/images/1.jpg - SIBLING_HIT/cache image/jpeg
1233490585.862 4 192.168.220.1 TCP_MISS/304 273 GET http: / /www.tgbus.com/index/tvgame/images/2.jpg - SIBLING_HIT/cache image/jpeg
1233490585.868 5 192.168.220.1 TCP_MISS/304 273 GET http: / /www.tgbus.com/index/tvgame/images/4.jpg - SIBLING_HIT/cache image/jpeg
1233490585.868 5 192.168.220.1 TCP_MISS/304 273 GET http: / /www.tgbus.com/images/v3/mc.jpg - SIBLING_HIT/cache image/jpeg
1233490585.871 4 192.168.220.1 TCP_MISS/304 273 GET http: / /www.tgbus.com/index/tvgame/images/3.jpg - SIBLING_HIT/cache image/jpeg
1233490585.873 3 192.168.220.1 TCP_MISS/304 273 GET http: / /www.tgbus.com/index/tvgame/images/5.jpg - SIBLING_HIT/cache image/jpeg
1233490585.885 4 192.168.220.1 TCP_MISS/304 272 GET http: / /www.tgbus.com/Images/v3/hot.gif - SIBLING_HIT/cache image/gif
1233490585.907 3 192.168.220.1 TCP_MISS/304 272 GET http: / /www.tgbus.com/Images/v3/tvgame.gif - SIBLING_HIT/cache image/gif
1233490585.943 3 192.168.220.1 TCP_MISS/304 272 GET http: / /www.tgbus.com/Images/v3/league.gif - SIBLING_HIT/cache image/gif
1233490586.382 555 192.168.220.1 TCP_MISS/304 292 GET http: / /www.tgbus.com/index/domain/Images/3.jpg - FIRST_UP_PARENT/www2 -
1233490586.477 11 192.168.220.1 TCP_MISS/304 273 GET http: / /www.tgbus.com/index/comic/images/manhua01.jpg - SIBLING_HIT/cache image/jpeg
1233490586.479 11 192.168.220.1 TCP_MISS/304 273 GET http: / /www.tgbus.com/index/comic/images/manhua02.jpg - SIBLING_HIT/cache image/jpeg
1233490586.479 11 192.168.220.1 TCP_MISS/304 273 GET http: / /www.tgbus.com/index/comic/images/manhua03.jpg - SIBLING_HIT/cache image/jpeg

由上面可以看到,在b上tcp_miss的大部分在a上都是sibling_hit的,这样说来,我的想法还是成立的,可以达到提高squid命中与效率,增大缓存容量的作用

Related Post

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *