淘宝服务器架构框架图,简单实现....

这几天闲着没事从老男孩老师看到一个淘宝网的框架图,挺感兴趣的,cdn和集群线上的架构我都做过,但是没有接触过这么大的环境,先简单的实现看看,   当然了真正的淘宝架构肯定不能像我这样的,但是自己过过实验瘾也挺爽的。

陆续的把脚本贴出来。。。

脚本有不严谨的地方,请大家指出。。。。。 

脚本的ip貌似和图上都对不上,自己修改和增加吧~~~~~~~~~

 

 

 

 说实话,lvs配置是最简单,没什么好配置的,集群环境我用lvs较少,因为没有正则的功能,当然了他作为4层的东西,优势在于大流量的承载转发。

 
 
  1. mkdir /usr/local/src/lvs   
  2. cd /usr/local/src/lvs   
  3. wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz   
  4. wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz  
  5.  
  6. lsmod |grep ip_vs  
  7. uname -r  
  8. ln -s /usr/src/kernels/$(uname -r)/usr/src/linux  
  9. tar zxvf ipvsadm-1.24.tar.gz  
  10. cd ipvsadm-1.24  
  11. make && make install  
  12. tar zxvf keepalived-1.1.15.tar.gz  
  13. cd keepalived-1.1.15  
  14. ./configure&& make && make install  
  15. cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/  
  16. cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/  
  17. mkdir /etc/keepalived  
  18. cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/  
  19. cp /usr/local/sbin/keepalived /usr/sbin/  
  20. #you can service keepalived start|stop  
  21. #master  
  22.  
  23. cat >> /usr/local/etc/keepalived/keepalived.conf <<EOF 
  24.  
  25. ! Configuration File for keepalived  
  26. global_defs {  
  27.    notification_email {  
  28.         rfyiamcool@163.com  
  29.    }  
  30.    notification_email_from Alexandre.Cassen@firewall.loc  
  31.    smtp_server 127.0.0.1  
  32.    router_id LVS_DEVEL  
  33. }  
  34. vrrp_instance VI_1 {  
  35.     state MASTER   # other backup
  36.     interface eth0  
  37.     virtual_router_id 51  
  38.     priority 100    # other 90
  39.     advert_int 1  
  40.     authentication {  
  41.         auth_type PASS  
  42.         auth_pass 1111  
  43.     }  
  44.     virtual_ipaddress {  
  45.         10.10.10.88  
  46.     }  
  47. }  
  48. virtual_server 10.10.10.88 80 {  
  49.     delay_loop 6  
  50.     lb_algo rr  
  51.     lb_kind DR  
  52.     persistence_timeout 50  
  53.     protocol TCP  
  54.     real_server 10.10.10.21 80 {  
  55.         weight 3  
  56.         TCP_CHECK {  
  57.         connect_timeout 10  
  58.         nb_get_retry 3  
  59.         delay_before_retry 3  
  60.         connect_port 80  
  61.         }  
  62.     }  
  63.     real_server 10.10.10.22 80 {  
  64.         weight 3  
  65.         TCP_CHECK {  
  66.         connect_timeout 10  
  67.         nb_get_retry 3  
  68.         delay_before_retry 3  
  69.         connect_port 80  
  70.         }  
  71.     }  
  72. }  
  73.  
  74. EOF  
  75. service keepalived start 

