企业web高可用集群实战之haproxy篇

By:opsren

2012.6.15

本实验环境所用域名:www.opsren.com

【APP & Web架构】企业web高可用集群实战之haproxy篇(一)_第1张图片


下面是架构图:

【APP & Web架构】企业web高可用集群实战之haproxy篇(一)_第2张图片

整个实验只是详细说明架构环境的搭建,不会过多讲解各应用软件的原理性东西!

系统初使化—请参考:
http://linuxops.blog.51cto.com/2238445/841849

第一部分:harproxy+keepalived部署

在192.168.8.10和192.168.8.11上操作!!!!!!!
HAProxy是linux平台上的负载均衡软件,有完善的服务器健康检测和会话(session)保持功能,性能高,支持tcp和http网络连接分发。

下载软件:
[root@haproxy1 ~]# cd /usr/local/src
[root@haproxy1 src]# wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.19.tar.gz
[root@haproxy1 src]# wget http://keepalived.org/software/keepalived-1.2.2.tar.gz

一、安装keepalived(主备略有不同,配置中有说明)
[root@haproxy1 ~]# cd /usr/local/src
[root@haproxy1 src]# tar zxf keepalived-1.2.2.tar.gz
[root@haproxy1 src]# cd keepalived-1.2.2
[root@haproxy1 keepalived-1.2.2]# ./configure
[root@haproxy1 keepalived-1.2.2]# make
[root@haproxy1 keepalived-1.2.2]# make install

