运维概述、Squid代理服务器、Varnish服务器

###################################################

环境准备:

-----------------------------------------------------------------------

Client:  eth0(192.168.4.100)    4网段

-----------------------------------------------------------------------

Proxy:   eth0(192.168.4.5)           4网段

       eth1(192.168.2.5)           2网段

-------------------------------------------------------------------------

Web1:  eth1(192.168.2.100)          2网段

关闭防火墙

##################################

一、

运行&维护   opration&maintance

IT运维、业务运维、日常管理运维

Linux运维最佳实践

集中化:ansiblesaltstack

自动化:shellpython

标准化:

虚拟化:kvmvmware

分布式:

squid 缓存页面是在硬盘上/var/spool/squid/

squid传统,资料多,排错,正向,反向,透明代理

varnish 缓存数据在硬盘或内存,反向代理,http加速

源码安装软件:

1.tar -xf 解压

2. cd ; ./configure ; make ;make install

备注:解决依赖关系

3.作软链接或者修改PATH路径

4.cp提供一个启动服务

5.cp配置文件

##################################

二、代理服务器概述

Proxy,代理人、经纪人、中介

代替用户处理事务

- 能提供正常情况下不便或无法访问的资源

应用场景:

- web访问加速(正/反向)

- IP伪装、FQ

代理服务器工作机制

- Cache池的复用

- 线路转移

##################################

三、Squid代理服务器

Squid服务器(缓存服务器)(代理软件)

软件包:squid-3.38-26.el7.x86_64.rpm

系统服务:squid

主程序:/usr/sbin/squid

主配置文件:/etc/squid/squid.conf

默认监听端口:TCP 3128

默认访问日志:/var/log/squid/access.log

ufs 存储格式   

思路:

1.搭建一个源站点  httpd