咱们先把二层的haproxy搞定,ip什么的大家自己改吧。

 
 
  1. #!/bin/bash  
  2. cd /usr/local/src/  
  3. wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.8.tar.gz  
  4. tar zxf haproxy-1.4.8.tar.gz  
  5. cd haproxy-1.4.8  
  6. uname -a  
  7. make TARGET=linux26 PREFIX=/usr/local/haproxy  
  8. make install PREFIX=/usr/local/haproxy  
  9. cat >> /usr/local/haproxy/haproxy.cfg << EOF 
  10. global  
  11.         log 127.0.0.1   local0     ###全局日志  
  12.  
  13.         maxconn 4096           ###最大连接数  
  14.  
  15.         chroot /usr/local/haproxy  
  16.  
  17.         uid 501      ###用户ID  
  18.  
  19.         gid 501      ###组ID  
  20.  
  21.         daemon     ###后台运行  
  22.  
  23.         nbproc 1             ###创建进程数  
  24.  
  25.         pidfile /usr/local/haproxy/haproxy.pid      ###pid文件  
  26.  
  27. defaults  
  28.  
  29.         log     127.0.0.1       local3  
  30.  
  31.         mode    http           ###支持的模式  
  32.  
  33.         option httplog            ###日志格式  
  34.  
  35.         option httpclose        ###请求完成后关闭http通道  
  36.  
  37.         option dontlognull  
  38.  
  39.         option forwardfor     ###apache日志转发  
  40.  
  41.         option redispatch  
  42.  
  43.         retries 2             ###重连次数  
  44.  
  45.         maxconn 2000  
  46.  
  47.         balance roundrobin            ###算法类型  
  48.  
  49.         stats   uri     /haproxy-stats      ###状态统计页面  
  50.  
  51.         #stats   auth   admin:admin       ###状态统计页面用户名密码,可选  
  52.  
  53.         contimeout      5000            ###连接超时  
  54.  
  55.         clitimeout      50000             ###客户端超时  
  56.  
  57.         srvtimeout      50000          ###服务器超时  
  58.  
  59. listen   proxy *:80          ###访问地址及端口  
  60.  
  61.         option httpchk HEAD /index.html  HTTP/1.0           ###健康检查页面  
  62.  
  63.     server web2 10.10.10.30:88 cookie app1inst2 check inter 2000 rise 2 fall 5  
  64.     server web2 10.10.10.31:88 cookie app1inst2 check inter 2000 rise 2 fall 5  
  65.     server web2 10.10.10.32:88 cookie app1inst2 check inter 2000 rise 2 fall 5  
  66.         server web2 10.10.10.33:88 cookie app1inst2 check inter 2000 rise 2 fall 5  
  67.     server web2 10.10.10.34:88 cookie app1inst2 check inter 2000 rise 2 fall 5  
  68.     server web2 10.10.10.35:88 cookie app1inst2 check inter 2000 rise 2 fall 5  
  69. EOF  
  70. cat >> /etc/init.d/haproxy << EOF 
  71. #! /bin/sh  
  72. set -e  
  73. PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/haproxy/sbin  
  74. PROGDIR=/usr/local/haproxy  
  75. PROGNAME=haproxy 
  76. DAEMON=\$PROGDIR/sbin/\$PROGNAME  
  77. CONFIG=\$PROGDIR/\$PROGNAME.cfg  
  78. PIDFILE=\$PROGDIR/\$PROGNAME.pid  
  79. DESC="HAProxy daemon" 
  80. SCRIPTNAME=/etc/init.d/\$PROGNAME  
  81.    
  82. # Gracefully exit if the package has been removed.  
  83. test -x \$DAEMON || exit 0  
  84.    
  85. start()  
  86. {  
  87.         echo -n "Starting \$DESC: \$PROGNAME"  
  88.         \$DAEMON -f \$CONFIG  
  89.         echo "."  
  90. }  
  91.    
  92. stop()  
  93. {  
  94.         echo -n "Stopping \$DESC: \$PROGNAME"  
  95.         haproxy_pid=cat /usr/local/haproxy/haproxy.pid  
  96.         kill \$haproxy_pid  
  97.         echo "."  
  98. }  
  99.    
  100. restart()  
  101. {  
  102.         echo -n "Restarting \$DESC: \$PROGNAME"  
  103.         \$DAEMON -f \$CONFIG -p \$PIDFILE -sf \$(cat \$PIDFILE)  
  104.         echo "."  
  105. }  
  106.    
  107. case "\$1" in  
  108.   start)  
  109.         start  
  110.         ;;  
  111.   stop)  
  112.         stop  
  113.         ;;  
  114.   restart)  
  115.         restart  
  116.         ;;  
  117.   *)  
  118.         echo "Usage: \$SCRIPTNAME {start|stop|restart}" >&2  
  119.         exit 1  
  120.         ;;  
  121. esac   
  122. exit 0  
  123. EOF  
  124. chmod +x /etc/rc.d/init.d/haproxy  
  125. chkconfig --add haproxy  
  126. chmod 777 /usr/local/haproxy/haproxy.pid  
  127.  
  128. sed -i '/SYSLOGD_OPTIONS/c\SYSLOGD_OPTIONS="-r -m 0"' /etc/sysconfig/syslog  
  129. echo "local3.*        /var/log/haproxy.log" /etc/syslog.conf  
  130. echo "local0.*        /var/log/haproxy.log" /etc/syslog.conf  
  131. service syslog restart  
  132.  
  133. #启动haproxy  
  134. # /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg  
  135. #重启haproxy  
  136. # /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg -st `cat /usr/local/haproxy/haproxy.pid`   
  137. #停止haproxy  
  138. # killall haproxy  
  139. # service haproxy start restart stop 

