Varnish
Varnish官网地址: http://varnish-cache.org
Varnish 简介:
- Varnish Cache是一款高性能且开源的Web应用程序加速器,也称为缓存HTTP反向代理。您可以将其安装在任何使用HTTP的服务器之前,并将其配置为缓存内容。
- Varnish设计之初就针对现代全新的软件体系机构,和最新硬件体系紧密配合,与传统的 squid 相比,varnish 具有性能更高、速度更快、管理更加方便等诸多优点
Varnish 版本以及应用场景:
Varnish当前最新的主干版本是 5.0 ,最新稳定版是4.0。就功能讲,企业中更多的应用场景是Web缓存功能,架构中通常扮演的角色如下:
Varnish缓存策略的实现是通过VCL(Varnish Configuration Language)实现,VCL的语法简单,继承了C语言的很多特性,使得VCL样式看起来很像C和PELR语言,VCL配置文件也是通过VCL编译为C代码后继而执行,高效与生俱来。
varnish基础理论简介
varnish的程序结构
- varnish主要运行两个进程:Management进程和Child进程(也叫Cache进程)
- Management进程主要实现应用新的配置、编译VCL、监控varnish、初始化varnish以及提供一个命令行接口等。Management进程会每隔几秒钟探测一下Child进程以判断其是否正常运行,如果在指定的时长内未得到Child进程的回应,Management将会重启此Child进程
- Child进程包含多种类型的线程,常见的如:
- Acceptor线程:接收新的连接请求并响应;
- Worker线程:child进程会为每个会话启动一个worker线程,此worker线程真正来管理缓存,构建响应报文,因此,在高并发的场景中可能会出现数百个worker线程甚至更多;
- Expiry线程:从缓存中清理过期内容;
Varnish架构:
varnish日志
- varnish通过可以基于文件系统接口进行访问的共享内存区域来记录日志 ,为了与系统的其它部分进行交互,Child进程使用了可以通过文件系统接口进行访问的共享内存日志(shared memory log),因此,如果某线程需要记录信息,其仅需要持有一个锁,而后向共享内存中的某内存区域写入数据,再释放持有的锁即可。而为了减少竞争,每个worker线程都使用了日志数据缓存。
- 共享内存日志大小一般为90M,其分为两部分,前一部分为计数器,后半部分为客户端请求的数据 arnish提供了多个不同的工具如varnishlog、varnishncsa或varnishstat等来分析共享内存日志中的信息并能够以指定的方式进行显示。
- 当日志区域超过90M后,默认情况下前面的日志将会被后面的日志覆盖,如果希望保存超出90M空间限制的日志,可以开启varnishncsa服务
varnish的缓存存储机制(也就是缓存存储在哪)
- file:自管理的文件系统,使用特定的一个文件存储全部的缓存数据,并通过操作系统的mmap()系统调用将整个缓存文件映射至内存区域(如果内存大小条件允许);varnish重启时,所有缓存对象都将被清除
- malloc:使用malloc()库调用在varnish启动时向操作系统申请指定大小的内存空间以存储缓存对象;varnish重启时,所有缓存对象都将被清除
- persistent:与file的功能相同,但可以持久存储数据(即重启varnish数据时不会被清除);仍处于测试期;
vcl简介
VCL是什么
- VCL,Varnish Configuration Language 是varnish配置缓存策略的工具,它是一种基于“域”(domain specific,可想象与iptables的几个链,也就是类似钩子函数)的简单编程语言。
- ==使用VCL编写的缓存策略通常保存至.vcl文件中,其需要编译成二进制的格式后才能由varnish调用。==事实上,整个缓存策略就是由几个特定的子例程如vcl_recv、vcl_hash等组成,它们分别在不同的位置(或时间)执行,如果没有事先为某个位置自定义子例程,varnish将会执行默认的定义。
- ==VCL策略在启用前,会由management进程将其转换为C代码,而后再由gcc编译器将C代码编译成二进制程序。编译完成后,management负责将其连接至varnish实例,即child进程。==正是由于编译工作在child进程之外完成,它避免了装载错误格式VCL的风险。因此,varnish修改配置的开销非常小,其可以同时保有几份尚在引用的旧版本配置,也能够让新的配置即刻生效。
vcl的状态引擎
-
vcl_recv:接受用户请求进varnish的入口的引擎,接受到结果之后,利用return(lookup),将请求转交给vcl_hash引擎进行处理
-
vcl_hash:接受到用户请求后,对用户请求的URL进行hash计算,根据请求的首部信息,以及hash结果进行下一步处理的引擎
-
vcl_hit:经过vcl_hash引擎处理后,发现用户请求的资源本地有缓存,则vcl_hash引擎通过return(hit)将请求交给vcl_hit引擎进行处理,vcl_hit引擎处理后将请求交给vcl_deliver引擎,vcl_deliver引擎构建响应报文,响应给用户
-
vcl_miss:经过vcl_hash引擎处理后,发现用户请求的资源本地没有缓存,则vcl_hash引擎通过return(miss)将请求交给vcl_miss引擎进行处理
-
vcl_purge:经过vcl_hash引擎处理后,发现请求是对缓存的内容进行修剪时,则通过return(purge)交给vcl_purge引擎进行处理,vcl_purge引擎处理后,利用vcl_synth引擎将处理的结果告知给用户
-
vcl_pipe:经过vcl_hash引擎处理后,发现用户请求的报文varnish无法理解,则通过return(pipe),将请求交给vcl_pipe引擎,pipe引擎直接将请求交给后端真实服务器
-
vcl_pass:当请求经过vcl_hash处理后,发现请求报文不让从缓存中进行响应或其他原因没办法查询缓存,则由return(pass)或return(hit-for-pass)交由vcl_pass引擎进行处理
-
vcl_backend_fetch:当发现缓存未命中或由vcl_pass传递过来的某些不能查询缓存的请求,交由vcl_backend_fetch引擎处理,vcl_backend_fetch引擎会向后端真实web服务器发送请求报文,请求对应的资源
-
vcl_backend_response:当后端发送响应报文到varnish后,会由vcl_backend_resonse引擎进行处理,如:判断响应的内容是否可缓存,如果能缓存,则缓存下来后,交给vcl_deliver引擎,如果不能缓存,则直接交给vcl_deliver引擎,vcl_deliver引擎构建响应报文给客户端
-
varnish4.0版本的两个特殊的引擎
- vcl_init:在处理任何请求之前要执行的vcl的代码,主要用于初始化VMODS,可用在后端主机有多台时,借助此引擎完成多台主机的负载均衡效果
- vcl_fini:所有的请求都已经结束,在vcl配置被丢弃时调用;主要用于清理VMODS
因此,常见的状态引擎之间的处理流程为:
如果缓存命中: 用户请求–>vcl_recv–>vcl_hash–>vcl_hit–>vcl_deliver–>响应给用户 如果缓存未命中: 用户请求–>vcl_recv–>vcl_hash–>vcl_miss–>vcl_backend_fetch–>后端服务器接受请求发送响应报文–>vcl_backend_response–>vcl_deliver 或: 用户请求–>vcl_recv–>vcl_hash–>vcl_miss–>vcl_pass–>vcl_backend_fetch–>后端服务器接受请求发送响应报文–>vcl_backend_response–>vcl_deliver–>响应给用户 如果不能从缓存中进行响应 用户请求–>vcl_recv–>vcl_hash–>vcl_pass–>vcl_backend_fetch–>后端服务器接受请求发送响应报文–>vcl_backend_response–>vcl_deliver–>响应给用户 如果进行缓存修剪 用户请求–>vcl_recv–>vcl_hash–>vcl_purge–>vcl_synth–>返回给用户 如果请求报文无法理解 用户请求–>vcl_recv–>vcl_hash–>vcl_pipe–>交给后端服务器
常见缓存工具Varnish与Squid的优缺点分析 :
软件 | 储存模式 | 性能 | 配置复杂度 | purge效率 | 共享存储 |
---|---|---|---|---|---|
squid | 硬盘 | 较高 | 简单 | 低 | 可以并联,但是配置复杂 |
varnish | 硬盘/内存 | 高 | 比较简单 | 低 | 不能 |
Varnish核心工具集介绍 :
- varnishd varnishd
varnish的核心进程,以Daemon方式运行,接受HTTP请求,转发前端请求至后端backend,缓存返回的缓存对象并且回应请求的客户端。 - varnishtest
验证Varnish的安装 ;功能强大,可自定义client请求模型或从后端真实用品拉取内容;支持与Varnish交互性 - varnishadm ,Varnish实例命令行管理工具
start/stop Varnishd;更新配置文件参数;重载Varnish Configuration Language(VCL);查看最新的参数文档 - varnishlog ,Varnish日志展示工具
varnish的安装配置
安装:
yum install -y varnish-* jemalloc-*
安装生成的程序环境:
监听的端口为:
- 服务监听的端口默认为6081
- 管理接口默认监听的端口为6082
配置varnish服务进程的工作特性,官方提供的rpm包安装的程序,其对应的程序自身配置文件在/etc/sysconfig/varnishd,例如监听的地址和端口,缓存机制;
- /etc/varnish/varnish.params
配置各Child/Cache线程的工作属性:
- /etc/varnish/default.vcl
主程序:
- /usr/sbin/varnishd
命令行管理工具程序:
- /usr/bin/varnishadm
Shared Memory Log交互工具:
- /usr/bin/varnishhist
- /usr/bin/varnishlog
- /usr/bin/varnishncsa
- /usr/bin/varnishstat
- /usr/bin/varnishtop
测试工具程序:
- /usr/bin/varnishtest
VCL配置文件重载程序:
- /usr/sbin/varnish_reload_vcl
Systemd Unit File:
- /usr/lib/systemd/system/varnish.service
varnish服务
- /usr/lib/systemd/system/varnishlog.service
- /usr/lib/systemd/system/varnishncsa.service
配置启动环境
[root@ser1 varnish]# vim /etc/varnish/varnish.params
# 设置为1表示当使用systemctl reload varnish时,会自动重新装载vcl的配置文件,也就是能够让新的配置生效
RELOAD_VCL=1
# 主要的配置文件,加载的缓存策略的配置文件路径
VARNISH_VCL_CONF=/etc/varnish/default.vcl
# varnish服务监听的地址,默认是监听在本机所有可用的地址上
VARNISH_LISTEN_PORT=80
# varnish管理接口监听的地址,监听在127.0.0.1表示只允许从本机登录进行管理
VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1
# varnish管理接口监听的端口
VARNISH_ADMIN_LISTEN_PORT=6082
# varnish管理时的秘钥文件
VARNISH_SECRET_FILE=/etc/varnish/secret
# arnish缓存时,使用哪种存储方式对缓存内容进行存储,本处是指使用file文件方式,存在/var/lib/varnish/varnish_storage.bin文件中,总共使用1G大小的空间
# 指定 256M 内存空间大小来进行缓存
VARNISH_STORAGE="malloc,256M"
# varnishd工作进程的用户和组 ,varnishd运行时调用时读取的变量
VARNISH_USER=varnish
VARNISH_GROUP=varnish
启动服务:
- systemctl start varnish
配置优化:
[root@ser1 system]# vim /etc/security/limits.conf
...
# End of file
#
nginx - nofile 65535
varnish - nofile 131072
varnish - memlock 90000
Varnish 实例应用
Varnish 实现 负载均衡
- cd /etc/varnish/
- vim default.vcl
vcl 4.0;
import directors; # 引用函数 directors
# 后端服务器是提供内容Varnish将通过缓存加速的服务器。
# Varnish可以定义多个后端,甚至可以将多个后端连接到后端集群中以实现负载平衡,让Varnish根据不同算法选择一个后端
backend web1 { # 在Varnish中设置了一个后端服务器 web1
.host = "172.25.1.250"; # 该后端从端口80上的主机172.25.1.250 获取内容
.port = "80";
}
backend web2 { # 在Varnish中设置了一个后端服务器 web2
.host = "172.25.1.2";
.port = "80";
}
sub vcl_init { # directors 模块含四种对象 (round_robin 轮循 /fallback 回退/random 随机/hash 哈西)即 四种负载均衡模式
new web_cluster = directors.round_robin();
web_cluster.add_backend(web1);
web_cluster.add_backend(web2);
}
sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host="westos.org";
set req.backend_hint = web_cluster.backend();
return(pass);
}
elseif (req.http.host ~ "^bbs.westos.org") {
set req.backend_hint = web_cluster.backend();
return(pass);
}
else {
return(synth(404,"Not in cache"));
}
}
systemctl restart varnish
Varnish 实现 健康检查
probe backend_healthcheck { # 设置健康检查
.url = "/index.html";
.window = 3;
.threshold =2;
.interval = 3s;
}
backend web1 {
.host = "172.25.1.250";
.port = "80";
.probe = backend_healthcheck;
}
backend web2 {
.host = "172.25.1.2";
.port = "80";
.probe = backend_healthcheck;
}
测试:
# [root@ser2 sbin]# nginx -s stop 关闭 nginx 服务,只获取到 ser1 的 index.html 数据
[root@foundation1 ~]# curl bbs.westos.org
www.westos.org
westos.org
[root@foundation1 ~]# curl www.westos.org
www.westos.org
westos.org
# [root@ser2 sbin]# nginx -s start ser2 启用 nginx 服务, 后端两台服务器 ,轮询获取到 ser1 ,ser2 的 index.html 数据
[root@foundation1 ~]# curl www.westos.org
bbs.westos.org
[root@foundation1 ~]# curl www.westos.org
www.westos.org
westos.org
[root@foundation1 ~]# curl www.westos.org
bbs.westos.org
Varnish 配置实现后端服务器获取客户端 Client 的 IP
在这里插入代码片
ZABBIX
下载
rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
cd /etc/yum.repos.d/
vim zabbix.repo
yum repolist
yum install zabbix-agent zabbix-server-mysql zabbix-web zabbix-wed-mysql.noarch php-bcmath-5.4.16-46.el7.x86_64.rpm php-mbstring-5.4.16-46.el7.x86_64.rpm
rpm -qa|gerp zabbix
安装zabbix server 并启用 mysql 数据库
#yum install -y mariadb-server
# systemctl start mariadb
# mysql -p
MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@localhost identified by 'westos';
Query OK, 0 rows affected (0.00 sec)
导入数据
[root@ser5 ~]# cd /usr/share/doc/zabbix-server-mysql-4.0.23/
[root@ser5 zabbix-server-mysql-4.0.23]# ls
AUTHORS ChangeLog COPYING create.sql.gz NEWS README
[root@ser5 zabbix-server-mysql-4.0.23]# zcat create.sql.gz | mysql -uzabbix -pwestos zabbix
配置
[root@ser5 zabbix]# cd /etc/httpd/conf.d/
[root@ser5 conf.d]# ls
autoindex.conf php.conf README userdir.conf welcome.conf zabbix.conf
[root@ser5 conf.d]# vim zabbix.conf
[root@ser5 conf.d]# systemctl restart httpd
[root@ser5 ~]# vim /etc/php.ini # 设置时区
[root@ser5 ~]# systemctl restart httpd
[root@ser5 ~]# grep timezone /etc/php.ini
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = Asia/Shanghai # 设置时区
[root@ser5 ~]# rpm -qa | grep zabbix
zabbix-agent-4.0.23-1.el7.x86_64
zabbix-web-mysql-4.0.23-1.el7.noarch
zabbix-web-4.0.23-1.el7.noarch
zabbix-server-mysql-4.0.23-1.el7.x86_64
zabbix-release-4.0-2.el7.noarch
[root@ser5 zabbix-server-mysql-4.0.23]# cd /etc/zabbix/
[root@ser5 zabbix]# ls
web zabbix_agentd.conf zabbix_agentd.d zabbix_server.conf
[root@ser5 zabbix]# vim zabbix_server.conf
[root@ser5 zabbix]# grep -v ^# zabbix_server.conf | uniq
DBName=zabbix
DBUser=zabbix
DBPassword=westos
...
[root@ser5 zabbix]# systemctl start zabbix-server
[root@ser5 zabbix]# systemctl enable zabbix-agent
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.
[root@ser5 zabbix]# systemctl enable zabbix-server
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-server.service to /usr/lib/systemd/system/zabbix-server.service.
测试登陆使用:
http://172.25.1.5/zabbix/setup.php
[root@ser5 network-scripts]# cd /etc/yum.repos.d/
[root@ser5 yum.repos.d]# scp zabbix.repo ser6:/etc/yum.repo.d/
[root@ser6 yum.repos.d]# yum install -y zabbix-agent
[root@ser6 yum.repos.d]# cd /etc/zabbix/
[root@ser6 zabbix]# ls
zabbix_agentd.conf zabbix_agentd.d
[root@ser6 zabbix]# vim zabbix_agentd.conf
ServerActive=172.25.1.5 # 服务端 通过 IP连接
Hostname=ser5 # 服务端主机名
[root@ser6 zabbix]# systemctl enable --now zabbix-agent
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.