2.各个地区搭建缓存服务器   squid   你是谁?80 vhost(注意关闭httpd

源是谁?cacge_peer 2.100如何加速?cache_dir  /var/spool/squid

3.用户访问缓存服务器   curl firefoxelinks

4.DNS分离解析nslookup

优势:功能全、历史悠久;负载均衡、访问速率提高

##################################

反向代理:

步骤一:构建web服务器

Web1: eth1(192.168.2.100)

[root@web ~]# yum  -y  install  httpd

[root@web ~]# rpm  -q  httpd

[root@web ~]# systemctl start httpd  ;  systemctl enable httpd

[root@web ~]# netstat  -anptu  |  grep httpd

[root@web ~]#vim  /var/www/html/index.html

<html>

<title>Welcome</title>

<body><h1>wodewangzhishi192.168.4.100</h1>nihao</body>

</html>

[root@web ~]# tail /var/log/httpd/access_log

步骤二:部署Squid代理服务器

Proxy:   eth0(192.168.4.5) eth1(192.168.2.5)

[root@svr5 ~]# yum  -y  install  squid

[root@svr5 ~]# vim /etc/squid/squid.conf 修改配置文件

http_access allow all          //允许本机所有主机使用代理服务器---56

http_port 80  vhost            //设置反向代理  ---伪装成80服务 ---59

visible_hostname svr5.tarena.com                //设置主机名   ---随意

cache_peer  192.168.2.100  parent 80   0  originserver    //定义后端真实服务器信息   ---后台2.100  0-icp不用  --源服务

cache_dir ufs   /var/spool/squid 200 16  128        //硬盘缓存 --目录空间(MB)默认100、一级子目录数默认16,二级子目录数默认256

[root@svr5 ~]# systemctl start squidsystemctl enable squid

[root@svr5 ~]# netstat  -anptu  |  grep 80    //

步骤三:客户端测试

Client:  eth0(192.168.4.100)

[root@client ~]# cat /etc/hosts   

//配置域名解析,将域名解析为Squid服务器IP地址

192.168.4.5    www.tarena.com

[root@client ~]# firefox   http://www.tarena.com

[root@client ~]# curl   http://192.168.4.5

##################################

[root@client ~]# curl -I  http://192.168.4.5

(浏览器的选项-开发者-网络)

X-Cache:”MISS/HIT  from  chen.tedu.com”

miss缓存里没找到,缓存服务器会到后台找到并缓存(一般在第一次访问一个页面时),hit 有缓存访问; 用户都能看到页面  

curl 基于命令行的浏览器

##################################

四、Varnish服务器

Varnish(反向代理软件):高性能且开源的反向代理服务器和http加速器

优点:性能更好,管理更方便、更实惠,速度更快,缓存数据可以在内存里,内存里速度比比硬盘快很多

(注意关闭httpdsquid

[root@proxy ~]# systemctl stop squid

[root@proxy ~]# netstat -antpu | grep 80

程序program硬盘 ;进程proccess 内存 占内存相互独立 ; 线程thread 不占内存但相互关联

VARNISH_MIN_THREADS=50

malloc  分配内存

/etc/init.d/varnish    #varnish启动程序

/etc/sysconfig/varnish  前端配置--定义varnish自身属性

/etc/varnish/default.vcl后端配置--默认配置文件,定义后端节点

##################################

步骤一:构建Web服务器

步骤二:部署Varnish缓存服务器

源码包

scp  -r  /root/桌面/lnmp_soft-2017-03-28.tar.gz  192.168.4.5:/opt/

tar -xf /opt/lnmp_soft-2017-03-28.tar.gz -C /opt/

cd /opt/lnmp_soft/

./install_lnmp.sh

1. 编译安装

#yum -y install  gcc  readline-devel  pcre-devel

#useradd -s /sbin/nologin  varnish

#tar -xf varnish-3.0.6.tar.gz

#cd varnish-3.0.6/

# ./configure --prefix=/usr/local/varnish

#make  && make  install

2.复制启动脚本及配置文件

# cp redhat/varnish.initrc    /etc/init.d/varnish

#cp  redhat/varnish.sysconfig    /etc/sysconfig/varnish

#cp redhat/varnish_reload_vcl  /usr/bin

#ln -s /usr/local/varnish/sbin/varnishd   /usr/sbin/

#ln -s /usr/local/varnish/bin/*    /usr/bin/

#mkdir  /etc/varnish

# cp /usr/local/varnish/etc/varnish/default.vcl   /etc/varnish/

#uuidgen > /etc/varnish/secret  /改密码

3.修改Varnish文件

    [root@svr5 ~]# vim /etc/sysconfig/varnish

    VARNISH_VCL_CONF=/etc/varnish/default.vcl          #vcl文件路径---60

    VARNISH_LISTEN_PORT=80                                #默认端口---66

    VARNISH_SECRET_FILE=/etc/varnish/secret                #密钥文件---73

    VARNISH_STORAGE_SIZE=64M                                #缓存大小---89

    VARNISH_STORAGE="malloc,${VARNISH_STORAGE_SIZE}"        #基于内存方式缓存(默认文件缓存)---92

4.修改代理配置文件

  [root@svr5 ~]# vim  /etc/varnish/default.vcl

    backend default {

         .host = "192.168.2.100";                 #后端webIP

         .port = "80";                #后端端口号

     }

  [root@svr5 ~]#/etc/init.d/varnish   start

  [root@svr5 ~]#netstat -antpu | grep 80

步骤三:客户端测试

[root@client ~]# firefox http://www.tarena.com

日志文件

[root@svr5 ~]#varnishlog  本身日志

[root@svr5 ~]#varnishncsa  访问日志

步骤更新缓存

[root@svr5 ~]#varnishadm  -S   /etc/varnish/secret   -T   127.0.0.1:6082    

varnish>help

varnish> ban.url  3.html   页面文件    ---支持正则       //清缓存

#varnishadmin  backend.list

#varnishadmin  storage.list

 

Top 

NSD OPERATION DAY01

案例1:反向代理 

案例2:使用Varnish加速Web 

1 案例1:反向代理

1.1 问题

通过配置代理服务器,实现以下目标:

代理服务器可以将远程的Web服务器页面缓存在本地

代理服务器端口设置为80端口

用户通过访问代理服务器即可获得远程Web服务器上的页面内容

远程Web服务器对客户端用户是透明的

利用缓存机制提高网站的响应速度

1.2 方案

使用3台RHEL7虚拟机,其中一台作为Squid代理服务器,该服务器用来连接两个网段,因此需要配置两块网卡,地址分别为 192.168.4.5和192.168.2.5。一台作为客户端测试主机,IP地址为192.168.4.100。一台Web服务器,地址为 192.168.2.100,该Web服务器为其他代理提供Web数据源,拓扑如图-1所示。

-1

1.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:构建web服务器

1)使用yum安装web软件包

1 [root@web ~]# yum  -y  install  httpd

2 .. ..

3 [root@web ~]# rpm  -q  httpd

4 httpd-2.4.6-40.el7.x86_64

2)启用httpd服务,并设为开机自动运行