[root@haproxy1 keepalived-1.2.2]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
[root@haproxy1 keepalived-1.2.2]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@haproxy1 keepalived-1.2.2]# mkdir /etc/keepalived
[root@haproxy1 keepalived-1.2.2]# cp /usr/local/sbin/keepalived /usr/sbin/
[root@haproxy1 keepalived-1.2.2]#
[root@haproxy1 keepalived-1.2.2]# vi /etc/keepalived/keepalived.conf
加入如下内容:

 
  
  1. ! Configuration File for keepalived

  2. global_defs {

  3.   notification_email {

  4.      [email protected]

  5.   }

  6.   notification_email_from [email protected]

  7.   smtp_server smtp.163.com

  8.  # smtp_connect_timeout 30

  9.   router_id LVS_DEVEL

  10. }

  11. # VIP1

  12. vrrp_instance VI_1 {

  13.    state MASTER             #备份服务器上将MASTER改为BACKUP    

  14. interface eth0

  15.    lvs_sync_daemon_inteface eth0

  16.    virtual_router_id 51

  17.    priority 100    # 备份服务上将100改为90

  18.    advert_int 5

  19.    authentication {

  20.        auth_type PASS

  21.        auth_pass 1111

  22.    }

  23.    virtual_ipaddress {

  24.        192.168.8.12

  25.    }

  26. }

  27. virtual_server 192.168.8.12 80 {

  28.    delay_loop 6                  #(每隔10秒查询realserver状态)

  29.    lb_algo wlc                  #(lvs 算法)

  30.    lb_kind DR                  #(Direct Route)

  31.    persistence_timeout 60        #(同一IP的连接60秒内被分配到同一台realserver)

  32.    protocol TCP                #(用TCP协议检查realserver状态)

  33.    real_server 192.168.8.20 80 {

  34.        weight 100               #(权重)

  35.        TCP_CHECK {

  36.        connect_timeout 10       #(10秒无响应超时)

  37.        nb_get_retry 3

  38.        delay_before_retry 3

  39.        connect_port 80

  40.        }  

  41. real_server 192.168.8.21 80 {          

  42.       weight 100                        

  43.       TCP_CHECK {

  44.       connect_timeout 10

  45.       nb_get_retry 3          

  46.       delay_before_retry 3

  47.       connect_port 80

  48.      }

  1.    }

  2. }


二、安装haproxy(主备都一样)
[root@haproxy1 ~]# cd /usr/local/src
[root@haproxy1 src]# tar zxf haproxy-1.4.19.tar.gz
[root@haproxy1 src]# cd haproxy-1.4.19
[root@haproxy1 haproxy-1.4.19]# make TARGET=linux26 PREFIX=/usr/local/haproxy
[root@haproxy1 haproxy-1.4.19]# make install PREFIX=/usr/local/haproxy

创建配置文件
[root@haproxy1 haproxy-1.4.19]# cd /usr/local/haproxy
[root@haproxy1 haproxy]# vi haproxy.conf
加入如下内容:

 
  
  1. global

  2.        maxconn 4096

  3.        chroot /usr/local/haproxy

  4.        uid 188

  5.        gid 188

  6.        daemon

  7.        quiet

  8.        nbproc  2

  9.        pidfile /usr/local/haproxy/haproxy.pid

  10. defaults

  11.        log     global

  12.        mode    http

  13.        option  httplog

  14.        option  dontlognull

  15.        log 127.0.0.1 local3

  16.        retries 3

  17.        option redispatch

  18.        maxconn 20000

  19.        contimeout      5000

  20.        clitimeout      50000

  21.        srvtimeout      50000

  22. listen www.opsren.com 0.0.0.0:80

  23.        stats uri /status

  24.        stats realm Haproxy\ statistics

  25.        stats auth admin:admin

  26.        balance source

  27.        option httpclose

  28.        option forwardfor

  29.       #option httpchk HEAD /index.php  HTTP/1.0

  30.   server cache1_192.168.8.20 192.168.8.20:80 cookie app1inst1 check inter 2000 rise 2 fall 5

  31.   server cache2_192.168.8.21 192.168.8.21:80 cookie app1inst2 check inter 2000 rise 2 fall 5


或采用下面这种模式:

 
  
  1. global

  2.    log 127.0.0.1 local3

  3.        maxconn 4096

  4.        chroot /usr/local/haproxy

  5.        uid 188

  6.        gid 188

  7.        daemon

  8.        quiet

  9.        nbproc  2

  10.        pidfile /usr/local/haproxy/haproxy.pid

  11. defaults

  12.        log     global

  13.        mode    http

  14.        retries 3

  15.        option redispatch

  16.        maxconn 20000

  17.    stats enable

  18.    stats hide-version

  19.    stats uri /status

  20.        contimeout      5000

  21.        clitimeout      50000

  22.        srvtimeout      50000

  23. frontend www.opsren.com  

  24.    bind *:80

  25.    mode    http

  26.    option httplog  

  27.        log global

  28.    default_backend php_opsren

  29. backend php_opsren

  30.        balance source

  31.    #option httpclose

  32.        #option forwardfor

  33.   server cache1_192.168.8.20 192.168.8.20:80 cookie app1inst1 check inter 2000 rise 2 fall 5

  34.   server cache2_192.168.8.21 192.168.8.21:80 cookie app1inst2 check inter 2000 rise 2 fall 5


至于有朋友问到这两种模式有什么区别,本人暂时发现区别主要是第二种方法有以下两点好处。
1.3版本引入了frontend,backend 前后端模式;frontend根据任意 HTTP请求头内容做规则匹配,然后把请求定向到相关的backend.主要表现在以下两个方面:
1.可以利用haproxy的正则实现动静分离
2.可以根据不同类型的访问请求转发到不同的访问池:比较针对PHP和JSP的访问等

三、启动haproxy
正常启动haproxy:
[root@haproxy1 ~]# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.conf
关闭:
[root@haproxy1 ~]# pkill -9 haproxy
这样启动不够方便,我们可以设置alias
alias haproxyd=’ /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.conf’
我们也可以把它写到/root/.bashrc、/etc/bashrc中!

也可以使用启动、关闭脚本:
[root@haproxy1 ~]# cat /etc/init.d/haproxy

 
  
  1. #!/bin/bash  

  2. # chkconfig 35 on  

  3. # description: HAProxy is a TCP/HTTP reverse proxy which is particularly suited for high availability environments.  

  4. # Source function library.  

  5. if [ -f /etc/init.d/functions ]; then

  6.  . /etc/init.d/functions

  7. elif [ -f /etc/rc.d/init.d/functions ] ; then

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

  9. else

  10.  exit 0

  11. fi

  12. # Source networking configuration.  

  13. . /etc/sysconfig/network

  14. # Check that networking is up.  

  15. [ ${NETWORKING} = "no" ] && exit 0

  16. [ -f /usr/local/haproxy/haproxy.conf ] || exit 1

  17. RETVAL=0

  18. start() {

  19.  /usr/local/haproxy/sbin/haproxy -c -q -f /usr/local/haproxy/haproxy.conf

  20. if [ $? -ne 0 ]; then

  21.    echo "Errors found in configuration file."

  22. return 1

  23.  fi

  24.  echo -n "Starting HAproxy: "

  25.  daemon /usr/local/haproxy/sbin/haproxy -D -f /usr/local/haproxy/haproxy.conf -p /var/run/haproxy.pid

  26.  RETVAL=$?

  27.  echo  

  28.  [ $RETVAL -eq 0 ] && touch /var/lock/subsys/haproxy

  29. return $RETVAL

  30. }

  31. stop() {

  32.  echo -n "Shutting down HAproxy: "

  33.  killproc haproxy -USR1

  34.  RETVAL=$?

  35.  echo  

  36.  [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/haproxy

  37.  [ $RETVAL -eq 0 ] && rm -f /var/run/haproxy.pid

  38. return $RETVAL

  39. }

  40. restart() {

  41.  /usr/local/haproxy/sbin/haproxy -c -q -f /usr/local/haproxy/haproxy.conf

  42. if [ $? -ne 0 ]; then

  43.    echo "Errors found in configuration file, check it with 'haproxy check'."

  44. return 1

  45.  fi

  46.  stop

  47.  start

  48. }

  49. check() {

  50.  /usr/local/haproxy/sbin/haproxy -c -q -V -f /usr/local/haproxy/haproxy.conf

  51. }

  52. rhstatus() {

  53.  status haproxy

  54. }

  55. condrestart() {

  56.  [ -e /var/lock/subsys/haproxy ] && restart || :

  57. }

  58. # See how we were called.  

  59. case"$1"in

  60.  start)

  61.    start

  62.    ;;

  63.  stop)

  64.    stop

  65.    ;;

  66.  restart)

  67.    restart

  68.    ;;

  69.  reload)

  70.    restart

  71.    ;;

  72.  condrestart)

  73.    condrestart

  74.    ;;

  75.  status)

  76.    rhstatus

  77. ;;

  78.  check)

  79.    check

  80.    ;;

  81.  *)

  82.    echo $"Usage: haproxy {start|stop|restart|reload|condrestart|status|check}"

  83.    RETVAL=1

  84. esac

  85. exit $RETVAL


