Varnish的部署与使用实例(内附一键安装部署脚本github链接)

来源:互联网 发布:网络割接回退方案 编辑:程序博客网 时间:2024/04/19 14:35

Varnish的部署与使用

脚本及源码安装包链接

https://github.com/Liuhaiyuan/Varnish_Install.git
github脚本链接

GitHub

概述

  • Varnish是一款高性能且开源的反向代理服务器和http加速器
  • 与传统的Squid相比,Varnish具有性能更高,速度更快,管理更方便等诸多优点。

编译安装

这里展示脚本文件的一部分

tar -xf $INSTALL_FILEcd $CODE_DIR[ -f configure ] && ./configure --prefix=/usr/local/varnish  &> $NULL || exit 40echo "configure success."make &> $NULL[ $? -eq 0 ] && echo "make success."  || exit 41make install &> $NULL[ $? -eq 0 ] && echo "make install success." || exit 42

由于这是源码包安装,所以安装后并不能像其他服务那样会在etc下有相应的配置文件等,
虽然这些文件在源码包中都有,所以很明显我们需要将一些对应的文件进行操作。

##进行配置文件的复制操作;cp redhat/varnish.initrc /etc/init.d/varnishcp redhat/varnish.sysconfig  /etc/sysconfig/varnishcp redhat/varnish_reload_vcl /usr/bin/ln -s /usr/local/varnish/sbin/varnishd /usr/sbin/mkdir /etc/varnishcp /usr/local/varnish/etc/varnish/default.vcl /etc/varnish/uuidgen > /etc/varnish/secret

文件的具体说明:

  • /etc/varnish/ :配置文件目录
    • /etc/init.d/varnish :varnish的启动程序
    • /etc/sysconfig/varnish :配置文件,varnish定义自身属性
    • /etc/varnish/default.vcl :默认配置文件,定义后端节点的
  • /usr/bin/varnish_reload_vcl :加载vcl
  • /usr/bin/varnishadm : 客户端程序
  • /usr/bin/varnishstat :状态监控

配置文件参数说明:

[root@svr5 ~]# vim /etc/sysconfig/varnishVARNISH_VCL_CONF=/etc/varnish/default.vcl                #vcl文件路径VARNISH_LISTEN_PORT=80                                #默认端口VARNISH_SECRET_FILE=/etc/varnish/secret                #密钥文件VARNISH_STORAGE_SIZE=64M                                #缓存大小VARNISH_STORAGE="malloc,${VARNISH_STORAGE_SIZE}"        #基于内存方式[root@svr5 ~]# vim  /etc/varnish/default.vclbackend default {     .host = "192.168.4.205";   ##后端web服务器的ip     .port = "80";      ##后端web服务器的httpd使用的端口号 }

操作实例

实例需求

通过配置Varnish缓存服务器,实现如下目标:

  • 使用Varnish加速后端Apache Web服务
  • 使用varnishadm管理缓存页面
  • 使用varnishstat查看Varnish状态

具体操作

使用3台RHEL6虚拟机,其中一台Web服务器,一台Varnish代理服务器,一台作为测试用的Linux客户机。
这几台服务器都不需要配网关,客户端计算机和web服务器不能之间ping通,具体的ip地址见下文。

首先在进行web服务器的搭建,这里我们进行最简单的httpd的服务器就可以了,以便于测试。

[root@web02 ~]# ifconfig eth1eth1      Link encap:Ethernet  HWaddr 54:52:01:01:16:02            inet addr:192.168.2.16  Bcast:192.168.2.255  Mask:255.255.255.0[root@web02 ~]# yum clean all[root@web02 ~]# yum repolist...repolist: 3,819[root@web02 ~]# yum -y install httpd[root@web02 ~]# echo "This is index.html." > /var/www/html/index.html[root@web02 ~]# service httpd restart停止 httpd:                                               [失败]正在启动 httpd:httpd: apr_sockaddr_info_get() failed for web02.wolf.cnhttpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName                                                           [确定][root@web02 ~]# chkconfig httpd on//测试web服务,测试web服务正常[root@web02 ~]# curl http://192.168.2.16This is index.html.

然后进行代理服务器的配置和搭建,这里使用自己编写的脚本进行服务的安装和部署,
脚本文件和源码包都放在gethub上了,链接:

[root@proxy02 varnish]# ifconfig eth0eth0      Link encap:Ethernet  HWaddr 54:52:01:01:13:01            inet addr:192.168.4.6  Bcast:192.168.4.255  Mask:255.255.255.0[root@proxy02 varnish]# ifconfig eth1eth1      Link encap:Ethernet  HWaddr 54:52:01:01:13:02            inet addr:192.168.2.6  Bcast:192.168.2.255  Mask:255.255.255.0[root@proxy02 varnish]# lltotal 2008-rwxr-xr-x. 1 root root    2969 Jan  5 12:27 install_varnish.sh-rw-r--r--. 1 root root 2049810 Jan  5 09:47 varnish-3.0.6.tar.gz[root@proxy02 varnish]# ./install_varnish.sh configure success.make success.make install success.//对于对应的配置文件的修改,都在脚本中使用sed进行修改,//在这里就直接开启服务,并设置为开机启动即可。[root@proxy02 varnish]# service varnish restartStopping Varnish Cache:                                    [确定]Starting Varnish Cache:                                    [确定][root@proxy02 varnish]# chkconfig varnish on[root@proxy02 varnish]# chkconfig varnish --listvarnish         0:off   1:off   2:on    3:on    4:on    5:on    6:off