haproxy是识别主机名的判断的 主机名的判断的例子格式如下:

 
 
  1. acl url_aaa    hdr_dom(host)    www.aaa.com 
  2. acl url_bbb    hdr_dom(host)    www.bbb.com 
  3.  
  4. acl tm_policy             hdr_dom(host) -i trade.gemini.taobao.net 
  5. acl denali_policy         hdr_reg(host) -i ^(my.gemini.taobao.net|auction1.gemini.taobao.net)$ 
  6.  
  7.  
  8. acl path_url163  path_beg  -i /163 
  9. acl path_url_bbb path_beg  -i / 
  10.  
  11.  
  12.  
  13. use_backend aaa      if url_aaa 
  14. use_backend bbb      if url_bbb 
  15. use_backend url163   if url_aaa path_url163 
  16.  
  17. backend url163 
  18.    mode http 
  19.    balance roundrobin 
  20.    option httpchk GET /163/test.jsp 
  21.    server url163 10.10.10.31:8080 cookie 1 check inter 2000 rise 3 fall 3 maxconn 50000 
  22.  
  23. backend aaa 
  24.    mode http 
  25.    balance roundrobin 
  26.    option httpchk GET /test.jsp 
  27.    srever app_8080 10.10.10.32:8080 cookie 1 check inter 1500 rise 3 fall 3 maxconn 50000 
  28.  
  29. backend bbb 
  30.     
  31.    mode http 
  32.    balance roundrobin 
  33.    option httpchk GET /test.jsp 
  34.    srever app_8080 10.10.10.33:8090 cookie 1 check inter 1500 rise 3 fall 3 maxconn 50000 