chmod +x /etc/init.d/haproxy
这样我们可以通过:/etc/init.d/haproxy start|restart|stop 来启动和关闭!
以上就是关于关闭和启动的方法,大家可以根据自己的爱好来选择!

到此,整个haproxy+keepalived架构已部署完毕!下面接着部署varnish集群架构!

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
第二部分:varnish集群部署

在192.168.8.20 和 192.168.81.21 上操作!!!

一、varnish服务器安装
安装varnish之前必须要先安装PCRE等一些依赖包
[root@varnish1 ~]# yum install -y automake autoconf libtool ncurses-devel libxslt groff pcre-devel pkgconfig

下载varnish软件(此架构用的是3.0.2最新版本)
[root@varnish1 ~]# wget http://repo.varnish-cache.org/source/varnish-3.0.2.tar.gz
[root@varnish1 ~]# tar zxvf varnish-3.0.2.tar.gz
[root@varnish1 ~]# cd varnish-3.0.2
[root@varnish1 varnish-3.0.2]# ./configure --prefix=/usr/local/varnish
[root@varnish1 varnish-3.0.2]# make; make install

二、创建配置文件
在各节点上设置hosts
[root@varnish1 ~]# vi /etc/hosts
加入如下内容:
192.168.8.30 www.opsren.com
192.168.8.31 www.opsren.com

