Varnishのインストールから動作確認

目次

Varnishの導入方法

試した環境

  • OS Ubuntu server 8.10
    ※ debian系OSです。
    ※ Windows上の仮想環境内(VMware)でゲストOSとして稼働させています。
    ホストOS:WindowsVista
    ゲストOS:Ubuntu

  • 前提条件 Apacheが稼働しているものとします。

  • はまりポイント
    Windowsファイアーウォールで、利用するポートを許可しておきます。
    6081、80番

インストール方法

duke@ubuntu% sudo apt-get install varnish
[sudo] password for duke:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  libvarnish0
The following NEW packages will be installed:
  libvarnish0 varnish
0 upgraded, 2 newly installed, 0 to remove and 63 not upgraded.
Need to get 194kB of archives.
After this operation, 696kB of additional disk space will be used.
Do you want to continue [Y/n]? Y
Get:1 http://jp.archive.ubuntu.com intrepid/universe libvarnish0 1.1.2 [49.2kB]
Get:2 http://jp.archive.ubuntu.com intrepid/universe varnish 1.1.2 [145kB]
Fetched 194kB in 0s (540kB/s)
Selecting previously deselected package libvarnish0.
(Reading database ... 52141 files and directories currently installed.)
Unpacking libvarnish0 (from .../libvarnish0_1.1.2_i386.deb) ...
Selecting previously deselected package varnish.
Unpacking varnish (from .../varnish_1.1.2_i386.deb) ...
Processing triggers for man-db ...
Setting up libvarnish0 (1.1.2) ...

Setting up varnish (1.1.2) ...
 * Starting HTTP accelerator
   ...done.
 * Starting HTTP accelerator log deamon
   ...done.

Processing triggers for libc6 ...
ldconfig deferred processing now taking place

コンフィグ設定

  • Varnishの設定ファイルを編集する
duke@ubuntu% sudo vi /etc/default/varnish

/etc/default/varnish

<pre class="code-xml"> 21 DAEMON_OPTS=-a :6081
 22              -T localhost:6082
 23              -b localhost:80    ## ← 8080を80にした
 24              -u varnish -g varnish
 25              -s file,/var/lib/varnish/$INSTANCE/varnish_storage.bin,1G
  • Varnishを再起動する
sudo /etc/init.d/varnish restart

実験的にVarnishを使うなら設定はこれで完了です。 URLにポート6081番を付けてアクセスすることでVarnish経由でWebサーバーに接続されます。

http://localhost:6081/
広告

ポート80番でのアクセスで運用するための設定

VMWare環境の場合です。 普段の運用(ポート80番でのアクセス)でVarnishを使うなら、VMWareのNAT設定をします。 ゲストOSの80番ポートへのアクセスを、6081番に転送します。

VMwareのメニュー->HOST-> VirtualServerSetting->NATタブクリック->Editボタンクリック
->PorForwardingボタンクリック
  • PorForwarding設定画面
TCP
Hostport 80
VirtualMachine IP Address 192.168.11.35   #ゲストOSのIPを入力する。
Port 6081   ## Varnishの待ち受けポート。Varnishの設定ファイルの「-a」オプションの値

これで下記のURL(ポート80番)でもVarnish経由でのアクセスとなります。

http://localhost/

動作確認

varnishstatコマンドでページキャッシングヒット率などの接続状態をモニタリングできます。

duke@ubuntu% varnishstat

0+00:15:47                                                                      ubuntu
Hitrate ratio:        2        2        2
Hitrate avg:     0.4754   0.4754   0.4754

           6         0.00         0.01 Client connections accepted
          62         0.00         0.07 Client requests received
          58         0.00         0.06 Cache hits      #← キャッシュヒット数
           0         0.00         0.00 Cache hits for pass
           4         0.00         0.00 Cache misses
           4         0.00         0.00 Backend connections success
           0         0.00         0.00 Backend connections failures
           1         0.00         0.00 Backend connections reuses
           4         0.00         0.00 Backend connections recycles
           0         0.00         0.00 Backend connections unused
           1          .            .   N struct srcaddr
           0          .            .   N active struct srcaddr
           2          .            .   N struct sess_mem
           1          .            .   N struct sess
           1          .            .   N struct object
           1          .            .   N struct objecthead
           3          .            .   N struct smf
           0          .            .   N small free smf
           2          .            .   N large free smf
           1          .            .   N struct vbe_conn
           1          .            .   N worker threads
           1         0.00         0.00 N worker threads created
           0         0.00         0.00 N worker threads not created
           0         0.00         0.00 N worker threads limited
           0         0.00         0.00 N queued work requests
           1         0.00         0.00 N overflowed work requests
           0         0.00         0.00 N dropped work requests
           3          .            .   N expired objects
           0          .            .   N objects on deathrow
           0         0.00         0.00 HTTP header overflows
           0         0.00         0.00 Objects sent with sendfile
           3         0.00         0.00 Objects sent with write
           6         0.00         0.01 Total Sessions
          62         0.00         0.07 Total Requests
広告

効果測定

apacheのベンチマークプログラムabを使って効果測定をしました。 abについての解説はこちら

測定条件

同時に10のリクエストが発生したときに、1000リクエストを処理するまでの性能を測定します。 同時接続数 10 総リクエスト数 1000 abのオプションは、基本的に「同時接続数」と「リクエスト数」を指定する

duke@ubuntu% ab -n 1000 -c 10 http://localhost/

測定結果

★約10倍の性能アップ

項目 Varnishあり Varnishなし
1秒間あたりの平均リクエスト処理数 (Requests per second) 1.85 13.87
1リクエストの平均処理時間 (Time per request) 5秒 0.7 秒

測定結果詳細

Varnishあり

duke@ubuntu% ab -n 1000 -c 10 http://localhost:6081/
Concurrency Level:      10
Time taken for tests:   72.112 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      4813513 bytes
HTML transferred:       4396016 bytes
Requests per second:    13.87 [#/sec] (mean)
Time per request:       721.122 [ms] (mean)
Time per request:       72.112 [ms] (mean, across all concurrent requests)
Transfer rate:          65.19 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:      152  268 101.6    261    3125
Processing:   230  450 114.6    430    2018
Waiting:      104  229  77.4    220     636
Total:        506  718 157.4    685    3554

Percentage of the requests served within a certain time (ms)
  50%    685
  66%    726
  75%    750
  80%    774
  90%    866
  95%    974
  98%   1096
  99%   1148
 100%   3554 (longest request)

Varnishなし

duke@ubuntu% ab -n 1000 -c 10 http://localhost/
Concurrency Level:      10
Time taken for tests:   539.472 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      4741000 bytes
HTML transferred:       4386000 bytes
★Requests per second:    1.85 [#/sec] (mean)
★Time per request:       5394.719 [ms] (mean)
Time per request:       539.472 [ms] (mean, across all concurrent requests)
Transfer rate:          8.58 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       27   73 138.5     46    2915
Processing:  2732 5311 560.2   5249    9467
Waiting:     2705 5269 558.8   5200    9422
Total:       2825 5384 580.5   5312    9745

Percentage of the requests served within a certain time (ms)
  50%   5312
  66%   5505
  75%   5624
  80%   5713
  90%   6020
  95%   6322
  98%   6859
  99%   7412
 100%   9745 (longest request)

以上

広告
デル株式会社