Varnish 6.2.2 的介绍与安装

一、简介

      Varnish 是一款高性能且开源的反向代理服务器和 HTTP 加速器,其采用全新的软件体系机构,和现在的硬件体系紧密配合,与传统的 Squid 相比,Varnish 具有性能更高、速度更快、管理更加方便等诸多优点。

二、Varnish 与 Squid 的对比

相同点

  • 都是一个反向代理服务器。
  • 都是开源软件。

Varnish的优势

  • Varnish 的稳定性很高。两者在完成相同负荷的工作时,Squid服务器发生故障的几率要高于Varnish,因为使用Squid要经常重启。
  • Varnish 访问速度更快。因为采用了“Visual Page Cache”技术,所有缓存数据都直接从内存读取,而squid是从硬盘读取,因而Varnish在访问速度方面会更快。
  • Varnish 可以支持更多的并发连接。因为Varnish的TCP连接释放要比Squid快,因而在高并发连接情况下可以支持更多TCP连接。
  • Varnish 可以通过管理端口,使用正则表达式批量的清除部分缓存,而Squid是做不到的。
  • Squid属于是单进程使用单核CPU,但Varnish是通过fork形式打开多进程来做处理,所以可以合理的使用所有核来处理相应的请求。

Varnish的劣势

  • Varnish进程一旦Hang、Crash或者重启,缓存数据都会从内存中完全释放,此时所有请求都会发送到后端服务器,在高并发情况下,会给后端服务器造成很大压力。
  • 在Varnish使用中如果单个url的请求通过HA/F5等负载均衡,则每次请求落在不同的varnish服务器中,造成请求都会被穿透到后端;而且同样的请求在多台服务器上缓存,也会造成varnish的缓存的资源浪费,造成性能下降。

Varnish劣势的解决方案

  • 针对劣势一:在访问量很大的情况下推荐使用varnish的内存缓存方式启动,而且后面需要跟多台squid服务器。主要为了防止前面的varnish服 务、服务器被重启的情况下,大量请求穿透varnish,这样squid可以就担当第二层CACHE,而且也弥补了varnish缓存在内存中重启都会释 、放的问题。
  • 针对劣势二:可以在负载均衡上做url哈希,让单个url请求固定请求到一台varnish服务器上。

三、Varnish 6.2.2 的安装

      Varnish 的官方链接:https://varnish-cache.org/

      Varnish 的部署文档:https://varnish-cache.org/docs/index.html#

      注意:Centos7默认yum安装版本为4.0.5,网上文档支持比较多;最新版本为6.3.0,较4.x老版本变化较大,需要参照官方文档进行学习

1、Varnish 安装前的准备

      Varnish 安装环境的准备:

  • 主机名:Varnish
  • 操作系统:CentOS Linux release 7.6.1810 (Core)
  • IP地址:192.168.1.194