[root@varnish1 ~]# groupadd www
[root@varnish1 ~]# useradd www -g www -s /sbin/nologin
[root@varnish1 ~]# mkdir -p /data/varnish/{cache,logs}
[root@varnish1 ~]# chmod +w /data/varnish/{cache,logs}
[root@varnish1 ~]# chown -R www:www /data/varnish/{cache,logs}
[root@varnish1 ~]# vim /usr/local/varnish/etc/varnish/vcl.conf

 
  
  1. #Cache for opsren sites

  2. #backend vhost

  3. backend  opsren1 {  

  4. .host = "192.168.8.30";  

  5. .port = "80";  

  6. }

  7. backend  opsren2 {  

  8. .host = "192.168.8.31";  

  9. .port = "80";  

  10. }  


  11. director webserver random {    

  12.   {.backend = opsren1; .weight = 5;  }  

  13.   {.backend = opsren2; .weight = 8;  }  

  14. }  


  15. #acl

  16. acl purge {  

  17. "localhost";  

  18. "127.0.0.1";  

  19. "192.168.0.0"/24;  

  20. }  

  21. sub vcl_recv {  

  22. if (req.http.Accept-Encoding) {  

  23. if (req.url ~ "\.(jpg|png|gif|jpeg|flv)$" ) {  

  24.                remove req.http.Accept-Encoding;  

  25.                remove req.http.Cookie;  

  26.            } elseif (req.http.Accept-Encoding ~ "gzip") {  

  27.                set req.http.Accept-Encoding = "gzip";  

  28.            } elseif (req.http.Accept-Encoding ~ "deflate") {  

  29.                set req.http.Accept-Encoding = "deflate";  

  30.            } else {  

  31.                remove req.http.Accept-Encoding;  

  32.            }  

  33.        }  

  34. if (req.http.host ~  "(.*)opsren.com") {  

  35.                       set req.backend = webserver;  

  36.                 }  

  37. else {  

  38.                        error 404 "This website is maintaining or not exist!";  

  39.                }  

  40. if (req.request == "PURGE") {  

  41. if (!client.ip ~purge) {  

  42.       error 405 "Not Allowed";  

  43.   }  

  44. return(lookup);  

  45.  }  

  46. if (req.request == "GET"&& req.url ~ "\.(png|gif|jpeg|jpg|ico|swf|css|js|html|htm|gz|tgz|bz2|tbz|mp3|ogg|mp4|flv|f4v|pdf)$") {  

  47.        unset req.http.cookie;  

  48.  }  

  49. if (req.request =="GET"&&req.url ~ "\.php($|\?)"){  

  50. return (pass);  

  51.  }  

  52. #     if (req.restarts == 0) {

  53. if (req.http.x-forwarded-for) {  

  54.            set req.http.X-Forwarded-For =  

  55.                req.http.X-Forwarded-For + ", " + client.ip;  

  56.        } else {  

  57.            set req.http.X-Forwarded-For = client.ip;  

  58.        }  

  59. #   }

  60. if (req.request != "GET" &&  

  61.      req.request != "HEAD" &&  

  62.      req.request != "PUT" &&  

  63.      req.request != "POST" &&  

  64.      req.request != "TRACE" &&  

  65.      req.request != "OPTIONS" &&  

  66.      req.request != "DELETE") {  

  67. return (pipe);  

  68.    }  

  69. if (req.request != "GET" && req.request != "HEAD") {  

  70. return (pass);  

  71.    }  

  72. if (req.http.Authorization) {  

  73. return (pass);  

  74.    }  

  75. return (lookup);  

  76. }

  77. sub vcl_hash {  

  78.    hash_data(req.url);  

  79. if (req.http.host) {  

  80.        hash_data(req.http.host);  

  81.    } else {  

  82.        hash_data(server.ip);  

  83.    }  

  84. return (hash);  

  85. }  

  86. sub vcl_hit {  

  87. if (req.request == "PURGE") {  

  88.       set obj.ttl = 0s;  

  89.       error 200 "Purged";  

  90.    }  

  91. return (deliver);  

  92. }  

  93. sub vcl_fetch {  

  94. if (req.url ~ "\.(jpeg|jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|ico|swf|flv|dmg|js|css|html|htm)$") {  

  95.                   set beresp.ttl = 2d;  

  96.                   set beresp.http.expires = beresp.ttl;  

  97.                   set beresp.http.Cache-Control = "max-age=172800";  

  98.                   unset beresp.http.set-cookie;  

  99.          }  

  100. if (req.url ~ "\.(dmg|js|css|html|htm)$") {  

  101.                   set beresp.do_gzip = true;  

  102.          }  

  103. if (beresp.status == 503) {  

  104.                         set beresp.saintmode = 15s;  

  105.          }  

  106. }  

  107. sub vcl_deliver {  

  108.        set resp.http.x-hits = obj.hits ;  

  109. if (obj.hits > 0) {  

  110.                set resp.http.X-Cache = "HIT You!";  

  111.        } else {  

  112.                set resp.http.X-Cache = "MISS Me!";  

  113.        }  

  114. }