haproxy端还要做lvs客户端模式,绑定回环口。

 
 
  1. #!/bin/bash  
  2. SNS_VIP=10.10.10.88  
  3. source /etc/rc.d/init.d/functions  
  4. case "$1" in  
  5. start)  
  6.        ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP  
  7.        /sbin/route add -host $SNS_VIP dev lo:0  
  8.        echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore  
  9.        echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce  
  10.        echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore  
  11.        echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce  
  12.        echo "RealServer Start OK"  
  13.        ;;  
  14. stop)  
  15.        ifconfig lo:0 down  
  16.        route del $SNS_VIP >/dev/null 2>&1  
  17.        echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore  
  18.        echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce  
  19.        echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore  
  20.        echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce  
  21.        echo "RealServer Stoped"  
  22.        ;;  
  23. *)  
  24.        echo "Usage: $0 {start|stop}"  
  25.        exit 1  
  26. esac  
  27.    
  28. exit 0  

 下面是squid的设置

 
 
  1. #!/bin/bash  
  2. wget  http://www.squid-cache.org/Versions/v2/2.6/squid-2.6.STABLE6.tar.bz2  
  3. tar jxvf squid-2.6.STABLE6.tar.bz2  
  4. ./configure --prefix=/usr/local/squid \  
  5. --enable-async-io=320 \  
  6. --enable-storeio="aufs,diskd,ufs" \  
  7. --enable-useragent-log \  
  8. --enable-referer-log \  
  9. --enable-kill-parent-hack \  
  10. --enable-forward-log \  
  11. --enable-snmp \  
  12. --enable-cache-digests \  
  13. --enable-default-err-language=Simplify_Chinese \  
  14. --enable-epoll \  
  15. --enable-removal-policies="heap,lru" \  
  16. --enable-large-cache-files \  
  17. --disable-internal-dns \  
  18. --enable-x-accelerator-vary \  
  19. --enable-follow-x-forwarded-for \  
  20. --disable-ident-lookups \  
  21. --with-large-files \  
  22. --with-filedescriptors=65536 
  23.  
  24. cat >> /usr/local/squid/etc/squid.conf <<EOF 
  25. visible_hostname cache1.taobao.com  
  26. http_port 192.168.1.44:80 vhost vport  
  27. icp_port 0  
  28. cache_mem 512 MB  
  29. cache_swap_low 90  
  30. cache_swap_high 95  
  31. maximum_object_size 20000 KB  
  32.  
  33. maximum_object_size_in_memory 4096 KB  
  34.  
  35. cache_dir ufs /tmp1 3000 32 256  
  36.  
  37. cache_store_log none  
  38.  
  39. emulate_httpd_log on  
  40.  
  41. efresh_pattern ^ftp:           1440    20%     10080  
  42.  
  43. refresh_pattern ^gopher:        1440    0%      1440  
  44.  
  45. refresh_pattern .               0       20%     4320  
  46.  
  47. negative_ttl 5 minutes  
  48.  
  49. positive_dns_ttl 6 hours  
  50.  
  51. negative_dns_ttl 1 minute  
  52.  
  53. connect_timeout 1 minute  
  54.  
  55. read_timeout 15 minutes  
  56.  
  57. request_timeout 5 minutes  
  58.  
  59. client_lifetime 1 day  
  60.  
  61. half_closed_clients on  
  62.  
  63. maximum_single_addr_tries 1  
  64.  
  65. uri_whitespace strip  
  66.  
  67. ie_refresh off  
  68.  
  69. logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh  
  70.  
  71. pid_filename /var/log/squid/squid.pid  
  72. cache_log /var/log/squid/cache.log  
  73. access_log /var/log/squid/access.log combined  
  74.  
  75. acl all src 0.0.0.0/0.0.0.0  
  76.  
  77. acl QUERY urlpath_regex cgi-bin .php .cgi .avi .wmv .rm .ram .mpg .mpeg .zip .exe  
  78. cache deny QUERY  
  79.  
  80. acl picurl url_regex -i \.bmp$ \.png$ \.jpg$ \.gif$ \.jpeg$  
  81. acl mystie1 referer_regex -i aaa  
  82. http_access allow mystie1 picurl  
  83. acl mystie2 referer_regex -i bbb  
  84. http_access allow mystie2 picurl  
  85.  
  86. acl nullref referer_regex -i ^$  
  87. http_access allow nullref  
  88. acl hasref referer_regex -i .+  
  89. http_access deny hasref picurl  
  90.  
  91. cache_peer 192.168.1.7 parent 80 0 no-query originserver no-digest name=all 
  92. cache_peer_domain all *.taobao.com  
  93.  
  94. cache_effective_user nobody  
  95. cache_effective_group nobody  
  96.  
  97.  
  98. acl localhost src 127.0.0.1  
  99. acl my_other_proxy srcdomain .a.com  
  100. follow_x_forwarded_for allow localhost  
  101. follow_x_forwarded_for allow all   #允许转发 head ip 头  
  102. acl_uses_indirect_client on     #只有2.6才有这这个个参数  
  103. delay_pool_uses_indirect_client on  #只有2.6才有这这个个参数  
  104. log_uses_indirect_client on    # 只有2.6才有这这个个参数  
  105.  
  106. #refresh_pattern ^ftp: 60 20% 10080  
  107. #refresh_pattern ^gopher: 60 0% 1440  
  108. #refresh_pattern ^gopher: 60 0% 1440  
  109. #refresh_pattern . 0 20% 1440  
  110.      
  111. refresh_pattern -i \.js$        1440    50%     2880   reload-into-ims ignore-reload ignore-no-cache ignore-auth ignore-private     
  112. refresh_pattern -i \.html$      720     50%     1440    reload-into-ims ignore-reload ignore-no-cache ignore-auth ignore-private    
  113. refresh_pattern -i \.jpg$       1440    90%     2880    reload-into-ims ignore-reload ignore-no-cache ignore-auth ignore-private    
  114. refresh_pattern -i \.gif$       1440    90%     2880    reload-into-ims ignore-reload ignore-no-cache ignore-auth ignore-private    
  115. refresh_pattern -i \.swf$       1440    90%     2880    reload-into-ims ignore-reload ignore-no-cache ignore-auth ignore-private    
  116. refresh_pattern -i \.jpg$       1440    50%     2880    reload-into-ims ignore-reload ignore-no-cache ignore-auth ignore-private    
  117. refresh_pattern -i \.png$       1440    50%     2880     reload-into-ims ignore-reload ignore-no-cache ignore-auth ignore-private   
  118. refresh_pattern -i \.bmp$       1440    50%     2880      reload-into-ims ignore-reload ignore-no-cache ignore-auth ignore-private  
  119. refresh_pattern -i \.doc$       1440    50%     2880       reload-into-ims ignore-reload ignore-no-cache ignore-auth ignore-private  
  120. refresh_pattern -i \.ppt$       1440    50%     2880      reload-into-ims ignore-reload ignore-no-cache ignore-auth ignore-private   
  121. refresh_pattern -i \.xls$       1440    50%     2880    reload-into-ims ignore-reload ignore-no-cache ignore-auth ignore-private     
  122. refresh_pattern -i \.pdf$       1440    50%     2880   reload-into-ims ignore-reload ignore-no-cache ignore-auth ignore-private      
  123. refresh_pattern -i \.rar$       1440    50%     2880    reload-into-ims ignore-reload ignore-no-cache ignore-auth ignore-private     
  124. refresh_pattern -i \.zip$       1440    50%     2880    reload-into-ims ignore-reload ignore-no-cache ignore-auth ignore-private     
  125. refresh_pattern -i \.txt$       1440    50%     2880    reload-into-ims ignore-reload ignore-no-cache ignore-auth ignore-private
  126.  
  127. EOF  
  128. #建立缓存和日志目录,并改变权限使squid能写入  
  129. mkdir /tmp1  
  130. mkdir /var/log/squid  
  131. chown -R nobody:nobody /tmp1  
  132. chmod 666 /tmp1  
  133. chown -R nobody:nobody /var/log/squid  
  134.  
  135. #首次运行squid要先建立缓存  
  136. /usr/local/squid/sbin/squid -z  
  137.  
  138. #启动squid  
  139. echo "65535" > /proc/sys/fs/file-max  
  140. ulimit -HSn 65535  
  141. /usr/local/squid/sbin/squid 