[root@varnish ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[root@varnish ~]# mkdir -p /data/varnish/{etc,log} [root@varnish ~]# ll /data/varnish/ total 0 drwxr-xr-x. 2 varnish varnish 6 Nov 25 05:45 etc drwxr-xr-x. 2 varnish varnish 6 Nov 25 05:45 log

     

2、Varnish 安装前的介绍

      Varnish 是一个开源软件,可以选择安全二进制包,或者从源码定制编译安装。

      在相关操作系统上,可以使用系统自带的包管理器来安装,常见的用例:

       FreeBSD:

               varnish 5.x 以前的安装方式:

                          源码安装:cd /usr/ports/varnish && make install clean
                          二进制包安装: pkg_add -r varnish

               varnish 5.x 以后的安装方式:

                          源码安装:cd /usr/ports/www/varnish4 && make install clean
                          二进制包安装: pkg_add -r varnish4

       CentOS/RedHat:

               

# 编译安装时,所需安装的依赖包:
    yum install autoconf automake jemalloc-devel libedit-devel libtool ncurses-devel pcre-devel pkgconfig python-docutils python-sphinx

       Debian/Ubuntu:

                Varnish 已经发布了 Debian 和 Ubuntu 的软件包,只需要使用一条命令就可以安装。(注意:这样安装可能不是最新的版本)

                 apt-get  install  varnish

       Other Systems:

                建议:最好使用源码安装

3、获取 Varnish 软件

       Varnish 的官网网址:http://varnish-cache.org/

        在官网有 Varnish 的最新说明文档及版本升级记录,也可以找到 Varnish 的 SourceForge 的下载链接。

        目前,Varnish 的最新版本是 Varnish 6.3.1,此处,以 安全版本 Varnish 6.2.2 为例。

# 获取 Varnish 6.2.2 软件
[root@varnish ~]# mkdir /soft
[root@varnish ~]# cd /soft/
[root@varnish soft]# wget http://varnish-cache.org/_downloads/varnish-6.2.2.tgz

4、开始安装 Varnish

方法1:二进制包的rpm安装方式

方法2:源码包的编译安装方式

安装基础包:

yum install 
    make 
    autoconf 
    automake 
    jemalloc-devel 
    libedit-devel 
    libtool 
    ncurses-devel 
    pcre-devel 
    pkgconfig 
    python3-docutils 
    python3-sphinx
# 开始解压安装 Varnish
[root@varnish soft]# tar xzvf varnish-6.2.2.tgz
[root@varnish soft]# cd varnish-6.2.2
[root@varnish varnish-6.2.2]# ./configure --prefix=/data/varnish
[root@varnish varnish-6.2.2]# make && make install

[root@varnish varnish-6.2.2]# ll /data/varnish/
total 0
drwxr-xr-x. 2 root root 136 Nov 25 06:10 bin
drwxr-xr-x. 2 root root   6 Nov 25 06:20 etc
drwxr-xr-x. 3 root root  21 Nov 25 06:10 include
drwxr-xr-x. 4 root root 142 Nov 25 06:10 lib
drwxr-xr-x. 2 root root   6 Nov 25 06:20 log
drwxr-xr-x. 2 root root  22 Nov 25 06:10 sbin
drwxr-xr-x. 6 root root  58 Nov 25 06:10 share
drwxr-xr-x. 3 root root  21 Nov 25 06:10 var


# 配置环境变量
[root@varnish ~]# echo 'export PATH=$PATH:/data/varnish/sbin:/data/varnish/bin' >> /etc/profile
[root@varnish ~]# source /etc/profile
[root@varnish ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/data/varnish/sbin:/data/varnish/bin

# 查看 Varnish 的版本
[root@varnish varnish-6.2.2]# varnishd -V
varnishd (varnish-6.2.2 revision 3ed1506895ecaddb91f658bee11742f0b0b982b5)
Copyright (c) 2006 Verdens Gang AS
Copyright (c) 2006-2019 Varnish Software AS
# 拷贝配置文件
[root@varnish varnish-6.2.2]# cp /data/varnish/share/doc/varnish/example.vcl  /data/varnish/etc/default.vcl
[root@varnish varnish
-6.2.2]# cat /data/varnish/etc/default.vcl # # This is an example VCL file for Varnish. # # It does not do anything by default, delegating control to the # builtin VCL. The builtin VCL is called when there is no explicit # return statement. # # See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/ # and https://www.varnish-cache.org/trac/wiki/VCLExamples for more examples. # Marker to tell the VCL compiler that this VCL has been adapted to the # new 4.0 format. vcl 4.0; # Default backend definition. Set this to point to your content server. backend default { .host = "127.0.0.1"; .port = "8080"; } sub vcl_recv { # Happens before we check if we have this in cache already. # # Typically you clean up the request here, removing cookies you don't need, # rewriting the request, etc. } sub vcl_backend_response { # Happens after we have read the response headers from the backend. # # Here you clean the response headers, removing silly Set-Cookie headers # and other mistakes your backend does. } sub vcl_deliver { # Happens when we have all the pieces we need, and are about to send the # response to the client. # # You can do accounting or modifying the final object here. }
原文地址:https://www.cnblogs.com/morgan363/p/11929541.html