以上就是配置文件!!!关于配置文件中各语句的功能请参考官方手册!

要注意的一点:必须要设置hosts解析,不然启动会出现如下错误:
[root@varnish1 varnish]# service varnish restart
Stopping varnish HTTP accelerator: Starting varnish HTTP accelerator: Message from VCC-compiler:
Backend host '"www.opsren.com"' could not be resolved to an IP address:
Name or service not known
(Sorry if that error message is gibberish.)
('input' Line 4 Pos 9)
.host = "www.opsren.com";
--------################--


In backend specification starting at:
('input' Line 3 Pos 1)
backend  opsren {
#######-----------

Running VCC-compiler failed, exit 1

VCL compilation failed

三、启动varnish
启动varnish(介绍两种方法)
第一种方法:
20服务器:
[root@varnish1 varnish]# usr/local/varnish/sbin/varnishd -u www -g www -f /usr/local/varnish/etc/varnish/vcl.conf -a 192.168.8.20:80 -s file,/data/varnish/cache/varnish_cache.data,1G -w 1024,51200,10 -t 3600 -T 192.168.8.20:3000 &

加入开机启动
[root@varnish1 varnish]# echo "/usr/local/varnish/sbin/varnishd -u www -g www -f /usr/local/varnish/etc/varnish/vcl.conf -a 192.168.8.20:80 -s file,/data/varnish/cache/varnish_cache.data,1G -w 1024,51200,10 -t 3600 -T 192.168.8.20:3000 &" >> /etc/rc.local

21服务器:
[root@varnish2 varnish]# /usr/local/varnish/sbin/varnishd -u www -g www -f /usr/local/varnish/etc/varnish/vcl.conf -a 192.168.8.21:80 -s file,/data/varnish/cache/varnish_cache.data,1G -w 1024,51200,10 -t 3600 -T 192.168.8.21:3000 &
[root@varnish2 varnish]# echo "/usr/local/varnish/sbin/varnishd -u www -g www -f /usr/local/varnish/etc/varnish/vcl.conf -a 192.168.8.21:80 -s file,/data/varnish/cache/varnish_cache.data,1G -w 1024,51200,10 -t 3600 -T 192.168.8.21:3000 &" >> /etc/rc.local

重要参数说明:
-u 指定运行用户
-g 指定运行组
-f 选项指定 varnishd 使用哪个配置文件
-a 指定 varnish 监听所有 ip 发给 80 的 http 请求
-s 选项用来确定 varnish 使用的存储类型和存储容量。1G表示指定大小为1G的缓存空间。也可以指定百分比,如 80%是指占用磁盘 80%的空间。
-w 这里指定了三个数据值,分别代表 最小,最大线程和超时时间
-T varnish管理地址和端口,主要用来清除缓存之用
-p client_http11=on 支持http1.1协议
-P(大P) /usr/local/varnish/var/varnish.pid 指定其进程码文件的位置,实现管理

启动日志,方便分析网站访问情况
[root@varnish1 varnish]# /usr/local/varnish/bin/varnishncsa -w /data/varnish/logs/varnish.log &
[root@varnish1 varnish]# echo "/usr/local/varnish/bin/varnishncsa -w /data/varnish/logs/varnish.log &" >> /etc/rc.local
参数: -w 指定varnish访问日志要写入的目录与文件

第二种方法:
我们也可以把Varnish添加到系统服务,方便日常操作!
[root@varnish1 varnish]# cat /etc/init.d/varnish

 
  
  1. # varnish Control the varnish HTTP accelerator

  2. # chkconfig: - 90 10

  3. # description: Varnish is a high-perfomance HTTP accelerator

  4. # processname: varnishd

  5. # config: /usr/local/varnish/etc/varnish.conf

  6. # pidfile: /var/run/varnishd.pid

  7. ### BEGIN INIT INFO

  8. # Provides: varnish

  9. # Required-Start: $network $local_fs $remote_fs

  10. # Required-Stop: $network $local_fs $remote_fs

  11. # Should-Start: $syslog

  12. # Short-Description: start and stop varnishd

  13. # Description: Varnish is a high-perfomance HTTP accelerator

  14. ### END INIT INFO

  15. # Source function library.

  16. start() {

  17.    echo -n "Starting varnish HTTP accelerator: "

  18.    # Open files (usually 1024, which is way too small for varnish)

  19.    ulimit -n ${NFILES:-131072}

  20.    # Varnish wants to lock shared memory log in memory.

  21.    ulimit -l ${MEMLOCK:-82000}

  22.    usr/local/varnish/sbin/varnishd -u www -g www -f /usr/local/varnish/etc/varnish/vcl.conf -a 192.168.8.20:80 -s file,/data/varnish/cache/varnish_cache.data,1G -w 1024,51200,10 -t 3600 -T 192.168.8.20:3000 &

  23.    sleep 15

  24.    /usr/local/varnish/bin/varnishncsa -w /data/varnish/logs/varnish.log &

  25. }

  26. stop() {

  27.    echo -n "Stopping varnish HTTP accelerator: "

  28.    pkill -9 varnish

  29. }

  30. restart() {

  31.    stop

  32.    start

  33. }

  34. reload() {

  35.    /etc/init.d/varnish_reload.sh

  36. }

  37. # See how we were called.

  38. case"$1"in

  39.    start)

  40.        start && exit 0

  41.        ;;

  42.    stop)

  43.        stop || exit 0

  44.        ;;

  45.    restart)

  46.        restart

  47.        ;;

  48.    reload)

  49.        reload || exit 0

  50.        ;;

  51.    *)

  52.    echo "Usage: $0 {start|stop|restart|reload}"

  53.    exit 2

  54. esac

  55. exit $?