最后进行客户端的测试即可。

[root@client02 ~]# ifconfig eth0eth0      Link encap:Ethernet  HWaddr 54:52:01:01:15:01            inet addr:192.168.4.15  Bcast:192.168.4.255  Mask:255.255.255.0//这里就可以看到我们访问的是代理服务器的ip就可以访问web服务[root@client02 ~]# curl http://192.168.4.6This is index.html.

当对网页的信息更新频率要求很高时,就可以使用下列命令进行设置
[root@proxy02 varnish]#ln -s /usr/local/varnish/bin/* /usr/bin/
[root@proxy02 varnish]#varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082 ban.url index.html
[root@proxy02 varnish]#varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082 ban.url “.*”

附脚本全文

#!/bin/bash#LANG=en_US.UTF-8#      exit code# exit 39 yum error# exit 40 configure error# exit 41 make error# exit 42 make install errot#static variableINSTALL_FILE="varnish-3.0.6.tar.gz"CODE_DIR=$(tar -tf $INSTALL_FILE | head -1)NULL=/dev/nulltest_yum () {#set yum configure file do not display Red Hat Subscription Management info.    if [ -f /etc/yum/pluginconf.d/subscription-manager.conf ];then        sed -i '/enabled/s/1/0/' /etc/yum/pluginconf.d/subscription-manager.conf    fi    yum clean all &>$NULL    repolist=$(yum repolist 2>/dev/null |awk '/repolist:/{print $2}'|sed 's/,//')    if [ $repolist -le 0 ];then        exit 39    fi}test_yum##安装variable需要依赖包yum -y install gcc* readline-devel pcre-devel &> $NULL##添加对应用户,以该用户进行操作后续useradd -s /sbin/nologin varnishtar -xf $INSTALL_FILEcd $CODE_DIR[ -f configure ] && ./configure --prefix=/usr/local/varnish  &> $NULL || exit 40echo "configure success."make &> $NULL[ $? -eq 0 ] && echo "make success."  || exit 41make install &> $NULL[ $? -eq 0 ] && echo "make install success." || exit 42##进行配置文件的复制操作;cp redhat/varnish.initrc /etc/init.d/varnishcp redhat/varnish.sysconfig  /etc/sysconfig/varnishcp redhat/varnish_reload_vcl /usr/bin/ln -s /usr/local/varnish/sbin/varnishd /usr/sbin/mkdir /etc/varnishcp /usr/local/varnish/etc/varnish/default.vcl /etc/varnish/uuidgen > /etc/varnish/secret#最大的线程数和最小线程数受计算机的配置有关,其中什么时候增加进程,在有些配置文件会添加一个空#闲线程的参数,通过该参数进行相关的操作#VARNISH_ADMIN_LISTEN_ADDRESS 最小线程数#VARNISH_MAX_THREADS=1000 最大线程数#VARNISH_STORAGE_SIZE=64M 缓存大小,缓存大小受业务和缓存总容量影响。#VARNISH_STORAGE="malloc,${VARNISH_STORAGE_SIZE}" 使用内存缓存页面,内存大小为64M,#还可以使用硬盘进行缓存操作,不过那样速度会慢,无法体现出varnish的优势# VARNISH_VCL_CONF=/etc/varnish/default.vcl  #sed -n '/VARNISH_VCL_CONF=/p' /etc/sysconfig/varnish #VARNISH_LISTEN_PORT 默认端口,修改为httpd默认的端口sed -i '/VARNISH_LISTEN_PORT=/  s/=.*/=80/' /etc/sysconfig/varnishsed -i '/VARNISH_MIN_THREADS=/ s/=.*/=1000/' /etc/sysconfig/varnishsed -i '/VARNISH_MAX_THREADS=/ s/=.*/=10000/' /etc/sysconfig/varnishsed -i '/VARNISH_STORAGE_SIZE=/ s/=.*/=128M/' /etc/sysconfig/varnishsed -i '/VARNISH_STORAGE=/ s/=.*/="malloc,${VARNISH_STORAGE_SIZE}"/' /etc/sysconfig/varnish #修改主配置文件(定义后台服务器)default.vcl#backend default {#     .host = "192.168.2.100";#         .port = "80";#      }sed -i '/backend default/ s/.*back/back/' /etc/varnish/default.vclsed -i '/.host =/ s/.*\.host =.*/    .host = "192.168.2.16";/' /etc/varnish/default.vclsed -i '/.port =/ s/.*\.port =.*/    .port = "80";/' /etc/varnish/default.vclsed -i '/.port =/a }' /etc/varnish/default.vcl#当对网页的信息更新频率要求很高时,就可以使用下列命令进行设置#ln -s /usr/local/varnish/bin/* /usr/bin/#varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082 ban.url index.html#varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082 ban.url ".*"
1 0
原创粉丝点击