1 [root@web ~]# systemctl start httpd  ;  systemctl enable httpd

httpd服务默认通过TCP 80端口监听客户端请求:

1 [root@web ~]# netstat  -anptu  |  grep httpd

2 tcp        0        0        :::80        :::*        LISTEN        2813/httpd

3)为Web访问建立测试文件

在网站根目录/var/www/html下创建一个名为index.html的首页文件:

1 [root@web ~]# cat /var/www/html/index.html

2 <html>

3 <title>Welcome</title>

4 <body>

5 <h1>www.tarena.com</h1>

6 </body>

7 </html>

步骤二:部署Squid代理服务器

1)使用yum安装squid软件包:

1 [root@svr5 ~]# yum  -y  install  squid

2 .. ..

3 [root@svr5 ~]# rpm  -q  squid

4 squid-3.3.8-26.el7.x86_64

2)修改/etc/squid/squid.conf配置文件:

1 [root@svr5 ~]# vim /etc/squid/squid.conf

2 .. ..

3 http_port 80 vhost                            //设置反向代理  ---伪装成80---59

4 visible_hostname svr5.tarena.com                //设置主机名   ---随意

5 cache_peer  192.168.2.100  parent 80   0  originserver    //定义后端真实服务器信息   ---后台2.100  0-icp不用  --源服务

6 cache_mem 128 MB                                //内存缓存

7 cache_dir ufs   /var/spool/squid 200 16 128        //硬盘缓存 --

8 http_access allow all                             //允许本机所有主机使用代理服务器---56

3)启动squid服务,并设置为开机启动:

1 [root@svr5 ~]# systemctl start squidsystemctl enable squid

4)squid服务通过TCP 80端口监听客户端请求:

1 [root@svr5 ~]# netstat  -anptu  |  grep 80

2 tcp        0        0        :::80        :::*        LISTEN        3213/(squid)

步骤三:客户端测试

通过/etc/hosts文件,配置域名解析,将域名解析为Squid服务器IP地址

1 [root@client ~]# cat /etc/hosts

2 .. ..

3 192.168.4.5    www.tarena.com

2)客户端开启浏览器,分别访问两个不同的域名站点

1 [root@client ~]# firefox http://www.tarena.com

2 [root@client ~]# curl   http://192.168.4.5  ---访问的时4.100的页面

2 案例2:使用Varnish加速Web

2.1 问题

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

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

2.2 方案

通过源码编译安装Varnish缓存服务器

  • 编译安装Varnish软件
  • 复制启动脚本与配置文件

修改配置文件,缓存代理源Web服务器,实现Web加速功能