给予可执行权限
[root@varnish1 varnish]# chmod +x /etc/init.d/varnish
添加到系统服务,开机自启动
[root@varnish1 varnish]# chkconfig --add varnish
[root@varnish1 varnish]# chkconfig varnish on
注意:发现从安装包中拷贝过来的脚本无法进行日志记录,这里是我自己定义的一个启动控制脚本!要是想使用安装包的启动控制脚本,可以这样做:
cp /root/varnish-3.0.2/redhat/varnish.initrc /etc/init.d/varnish

从安装包中复制过来的控制脚本必须要指定启动配置,配置文件实例如下:
[root@varnish1 ~] vi /usr/local/varnish/etc/varnish.conf

 
  
  1. # Configuration file for varnish

  2. # /etc/init.d/varnish expects the variable $DAEMON_OPTS to be set from this

  3. # shell script fragment.

  4. # Maximum number of open files (for ulimit -n)

  5. NFILES=131072

  6. # Locked shared memory (for ulimit -l)

  7. # Default log size is 82MB + header

  8. MEMLOCK=1000000

  9. ## Alternative 2, Configuration with VCL

  10. DAEMON_OPTS="-a 192.168.8.20:80 \

  11.             -f /usr/local/varnish/etc/varnish/vcl.conf \

  12.             -T 192.168.9.20:3000 \

  13.             -u www -g www \

  14.             -n /data/varnish/cache \

  15.             -s file,/data/varnish/cache/varnish_cache.data,1G"

用经过我修改的那脚本不用指定这个配置文件!