缓存的清理脚本是从洒哥那里搞到的

只是根据洒哥的脚本很简单的延伸了下,以前那个分享的脚本可以去除域名和特定的文件格式,然后我就想了能不能去除一个网址的所有jpg    或者是 www.92hezu.com/123/bbb/ 这样的。  原来多家几个后缀,用grep过滤就ok了

qingli.sh      www.xiuxiukan.com

qingli.sh      jpg

qingli.sh       xiuxiukan.com 123  bbb  jpg

 

 
 
  1. #!/bin/sh 
  2. squidcache_path="/squidcache" 
  3. squidclient_path="/home/local/squid/bin/squidclient" 
  4. #grep -a -r $1 $squidcache_path/* | grep "http:" | awk -F 'http:' '{print "http:"$2;}' | awk -F\' '{print $1}' > cache.txt 
  5. if [[ "$1" == "swf" || "$1" == "png" || "$1" == "jpg" || "$1" == "ico" || "$1" == "gif" || "$1" == "css" || "$1" == "js" || "$1" == "html" || "$1" == "shtml" || "$1" == "htm"   ]]; then 
  6. grep -a -r .$1 $squidcache_path/* | strings | grep "http:" | awk -F 'http:' '{print "http:"$2;}' | awk -F\' '{print $1}' | grep "$1$" | uniq > cache.txt 
  7. else 
  8. grep -a -r $1 $squidcache_path/* | strings | grep "http:" |grep $2$ |grep $3$|grep $4$|grep $5$ |grep $6$| awk -F 'http:' '{print "http:"$2;}' | awk -F\' '{print $1}' | uniq > cache.txt 
  9. fi 
  10. sed -i "s/\";$//g" cache.txt 
  11. cat cache.txt | while read LINE 
  12. do 
  13. $squidclient_path -p 80 -m PURGE $LINE 
  14. done 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值