使用3台RHEL7虚拟机,其中一台作为Web服务器(192.168.2.100)、一台作为Varnish代理服务器(192.168.4.5,192.168.2.5),另外一台作为测试用的Linux客户机(192.168.2.100),如图-2所示。

-2

对于Web服务器的部署,此实验中仅需要安装httpd软件、启动服务,并生成测试首页文件即可,默认httpd网站根路径为/var/www/html,首页文档名称为index.html。

2.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:构建Web服务器

1)使用yum安装web软件包

1 [root@web1 ~]# yum  -y  install  httpd

2 .. ..

3 [root@web1 ~]# rpm  -q  httpd

4 httpd-2.4.6-40.el7.x86_64

2)启用httpd服务,并设为开机自动运行

1 [root@web1 ~]# systemctl start httpd  ;  systemctl enable httpd

httpd服务默认通过TCP 80端口监听客户端请求:

1 [root@pc205 ~]# netstat  -anptu  |  grep httpd

2 tcp        0        0        :::80        :::*        LISTEN        2813/httpd

3)为Web访问建立测试文件

在网站根目录/var/www/html下创建一个名为index.html的首页文件:

1 [root@pc205 ~]# cat /var/www/html/index.html

2 <html>

3 <title>Welcome</title>

4 <body>

5 <h1>hello</h1>

6 <h2>hello</h2>

7 </body>

8 </html>

步骤二:部署Varnish缓存服务器

1)编译安装软件

1 [root@svr5 ~]# yum -y install readline-devel pcre-devel    //安装软件依赖包

2 [root@svr5 ~]# useradd -s /sbin/nologin varnish                //创建账户

3 [root@svr5 ~]# tar -xzf varnish-3.0.6.tar.gz

4 [root@svr5 ~]# cd varnish-3.0.6

5 [root@svr5 varnish-3.0.6]# ./configure --prefix=/usr/local/varnish

6 [root@svr5 varnish-3.0.6]# make && make install

2)复制启动脚本及配置文件

1 [root@svr5 varnish-3.0.6]# cp redhat/varnish.initrc /etc/init.d/varnish

2 [root@svr5 varnish-3.0.6]# cp redhat/varnish.sysconfig /etc/sysconfig/varnish

3 [root@svr5 varnish-3.0.6]# ln -s /usr/local/varnish/sbin/varnishd /usr/sbin/

4 [root@svr5 varnish-3.0.6]# ln -s /usr/local/varnish/bin/* /usr/bin/

3)修改Varnish文件

1 [root@svr5 ~]# vim /etc/sysconfig/varnish

2 VARNISH_VCL_CONF=/etc/varnish/default.vcl                #vcl文件路径

3 VARNISH_LISTEN_PORT=80                                #默认端口

4 VARNISH_SECRET_FILE=/etc/varnish/secret                #密钥文件

5 VARNISH_STORAGE_SIZE=64M                                #缓存大小

6 VARNISH_STORAGE="malloc,${VARNISH_STORAGE_SIZE}"        #基于内存方式

4)修改代理配置文件

1 [root@svr5 ~]# mkdir /etc/varnish

2 [root@svr5 ~]# cp /usr/local/varnish/etc/default.vcl /etc/varnish/

3 [root@svr5 ~]# uuidgen > /etc/varnish/secret

4 [root@svr5 ~]# vim  /etc/varnish/default.vcl

5 backend default {

6      .host = "192.168.2.100";

7      .port = "80";

8  }

9 [root@svr5 ~]# service  varnish  start

步骤三:客户端测试

1)通过/etc/hosts文件,配置域名解析,将域名解析为Squid服务器IP地址

1 [root@client ~]# cat /etc/hosts

2 .. ..

3 192.168.4.5    www.tarena.com

2)客户端开启浏览器,分别访问两个不同的域名站点

1 [root@client ~]# firefox http://www.tarena.com

 

猜你喜欢

转载自www.cnblogs.com/fuzhongfaya/p/8952584.html