四、varnish平滑启动
Varnish 如果用/etc/init.d/varnish restart 重启的话,那么之前所有的缓存都会丢失,造成回源压力大,甚至源挂掉,如果我们更改了 VCL 配置,又需要生效,那么需要平滑重启。
[root@varnish1 ~]# cat /etc/init.d/varnish_reload.sh

 
  
  1. #!/bin/bash

  2. #Reload a varnish config

  3. FILE="/usr/local/varnish/etc/varnish/vcl.conf"

  4. #Hostname and management port

  5. #(defined in /etc/default/varnish or on startup) HOSTPORT="IP:6082"

  6. NOW=`date +%s`  

  7. BIN_DIR=/usr/local/varnish/bin

  8. error()

  9. {

  10.    echo 1>&2 "Failed to reload $FILE."

  11.    exit 1

  12. }

  13. $BIN_DIR/varnishadm -T $HOSTPORT vcl.load reload$NOW $FILE || error  

  14. sleep 0.1

  15. $BIN_DIR/varnishadm -T $HOSTPORT vcl.use reload$NOW || error  

  16. sleep 0.1

  17. echo Current configs:

  18. $BIN_DIR/varnishadm -T $HOSTPORT vcl.list

给予可执行权限
[root@varnish1 ~]# chmod +x /etc/init.d/varnish_reload.sh

五、varnish日志切割
[root@varnish1 ~]# vi /root/cut_varnish_log.sh

 
  
  1. #!/bin/bash

  2. logs_path=/data/varnish/logs

  3. date=$(date -d "yesterday" +"%Y-%m-%d")

  4. pkill -9 varnishncsa

  5. mkdir -p ${logs_path}/$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/

  6. mv /data/varnish/logs/varnish.log ${logs_path}/$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/varnish-${date}.log

  7. /usr/local/varnish/bin/varnishncsa -w /data/varnish/logs/varnish.log &


[root@varnish1 ~]# chmod 755 /root/cut_varnish_log.sh
使用计划任务,每天晚上凌晨00点运行日志切割脚本:
[root@varnish1 ~]#  echo "0 0 * * * /root/cut_varnish_log.sh" >> /etc/crontab

六、针对varnish内核优化
[root@varnish1 ~]# vi /etc/sysctl.conf

 
  
  1. net.ipv4.tcp_syncookies = 1

  2. net.ipv4.tcp_tw_reuse = 1

  3. net.ipv4.tcp_tw_recycle = 1

  4. #net.ipv4.tcp_fin_timeout = 30

  5. #net.ipv4.tcp_keepalive_time = 300

  6. net.ipv4.ip_local_port_range = 1024 65000

  7. net.ipv4.tcp_max_syn_backlog = 8192

  8. net.ipv4.tcp_max_tw_buckets = 5000

  9. net.ipv4.tcp_max_syn_backlog = 65536

  10. net.core.netdev_max_backlog =  32768

  11. net.core.somaxconn = 32768

  12. net.core.wmem_default = 8388608

  13. net.core.rmem_default = 8388608

  14. net.core.rmem_max = 16777216

  15. net.core.wmem_max = 16777216

  16. net.ipv4.tcp_timestamps = 0

  17. net.ipv4.tcp_synack_retries = 2

  18. net.ipv4.tcp_syn_retries = 2

  19. net.ipv4.tcp_tw_recycle = 1

  20. #net.ipv4.tcp_tw_len = 1

  21. net.ipv4.tcp_tw_reuse = 1

  22. net.ipv4.tcp_mem = 94500000 915000000 927000000

  23. net.ipv4.tcp_max_orphans = 3276800

[root@varnish1 ~]# sysctl -p
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

清除所有缓存
/usr/local/varnish/bin/varnishadm -T 192.168.9.201:3000 url.purge *$

清除p_w_picpath目录下所有缓存
/usr/local/varnish/bin/varnishadm -T 192.168.9.201:3000 url.purge /p_w_picpath/

查看Varnish服务器连接数与命中率
/usr/local/varnish/bin/varnishstat –n /data/varnish/cache/varnish_cache.data
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
到此varnish集群以部署完成!!!!!!!!
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
未完,待续。。。。。承接下篇!!!!!!!!!
http://linuxops.blog.51cto.com/2238445/899652