<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Web IT Services</title>
    <description>Solid knowledge about web and web-technologies. Focused on Back-End, Front-End and DevOps topics. Docker, Python, Nginx, Linux, IT-Architecture, Vagrant, Ansible, MongoDB and Elasticsearch.
</description>
    <link>http://czerasz.com/</link>
    <atom:link href="http://czerasz.com/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sat, 05 Sep 2015 09:14:37 +0200</pubDate>
    <lastBuildDate>Sat, 05 Sep 2015 09:14:37 +0200</lastBuildDate>
    <generator>Jekyll v2.4.0</generator>
    
      <item>
        <title>WRK the HTTP benchmarking tool - Advanced Example</title>
        <description>&lt;p class=&quot;post__tldr&quot; itemprop=&quot;description&quot;&gt;wrk is the HTTP benchmarking tool which combined with Lua scripts can be the bazooka in your benchmarking arsenal. This article shows an advanced example of using Lua scripts with wrk. I will also show you how to debug wrk.&lt;/p&gt;

&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;Recently I published an &lt;a href=&quot;https://www.digitalocean.com/community/tutorials/how-to-benchmark-http-latency-with-wrk-on-ubuntu-14-04&quot;&gt;article on Digital Ocean&lt;/a&gt; which is an introduction to a great HTTP benchmarking tool called &lt;a href=&quot;https://github.com/wg/wrk&quot;&gt;wrk&lt;/a&gt;. It describes the basic concepts and shows usage examples. I recommend you read it before continuing with this post.&lt;/p&gt;

&lt;p&gt;And after you are back we will focus on a more advanced example.&lt;/p&gt;

&lt;h2 id=&quot;requirements&quot;&gt;Requirements&lt;/h2&gt;

&lt;p&gt;For some time now I use Docker instead of virtual machines when possible.&lt;/p&gt;

&lt;p&gt;I would like to share the Docker awesomeness with you, so please make sure you have the following tools installed:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.docker.com/installation/&quot;&gt;Docker&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://docs.docker.com/compose/install/&quot;&gt;Docker Compose&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;json-file-example&quot;&gt;JSON File Example&lt;/h2&gt;

&lt;p&gt;The idea is very simple. We have an JSON file with request details and wrk will use this information in its benchmark. wrk doesn’t have this feature build in but we will use a Lua script to tell it what to do.&lt;/p&gt;

&lt;p&gt;The JSON file might look like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;quot;path&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;/path-1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;quot;body&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;some content&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;quot;method&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;GET&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;quot;headers&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;quot;X-Custom-Header-1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;test 1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;quot;X-Custom-Header-2&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;test 2&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;quot;path&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;/path-2&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;quot;body&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;some content&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;quot;method&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;POST&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;quot;headers&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;quot;X-Custom-Header-1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;test 3&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;quot;X-Custom-Header-2&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;test 4&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;As you can see each request has different properties.&lt;/p&gt;

&lt;p&gt;Save it in the &lt;code&gt;data&lt;/code&gt; directory as &lt;code&gt;data/requests.json&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now we need a proper lua script.&lt;/p&gt;

&lt;p&gt;I prepared one below, save it as &lt;code&gt;scripts/multi-request-json.lua&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-lua&quot; data-lang=&quot;lua&quot;&gt;&lt;span class=&quot;c1&quot;&gt;-- Module instantiation&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cjson&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;cjson&amp;quot;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cjson2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cjson&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cjson_safe&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;cjson.safe&amp;quot;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;-- Initialize the pseudo random number generator&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;-- Resource: http://lua-users.org/wiki/MathLibraryTutorial&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;math.randomseed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;os.time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;math.random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;math.random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;math.random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;-- Shuffle array&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;-- Returns a randomly shuffled array&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;shuffle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paths&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paths&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;math.random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;math.random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;paths&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paths&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paths&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paths&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paths&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;-- Load URL paths from the file&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;load_request_objects_from_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;-- Check if the file exists&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;-- Resource: http://stackoverflow.com/a/4991602/325852&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;io.open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;r&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;~=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;content&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;*all&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;nb&quot;&gt;io.close&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;-- Return the empty array&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;-- Translate Lua value to/from JSON&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cjson&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shuffle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;-- Load URL requests from file&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;requests&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_request_objects_from_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/data/requests.json&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;-- Check if at least one path was found in the file&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;requests&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;multiplerequests: No requests found.&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;os.exit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;multiplerequests: Found &amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;..&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;requests&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;..&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; requests&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;-- Initialize the requests array iterator&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;counter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;-- Get the next requests array element&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request_object&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;requests&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;-- Increment the counter&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;counter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;counter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;-- If the counter is longer than the requests array length then reset it&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;counter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;requests&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;counter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;-- Return the request object with the current URL path&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wrk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;request_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;headers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This script is similar to the example presented in the &lt;a href=&quot;https://www.digitalocean.com/community/tutorials/how-to-benchmark-http-latency-with-wrk-on-ubuntu-14-04#&quot;&gt;Digital Ocean artilce&lt;/a&gt;. It is extended with an shuffle functionality and instead of loading URL paths line by line we laod a JSON file. Parsing JSON requires a JSON library, which is loaded at the very top. This library transforms the request details from a JSON string into an Lua array.&lt;/p&gt;

&lt;p&gt;The only thing that stops us from testing this example is the missing JSON library itself - we need to install it. To save time for the installation and setup, I prepared a Docker image, which is &lt;a href=&quot;https://registry.hub.docker.com/u/czerasz/wrk-json/&quot;&gt;available here&lt;/a&gt;. If you are interested in how the library is installed simply take a look at the &lt;a href=&quot;https://github.com/czerasz/docker-wrk-json/blob/master/Dockerfile&quot;&gt;&lt;code&gt;Dockerfile&lt;/code&gt;&lt;/a&gt; - it’s well documented.&lt;/p&gt;

&lt;p&gt;Let’s summarise: you need to save the JSON file as &lt;code&gt;data/requests.json&lt;/code&gt; and the Lua script as &lt;code&gt;scripts/multi-request-json.lua&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We will run wrk in a Docker container. This diagram will give you a good overview of how this is done:&lt;/p&gt;

&lt;p&gt;&lt;img itemprop=&quot;image&quot; width=&quot;100%&quot; src=&quot;/assets/1441437275524/img/posts/wrk/container-overview-json-example.png&quot; alt=&quot;container overview for the wrk JSON example&quot; title=&quot;container overview for the wrk JSON example&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Run the benchmark with:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;docker run --rm &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           -v &lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;pwd&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;/scripts:/scripts &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           -v &lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;pwd&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;/data:/data &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           czerasz/wrk-json wrk -c1 -t1 -d5s -s /scripts/multi-request-json.lua http://&lt;span class=&quot;nv&quot;&gt;$APPLICATION_IP&lt;/span&gt;:&lt;span class=&quot;nv&quot;&gt;$APPLICATION_PORT&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This command will pull the &lt;a href=&quot;https://registry.hub.docker.com/u/czerasz/wrk-json/&quot;&gt;&lt;code&gt;czerasz/wrk-json&lt;/code&gt;&lt;/a&gt; image from the public Docker registry. Then it executes the wrk command (inside the Docker container) which additionally takes the Lua script. The Lua script opens the &lt;code&gt;/data/requests.json&lt;/code&gt; file, parses it and feeds wrk with the data. Everything is possible because we shared the appropriate direcotries with the Docker container by using the &lt;code&gt;-v&lt;/code&gt; option.&lt;/p&gt;

&lt;p&gt;The returned output might look like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;multiplerequests: Found &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt; requests
multiplerequests: Found &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt; requests
Running 5s &lt;span class=&quot;nb&quot;&gt;test&lt;/span&gt; @ http://10.135.232.163:3000
  &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; threads and &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.32ms    1.41ms  18.91ms   91.81%
    Req/Sec     0.89k   304.69     1.47k    64.00%
  &lt;span class=&quot;m&quot;&gt;4447&lt;/span&gt; requests in 5.00s, 0.85MB &lt;span class=&quot;nb&quot;&gt;read&lt;/span&gt;
Requests/sec:    888.67
Transfer/sec:    174.44KB&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Adding weights to this example could be also very easy: just duplicate the request objects or adjust the Lua script to recognize a &lt;code&gt;weight&lt;/code&gt; property.&lt;/p&gt;

&lt;p&gt;I hope that you understand now that “sky is the limit” with wrk and Lua.&lt;/p&gt;

&lt;h2 id=&quot;debugging-wrk&quot;&gt;Debugging WRK&lt;/h2&gt;

&lt;p&gt;The ability to debug a technology properly is worth more than knowing the technology itself.&lt;/p&gt;

&lt;p&gt;To debug wrk I use a special Node.js application and a custom Lua script. The whole environment (based on Docker) is &lt;a href=&quot;https://github.com/czerasz/wrk-debugging-environment&quot;&gt;available on Github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you want to use it, then we need to install the required software. We just need:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code&gt;git&lt;/code&gt; - to download the project&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;docker-compose&lt;/code&gt; - to start the Docker container environment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I will help you setup them on Ubuntu.&lt;/p&gt;

&lt;p&gt;Install &lt;code&gt;git&lt;/code&gt; simply by using Ubuntu’s package manager:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;apt-get install -y git&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Docker Compose can be installed with this commands:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;sudo curl -L https://github.com/docker/compose/releases/download/1.3.1/docker-compose-&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;uname -s&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;-&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;uname -m&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt; &amp;gt; /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you have any trouble or want to install &lt;code&gt;docker-compose&lt;/code&gt; on another system please refer to this &lt;a href=&quot;https://docs.docker.com/compose/install/&quot;&gt;Docker Compose installation guide&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now clone the project:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;git clone https://github.com/czerasz/wrk-debugging-environment.git&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Enter it:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;wrk-debugging-environment&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And spin up the required containers with:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;docker-compose run --rm wrk bash&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The command above will create two containers - one for the &lt;strong&gt;application&lt;/strong&gt; and one for &lt;strong&gt;wrk&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;An overview is presented below:&lt;/p&gt;

&lt;p&gt;&lt;img itemprop=&quot;image&quot; width=&quot;100%&quot; src=&quot;/assets/1441437275524/img/posts/wrk/debugging-container-overview.png&quot; alt=&quot;container overview for the wrk debugging scenario&quot; title=&quot;container overview for the wrk debugging scenario&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The command will also automatically log you in to the &lt;strong&gt;wrk&lt;/strong&gt; container.&lt;/p&gt;

&lt;p&gt;Now whenever you execute this command inside the &lt;strong&gt;wrk&lt;/strong&gt; Docker container:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;wrk -c3 -d1s -t2 -s /scripts/debug.lua http://app:3000 -- debug &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You will see what happens inside wrk because &lt;a href=&quot;https://github.com/czerasz/wrk-debugging-environment/blob/master/environments/wrk/scripts/debug.lua&quot;&gt;the debugging scipt&lt;/a&gt; has a lot of debugging messages (integrated simply with &lt;code&gt;io.write&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Sample output is presented below:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;...
------------------------------
Response &lt;span class=&quot;m&quot;&gt;114&lt;/span&gt; with status: &lt;span class=&quot;m&quot;&gt;200&lt;/span&gt; on thread 1
------------------------------
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;response&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; Headers:
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;response&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;  - Content-Length: 2
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;response&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;  - ETag: W/&lt;span class=&quot;s2&quot;&gt;&amp;quot;2-REvLOj/Pg4kpbElGfyfh1g&amp;quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;response&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;  - Connection: keep-alive
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;response&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;  - Date: Tue, &lt;span class=&quot;m&quot;&gt;23&lt;/span&gt; Jun &lt;span class=&quot;m&quot;&gt;2015&lt;/span&gt; 20:18:01 GMT
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;response&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;  - Content-Type: text/html&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;charset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;utf-8
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;response&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;  - X-Debug: &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;response&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;  - X-Powered-By: Express
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;response&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; Body:
ok

  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     3.42ms    4.23ms  36.86ms   93.15%
    Req/Sec   305.55    162.64   590.00     70.00%
  &lt;span class=&quot;m&quot;&gt;611&lt;/span&gt; requests in 1.00s, 128.29KB &lt;span class=&quot;nb&quot;&gt;read&lt;/span&gt;
Requests/sec:    608.23
Transfer/sec:    127.70KB
------------------------------
Requests
------------------------------
userdata&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;bytes&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;49020&amp;quot;&lt;/span&gt;,
  &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;errors&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;   &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;write&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;0&amp;quot;&lt;/span&gt;,
    &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;read&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;0&amp;quot;&lt;/span&gt;,
    &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;status&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;0&amp;quot;&lt;/span&gt;,
    &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;timeout&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;0&amp;quot;&lt;/span&gt;,
    &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;connect&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;0&amp;quot;&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;,
  &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;duration&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;1009245&amp;quot;&lt;/span&gt;,
  &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;requests&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;228&amp;quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Additionally the application container will log all request details. Simply open another terminal, enter the &lt;code&gt;wrk-debugging-environment&lt;/code&gt; directory:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;wrk-debugging-environment&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then execute this command:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;docker logs -f --tail&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt;docker-compose ps &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; grep &lt;span class=&quot;s1&quot;&gt;&amp;#39;_application_1&amp;#39;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; awk &lt;span class=&quot;s1&quot;&gt;&amp;#39;{print $1}&amp;#39;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Whenever you execute the previous benchmak command (in the previous terminal) you should see output simmilar to this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;...

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

&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;2015-06-23 20:28:30&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; Request 486

GET/1.1 / on :::3000

Headers:
 - host: app:3000

No cookies

Body:&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now just edit the &lt;a href=&quot;https://github.com/czerasz/wrk-debugging-environment/blob/master/environments/wrk/scripts/debug.lua&quot;&gt;&lt;code&gt;environments/wrk/scripts/debug.lua&lt;/code&gt;&lt;/a&gt; file and see the output changing. Play around and have fun.&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;I hope that this article and &lt;a href=&quot;https://www.digitalocean.com/community/tutorials/how-to-benchmark-http-latency-with-wrk-on-ubuntu-14-04&quot;&gt;the one on Digital Ocean&lt;/a&gt; gave you a better understanding about wrk and explained how to precisely shoot with heavy benchmarking bullets.&lt;/p&gt;

&lt;h2 id=&quot;resources&quot;&gt;Resources&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://charmyin.github.io/informationtechnology/2014/08/11/multiple-file-upload-express/&quot;&gt;WRK - A HTTP benchmarking tool&lt;/a&gt; - very good article about &lt;code&gt;wrk&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://revision3.com/haktip/understanding-high-latency/&quot;&gt;Wireshark 101: Understanding High Latency&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://nirajrules.wordpress.com/2009/09/17/measuring-performance-response-vs-latency-vs-throughput-vs-load-vs-scalability-vs-stress-vs-robustness/&quot;&gt;Performance Testing - Response vs. Latency vs. Throughput vs. Load vs. Scalability vs. Stress vs. Robustness&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=6Rs0p3mPNr0&quot;&gt;How NOT to measure latency - Gil Tene at USI&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://danielmendel.github.io/blog/2013/04/07/benchmarkers-beware-the-ephemeral-port-limit/&quot;&gt;Benchmarkers, Beware the Ephemeral Port Limit&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://kellabyte.com/2014/02/12/create-benchmarks-and-results-that-have-value/&quot;&gt;Create benchmarks and results that have value&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.infoq.com/presentations/latency-pitfalls&quot;&gt;How NOT to Measure Latency&lt;/a&gt; - must watch presentation&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/giltene/jHiccup&quot;&gt;jHiccup Project&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/HdrHistogram/HdrHistogram&quot;&gt;HdrHistogram Project&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Sun, 19 Jul 2015 00:00:00 +0200</pubDate>
        <link>http://czerasz.com/2015/07/19/wrk-http-benchmarking-tool-example/</link>
        <guid isPermaLink="true">http://czerasz.com/2015/07/19/wrk-http-benchmarking-tool-example/</guid>
        
        <category>wrk</category>
        
        <category>lua</category>
        
        <category>benchmarking</category>
        
        <category>http</category>
        
        <category>http benchmarking</category>
        
        <category>json</category>
        
        <category>debugging wrk</category>
        
        <category>wrk documentation</category>
        
        <category>lua script</category>
        
        
      </item>
    
      <item>
        <title>Nginx Maintenance Mode with Integration Testing</title>
        <description>&lt;p class=&quot;post__tldr&quot; itemprop=&quot;description&quot;&gt;This article shows how to enable a maintenance page in Nginx over HTTP and how to test such Nginx configuration with the Bats testing framework.&lt;/p&gt;

&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;Whenever possible avoid downtime!&lt;/p&gt;

&lt;p&gt;However sometimes it just happens so that we need to take our service offline for at least a few minutes of maintenance. To be able to act professionally in such situations we can configure Nginx, the popular web server to show a special maintenance page for all incoming requests. This page will inform users and search engine bots that our site is temporary down and will be back soon.&lt;/p&gt;

&lt;p&gt;There is a really great &lt;a href=&quot;http://www.smashingmagazine.com/2009/06/12/effective-maintenance-pages-examples-and-best-practices/&quot; target=&quot;_blank&quot;&gt;article about effective maintenance pages&lt;/a&gt; with a bunch of tips which allow you to design the perfect maintenance page for your service. Proper communication is as important as a stable technical integration.&lt;/p&gt;

&lt;p&gt;Let’s quickly go through our plan for today.&lt;/p&gt;

&lt;p&gt;Our initial situation is presented on the diagram below.&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy  img-responsive&quot; itemprop=&quot;image&quot; src=&quot;/assets/1441437275524/img/posts/nginx-maintenance-mode/initial-situation.png&quot; alt=&quot;Nginx Maintenance Mode: initial situation application request&quot; /&gt;&lt;/p&gt;

&lt;p&gt;For the sake of simplicity I have chosen the simplest scenario - one web application behind an Nginx server. The implementation for a more complex infrastructure is analogous and should be as simple as the one presented in this article. But if you have any troubles feel free to &lt;a href=&quot;/contact&quot;&gt;contact me&lt;/a&gt;. I’m sure I will be able to help.&lt;/p&gt;

&lt;p&gt;And here comes the best part. With a simple HTTP &lt;code&gt;PUT&lt;/code&gt; request we can enable the Nginx’s maintenance mode.&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy  img-responsive&quot; itemprop=&quot;image&quot; src=&quot;/assets/1441437275524/img/posts/nginx-maintenance-mode/enable-nginx-maintenance-mode-over-http.png&quot; alt=&quot;Nginx Maintenance Mode: enable maintenance mode with an http request&quot; /&gt;&lt;/p&gt;

&lt;p&gt;From now on all responses will contain the content of the &lt;code&gt;/usr/share/nginx/html/maintenance.html&lt;/code&gt; file and will be returned with the &lt;code&gt;503&lt;/code&gt; status code.&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy  img-responsive&quot; itemprop=&quot;image&quot; src=&quot;/assets/1441437275524/img/posts/nginx-maintenance-mode/nginx-maintenance-mode.png&quot; alt=&quot;Nginx Maintenance Mode: how maintenance mode works&quot; /&gt;&lt;/p&gt;

&lt;p&gt;For the full understanding the diagram above contains also the light blue path which represents the normal situation.&lt;/p&gt;

&lt;p&gt;The maintenance mode persists until we disable it with an HTTP &lt;code&gt;DELETE&lt;/code&gt; request.&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy  img-responsive&quot; itemprop=&quot;image&quot; src=&quot;/assets/1441437275524/img/posts/nginx-maintenance-mode/disable-nginx-maintenance-mode-over-http.png&quot; alt=&quot;Nginx Maintenance Mode: disable maintenance mode with an http request&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now for a better overview let’s see everything at once. The following sequential diagram shows the full situation described in this chapter:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy  img-responsive&quot; itemprop=&quot;image&quot; src=&quot;/assets/1441437275524/img/posts/nginx-maintenance-mode/sequential-diagram.png&quot; alt=&quot;Nginx Maintenance Mode: sequential diagram&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In this diagram the HTTP &lt;code&gt;PUT&lt;/code&gt; and &lt;code&gt;DELETE&lt;/code&gt; requests are fetched by the same client for the sake of simplicity. In a real world those requests are only available to the appropriate person/service.&lt;/p&gt;

&lt;h2 id=&quot;preparation-and-infrastructure&quot;&gt;Preparation and Infrastructure&lt;/h2&gt;

&lt;p&gt;The source code for the example presented in this article is available in the &lt;code&gt;0.0.1&lt;/code&gt; release of my &lt;a href=&quot;https://github.com/czerasz/nginx-configuration-examples/tree/0.0.1&quot; target=&quot;_blank&quot;&gt;Nginx Configuration Examples Github project&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Simply clone the git repository with this command:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;git clone --branch 0.0.1 git@github.com:czerasz/nginx-configuration-examples.git&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The project structure looks like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;.
├── bin
│   └── test.sh
├── config
│   ├── htpasswd
│   ├── maintenance-page.conf
│   └── nginx.conf
├── Dockerfile
└── &lt;span class=&quot;nb&quot;&gt;test&lt;/span&gt;
    └── nginx.bats&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It contains:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;a helper script inside the &lt;code&gt;bin&lt;/code&gt; directory&lt;/li&gt;
  &lt;li&gt;few Nginx configuration files in the &lt;code&gt;config&lt;/code&gt; directory&lt;/li&gt;
  &lt;li&gt;test specs and a &lt;code&gt;Dockerfile&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To be able to follow this tutorial you will just need &lt;a href=&quot;https://www.docker.com/&quot; target=&quot;_blank&quot;&gt;Docker&lt;/a&gt; installed on your machine. Follow this &lt;a href=&quot;https://docs.docker.com/installation/&quot; target=&quot;_blank&quot;&gt;installation guide&lt;/a&gt; if you haven’t installed Docker so far.&lt;/p&gt;

&lt;h2 id=&quot;the-dockerfile&quot;&gt;The &lt;code&gt;Dockerfile&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;We will work on and test our Nginx configuration inside a &lt;a href=&quot;https://www.docker.com/&quot; target=&quot;_blank&quot;&gt;Docker&lt;/a&gt; container. This will allow us to keep everything inside a sandbox additionally providing a really fast environment.&lt;/p&gt;

&lt;p&gt;The mentioned Docker container is created based on the following &lt;a href=&quot;https://github.com/czerasz/nginx-configuration-examples/blob/0.0.1/Dockerfile&quot;&gt;&lt;code&gt;Dockerfile&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;lineno&quot;&gt; 1&lt;/span&gt; FROM nginx:1.7.9
&lt;span class=&quot;lineno&quot;&gt; 2&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt; 3&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Set the reset cache variable&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4&lt;/span&gt; ENV REFRESHED_AT 2015-04-20
&lt;span class=&quot;lineno&quot;&gt; 5&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt; 6&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Update repositories lists and install required tools and libraries&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7&lt;/span&gt; RUN apt-get update
&lt;span class=&quot;lineno&quot;&gt; 8&lt;/span&gt; RUN apt-get install -y wget curl git tree vim htop strace procps
&lt;span class=&quot;lineno&quot;&gt; 9&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Install bats testing framework&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;11&lt;/span&gt; RUN git clone https://github.com/sstephenson/bats.git /tmp/bats &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; /tmp/bats &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; ./install.sh /usr/local
&lt;span class=&quot;lineno&quot;&gt;12&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;13&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Create maintenance content file - this will be returned in maintenance mode&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;14&lt;/span&gt; RUN &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Site in maintenance&amp;#39;&lt;/span&gt; &amp;gt; /usr/share/nginx/html/maintenance.html
&lt;span class=&quot;lineno&quot;&gt;15&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;16&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Nginx owns the web content directory - required to save the maintenance file by WebDAV&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;17&lt;/span&gt; RUN chown -R nginx:nginx /usr/share/nginx/html/
&lt;span class=&quot;lineno&quot;&gt;18&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;19&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Add configuration files&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;20&lt;/span&gt; ADD ./config/nginx.conf /etc/nginx/nginx.conf
&lt;span class=&quot;lineno&quot;&gt;21&lt;/span&gt; ADD ./config/maintenance-page.conf /etc/nginx/maintenance-page.conf
&lt;span class=&quot;lineno&quot;&gt;22&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;23&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Add the base auth credentials&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;24&lt;/span&gt; ADD ./config/htpasswd /etc/nginx/htpasswd
&lt;span class=&quot;lineno&quot;&gt;25&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;26&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Add test spec&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;27&lt;/span&gt; ADD ./test/nginx.bats /&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;Dockerfile&lt;/code&gt; should be straight forward because it’s well documented, but here are some things which are worth mentioning:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;the line with &lt;code&gt;ENV REFRESHED_AT 2015-04-20&lt;/code&gt; is explained in my other article which is available &lt;a href=&quot;/2014/11/13/docker-tip-and-tricks/#use-refreshedat-variable-for-better-cache-control&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;the installed &lt;a href=&quot;https://packages.debian.org/sid/procps&quot; target=&quot;_blank&quot;&gt;&lt;code&gt;procps&lt;/code&gt; package&lt;/a&gt; (the &lt;code&gt;/proc&lt;/code&gt; file system utilities) contains &lt;code&gt;ps&lt;/code&gt; and &lt;code&gt;pgrep&lt;/code&gt; among many other useful tools. &lt;code&gt;pgrep&lt;/code&gt; is used later in our Nginx test catalogue&lt;/li&gt;
  &lt;li&gt;in line &lt;code&gt;11&lt;/code&gt; Docker will install the &lt;a href=&quot;https://github.com/sstephenson/bats&quot; target=&quot;_blank&quot;&gt;Bats: Bash Automated Testing System&lt;/a&gt;. It’s a simle and easy to use Bash testing framework&lt;/li&gt;
  &lt;li&gt;the &lt;code&gt;/usr/share/nginx/html/maintenance.html&lt;/code&gt; contains the maintenance information. If you want to add a nice looking page feel free to use the &lt;code&gt;ADD&lt;/code&gt; directive&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;nginx-configuration&quot;&gt;Nginx Configuration&lt;/h2&gt;

&lt;p&gt;The &lt;a href=&quot;https://github.com/czerasz/nginx-configuration-examples/blob/0.0.1/config/nginx.conf&quot; target=&quot;_blank&quot;&gt;&lt;code&gt;config/nginx.conf&lt;/code&gt;&lt;/a&gt; is the main Nginx configuration file which includes the most important file for us, mainly the &lt;a href=&quot;https://github.com/czerasz/nginx-configuration-examples/blob/0.0.1/config/maintenance-page.conf&quot; target=&quot;_blank&quot;&gt;&lt;code&gt;config/maintenance-page.conf&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let’s go through the &lt;code&gt;config/maintenance-page.conf&lt;/code&gt; file:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$maintenance&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

location /maintenance-mode &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    limit_except PUT DELETE &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        deny all&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    dav_methods PUT DELETE&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    auth_basic &lt;span class=&quot;s2&quot;&gt;&amp;quot;Maintenance&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    auth_basic_user_file /etc/nginx/htpasswd&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Note: if is evil only in the location block&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# If the request goes to /maintenance-mode ignore the maintenance mode&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$uri&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;/maintenance-mode&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$maintenance&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;${maintenance}+disable&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Check if the maintenance mode is enabled&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;-f &lt;span class=&quot;nv&quot;&gt;$document_root&lt;/span&gt;/maintenance-mode&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$maintenance&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;${maintenance}+exists&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# If the request goes to anything different than /maintenance-mode return a 503 if the maintenance mode is enabled&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$maintenance&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;+exists&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; 503&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Service temporarily unavailable&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

error_page &lt;span class=&quot;m&quot;&gt;503&lt;/span&gt; @maintenance&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

location @maintenance &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    rewrite ^&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;.*&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;/maintenance.html &lt;span class=&quot;nb&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;/maintenance-mode&lt;/code&gt; location block contains directives which enable us to create or remove the &lt;code&gt;/usr/share/nginx/html/maintenance-mode&lt;/code&gt; file over HTTP. &lt;/p&gt;

&lt;p&gt;We can create the file with &lt;code&gt;curl&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;curl -i -u czerasz:password1234567890 -XPUT localhost/maintenance-mode -d &lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;
&lt;br /&gt;The &lt;code&gt;-u czerasz:password1234567890&lt;/code&gt; parameter is responsible for authentication. We could also use this url &lt;code&gt;http://czerasz:password1234567890@localhost/maintenance-mode&lt;/code&gt; instead.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Or we can delete the file with:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;curl -i -u czerasz:password1234567890 -XDELETE localhost/maintenance-mode&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;
&lt;br /&gt;We are using base authentication which should be improved by enabling SSL.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The existence of &lt;code&gt;/usr/share/nginx/html/maintenance-mode&lt;/code&gt; tells Nginx if it is in maintenance mode:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;if the &lt;code&gt;/usr/share/nginx/html/maintenance-mode&lt;/code&gt; file exists then Nginx will respond with a &lt;code&gt;503&lt;/code&gt; status code - the service is in maintenance mode&lt;/li&gt;
  &lt;li&gt;if the &lt;code&gt;/usr/share/nginx/html/maintenance-mode&lt;/code&gt; file doesn’t exists then Nginx works normally&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the &lt;code&gt;/usr/share/nginx/html/maintenance-mode&lt;/code&gt; file exists, Nginx will return the &lt;code&gt;/usr/share/nginx/html/maintenance.html&lt;/code&gt; file with the &lt;code&gt;503&lt;/code&gt; status code for all requests. The specific status code is important because it tells Google and co that the service is temporarily unavailable. And that they should try it later. I refer to &lt;a href=&quot;https://yoast.com/http-503-site-maintenance-seo/&quot; target=&quot;_blank&quot;&gt;this article&lt;/a&gt; which explains why it is so important from the seo perspective.&lt;/p&gt;

&lt;p&gt;The config contains also few &lt;code&gt;if&lt;/code&gt; statements which allow us to do the &lt;code&gt;DELETE&lt;/code&gt; request (which was discussed before) in maintenance mode.&lt;/p&gt;

&lt;h2 id=&quot;integration-tests&quot;&gt;Integration Tests&lt;/h2&gt;

&lt;p&gt;Now that we know how our configuration works, it’s time for a success experience. Let’s run our test suite and watch the tests pass:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;./bin/test.sh 
Build the container
Remove the old container
Start the container and run the tests
1..6
ok &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; Nginx binary is found in /usr/local/libexec:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ok &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt; Nginx is installed in version 1.7.9
ok &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt; Upstream server shouldn&lt;span class=&quot;err&quot;&gt;&amp;#39;&lt;/span&gt;t be available
ok &lt;span class=&quot;m&quot;&gt;4&lt;/span&gt; Nginx should be in maintenance mode &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; the /usr/share/nginx/html/maintenance-mode exists
ok &lt;span class=&quot;m&quot;&gt;5&lt;/span&gt; Allow the client to create the /usr/share/nginx/html/maintenance-mode file over HTTP
ok &lt;span class=&quot;m&quot;&gt;6&lt;/span&gt; Allow the client to remove the /usr/share/nginx/html/maintenance-mode file over HTTP&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The analysis of our &lt;a href=&quot;https://github.com/czerasz/nginx-configuration-examples/blob/0.0.1/bin/test.sh&quot; target=&quot;_blank&quot;&gt;&lt;code&gt;bin/test.sh&lt;/code&gt;&lt;/a&gt; helper script shows that it works according to this scenario/along those points:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;a Docker image is build based on the already discussed &lt;code&gt;Dockerfile&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;the previous, old container is removed, if it exists&lt;/li&gt;
  &lt;li&gt;a new container is created based on our build&lt;/li&gt;
  &lt;li&gt;in the newly created container, the &lt;code&gt;bats /nginx.bats&lt;/code&gt; command is executed to run our tests&lt;/li&gt;
  &lt;li&gt;the &lt;code&gt;--rm=true&lt;/code&gt; parameter tells Docker to remove our container after the tests have finished&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We know that our tests pass, so let’s see how they are written. The &lt;a href=&quot;https://github.com/czerasz/nginx-configuration-examples/blob/0.0.1/test/nginx.bats&quot; target=&quot;_blank&quot;&gt;&lt;code&gt;test/nginx.bats&lt;/code&gt;&lt;/a&gt; file which represent our test catalogue, is a simple Bash script which uses the &lt;a href=&quot;https://github.com/sstephenson/bats&quot; target=&quot;_blank&quot;&gt;Bats testing framework&lt;/a&gt;: &lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;#!/usr/bin/env bats&lt;/span&gt;

wait_for_nginx&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# Start Nginx&lt;/span&gt;
  nginx &lt;span class=&quot;p&quot;&gt;&amp;amp;&lt;/span&gt;
  
  &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; ! pgrep -xf &lt;span class=&quot;s2&quot;&gt;&amp;quot;nginx: worker process&amp;quot;&lt;/span&gt; &amp;gt; /dev/null &lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;wait for nginx&amp;#39;&lt;/span&gt;

    sleep 0.1&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;maintenance_file_path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/usr/share/nginx/html/maintenance-mode

teardown&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  pkill nginx &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;  &lt;/span&gt;rm -f &lt;span class=&quot;nv&quot;&gt;$maintenance_file_path&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;amp;&lt;/span&gt;&amp;gt; /dev/null
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

@test &lt;span class=&quot;s2&quot;&gt;&amp;quot;Nginx binary is found in $PATH&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  run which nginx

  &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;$status&amp;quot;&lt;/span&gt; -eq &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

@test &lt;span class=&quot;s2&quot;&gt;&amp;quot;Nginx is installed in version 1.7.9&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  run /usr/sbin/nginx -v

  &lt;span class=&quot;o&quot;&gt;[[&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;$output&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;~ &lt;span class=&quot;s2&quot;&gt;&amp;quot;1.7.9&amp;quot;&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

@test &lt;span class=&quot;s2&quot;&gt;&amp;quot;Upstream server shouldn&amp;#39;t be available&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  wait_for_nginx

  &lt;span class=&quot;nv&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;$(curl -Is http://localhost/ | head -n1 | awk &amp;#39;{print $2}&amp;#39;)&amp;quot;&lt;/span&gt;

  &lt;span class=&quot;o&quot;&gt;[[&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;$result&amp;quot;&lt;/span&gt; -eq &lt;span class=&quot;s1&quot;&gt;&amp;#39;502&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

@test &lt;span class=&quot;s2&quot;&gt;&amp;quot;Nginx should be in maintenance mode if the $maintenance_file_path exists&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  wait_for_nginx

  touch &lt;span class=&quot;nv&quot;&gt;$maintenance_file_path&lt;/span&gt;
  
  run bash -c &lt;span class=&quot;s2&quot;&gt;&amp;quot;curl -s -w &amp;#39;%{http_code}&amp;#39; http://localhost/ -o /dev/null&amp;quot;&lt;/span&gt;

  &lt;span class=&quot;o&quot;&gt;[[&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;$output&amp;quot;&lt;/span&gt; -eq &lt;span class=&quot;s1&quot;&gt;&amp;#39;503&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

@test &lt;span class=&quot;s2&quot;&gt;&amp;quot;Allow the client to create the $maintenance_file_path file over HTTP&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  wait_for_nginx

  &lt;span class=&quot;c&quot;&gt;# Shouldn&amp;#39;t be able to create the maintenance file without authentication&lt;/span&gt;
  run bash -c &lt;span class=&quot;s2&quot;&gt;&amp;quot;curl -s -w &amp;#39;%{http_code}&amp;#39; -XPUT localhost/maintenance-mode -d &amp;#39;&amp;#39; -o /dev/null&amp;quot;&lt;/span&gt;

  &lt;span class=&quot;o&quot;&gt;[[&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;$output&amp;quot;&lt;/span&gt; -eq &lt;span class=&quot;s1&quot;&gt;&amp;#39;401&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt;

  &lt;span class=&quot;c&quot;&gt;# File shouldn&amp;#39;t be created yet&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; -f &lt;span class=&quot;s2&quot;&gt;&amp;quot;$maintenance_file_path&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;[[&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; -eq &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;[[&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; -eq &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;fi&lt;/span&gt;

  &lt;span class=&quot;c&quot;&gt;# Allow with authentication&lt;/span&gt;
  run bash -c &lt;span class=&quot;s2&quot;&gt;&amp;quot;curl -s -u czerasz:password1234567890 -w &amp;#39;%{http_code}&amp;#39; -XPUT localhost/maintenance-mode -d &amp;#39;&amp;#39; -o /dev/null&amp;quot;&lt;/span&gt;

  &lt;span class=&quot;o&quot;&gt;[[&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;$output&amp;quot;&lt;/span&gt; -eq &lt;span class=&quot;s1&quot;&gt;&amp;#39;201&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt;

  &lt;span class=&quot;c&quot;&gt;# Check if maintenance file was successfully created&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; -f &lt;span class=&quot;s2&quot;&gt;&amp;quot;$maintenance_file_path&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;[[&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; -eq &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;[[&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; -eq &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;fi&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

@test &lt;span class=&quot;s2&quot;&gt;&amp;quot;Allow the client to remove the $maintenance_file_path file over HTTP&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  wait_for_nginx

  touch &lt;span class=&quot;nv&quot;&gt;$maintenance_file_path&lt;/span&gt;
  chown nginx:nginx &lt;span class=&quot;nv&quot;&gt;$maintenance_file_path&lt;/span&gt;

  &lt;span class=&quot;c&quot;&gt;# Shouldn&amp;#39;t be able to delete the maintenance file without authentication&lt;/span&gt;
  run bash -c &lt;span class=&quot;s2&quot;&gt;&amp;quot;curl -s -w &amp;#39;%{http_code}&amp;#39; -XDELETE localhost/maintenance-mode -o /dev/null&amp;quot;&lt;/span&gt;

  &lt;span class=&quot;o&quot;&gt;[[&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;$output&amp;quot;&lt;/span&gt; -eq &lt;span class=&quot;s1&quot;&gt;&amp;#39;401&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt;

  &lt;span class=&quot;c&quot;&gt;# File shouldn&amp;#39;t be deleted yet&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; -f &lt;span class=&quot;s2&quot;&gt;&amp;quot;$maintenance_file_path&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;[[&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; -eq &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;[[&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; -eq &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;fi&lt;/span&gt;

  &lt;span class=&quot;c&quot;&gt;# Allow with authentication&lt;/span&gt;
  run bash -c &lt;span class=&quot;s2&quot;&gt;&amp;quot;curl -s -u czerasz:password1234567890 -w &amp;#39;%{http_code}&amp;#39; -XDELETE localhost/maintenance-mode -o /dev/null&amp;quot;&lt;/span&gt;

  &lt;span class=&quot;c&quot;&gt;# File should be deleted&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; -f &lt;span class=&quot;s2&quot;&gt;&amp;quot;$maintenance_file_path&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;[[&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; -eq &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;[[&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; -eq &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;fi&lt;/span&gt;

  &lt;span class=&quot;o&quot;&gt;[[&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;$output&amp;quot;&lt;/span&gt; -eq &lt;span class=&quot;s1&quot;&gt;&amp;#39;204&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you have basic Bash understanding you should be able to easily understand it but let me explain the most important parts.&lt;/p&gt;

&lt;p&gt;The spec contains &lt;code&gt;@test&lt;/code&gt; blocks, variables and functions.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;@test&lt;/code&gt; block has the following structure:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;@test &lt;span class=&quot;s2&quot;&gt;&amp;quot;Name of the test&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# The test goes here&lt;/span&gt;
  run /usr/sbin/nginx -v

  &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;$status&amp;quot;&lt;/span&gt; -eq &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;[[&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;$output&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;~ &lt;span class=&quot;s2&quot;&gt;&amp;quot;1.7.9&amp;quot;&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Inside the &lt;code&gt;@test&lt;/code&gt; block we can &lt;code&gt;run&lt;/code&gt; any command afterwards analysing its output and its exit code.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;
&lt;br /&gt;A successful command returns a &lt;code&gt;0&lt;/code&gt;, while an unsuccessful one returns a non-zero value.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In case of more complex commands I use a &lt;code&gt;bash -c&lt;/code&gt; wrapper to encapsulate the entire command as presented in the example below:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;run bash -c &lt;span class=&quot;s2&quot;&gt;&amp;quot;curl -s -u czerasz:password1234567890 -w &amp;#39;%{http_code}&amp;#39; -XDELETE localhost/maintenance-mode -o /dev/null&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Because we need to test a web server, what could be better than the &lt;code&gt;curl&lt;/code&gt; command to achieve this task? The following parameters have proved to be very useful:&lt;/p&gt;

&lt;ul&gt;
    &lt;li&gt;&lt;code&gt;-s&lt;/code&gt; - silent mode. Don&#39;t output anything&lt;/li&gt;
    &lt;li&gt;&lt;code&gt;-w &#39;%{http_code}&#39;&lt;/code&gt; - return only the status code&lt;/li&gt;
    &lt;li&gt;&lt;code&gt;-XPUT&lt;/code&gt; - specify the request method&lt;/li&gt;
    &lt;li&gt;&lt;code&gt;-d &#39;&#39;&lt;/code&gt; - specify the &lt;code&gt;POST&lt;/code&gt; request data&lt;/li&gt;
    &lt;li&gt;&lt;code&gt;-u user:password&lt;/code&gt; - specifies the user and password for the base authentication&lt;/li&gt;
    &lt;li&gt;&lt;code&gt;-I&lt;/code&gt; - show response status with headers
    &lt;br /&gt;&lt;br /&gt;
    &lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;curl -I google.com
HTTP/1.1 &lt;span class=&quot;m&quot;&gt;302&lt;/span&gt; Found
Cache-Control: private
Content-Type: text/html&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;charset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;UTF-8
Location: http://www.google.de/?gfe_rd&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;cr&lt;span class=&quot;p&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;ei&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;uBo2VdWMAqSF8Qfex4DACA
Content-Length: 258
Date: Tue, &lt;span class=&quot;m&quot;&gt;21&lt;/span&gt; Apr &lt;span class=&quot;m&quot;&gt;2015&lt;/span&gt; 09:39:04 GMT
Server: GFE/2.0
Alternate-Protocol: 80:quic,p&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Bats has also some conventions. One of them is the &lt;code&gt;teardown&lt;/code&gt; function, which is executed after each test case to clean up the environment. In our script the cleanup consists of killing Nginx and removing the &lt;code&gt;/usr/share/nginx/html/maintenance-mode&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;You also noticed the &lt;code&gt;wait_for_nginx&lt;/code&gt; function which is there to spin up the Nginx process and wait until it’s up. This code could be also wrapped inside the &lt;code&gt;setup&lt;/code&gt; helper which is executed before each test.&lt;/p&gt;

&lt;p&gt;You can read more about the &lt;code&gt;teardown&lt;/code&gt; and the &lt;code&gt;setup&lt;/code&gt; helpers &lt;a href=&quot;https://github.com/sstephenson/bats#setup-and-teardown-pre--and-post-test-hooks&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In summary our tests consist of Bash commands which are later analysed in Bash for their correctness. &lt;/p&gt;

&lt;p&gt;Make sure you visit the &lt;a href=&quot;https://github.com/sstephenson/bats&quot; target=&quot;_blank&quot;&gt;Bats Github repository&lt;/a&gt; to star this awesome project.&lt;/p&gt;

&lt;h2 id=&quot;performance-considerations&quot;&gt;Performance Considerations&lt;/h2&gt;

&lt;p&gt;Swapping the Nginx configuration file by a another configuration file that contains only the maintenance logic would save Nginx a lot of processing power. I can imagine this could be achieved with &lt;a href=&quot;http://www.ansible.com/&quot; target=&quot;_blank&quot;&gt;Ansible&lt;/a&gt; or &lt;a href=&quot;https://puppetlabs.com/&quot; target=&quot;_blank&quot;&gt;Puppet&lt;/a&gt; fairly easy. But depending on your situation it might be harder to implement (than a HTTP request) and might also require an additional service/process which makes the architecture more complex. Read, compare features and decide.&lt;/p&gt;

&lt;h2 id=&quot;resources&quot;&gt;Resources&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/aptible/docker-nginx&quot; target=&quot;_blank&quot;&gt;Nginx Docker Image&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=XGIY9ezIzyE&quot; target=&quot;_blank&quot;&gt;Youtube: Test Drive Your Config File!&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/sstephenson/bats&quot; target=&quot;_blank&quot;&gt;Bats: Bash Automated Testing System&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://blog.engineyard.com/2014/bats-test-command-line-tools&quot; target=&quot;_blank&quot;&gt;How to use Bats to test your command line tools&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Tue, 21 Apr 2015 00:00:00 +0200</pubDate>
        <link>http://czerasz.com/2015/04/21/nginx-maintenance-mode/</link>
        <guid isPermaLink="true">http://czerasz.com/2015/04/21/nginx-maintenance-mode/</guid>
        
        <category>nginx</category>
        
        <category>nginx testing</category>
        
        <category>maintenance</category>
        
        <category>web server</category>
        
        <category>bats tutorial</category>
        
        <category>bash testing</category>
        
        <category>web server test</category>
        
        <category>integration testing</category>
        
        
      </item>
    
      <item>
        <title>Nginx Caching Tutorial - You Can Run Faster</title>
        <description>&lt;p class=&quot;post__tldr&quot; itemprop=&quot;description&quot;&gt;Let&#39;s dive together into the world of web content caching with Nginx. Starting with basics we will quickly move to more advanced topics and see some real world examples. If you can make it to the end of this blog post... You will bear the title of a &quot;Caching King&quot;.&lt;/p&gt;

&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;Let me start with an inspiring story.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;A friend of mine took a contract for creating a simple web application. This app was used during a crowdfunding event, to visualize live statistics about the raised money for a children hospital. People donated money and the total amount with other details were displayed on a big screen. Additionally this service was available online. The application was very simple and the server strong. Luckily the generosity and love were much bigger. The server started to slow down. My friend got in panic but he found a clever and easy solution - he used Nginx’s caching capabilities.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Today I will show you how to make the internet faster and more stable. How you can easily adapt Nginx’s caching, to boost your applications.&lt;/p&gt;

&lt;p&gt;We will start with caching concepts and then jump into available Nginx caching configuration - directives. I will explain why they are useful and how you can profit from them. Armored with the right knowledge we will fight our way through a real world example.&lt;/p&gt;

&lt;h2 id=&quot;the-caching-concept&quot;&gt;The Caching Concept&lt;/h2&gt;

&lt;p&gt;Before we start I need to make an a priori assumption. Namely that the content is quasi real time. This means that our data or if you wish our HTTP responses, do not change so often. Please note that the phrase &quot;not so often&quot; is not a strict definition. It can be 1 second, 1 hour, 1 week etc.&lt;/p&gt;

&lt;p&gt;Ok, having this in mind let’s start with our initial situation, with no caching involved, which looks like this. &lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy&quot; itemprop=&quot;image&quot; width=&quot;301&quot; height=&quot;60&quot; src=&quot;/assets/1441437275524/img/posts/nginx-caching/initial-situation-request.png&quot; alt=&quot;Nginx Caching: initial situation application request&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The user does a request to the application and gets a response back.&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy&quot; itemprop=&quot;image&quot; width=&quot;301&quot; height=&quot;60&quot; src=&quot;/assets/1441437275524/img/posts/nginx-caching/initial-situation-response.png&quot; alt=&quot;Nginx Caching: initial situation application response&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Adding caching to this scenario is a simple way to improve performance, capacity and availability.
It works by saving responses from servers or applications to local storage or memory.&lt;/p&gt;

&lt;p&gt;Let’s consider this simple situation. The client does a request to the application like in the previous example but this time he goes through the cache.&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy&quot; itemprop=&quot;image&quot; width=&quot;466&quot; height=&quot;60&quot; src=&quot;/assets/1441437275524/img/posts/nginx-caching/caching-intro-first-request.png&quot; alt=&quot;Nginx Caching: caching intro first request&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The application responses and the cache forwards this response to the client but it also saves it locally.&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy&quot; itemprop=&quot;image&quot; width=&quot;466&quot; height=&quot;200&quot; src=&quot;/assets/1441437275524/img/posts/nginx-caching/caching-intro-first-response.png&quot; alt=&quot;Nginx Caching: caching intro first response&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Next time the user does the same request the cache checks if it already has this information.&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy&quot; itemprop=&quot;image&quot; width=&quot;466&quot; height=&quot;200&quot; src=&quot;/assets/1441437275524/img/posts/nginx-caching/caching-intro-second-request.png&quot; alt=&quot;Nginx Caching: caching intro second request&quot; /&gt;&lt;/p&gt;

&lt;p&gt;And if that’s the case, it responds immediately to the user, by serving this cached content.&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy&quot; itemprop=&quot;image&quot; width=&quot;466&quot; height=&quot;200&quot; src=&quot;/assets/1441437275524/img/posts/nginx-caching/caching-intro-second-response.png&quot; alt=&quot;Nginx Caching: caching intro second response&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Please note that the second request doesn’t involve the application at all.&lt;/p&gt;

&lt;p&gt;Caching adds complexity to the system but it comes with great &lt;strong&gt;benefits&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;it improves site performance&lt;/strong&gt; - requests doesn’t have to go through the whole rendering process&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;it increases capacity&lt;/strong&gt; - by reducing load on origin servers&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;it also gives greater availability&lt;/strong&gt; - by serving stale content when the origin server is down&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before we start with Nginx, I want to mention that there are also alternatives for caching content. &lt;a href=&quot;http://www.squid-cache.org/&quot; target=&quot;_blank&quot;&gt;Squid Cache&lt;/a&gt; and &lt;a href=&quot;https://www.varnish-cache.org/&quot; target=&quot;_blank&quot;&gt;Varnish Cache&lt;/a&gt; are my favourite examples. The standard use case which I saw during my career looks like this:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy&quot; itemprop=&quot;image&quot; width=&quot;631&quot; height=&quot;192&quot; src=&quot;/assets/1441437275524/img/posts/nginx-caching/caching-alternatives-varnish-locally.png&quot; alt=&quot;Nginx Caching: caching alternatives varnish locally&quot; /&gt;&lt;/p&gt;

&lt;p&gt;You simply put the cache in front of the HTTP server. Either on the application server or on a specially dedicated machine:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy&quot; itemprop=&quot;image&quot; width=&quot;631&quot; height=&quot;168&quot; src=&quot;/assets/1441437275524/img/posts/nginx-caching/caching-alternatives-varnish-load-balancer.png&quot; alt=&quot;Nginx Caching: caching alternatives varnish load balancer&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This obviously adds even more complexity to the system, but it might be a better fit to your case. Read, compare features and decide.&lt;/p&gt;

&lt;p&gt;In this post we will focus our attention only on Nginx.&lt;/p&gt;

&lt;h2 id=&quot;caching-with-nginx&quot;&gt;Caching with Nginx&lt;/h2&gt;

&lt;p&gt;Nginx is a HTTP server and it’s brilliant for serving static files and proxying requests.
Because of its asynchronous nature it stands out with a light-weight resource utilization.&lt;/p&gt;

&lt;p&gt;When it comes to caching, Nginx has integrations for:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;HTTP servers&lt;/li&gt;
  &lt;li&gt;FastCGI&lt;/li&gt;
  &lt;li&gt;uwsgi&lt;/li&gt;
  &lt;li&gt;SCGI&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that we know how caching works and what Nginx is. Let’s look at Nginx’s implementation of caching.&lt;/p&gt;

&lt;p&gt;An example should make it clear. First the client does a request.&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy&quot; itemprop=&quot;image&quot; width=&quot;493&quot; height=&quot;382&quot; src=&quot;/assets/1441437275524/img/posts/nginx-caching/nginx-cache-1.png&quot; alt=&quot;Nginx Caching: &quot; /&gt;&lt;/p&gt;

&lt;p&gt;A sample HTTP request is presented on the diagram below:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy&quot; itemprop=&quot;image&quot; width=&quot;373&quot; height=&quot;202&quot; src=&quot;/assets/1441437275524/img/posts/nginx-caching/request.png&quot; alt=&quot;Nginx Caching: HTTP request visual example&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Based on some details from it Nginx generates a hash key.&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy&quot; itemprop=&quot;image&quot; width=&quot;493&quot; height=&quot;382&quot; src=&quot;/assets/1441437275524/img/posts/nginx-caching/nginx-cache-2.png&quot; alt=&quot;Nginx Caching: how the cache key is created&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now Nginx checks if this hash key already exists in memory. If not, the request goes to the application.&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy&quot; itemprop=&quot;image&quot; width=&quot;493&quot; height=&quot;382&quot; src=&quot;/assets/1441437275524/img/posts/nginx-caching/nginx-cache-3.png&quot; alt=&quot;Nginx Caching: &quot; /&gt;&lt;/p&gt;

&lt;p&gt;The application answers and its response is saved to the file system.&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy&quot; itemprop=&quot;image&quot; width=&quot;493&quot; height=&quot;382&quot; src=&quot;/assets/1441437275524/img/posts/nginx-caching/nginx-cache-4.png&quot; alt=&quot;Nginx Caching: &quot; /&gt;&lt;/p&gt;

&lt;p&gt;Additionally the hash key, generated earlier, is saved into memory. To make it easier to understand I visualise the hash key value together with the location of the saved file. But remember that Nginx stores in memory only the hash key.&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy&quot; itemprop=&quot;image&quot; width=&quot;493&quot; height=&quot;382&quot; src=&quot;/assets/1441437275524/img/posts/nginx-caching/nginx-cache-5.png&quot; alt=&quot;Nginx Caching: &quot; /&gt;&lt;/p&gt;

&lt;p&gt;Finally the user gets the response back.&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy&quot; itemprop=&quot;image&quot; width=&quot;493&quot; height=&quot;382&quot; src=&quot;/assets/1441437275524/img/posts/nginx-caching/nginx-cache-6.png&quot; alt=&quot;Nginx Caching: &quot; /&gt;&lt;/p&gt;

&lt;p&gt;When our client requests the same url a second time, Nginx again generates the hash key and checks if it exists in the memory. This time it’s there, so Nginx serves the cached file from the file system which is associated with the hash key.&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy&quot; itemprop=&quot;image&quot; width=&quot;493&quot; height=&quot;382&quot; src=&quot;/assets/1441437275524/img/posts/nginx-caching/nginx-cache-7.png&quot; alt=&quot;Nginx Caching: &quot; /&gt;&lt;/p&gt;

&lt;p&gt;Please note that the second request doesn’t involve the application at all.&lt;/p&gt;

&lt;p&gt;For the second request clients don’t wait for the application to first fetch data from the database and then render the page. Instead Nginx serves a static file with a cached version of the response.&lt;/p&gt;

&lt;p&gt;Additionally those files are most probably cached in memory. This time not by Nginx (although it gives the OS some hints) but by the operating system. It’s a Linux’s attribute to use the resources as efficient as possible. This makes reading files from the file system extremely fast.&lt;/p&gt;

&lt;h3 id=&quot;configuration---available-directives&quot;&gt;Configuration - Available Directives&lt;/h3&gt;

&lt;p&gt;Now that we know the technical background, of how Nginx’s cache works, let’s see how we can configure it.&lt;/p&gt;

&lt;p&gt;First on the &lt;code&gt;http&lt;/code&gt; level we define where the data should be stored. We specify the path on the file system and the memory zone and their size. The &lt;strong&gt;memory zone&lt;/strong&gt; stores &lt;strong&gt;only meta&lt;/strong&gt; information on cached items - hash keys.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;
&lt;br /&gt; Each item takes about &lt;code&gt;0.125 kB&lt;/code&gt; of memory so we can store a lot of them. In &lt;code&gt;1MB&lt;/code&gt; for example Nginx can store about &lt;code&gt;8000&lt;/code&gt; cache keys.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;basics&quot;&gt;Basics&lt;/h3&gt;

&lt;p&gt;The basic cache definition looks like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;proxy_cache_path /data/nginx/cache &lt;span class=&quot;nv&quot;&gt;keys_zone&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;zone_name_one:10m&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To enable it we just use the &lt;code&gt;proxy_cache&lt;/code&gt; directive.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;proxy_cache zone_name_one&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This video will show you the basics of Nginx caching.&lt;/p&gt;

&lt;iframe width=&quot;853&quot; height=&quot;480&quot; src=&quot;https://www.youtube.com/embed/I07MEdsSMOs?rel=0&quot; frameborder=&quot;0&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;

&lt;p&gt;The Github project mentioned in the video can be found &lt;a href=&quot;https://github.com/czerasz/nginx-caching-example&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It’s also possible to limit the size of the file system used to store cached content, simply by adding the &lt;code&gt;max_size&lt;/code&gt; parameter.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;proxy_cache_path /data/nginx/cache &lt;span class=&quot;nv&quot;&gt;keys_zone&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;one:10m &lt;span class=&quot;code-highlight-red&quot;&gt;max_size=200m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then we have also the option to say when files should be removed (from the cache regardless of their freshness) when they are not used for a specific amount of time.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;proxy_cache_path /data/nginx/cache &lt;span class=&quot;nv&quot;&gt;keys_zone&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;one:10m &lt;span class=&quot;code-highlight-red&quot;&gt;inactive=60m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And there is also a nice little parameter which let’s us define the hierarchy levels of a cache.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;proxy_cache_path /data/nginx/cache &lt;span class=&quot;code-highlight-red&quot;&gt;levels=1:2&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;keys_zone&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;one:10m&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The image below illustrates the hierarchy levels configuration:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy&quot; itemprop=&quot;image&quot; width=&quot;427&quot; height=&quot;102&quot; src=&quot;/assets/1441437275524/img/posts/nginx-caching/levels-example-1.png&quot; alt=&quot;Nginx Caching: proxy_cache_path levels example&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now let’s see if you understand it correctly. Ready for a quiz? Based on the illustration below, what should the levels parameter be?&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy&quot; itemprop=&quot;image&quot; width=&quot;427&quot; height=&quot;102&quot; src=&quot;/assets/1441437275524/img/posts/nginx-caching/levels-example-2.png&quot; alt=&quot;Nginx Caching: proxy_cache_path levels example&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The answer is &lt;code&gt;2:4&lt;/code&gt;, which is more clear if we look at the following image:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy&quot; itemprop=&quot;image&quot; width=&quot;427&quot; height=&quot;102&quot; src=&quot;/assets/1441437275524/img/posts/nginx-caching/levels-example-3.png&quot; alt=&quot;Nginx Caching: proxy_cache_path levels example&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;whatwhen-to-cache&quot;&gt;What/When to cache?&lt;/h3&gt;

&lt;p&gt;By default Nginx caches only GET and HEAD requests. You can change this with the &lt;code&gt;proxy_cache_methods&lt;/code&gt; directive:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;proxy_cache_methods GET HEAD POST&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can also instruct Nginx to cache the response only after it was requested at least 5 times. &lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;proxy_cache_min_uses 5&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This would be useful in a situation where you have a lot of content, but you would only like to cache only those requests which are really popular.&lt;/p&gt;

&lt;p&gt;Nginx can save us upstream bandwidth and disk writes as well. By respecting cache headers and &lt;code&gt;304, not modified&lt;/code&gt; responses Nginx will not download the content again if the following directive is turned on:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;proxy_cache_revalidate on&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id=&quot;how-to-cache&quot;&gt;How to cache?&lt;/h3&gt;

&lt;p&gt;With Nginx we are not limited to cache everything by the same rule. Instead we can tell Nginx which information should be used to generate the hash key. And we can do it on the &lt;code&gt;http&lt;/code&gt;, &lt;code&gt;server&lt;/code&gt; or &lt;code&gt;location&lt;/code&gt; level. Here are two examples:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;proxy_cache_key &lt;span class=&quot;s2&quot;&gt;&amp;quot;$host$request_uri$cookie_user&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;proxy_cache_key &lt;span class=&quot;s2&quot;&gt;&amp;quot;$scheme$proxy_host$uri$is_args$args&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Tip&lt;/strong&gt;
&lt;br /&gt;How to generate a Nginx cache hash key? Use this command:&lt;/p&gt;

  &lt;pre&gt;&lt;code&gt;echo -n ‘httpczerasz.com/time.php’ | md5sum
&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can also tell Nginx, under which conditions the request shouldn’t be stored in the cache.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;proxy_no_cache &lt;span class=&quot;nv&quot;&gt;$http_pragma&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$http_authorization&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$cookie_nocache&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$arg_nocache&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id=&quot;how-long-to-cache&quot;&gt;How long to cache?&lt;/h3&gt;

&lt;p&gt;There is a simple directive which tells Nginx how long to cache the responses of a certain type:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;proxy_cache_valid any      1m&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
proxy_cache_valid &lt;span class=&quot;m&quot;&gt;200&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;302&lt;/span&gt; 10m&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;But mainly the headers of the origin server define the cacheability of the content:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Expires&lt;/li&gt;
  &lt;li&gt;Cache-Control&lt;/li&gt;
  &lt;li&gt;X-Accel-Expires - Nginx special header.
&lt;br /&gt;&lt;strong&gt;Overrides other headers&lt;/strong&gt;. Used when you need to serve different headers to the client.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The priority of the mentioned options are presented on the chart below, with the strongest priority on top:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy&quot; itemprop=&quot;image&quot; width=&quot;440&quot; height=&quot;600&quot; src=&quot;/assets/1441437275524/img/posts/nginx-caching/nginx-cache-priority.png&quot; alt=&quot;Nginx Caching: caching headers priority&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;other&quot;&gt;Other&lt;/h3&gt;

&lt;p&gt;The following option adds a lot to the availability factor. It allows Nginx to serve stale (old, expired) content when the application response timed out or returned a &lt;code&gt;50x&lt;/code&gt; status code:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;proxy_cache_use_stale error timeout&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Yet another cool feature is the ability to let only the first request through to the application. This can be enabled with:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;proxy_cache_lock on&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You already know, that the returned content by the origin is streamed to disk. By default to a location defined in the &lt;code&gt;proxy_cache_path&lt;/code&gt; directive. You could also store those files in a temporary directory before they are moved to the cache path. If you need this behaviour use this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;proxy_temp_path /tmp/custom_cache/&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This directive works good with multiple caches. But remember that it will be always less efficient than if the temp path is the same as the cache path&lt;/p&gt;

&lt;h2 id=&quot;debugging-nginx39s-cache&quot;&gt;Debugging Nginx&#39;s Cache&lt;/h2&gt;

&lt;p&gt;I believe that debugging any software the right way is even more important than actually knowing the software itself. A good debugging process guarantees a better understanding and most important, solving challenges really fast.&lt;/p&gt;

&lt;p&gt;In this chapter I will show you few tricks to easily debug Nginx’s caching wold.&lt;/p&gt;

&lt;p&gt;The first trick is about bypassing the cache.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;proxy_cache_bypass &lt;span class=&quot;nv&quot;&gt;$arg_nocache&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$cookie_nocache&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$arg_comment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The directive above allows you to specify when to omit the cache. This means that each request with a &lt;code&gt;nocache=true&lt;/code&gt; query parameter goes to the origin. It might even be that Nginx will cache the result as well.&lt;/p&gt;

&lt;p&gt;Another trick is adding a header with cache status information. You can do it simply by adding a header:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;add_header X-Cache-Status &lt;span class=&quot;nv&quot;&gt;$upstream_cache_status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Or in a more sophisticated way presented below, which allows only local requests to view the header:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;map &lt;span class=&quot;nv&quot;&gt;$remote_addr&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$cache_status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    127.0.0.1 &lt;span class=&quot;nv&quot;&gt;$upstream_cache_status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    default &lt;span class=&quot;s2&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
...
add_header X-Cache-Status &lt;span class=&quot;nv&quot;&gt;$cache_status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;By debugging the &lt;code&gt;X-Cache-Status&lt;/code&gt; header you will appreciate the following table:&lt;/p&gt;

&lt;table class=&quot;table  table-striped&quot;&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;strong&gt;MISS&lt;/strong&gt;&lt;/td&gt;
            &lt;td&gt;Object was not found in the cache. Response was served from the origin. Response may have been saved to cache.&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;strong&gt;BYPASS&lt;/strong&gt;&lt;/td&gt;
            &lt;td&gt;Got response from upstream. Response may have been saved to cache.&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;strong&gt;EXPIRED&lt;/strong&gt;&lt;/td&gt;
            &lt;td&gt;Cached object has expired. Response was served from the upstream.&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;strong&gt;STALE&lt;/strong&gt;&lt;/td&gt;
            &lt;td&gt;Object served from cache because of issues with origin server response&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;strong&gt;UPDATING&lt;/strong&gt;&lt;/td&gt;
            &lt;td&gt;Serve stale content from cache because &lt;code&gt;proxy_cache_lock&lt;/code&gt; has timed out and &lt;code&gt;proxy_use_stale&lt;/code&gt; takes controll&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;strong&gt;REVALIDATED&lt;/strong&gt;&lt;/td&gt;
            &lt;td&gt;&lt;code&gt;proxy_cache_revalidate&lt;/code&gt; verified that the current cached content was still valid&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;strong&gt;HIT&lt;/strong&gt;&lt;/td&gt;
            &lt;td&gt;The object was found in the cache and it is served from there&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;In the following two chapters we will learn which processes are involved in the maintenance of cached files. This chapters are really short and right after we will jump into cool examples. So stay with me!&lt;/p&gt;

&lt;h2 id=&quot;cache-loader&quot;&gt;Cache Loader&lt;/h2&gt;

&lt;p&gt;The Nginx cache loader is a process responsible for loading cache from disk.&lt;/p&gt;

&lt;p&gt;It is run only once (on startup) and loads the metadata into the memory zone. It runs in iterations until all keys are loaded.&lt;/p&gt;

&lt;p&gt;We can tune it’s behaviour (to utilize CPU the right way) with the following options:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code&gt;loader_threshold&lt;/code&gt; - how long is one iteration time&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;loader_files&lt;/code&gt; - don’t load more items than&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;loader_sleeps&lt;/code&gt; - pause time between iterations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An example is presented below:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;proxy_cache_path /data/nginx/cache &lt;span class=&quot;nv&quot;&gt;keys_zone&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;one:10m &lt;span class=&quot;code-highlight-red&quot;&gt;[loader_files=number] [loader_sleep=time] [loader_threshold=time]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The comic below shows that the cache loader is used only on startup and that it works in iterations defined by the described parameters.&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy&quot; itemprop=&quot;image&quot; width=&quot;839&quot; height=&quot;261&quot; src=&quot;/assets/1441437275524/img/posts/nginx-caching/nginx-loader.png&quot; alt=&quot;Nginx Caching: how does the nginx cache loader works?&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;cache-manager&quot;&gt;Cache Manager&lt;/h2&gt;

&lt;p&gt;The Nginx cache manager is a process which purges the cache over time.&lt;/p&gt;

&lt;p&gt;It periodically checks file storage and removes least recently used data if the file size exceeds &lt;code&gt;max_size&lt;/code&gt;. It also removes files which were not used independently of the cache settings.&lt;/p&gt;

&lt;p&gt;Watch the comic below which illustrates cache manager duties.&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy&quot; itemprop=&quot;image&quot; width=&quot;839&quot; height=&quot;261&quot; src=&quot;/assets/1441437275524/img/posts/nginx-caching/nginx-manager.png&quot; alt=&quot;Nginx Caching: how does the nginx cache manager works?&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;examples&quot;&gt;Examples&lt;/h2&gt;

&lt;p&gt;In this chapter I will present two real world examples. &lt;/p&gt;

&lt;p&gt;The first one is based on what we have learned so far. We will see how to purge content which was previously cached. &lt;/p&gt;

&lt;p&gt;The second example is a little bit more advanced. I shows how to build a small CDN based on Nginx.&lt;/p&gt;

&lt;h3 id=&quot;how-to-create-a-small-cdn-with-nginx&quot;&gt;How to create a small CDN with Nginx?&lt;/h3&gt;

&lt;p&gt;This example describes how Nginx can help you create a simple, file based CDN with static content.&lt;/p&gt;

&lt;p&gt;Let’s assume we have a distributed file system cluster. It can be based on &lt;a href=&quot;http://en.wikipedia.org/wiki/Network_File_System&quot; target=&quot;_blank&quot;&gt;NFS&lt;/a&gt; or &lt;a href=&quot;http://www.gluster.org/&quot; target=&quot;_blank&quot;&gt;GlusterFS&lt;/a&gt; or on anything you prefer. One thing those technologies have in common is that they are slow. Luckily Nginx together with the Slow FS module can overcome this weakness by caching files from a network file system on the local drive.&lt;/p&gt;

&lt;p&gt;For this example I prepared a project on Github which you can find &lt;a href=&quot;https://github.com/czerasz/nginx-slowfs-example/&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;. An overview is presented on the diagram below:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;post__image--spacy&quot; itemprop=&quot;image&quot; width=&quot;100%&quot; src=&quot;/assets/1441437275524/img/posts/nginx-caching/nginx-slowfs.png&quot; alt=&quot;Nginx Caching: nginx slowfs overview&quot; /&gt;&lt;/p&gt;

&lt;p&gt;To get started simply clone the repository:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;git clone git@github.com:czerasz/nginx-slowfs-example.git&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Once you have it go to the project’s directory and start the Docker container with:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;docker-compose up&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This might take a while because a Nginx compilation process takes place inside the container. To be able to use the slowfs feature we need to compile Nginx together with the &lt;a href=&quot;http://labs.frickle.com/nginx_ngx_slowfs_cache/&quot; target=&quot;_blank&quot;&gt;Slow FS module&lt;/a&gt;. This is done in the &lt;code&gt;Dockerfile&lt;/code&gt;. If you are interested how this is done check it out &lt;a href=&quot;https://github.com/czerasz/nginx-slowfs-example/blob/master/Dockerfile&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Then request Nginx with &lt;code&gt;curl&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;curl -i &lt;span class=&quot;s1&quot;&gt;&amp;#39;localhost:8000/test-file.txt&amp;#39;&lt;/span&gt;
HTTP/1.1 &lt;span class=&quot;m&quot;&gt;200&lt;/span&gt; OK
Server: nginx/1.6.2
Date: Mon, &lt;span class=&quot;m&quot;&gt;23&lt;/span&gt; Mar &lt;span class=&quot;m&quot;&gt;2015&lt;/span&gt; 13:35:07 GMT
Content-Type: text/plain
Content-Length: 4
Last-Modified: Mon, &lt;span class=&quot;m&quot;&gt;23&lt;/span&gt; Mar &lt;span class=&quot;m&quot;&gt;2015&lt;/span&gt; 12:48:15 GMT
Connection: keep-alive
Accept-Ranges: bytes

&lt;span class=&quot;nb&quot;&gt;test&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;What you see is (the content of) the file stored in the &quot;mounted&quot; &lt;a href=&quot;https://github.com/czerasz/nginx-slowfs-example/tree/master/nfs-dummy&quot; target=&quot;_blank&quot;&gt;&lt;code&gt;nfs-dummy&lt;/code&gt; directory&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If this directory would be a busy NFS mount and the number of requests would be high, then the responses would be really slow.&lt;/p&gt;

&lt;p&gt;But in our example the files are cached locally. You can inspect the cache directory with the following command:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;docker &lt;span class=&quot;nb&quot;&gt;exec &lt;/span&gt;nginxslowfs_nginx_1 tree -A /data/cache/
/data/cache/
└── 1
    └── 27
        └── 2bba799df783554d8402137ca199a271&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The Nginx configuration for our example is simple and consists of two parts. The cache definition:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;# Configure slowfs module&lt;/span&gt;
slowfs_cache_path  /data/cache &lt;span class=&quot;nv&quot;&gt;levels&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1:2 &lt;span class=&quot;nv&quot;&gt;keys_zone&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;fastcache:10m&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
slowfs_temp_path   /data/temp &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; 2&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And the cache enabling section:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;location / &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    root                /data/nfs&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    slowfs_cache        fastcache&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    slowfs_cache_key    &lt;span class=&quot;nv&quot;&gt;$uri&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    slowfs_cache_valid  1d&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    index  index.html index.htm&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;One nifty feature of the Slow FS module is &lt;strong&gt;cache purging&lt;/strong&gt;. It can be enabled with the following configuration block:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;location ~ /purge&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;/.*&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    allow               127.0.0.1&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    deny                all&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    slowfs_cache_purge  fastcache &lt;span class=&quot;nv&quot;&gt;$1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This allows purging for internal requests&lt;/p&gt;

&lt;p&gt;Let’s test it with &lt;code&gt;curl&lt;/code&gt; from inside of the docker container:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;docker &lt;span class=&quot;nb&quot;&gt;exec &lt;/span&gt;nginxslowfs_nginx_1 curl -i &lt;span class=&quot;s1&quot;&gt;&amp;#39;localhost:80/purge/test-file.txt&amp;#39;&lt;/span&gt; 
HTTP/1.1 &lt;span class=&quot;m&quot;&gt;200&lt;/span&gt; OK
Server: nginx/1.6.2
Date: Tue, &lt;span class=&quot;m&quot;&gt;24&lt;/span&gt; Mar &lt;span class=&quot;m&quot;&gt;2015&lt;/span&gt; 13:04:25 GMT
Content-Type: text/html
Content-Length: 263
Connection: keep-alive

&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Successful purge&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;
&amp;lt;body &lt;span class=&quot;nv&quot;&gt;bgcolor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;white&amp;quot;&lt;/span&gt;&amp;gt;
&amp;lt;center&amp;gt;&amp;lt;h1&amp;gt;Successful purge&amp;lt;/h1&amp;gt;
&amp;lt;br&amp;gt;Key : /test-file.txt
&amp;lt;br&amp;gt;Path: /data/cache/1/27/2bba799df783554d8402137ca199a271
&amp;lt;/center&amp;gt;
&amp;lt;hr&amp;gt;&amp;lt;center&amp;gt;nginx/1.6.2&amp;lt;/center&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Nginx says that it was successful but you can always double-check with the following command:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;docker &lt;span class=&quot;nb&quot;&gt;exec &lt;/span&gt;nginxslowfs_nginx_1 tree -A /data/cache/                        
/data/cache/
└── 1
    └── 27&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id=&quot;bonus&quot;&gt;Bonus&lt;/h2&gt;

&lt;p&gt;Now, because you came so far, I would like to honour your effort. I want to appoint you as the Cache King of Nginx’s land.&lt;/p&gt;

&lt;p&gt;&lt;img itemprop=&quot;image&quot; width=&quot;100%&quot; src=&quot;/assets/1441437275524/img/posts/nginx-caching/crown-foto-preview.jpg&quot; alt=&quot;Nginx Caching: paper crown photo&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Please wear &lt;a href=&quot;/assets/1441437275524/caching-king-paper-crown.pdf&quot; target=&quot;_blank&quot;&gt;this crone&lt;/a&gt; with pride and swear to use cache wisely and spread its power all over the world.&lt;/p&gt;

&lt;p&gt;&lt;img itemprop=&quot;image&quot; width=&quot;100%&quot; src=&quot;/assets/1441437275524/img/posts/nginx-caching/crown-preview.png&quot; alt=&quot;Nginx Caching: paper crown template&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;resources&quot;&gt;Resources:&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://nginx.com/resources/admin-guide/caching/&quot;&gt;NGINX Content Caching&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://nginx.org/en/docs/http/ngx_http_proxy_module.html&quot;&gt;Nginx Caching Manual&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://kura.io/2010/02/14/more-nginx-proxy_cache-optimizations-and-nginx-load-balancing/&quot;&gt;More nginx proxy_cache optimizations and nginx load balancing&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.digitalocean.com/community/tutorials/understanding-nginx-http-proxying-load-balancing-buffering-and-caching&quot;&gt;Understanding Nginx HTTP Proxying, Load Balancing, Buffering, and Caching&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Mon, 30 Mar 2015 00:00:00 +0200</pubDate>
        <link>http://czerasz.com/2015/03/30/nginx-caching-tutorial/</link>
        <guid isPermaLink="true">http://czerasz.com/2015/03/30/nginx-caching-tutorial/</guid>
        
        <category>nginx</category>
        
        <category>caching</category>
        
        <category>performance</category>
        
        <category>web server</category>
        
        
      </item>
    
      <item>
        <title>New Face for czerasz.com - Logotype Analysis</title>
        <description>&lt;p class=&quot;post__tldr&quot; itemprop=&quot;description&quot;&gt;Read about czerasz.com new logotype. In this article you can follow the logotype creative process - from keywords through associations to achieve a new logotype.&lt;/p&gt;

&lt;p&gt;Today I have an important announcement: the website czerasz.com got a fresh face. We created a new logotype which respresents the things we believe in. Ladies and gentlemen here is the brand new appearance of czerasz.com:&lt;/p&gt;

&lt;p&gt;&lt;img itemprop=&quot;image&quot; id=&quot;new-logotype&quot; width=&quot;400&quot; height=&quot;300&quot; src=&quot;/assets/1441437275524/img/posts/new-logotype.png&quot; alt=&quot;new czerasz.com logotype&quot; title=&quot;&quot; class=&quot;img-responsive center-block&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We thought that some of you might be interested in the paths which we followed to reach this goal.&lt;/p&gt;

&lt;p&gt;One of the methods which apeared for us to be efficient during a creative process is mind mapping. The method as efficient as it is brings additionally a huge fun factor. It’s very simple - during a brainstorming session start with a single keyword and continue with others which are related. Watch out, it can quickly get very complex as shown in &lt;a href=&quot;http://www.mindmeister.com/104477225/summary-of-eric-ries-talk-at-google&quot; target=&quot;_blank&quot;&gt;this example&lt;/a&gt;. To be sure that your map is always readable use the right tool like &lt;a href=&quot;http://www.mindmeister.com/&quot; target=&quot;_blank&quot; title=&quot;mind map online tool&quot;&gt;mindmeister.com&lt;/a&gt; or &lt;a href=&quot;http://www.lucidchart.com/&quot; target=&quot;_blank&quot; title=&quot;mind map online tool&quot;&gt;lucidchart.com&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The mind map below visualises the path from brand attributes, various associations to finally achieve the logotype we love.&lt;/p&gt;

&lt;p&gt;&lt;img itemprop=&quot;image&quot; id=&quot;logotype-mind-map&quot; width=&quot;100%&quot; src=&quot;/assets/1441437275524/img/posts/brand-mind-map.png&quot; alt=&quot;czerasz.com logotype mind-map&quot; title=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;During this process we used the following keywords:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;label label-default&quot;&gt;strength&lt;/span&gt; &lt;span class=&quot;label label-default&quot;&gt;gladiator&lt;/span&gt; &lt;span class=&quot;label label-default&quot;&gt;goal oriented&lt;/span&gt; &lt;span class=&quot;label label-default&quot;&gt;will&lt;/span&gt; &lt;span class=&quot;label label-default&quot;&gt;idea&lt;/span&gt; &lt;span class=&quot;label label-default&quot;&gt;science&lt;/span&gt; &lt;span class=&quot;label label-default&quot;&gt;phisics&lt;/span&gt; &lt;span class=&quot;label label-default&quot;&gt;Möbius strip&lt;/span&gt; &lt;span class=&quot;label label-default&quot;&gt;atom&lt;/span&gt; &lt;span class=&quot;label label-default&quot;&gt;octagon&lt;/span&gt; &lt;span class=&quot;label label-default&quot;&gt;solution&lt;/span&gt; &lt;span class=&quot;label label-default&quot;&gt;complexity&lt;/span&gt; &lt;span class=&quot;label label-default&quot;&gt;think different&lt;/span&gt; &lt;span class=&quot;label label-default&quot;&gt;limitless&lt;/span&gt; &lt;span class=&quot;label label-default&quot;&gt;infinity&lt;/span&gt; &lt;span class=&quot;label label-default&quot;&gt;technology&lt;/span&gt; &lt;span class=&quot;label label-default&quot;&gt;IT&lt;/span&gt; &lt;span class=&quot;label label-default&quot;&gt;code&lt;/span&gt; &lt;span class=&quot;label label-default&quot;&gt;international&lt;/span&gt; &lt;span class=&quot;label label-default&quot;&gt;multilingual&lt;/span&gt; &lt;span class=&quot;label label-default&quot;&gt;communication&lt;/span&gt; &lt;span class=&quot;label label-default&quot;&gt;passion&lt;/span&gt; &lt;span class=&quot;label label-default&quot;&gt;energy&lt;/span&gt; &lt;span class=&quot;label label-default&quot;&gt;fire&lt;/span&gt; &lt;span class=&quot;label label-default&quot;&gt;flame&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;We analysed word after word, trying to find something on the abstract field. Something that has a clear shape and meaningful appearance. Finaly after  several tries and failures we found the basic shapes:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;octagon&lt;/strong&gt; represents our warrior nature, the nature to achieve… through a scientific approach&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;infinity&lt;/strong&gt; means for us that impossible is nothing&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;code&lt;/strong&gt; says that we are technology driven, that everything IT related fascinates us&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;communication&lt;/strong&gt; is our basic tool, whose importance we weight with the &lt;a href=&quot;http://en.wikipedia.org/wiki/Chandrasekhar_limit&quot;&gt;chandrasekhar limit&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;flame&lt;/strong&gt; stands for our passion, our will to lighten the internet sky more than a supernova&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Building on those entities we were able to combine the shapes into one clear homogeneous logotype.&lt;/p&gt;

&lt;p&gt;You can find the above diagram/mind map also as a PDF version &lt;a href=&quot;/assets/1441437275524/brand-mind-map.pdf&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The whole process was more complex but we wanted to share at least a part of it with you. The one which we think is the most important.&lt;/p&gt;

&lt;h2 id=&quot;resources&quot;&gt;Resources&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;icons for the infographic were taken from &lt;a href=&quot;http://iconmonstr.com/&quot; target=&quot;_blank&quot;&gt;iconmonstr&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;icons for the infographic were made by &lt;a href=&quot;http://fontawesome.io&quot; title=&quot;Dave Gandy&quot; target=&quot;_blank&quot;&gt;Dave Gandy&lt;/a&gt;, &lt;a href=&quot;http://www.freepik.com&quot; title=&quot;Freepik&quot; target=&quot;_blank&quot;&gt;Freepik&lt;/a&gt; from &lt;a href=&quot;http://www.flaticon.com&quot; title=&quot;Flaticon&quot; target=&quot;_blank&quot;&gt;www.flaticon.com&lt;/a&gt; is licensed by &lt;a href=&quot;http://creativecommons.org/licenses/by/3.0/&quot; title=&quot;Creative Commons BY 3.0&quot; target=&quot;_blank&quot;&gt;CC BY 3.0&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Thu, 12 Feb 2015 00:00:00 +0100</pubDate>
        <link>http://czerasz.com/2015/02/12/new-czerasz-logotype/</link>
        <guid isPermaLink="true">http://czerasz.com/2015/02/12/new-czerasz-logotype/</guid>
        
        
      </item>
    
      <item>
        <title>Vagrant Workflows</title>
        <description>&lt;p class=&quot;post__tldr&quot; itemprop=&quot;description&quot;&gt;This article describes how to use Vagrant on a daily basis. I will introduce to you Vagrant&#39;s workflow routines and the most important and useful Vagrant commands.&lt;/p&gt;

&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;Vagrant is a helpful wrapper arround different VM providers. Vagrant is a very powerful tool and it is driven by simple workflows. And exactly those workflows are described in this article.&lt;/p&gt;

&lt;p&gt;We will discuss five stages/phases which represent a typical Vagrant workflow/life cycle.
&lt;br /&gt;They are presented on the diagram below.&lt;/p&gt;

&lt;p&gt;&lt;img itemprop=&quot;image&quot; id=&quot;image-vagrant-workflows-phases&quot; width=&quot;100%&quot; src=&quot;/assets/1441437275524/img/posts/vagrant-workflows-phases.png&quot; alt=&quot;five main phases in the vagrant workflow&quot; title=&quot;&quot; /&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;The first one is the &lt;strong&gt;VM preparation phase&lt;/strong&gt;.&lt;/p&gt;

    &lt;p&gt;In this phase we need to choose the base box (operating system), configure network details, file sharing options, setup plugins, &lt;abbr title=&quot;et cetera&quot;&gt;etc&lt;/abbr&gt;. During this phase we mainly work on the &lt;code&gt;Vagrantfile&lt;/code&gt; which is an configuration file, used by Vagrant to build our virtual machines.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Then we move on to the second phase which is called &lt;strong&gt;the first run&lt;/strong&gt;.&lt;/p&gt;

    &lt;p&gt;In this phase we spin up our virtual machines for the first time.&lt;/p&gt;

    &lt;p&gt;If during this phase we run into errors, we can be sure that those are related to the VM configuration itself or to the host system in which we are trying to run our VM’s. If this is the case we need to fix those issues and then reload the VM’s again. &lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;The next step is the &lt;strong&gt;software configuration phase&lt;/strong&gt;.&lt;/p&gt;

    &lt;p&gt;Together with the provisioning phase it is the most time consuming part.&lt;/p&gt;

    &lt;p&gt;Here we need to decide which technology we want to use to install software on our VM’s And after this decision is made we need to configure it so the VM has the software which is required.&lt;/p&gt;

    &lt;p&gt;Vagrant supports many popular provisioners like &lt;a href=&quot;http://www.ansible.com/&quot;&gt;Ansible&lt;/a&gt;, &lt;a href=&quot;http://puppetlabs.com/&quot;&gt;Puppet&lt;/a&gt;, &lt;a href=&quot;https://www.chef.io/&quot;&gt;Chef&lt;/a&gt;, &lt;a href=&quot;http://www.saltstack.com/&quot;&gt;Salt&lt;/a&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;After the provisioner is setup we continue with the &lt;strong&gt;provisioning phase&lt;/strong&gt;.&lt;/p&gt;

    &lt;p&gt;During this phase Vagrant will install the software via the provisioner configured in the previous step.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;The last phase is the &lt;strong&gt;usage phase&lt;/strong&gt;.&lt;/p&gt;

    &lt;p&gt;In this phase we enjoy the fruits of the previous two steps - we use the VM’s to acomplish our actual goal.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;requirements&quot;&gt;Requirements&lt;/h2&gt;

&lt;p&gt;To be able to execute commands in the chapters below you need the following:&lt;/p&gt;

&lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;https://www.virtualbox.org/&quot; target=&quot;_blank&quot;&gt;Virtualbox&lt;/a&gt; or any other provider&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;http://www.vagrantup.com/&quot; target=&quot;_blank&quot;&gt;Vagrant&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;On Ubuntu host install NFS (on Mac it&#39;s already there):
&lt;p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;sudo apt-get install nfs-common nfs-kernel-server -y&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;vagrant-life-cycle-phases-in-detail&quot;&gt;Vagrant Life Cycle Phases in Detail&lt;/h2&gt;

&lt;h3 id=&quot;vm-preparation-phase&quot;&gt;VM Preparation Phase&lt;/h3&gt;

&lt;p&gt;This is the first step so we need to prepare our VM requirements.&lt;/p&gt;

&lt;p&gt;We should ask the following questions:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;which operating system do we want to use? Ubuntu, CentOS, CoreOS or maybe Gentoo?&lt;/li&gt;
  &lt;li&gt;do we need port forwarding?&lt;/li&gt;
  &lt;li&gt;do we need a static IP?&lt;/li&gt;
  &lt;li&gt;how much memory does our machine need?&lt;/li&gt;
  &lt;li&gt;do we need to expose any files from our VM?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When this is calified we start writing our &lt;code&gt;Vagrantfile&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can begin with creating a totally new, well commented &lt;code&gt;Vagrantfile&lt;/code&gt; by executing the following command:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;vagrant init ubuntu/trusty64&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;
&lt;br /&gt;To get more information about the used &lt;code&gt;ubuntu/trusty64&lt;/code&gt; base box &lt;a href=&quot;https://atlas.hashicorp.com/ubuntu/boxes/trusty64&quot;&gt;click here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Or just copy the the simple &lt;code&gt;Vagrantfile&lt;/code&gt; provided below:&lt;/p&gt;

&lt;div id=&quot;vagrantfile-example&quot;&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# -*- mode: ruby -*-&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# vi: set ft=ruby :&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Vagrantfile API/syntax version. Don&amp;#39;t touch unless you know what you&amp;#39;re doing!&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;VAGRANTFILE_API_VERSION&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;2&amp;quot;&lt;/span&gt;

&lt;span class=&quot;no&quot;&gt;Vagrant&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;configure&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;VAGRANTFILE_API_VERSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;box&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;ubuntu/trusty64&amp;quot;&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;network&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;forwarded_port&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;guest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;host&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8080&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;synced_folder&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;./shared&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;/home/vagrant/shared&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;nfs&amp;quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;
&lt;br /&gt;You can get more base boxes from the &lt;a href=&quot;https://atlas.hashicorp.com/boxes/search&quot;&gt;official Vagrant repository&lt;/a&gt;, form the &lt;a href=&quot;http://www.vagrantbox.es/&quot;&gt;community repository&lt;/a&gt; or configure your whole environment with Puppet at &lt;a href=&quot;https://puphpet.com/&quot;&gt;puphpet.com&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now that we have our simple Vagrant configuration we will start our first VM.&lt;/p&gt;

&lt;h3 id=&quot;first-run-phase&quot;&gt;First Run Phase&lt;/h3&gt;

&lt;p&gt;The first run phase is the most important step because we need to make sure that the environment is setup correctly.&lt;/p&gt;

&lt;p&gt;We can start our first VM with the following command:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;vagrant up&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you used the &lt;a href=&quot;#vagrantfile-example&quot;&gt;&lt;code&gt;Vagrantfile&lt;/code&gt; provided above&lt;/a&gt; you will experience the following issue:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;vagrant up
Bringing machine &lt;span class=&quot;s1&quot;&gt;&amp;#39;default&amp;#39;&lt;/span&gt; up with &lt;span class=&quot;s1&quot;&gt;&amp;#39;virtualbox&amp;#39;&lt;/span&gt; provider...
There are errors in the configuration of this machine. Please fix
the following errors and try again:

vm:
* The host path of the shared folder is missing: ./shared&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It’s easy to fix by creating a &lt;code&gt;shared&lt;/code&gt; directory inside the directory which contains the &lt;code&gt;Vagrantfile&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;mkdir shared&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Although this is an easy fix, it illustrates that when an error occured during the first run phase we need to reload or even start our VM’s again - like presented in the &lt;a href=&quot;#image-vagrant-workflows-phases&quot;&gt;Vagrant workflow phases diagram&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A good way to say either we need to start or reload, is to check the status by using the &lt;code&gt;vagrant status&lt;/code&gt; command, as in the example below:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;vagrant status
Current machine states:

default                   not created &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;virtualbox&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
...&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In our case the VM was not created at all so we need to start it again:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;vagrant up&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This time we should get the following issue:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;vagrant up  
Bringing machine &lt;span class=&quot;s1&quot;&gt;&amp;#39;default&amp;#39;&lt;/span&gt; up with &lt;span class=&quot;s1&quot;&gt;&amp;#39;virtualbox&amp;#39;&lt;/span&gt; provider...
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&amp;gt; default: Importing base box &lt;span class=&quot;s1&quot;&gt;&amp;#39;ubuntu/trusty64&amp;#39;&lt;/span&gt;...
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&amp;gt; default: Matching MAC address &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; NAT networking...
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&amp;gt; default: Checking &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; box &lt;span class=&quot;s1&quot;&gt;&amp;#39;ubuntu/trusty64&amp;#39;&lt;/span&gt; is up to date...
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&amp;gt; default: Setting the name of the VM: vagrant-test_default_1420393989632_21390
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&amp;gt; default: Clearing any previously &lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;forwarded ports...
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&amp;gt; default: Clearing any previously &lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;network interfaces...
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&amp;gt; default: Preparing network interfaces based on configuration...
    default: Adapter 1: &lt;span class=&quot;nv&quot;&gt;nat&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&amp;gt; default: Forwarding ports...
    default: &lt;span class=&quot;nv&quot;&gt;22&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&amp;gt; &lt;span class=&quot;m&quot;&gt;2222&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;adapter 1&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&amp;gt; default: Booting VM...
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&amp;gt; default: Waiting &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; its present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&amp;gt; default: Machine booted and ready!
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&amp;gt; default: Checking &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; guest additions in VM...
NFS requires a host-only network to be created.
Please add a host-only network to the machine &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;with either DHCP or a
static IP&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; NFS to work.&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It turns out that when a direcotry is shared over NFS (and that is our case), we need to assign an static IP to our VM.&lt;/p&gt;

&lt;p&gt;This can be easily achived by adding the following line to the &lt;code&gt;Vagrantfile&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;network&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;private_network&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;ip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;192.168.33.11&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This time we will just reload our VM because &lt;code&gt;vagrant status&lt;/code&gt; shows that it was already created:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;vagrant status 
Current machine states:

default                   running &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;virtualbox&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
...&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Reloading the VM can be achieved with the following command:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;vagrant reload&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now the first run phase is complete. Our VM has a basic configuration and it is up and running.&lt;/p&gt;

&lt;p&gt;We can summarize this chapter with this diagram:&lt;/p&gt;

&lt;p&gt;&lt;img width=&quot;100%&quot; src=&quot;/assets/1441437275524/img/posts/vagrant-workflows-1st-run.png&quot; alt=&quot;Vagrant workflows 1st run&quot; title=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Please note the shaded element on the right side of the diagram above. By executing &lt;code&gt;vagrant up&lt;/code&gt; for the first time, vagrant will also trigger the provisioning process. But because we didn’t configure it yet it will not run - that’s why it’s makred it as disabled.&lt;/p&gt;

&lt;p&gt;But heads up, we will continue with the provisioning phase in the next chapter.&lt;/p&gt;

&lt;h3 id=&quot;provisioner-configuration-and-provisioning&quot;&gt;Provisioner Configuration and Provisioning&lt;/h3&gt;

&lt;p&gt;The provisioning process often described as configuration management is my favourite part.&lt;/p&gt;

&lt;p&gt;It takes a lot of time to do it right but it adds a lot of value to the virtual machine - it describes the VM’s role. Specially the software installation (provisioning) part is very time consuming and sometimes requires a lot of tweaking.&lt;/p&gt;

&lt;p&gt;To minimize frustration and wasted time I recommennd to use the Vagrant cachier plugin which is described in detail in &lt;a href=&quot;/2014/12/10/top-vagrant-plugins/#vagrant-cachier-plugin&quot;&gt;this blog post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;By using Vagrant we can choose from many technologies starting from simple shell scripts to more abstracted ones like Ansible or Puppet. You can find a full list of supported Vagrant provisioners &lt;a href=&quot;https://docs.vagrantup.com/v2/provisioning/index.html&quot;&gt;here in the Vagrant documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this chapter we want to achieve the &lt;strong&gt;Configured VM&lt;/strong&gt; state presented on the diagram below - we want to install/update software on our previously created virtual machine.&lt;/p&gt;

&lt;p&gt;&lt;img width=&quot;100%&quot; src=&quot;/assets/1441437275524/img/posts/vagrant-workflows-full-run.png&quot; alt=&quot;Vagrant workflows full run&quot; title=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As an example we will add a simple shell script which updates Ubuntu on every start (or provisioning) but just once a month.&lt;/p&gt;

&lt;p&gt;First let’s tell vagrant to use a shell script to provision our VM. Add the following line to the &lt;code&gt;Vagrantfile&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;provision&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;shell&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;provisioners/shell/update.sh&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We will also need an &lt;code&gt;update.sh&lt;/code&gt; file located under &lt;code&gt;provisioners/shell/&lt;/code&gt; in our project directory. It’s a good convention to keep each provisioners configuration in a separate directory. That way things are always organised.&lt;/p&gt;

&lt;p&gt;Now let’s look at the &lt;code&gt;update.sh&lt;/code&gt; itself:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;#!/bin/bash&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Update the system once every month&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;#@BEGIN: SYSTEM UPDATE SCRIPT&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;HOSTNAME&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;hostname&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;DATE_FILE&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/vagrant/provisioners/shell/.last-system-update-&lt;span class=&quot;nv&quot;&gt;$HOSTNAME&lt;/span&gt;.date
&lt;span class=&quot;nv&quot;&gt;NOW&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;date +&lt;span class=&quot;s2&quot;&gt;&amp;quot;%Y%m&amp;quot;&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Check if the $DATE_FILE exists&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# This file stores the date of the latest update&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; ! -f &lt;span class=&quot;nv&quot;&gt;$DATE_FILE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# $DATE_FILE file was not found&lt;/span&gt;

  &lt;span class=&quot;c&quot;&gt;# Write the current date into a temporary file&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$NOW&lt;/span&gt; &amp;gt; &lt;span class=&quot;nv&quot;&gt;$DATE_FILE&lt;/span&gt;

  &lt;span class=&quot;c&quot;&gt;# Update system&lt;/span&gt;
  apt-get update --fix-missing
&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# Get the date of the last update&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;LAST_UPDATE&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;head -n &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$DATE_FILE&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;

  &lt;span class=&quot;c&quot;&gt;# Check if update was done in the last month&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# Used only for testing purposes&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# LAST_UPDATE=$(( $LAST_UPDATE - 1 ))&lt;/span&gt;

  &lt;span class=&quot;c&quot;&gt;# If update was done at least two months ago do it again&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$LAST_UPDATE&lt;/span&gt; -lt &lt;span class=&quot;nv&quot;&gt;$NOW&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# Write the current date into a temporary file&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$NOW&lt;/span&gt; &amp;gt; &lt;span class=&quot;nv&quot;&gt;$DATE_FILE&lt;/span&gt;

    &lt;span class=&quot;c&quot;&gt;# Update system&lt;/span&gt;
    apt-get update --fix-missing
  &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;The system updates only once a month&amp;quot;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;fi&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;fi&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#@END: SYSTEM UPDATE SCRIPT&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The script checks if an “update file” for the current VM already exists in the &lt;code&gt;/vagrant/provisioners/shell/&lt;/code&gt; directory. If it exists then it will check the date inside. If the file doesn’t exist or the date is older than a month then the script will perform a system update.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;
&lt;br /&gt;The directory which stores the &lt;code&gt;Vagrantfile&lt;/code&gt; (the project directory) is automatically mounted under &lt;code&gt;/vagrant/&lt;/code&gt; on the VM.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The update script above is pretty straightforward and hopefully well documented. But keep in mind that you need to remove &lt;code&gt;provisioners/shell/.last-system-update-*&lt;/code&gt; each time after the VM’s was destroyed. Remember also to put &lt;code&gt;provisioners/shell/.last-system-update-*&lt;/code&gt; to &lt;code&gt;.gitignore&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Finally we can apply our software updates by executing &lt;code&gt;vagrant provision&lt;/code&gt;. The outcome are debug messages like those presented below and an updated Ubuntu on the VM:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;vagrant &lt;span class=&quot;nv&quot;&gt;provision&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&amp;gt; default: Running provisioner: shell...
    default: Running: /tmp/vagrant-shell20150106-13476-l2dxvx.sh
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&amp;gt; default: stdin: is not a &lt;span class=&quot;nv&quot;&gt;tty&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&amp;gt; default: Ign http://archive.ubuntu.com trusty &lt;span class=&quot;nv&quot;&gt;InRelease&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&amp;gt; default: Ign http://security.ubuntu.com trusty-security InRelease
...
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&amp;gt; default: Ign http://archive.ubuntu.com trusty/universe Translation-en_US
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&amp;gt; default: Fetched 9,249 kB in 18s &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;487&lt;/span&gt; kB/s&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&amp;gt; default: Reading package lists...&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It often happens so that during the provisioning process something goes wrong. In this case you need to go back to the preparation phase, adjust the configuration and then depending on your changes and the provisioner, execute &lt;code&gt;vagrant provision&lt;/code&gt; or &lt;code&gt;vagrant destroy; vagrant up&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You might ask why should I use &lt;code&gt;vagrant destroy; vagrant up&lt;/code&gt;? Why should I destroy my VM and build it from the scratch?&lt;/p&gt;

&lt;p&gt;Again as mentioned above: it depends on Your changes, used provisioner or even team structure. The reason why you would like to destroy your VM and then build it from the scratch is the confidence that when somebody new wants to build it he won’t face any troubles.&lt;/p&gt;

&lt;h3 id=&quot;usage&quot;&gt;Usage&lt;/h3&gt;

&lt;p&gt;In this phase we use the VM to achieve our business goal.&lt;/p&gt;

&lt;p&gt;We can login to the machine with:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;vagrant ssh&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;After we are logged in, we can mess arround and destroy everything we like, without worring too much. We are relaxed because we know that the VM can be  recreated anytime, anywhere.&lt;/p&gt;

&lt;h2 id=&quot;vagrant-command-workflow-overview---bonus&quot;&gt;Vagrant Command Workflow Overview - Bonus&lt;/h2&gt;

&lt;p&gt;For those of you who are tough enough and reached the end of this article I prepared a bonus in form of a Vagrant life cycle diagram. It represents which command can be used during each phase of the Vagrant life cycle. Enjoy!&lt;/p&gt;

&lt;p&gt;&lt;img width=&quot;100%&quot; src=&quot;/assets/1441437275524/img/posts/vagrant-main-workflow.png&quot; alt=&quot;&quot; title=&quot;&quot; /&gt;&lt;/p&gt;
</description>
        <pubDate>Tue, 06 Jan 2015 00:00:00 +0100</pubDate>
        <link>http://czerasz.com/2015/01/06/vagrant-workflows/</link>
        <guid isPermaLink="true">http://czerasz.com/2015/01/06/vagrant-workflows/</guid>
        
        
      </item>
    
      <item>
        <title>Top Vagrant Plugins You Should Use</title>
        <description>&lt;p class=&quot;post__tldr&quot; itemprop=&quot;description&quot;&gt;In this article I present the top Vagrant plugins which you should have. Those Vagrant plugins will make working with virtual machines much easier.&lt;/p&gt;

&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://www.vagrantup.com/&quot; target=&quot;_blank&quot;&gt;Vagrant&lt;/a&gt; is a very useful tool which abstracts virtual machine providers. It has a lot of features and is open source. If you don’t know it yet, then go directly to the &lt;a href=&quot;https://docs.vagrantup.com/v2/getting-started/index.html&quot; target=&quot;_blank&quot;&gt;getting started section&lt;/a&gt; of the Vagrant project page.&lt;/p&gt;

&lt;p&gt;Vagrant is popular but not everyone knows that it’s extensible. Yes, Vagrant can be tuned! And you do this with plugins. And some of them are really awesome!&lt;/p&gt;

&lt;p&gt;Discover with me the magic of most wanted Vagrant plugins.&lt;/p&gt;

&lt;h3 id=&quot;how-to-install-a-vagrant-plugin&quot;&gt;How To Install a Vagrant Plugin&lt;/h3&gt;

&lt;p&gt;Vagrant plugins can be installed with this command:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;vagrant plugin install vagrant-example-plugin&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If You want to read more about Vagrant plugins visit &lt;a href=&quot;https://docs.vagrantup.com/v2/plugins/usage.html&quot; target=&quot;_blank&quot;&gt;this documentation section&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&quot;divider  divider--border&quot;&gt;&lt;/div&gt;

&lt;h2 id=&quot;vagrant-cachier-plugin&quot;&gt;Vagrant Cachier Plugin&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;vagrant-cachier&lt;/code&gt; enables caching for different package managers on many Linux distributions. You will love this plugin if You play a lot with provisioning!&lt;/p&gt;

&lt;p&gt;You can find the Vagrant cachier plugin &lt;strong&gt;project&lt;/strong&gt; on &lt;a href=&quot;https://github.com/fgrehm/vagrant-cachier&quot; target=&quot;_blank&quot;&gt;Github&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;basic-understanding&quot;&gt;Basic Understanding&lt;/h3&gt;

&lt;p&gt;When this plugin is enabled and I update one of my Ubuntu VM’s with &lt;code&gt;apt-get update&lt;/code&gt; or install any packages with &lt;code&gt;apt-get install [package]&lt;/code&gt;, any other VM which uses the same base box, will reuse the downloads. This leads to faster provisioning. And the reason for that is because the updates/packages are cached. This brings a little bit of &lt;a href=&quot;https://www.docker.com/&quot; target=&quot;_blank&quot;&gt;Docker&lt;/a&gt; goodnes into the world of Vagrant.&lt;/p&gt;

&lt;p&gt;The diagram below helps you understand this simple but effective logic:&lt;/p&gt;

&lt;p&gt;&lt;img itemprop=&quot;image&quot; width=&quot;100%&quot; src=&quot;/assets/1441437275524/img/posts/vagrant-cachier-plugin-deb-packages-overview.png&quot; alt=&quot;Vagrant Cachier Plugin - deb packages overview&quot; title=&quot;Vagrant Cachier Plugin - deb packages overview&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;requirements&quot;&gt;Requirements&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;NFS&lt;/li&gt;
  &lt;li&gt;Vagrant 1.4+&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;installation&quot;&gt;Installation&lt;/h3&gt;

&lt;p&gt;Install the &lt;code&gt;vagrant-cachier&lt;/code&gt; plugin with the following command:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;vagrant plugin install vagrant-cachier&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id=&quot;usage&quot;&gt;Usage&lt;/h3&gt;

&lt;p&gt;Enable it by extending Your &lt;code&gt;Vagrantfile&lt;/code&gt; like in the example below:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;lineno&quot;&gt; 1&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# -*- mode: ruby -*-&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# vi: set ft=ruby :&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt; 4&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# Vagrantfile API/syntax version. Don&amp;#39;t touch unless you know what you&amp;#39;re doing!&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;VAGRANTFILE_API_VERSION&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;2&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt; 7&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Vagrant&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;configure&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;VAGRANTFILE_API_VERSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# Every Vagrant virtual environment requires a box to build off of.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;box&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;ubuntu/trusty64&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;10&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;11&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# Configure vagrant-cachier plugin&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;12&lt;/span&gt;   &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Vagrant&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;has_plugin?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;vagrant-cachier&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;13&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;# Configure cached packages to be shared between instances of the same base box.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;14&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;# More info on http://fgrehm.viewdocs.io/vagrant-cachier/usage&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;15&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scope&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:box&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;16&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;17&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;# OPTIONAL: If you are using VirtualBox, you might want to use that to enable&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;18&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;# NFS for shared folders. This is also very useful for vagrant-libvirt if you&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;19&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;# want bi-directional sync&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;20&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;synced_folder_opts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;21&lt;/span&gt;       &lt;span class=&quot;ss&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:nfs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;22&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;# The nolock option can be useful for an NFSv3 client that wants to avoid the&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;23&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;# NLM sideband protocol. Without this option, apt-get might hang if it tries&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;24&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;# to lock files needed for /var/cache/* operations. All of this can be avoided&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;25&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;# by using NFSv4 everywhere. Please note that the tcp option is not the default.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;26&lt;/span&gt;       &lt;span class=&quot;ss&quot;&gt;mount_options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;rw&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;vers=3&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;tcp&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;nolock&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;27&lt;/span&gt;     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;28&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;# For more information please check http://docs.vagrantup.com/v2/synced-folders/basic_usage.html&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;29&lt;/span&gt;   &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;30&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# @end: Configure vagrant-cachier plugin&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;31&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;32&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;provision&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;shell&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;inline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;apt-get update --fix-missing&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;33&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;provision&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;shell&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;inline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;apt-get install nginx -y&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;34&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;35&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;web1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;primary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;web1_config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;36&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;web1_config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;network&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;private_network&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;ip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;192.168.90.11&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;37&lt;/span&gt;   &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;38&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;39&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;web2&amp;quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;web2_config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;40&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;web2_config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;network&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;private_network&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;ip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;192.168.90.12&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;41&lt;/span&gt;   &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;42&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;
&lt;br /&gt;Next steps will be based on the &lt;code&gt;Vagrantfile&lt;/code&gt; presented above.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now spin up one of Your VM’s which does some provisioning:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;vagrant up web1&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then check out the &lt;code&gt;~/.vagrant.d/cache&lt;/code&gt; host directory. You will find there cached downloads like in the example below:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;tree ~/.vagrant.d/cache
└── ubuntu
    └── trusty64
        ├── apt
        │   ├── fontconfig-config_2.11.0-0ubuntu4.1_all.deb
        │   ├── ...
        │   ├── nginx_1.4.6-1ubuntu3.1_all.deb
        │   ├── nginx-common_1.4.6-1ubuntu3.1_all.deb
        │   ├── nginx-core_1.4.6-1ubuntu3.1_amd64.deb
        │   └── ...
        ├── apt_lists
        │   ├── archive.ubuntu.com_ubuntu_dists_trusty_main_binary-amd64_Packages
        │   └── ...
        └── ...&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now start the second machine. This one will reuse the cache:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;vagrant up web2&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It should boot faster because it doesn’t need to download the updates and the &lt;code&gt;nginx&lt;/code&gt; package.&lt;/p&gt;

&lt;p&gt;Isn’t it great?&lt;/p&gt;

&lt;p&gt;But that’s not everything. The &lt;code&gt;vagrant-cachier&lt;/code&gt; plugin supports also other package managers:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;apt-cacher&lt;/li&gt;
  &lt;li&gt;apt-lists&lt;/li&gt;
  &lt;li&gt;apt&lt;/li&gt;
  &lt;li&gt;chef&lt;/li&gt;
  &lt;li&gt;composer&lt;/li&gt;
  &lt;li&gt;generic&lt;/li&gt;
  &lt;li&gt;npm&lt;/li&gt;
  &lt;li&gt;pacman&lt;/li&gt;
  &lt;li&gt;rubygems&lt;/li&gt;
  &lt;li&gt;rvm&lt;/li&gt;
  &lt;li&gt;yum&lt;/li&gt;
  &lt;li&gt;zypper&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If You are interested in deeper understanding how this plugin works visit &lt;a href=&quot;https://github.com/fgrehm/vagrant-cachier/blob/master/docs/how-does-it-work.md&quot; target=&quot;_blank&quot;&gt;this documentation page&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;is-it-really-faster&quot;&gt;Is it Really Faster?&lt;/h3&gt;

&lt;p&gt;Yes it is. Here are some statistics:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;time &lt;/span&gt;vagrant up web1
real    1m49.664s
user    0m8.164s
sys 0m4.679s

&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;time &lt;/span&gt;vagrant up web2
real    1m20.674s
user    0m8.214s
sys 0m4.949s&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;About &lt;code&gt;30&lt;/code&gt; seconds faster.&lt;/p&gt;

&lt;p&gt;And we installed only one package. The cache has only &lt;code&gt;103 MB&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;du -sh ~/.vagrant.d/cache/ubuntu/trusty64/
103M    /home/czerasz/.vagrant.d/cache/ubuntu/trusty64/&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Imagine how much time You can save when building more complex machines.&lt;/p&gt;

&lt;p&gt;You can find more benchmarks &lt;a href=&quot;https://github.com/fgrehm/vagrant-cachier/blob/master/docs/benchmarks.md&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&quot;divider  divider--border&quot;&gt;&lt;/div&gt;

&lt;h2 id=&quot;vagrant-host-manager-plugin&quot;&gt;Vagrant Host Manager Plugin&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;vagrant-hostmanager&lt;/code&gt; makes it possible to “talk” between VM’s by their host names.&lt;/p&gt;

&lt;p&gt;You can find the Vagrant host manager plugin &lt;strong&gt;project&lt;/strong&gt; on &lt;a href=&quot;https://github.com/smdahlen/vagrant-hostmanager&quot; target=&quot;_blank&quot;&gt;Github&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;basic-understanding-1&quot;&gt;Basic Understanding&lt;/h3&gt;

&lt;p&gt;To make the network communication based on host names possible, the &lt;code&gt;vagrant-hostmanager&lt;/code&gt; plugin adjusts the &lt;code&gt;/etc/hosts&lt;/code&gt; file on the host and guest machines.&lt;/p&gt;

&lt;h3 id=&quot;requirements-1&quot;&gt;Requirements&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Vagrant 1.1+&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;installation-1&quot;&gt;Installation&lt;/h3&gt;

&lt;p&gt;Install the &lt;code&gt;vagrant-hostmanager&lt;/code&gt; plugin with the following command:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;vagrant plugin install vagrant-hostmanager&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id=&quot;usage-1&quot;&gt;Usage&lt;/h3&gt;

&lt;p&gt;Enable it by extending Your Vagrantfile like in the example below:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;lineno&quot;&gt; 1&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# -*- mode: ruby -*-&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# vi: set ft=ruby :&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt; 4&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# Vagrantfile API/syntax version. Don&amp;#39;t touch unless you know what you&amp;#39;re doing!&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;VAGRANTFILE_API_VERSION&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;2&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt; 7&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Vagrant&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;configure&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;VAGRANTFILE_API_VERSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# Every Vagrant virtual environment requires a box to build off of.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;box&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;ubuntu/trusty64&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;10&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;11&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# Configure vagrant-cachier plugin&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;12&lt;/span&gt;   &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Vagrant&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;has_plugin?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;vagrant-hostmanager&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;13&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hostmanager&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;enabled&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;14&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hostmanager&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;manage_host&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;15&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hostmanager&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ignore_private_ip&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;16&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hostmanager&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;include_offline&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;17&lt;/span&gt;   &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;18&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# @end: Configure vagrant-cachier plugin&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;19&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;20&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;web1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;primary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;web1_config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;21&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;web1_config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;network&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;private_network&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;ip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;192.168.90.11&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;22&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;web1_config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hostname&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;web1&amp;#39;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;23&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;24&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;provision&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;shell&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;inline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;echo &amp;#39;web1&amp;#39; &amp;gt; /home/vagrant/index.html; chown vagrant:vagrant /home/vagrant/index.html&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;25&lt;/span&gt;   &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;26&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;27&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;web2&amp;quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;web2_config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;28&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;web2_config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;network&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;private_network&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;ip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;192.168.90.13&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;29&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;web2_config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hostname&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;web2&amp;#39;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;30&lt;/span&gt;   &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;31&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;
&lt;br /&gt;Next steps will be based on the &lt;code&gt;Vagrantfile&lt;/code&gt; presented above.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Start the VM’s:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;vagrant up&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Let’s audit our &lt;code&gt;/etc/hosts&lt;/code&gt; files.&lt;/p&gt;

&lt;p&gt;On the host we can see adjustments done by the &lt;code&gt;vagrant-hostmanager&lt;/code&gt; plugin:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;cat /etc/hosts
...

&lt;span class=&quot;c&quot;&gt;## vagrant-hostmanager-start id: dbf96559-0f67-4d15-92a5-c41f255cbfc8&lt;/span&gt;
192.168.90.11   web1
192.168.90.12   web2
&lt;span class=&quot;c&quot;&gt;## vagrant-hostmanager-end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;On the guest as well:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;vagrant ssh web1 -c &lt;span class=&quot;s1&quot;&gt;&amp;#39;cat /etc/hosts&amp;#39;&lt;/span&gt;
...

&lt;span class=&quot;c&quot;&gt;## vagrant-hostmanager-start&lt;/span&gt;
192.168.90.11   web1
192.168.90.12   web2
&lt;span class=&quot;c&quot;&gt;## vagrant-hostmanager-end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now, let’s run a simple static server with &lt;code&gt;python&lt;/code&gt; on &lt;code&gt;web1&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;vagrant ssh web1
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;python -c &lt;span class=&quot;s1&quot;&gt;&amp;#39;import BaseHTTPServer as bhs, SimpleHTTPServer as shs; bhs.HTTPServer((&amp;quot;0.0.0.0&amp;quot;, 8000), shs.SimpleHTTPRequestHandler).serve_forever()&amp;#39;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It will serve the &lt;code&gt;/home/vagrant/index.html&lt;/code&gt; file which we created during the provisioning process.&lt;/p&gt;

&lt;p&gt;Open a new terminal. Finally we can request the static server on &lt;code&gt;web1&lt;/code&gt; from &lt;code&gt;web2&lt;/code&gt; by it’s host name:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;vagrant ssh web2 -c &lt;span class=&quot;s1&quot;&gt;&amp;#39;curl web1:8000&amp;#39;&lt;/span&gt;
  web1&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Cool stuff!&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Tip&lt;/strong&gt;
&lt;br /&gt;If You change the private network ip of any box remember to execute &lt;code&gt;vagrant halt&lt;/code&gt; and &lt;code&gt;vagrant up&lt;/code&gt; afterwords. &lt;code&gt;vagrant reload&lt;/code&gt; won’t do it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;section class=&quot;callout&quot;&gt;
    &lt;h3&gt;Call To Action&lt;/h3&gt;

    &lt;p style=&quot;margin-top: 0;&quot;&gt;Do You know any other amazing Vagrant plugins? Let me know and I will add them to this list.&lt;/p&gt;

    &lt;div class=&quot;divider  clearfix&quot;&gt;&lt;/div&gt;

    &lt;a class=&quot;btn  btn-lg  btn-success&quot; href=&quot;/contact&quot;&gt;Contact Me&lt;/a&gt;
&lt;/section&gt;

&lt;h2 id=&quot;resources&quot;&gt;Resources:&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.stepquick.net/blog/2014/09/08/use-gulp-notify-with-terminal-notifier/&quot;&gt;Using Gulp-Notify on Vagrant&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Wed, 10 Dec 2014 00:00:00 +0100</pubDate>
        <link>http://czerasz.com/2014/12/10/top-vagrant-plugins/</link>
        <guid isPermaLink="true">http://czerasz.com/2014/12/10/top-vagrant-plugins/</guid>
        
        <category>vagrant</category>
        
        <category>plugins</category>
        
        <category>vagrant-cachier</category>
        
        <category>vagrant-hostmanager</category>
        
        
      </item>
    
      <item>
        <title>Monitoring Systems and Applications with Monit</title>
        <description>&lt;p class=&quot;post__tldr&quot; itemprop=&quot;description&quot;&gt;In this article we will focus on &lt;a href=&quot;https://mmonit.com/monit/&quot; target=&quot;_blank&quot;&gt;Monit&lt;/a&gt; which is a system monitoring and error recovery tool.&lt;/p&gt;

&lt;p&gt;Let’s start with a simple question:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;What is actually worth to be monitored?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Luckily the answer is also simple:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Everything what is critical to our business.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At application, service level it could be:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;process existence&lt;/li&gt;
  &lt;li&gt;is the process responding on a given port&lt;/li&gt;
  &lt;li&gt;resources consumption&lt;/li&gt;
  &lt;li&gt;configuration file changes&lt;/li&gt;
  &lt;li&gt;logfiles existence&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At system level one should consider the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;filesystem checks&lt;/li&gt;
  &lt;li&gt;network monitoring&lt;/li&gt;
  &lt;li&gt;CPU monitoring&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All those tasks can be accomplished with Monit, because it’s able to monitor:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Files and Directories&lt;/li&gt;
  &lt;li&gt;Disks&lt;/li&gt;
  &lt;li&gt;Processes&lt;/li&gt;
  &lt;li&gt;System&lt;/li&gt;
  &lt;li&gt;Hosts&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;installation&quot;&gt;Installation&lt;/h2&gt;

&lt;h3 id=&quot;monit-installation-on-gentoo&quot;&gt;Monit Installation on Gentoo&lt;/h3&gt;

&lt;p&gt;Install Monit on Gentoo with the following command:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;emerge monit&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;On Gentoo You can always do a installation dry-run:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;emerge --pretend monit&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;
&lt;br /&gt;This basically says what would happen if You would install the given package&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;installation-from-source&quot;&gt;Installation From Source&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;
&lt;br /&gt;Most packages (like the one on Ubuntu - contains Monit 5.6) are out of date. That’s why it’s recommended to install Monit from source.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Analyse &lt;a href=&quot;https://registry.hub.docker.com/u/czerasz/monit-base/&quot;&gt;this&lt;/a&gt; Docker image to learn how to install Monit on Ubuntu from source.&lt;/p&gt;

&lt;p&gt;If you don’t want to visit that link, here is a short version:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;apt-get install -y libssl-dev
&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; /tmp/
curl &lt;span class=&quot;s1&quot;&gt;&amp;#39;https://mmonit.com/monit/dist/monit-5.10.tar.gz&amp;#39;&lt;/span&gt; -O
tar -xzvf monit-5.10.tar.gz
rm monit-5.10.tar.gz
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;monit-5.10/
./configure --bindir&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/usr/bin/
make &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; make install
&lt;span class=&quot;c&quot;&gt;# Create required files&lt;/span&gt;
mkdir -p /var/lib/monit
&lt;span class=&quot;c&quot;&gt;# Make the init file executable&lt;/span&gt;
chmod &lt;span class=&quot;m&quot;&gt;755&lt;/span&gt; /etc/init.d/monit&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id=&quot;file-structure&quot;&gt;File Structure&lt;/h2&gt;

&lt;p&gt;All Monit configuration files are kept inside of the &lt;code&gt;/etc/monit&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;A standard file structure, to which I got used to looks like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;/etc/monit
├── conf.d       - Monit configuration files
│   ├── apps     - in-house applications
│   ├── services - services like nginx, elasticsearch
│   ├── sys      - system related
│   └── disabled - all disabled
├── bin          - start/stop scripts
│   └── services - services like nginx, elasticsearch
├── monitrc      - main configuration file
├── monitrc.d    - directory with examples
└── templates&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;
&lt;br /&gt;I assume that the application start/stop scripts are deployed together with the application codebase.
&lt;br /&gt;This way if developers change somethig in the application, Monit scripts don’t need to be adjusted (convention over configuration).
&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Just remember to force this standard.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;application-monitoring-script-template&quot;&gt;Application Monitoring Script Template&lt;/h2&gt;

&lt;p&gt;A typical Monit scripts consists of the following parts:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;binaries monitoring&lt;/li&gt;
  &lt;li&gt;configuration files monitoring&lt;/li&gt;
  &lt;li&gt;log files monitoring&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An example is presented below:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;lineno&quot;&gt; 1&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Monit &amp;lt;application name&amp;gt; configuration&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt; 3&lt;/span&gt; check &amp;lt;application name&amp;gt; matching &lt;span class=&quot;s2&quot;&gt;&amp;quot;&amp;lt;phrase which should exits in the ps output&amp;gt;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4&lt;/span&gt;     start &lt;span class=&quot;nv&quot;&gt;program&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;/bin/su &amp;lt;user&amp;gt; -c &amp;#39;/usr/lib/&amp;lt;application name&amp;gt;/current/bin/start.sh&amp;#39;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5&lt;/span&gt;     stop &lt;span class=&quot;nv&quot;&gt;program&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;/bin/su &amp;lt;user&amp;gt; -c &amp;#39;/usr/lib/&amp;lt;application name&amp;gt;/current/bin/stop.sh&amp;#39;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6&lt;/span&gt;     &lt;span class=&quot;c&quot;&gt;# CPU&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; cpu &amp;gt; 5% &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt; cycles &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; alert
&lt;span class=&quot;lineno&quot;&gt; 8&lt;/span&gt;     &lt;span class=&quot;c&quot;&gt;# RAM&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; mem &amp;gt; &lt;span class=&quot;m&quot;&gt;80&lt;/span&gt; MB &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;5&lt;/span&gt; cycles &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; alert
&lt;span class=&quot;lineno&quot;&gt;10&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;11&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Check if the application directory exists&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;12&lt;/span&gt; check directory &amp;lt;application name&amp;gt;-dir path /usr/lib/&amp;lt;application name&amp;gt;/current/
&lt;span class=&quot;lineno&quot;&gt;13&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; failed uid &amp;lt;user&amp;gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; alert
&lt;span class=&quot;lineno&quot;&gt;14&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; failed gid &amp;lt;user&amp;gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; alert
&lt;span class=&quot;lineno&quot;&gt;15&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; failed permission &lt;span class=&quot;m&quot;&gt;0755&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; alert
&lt;span class=&quot;lineno&quot;&gt;16&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; does not exist &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; alert
&lt;span class=&quot;lineno&quot;&gt;17&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;18&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# --- --- --- --- --- --- --- --- --- --- --- ---&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;19&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Configuration&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;20&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Check if the configuration directory is present&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;21&lt;/span&gt; check &amp;lt;application name&amp;gt;-conf-dir path /usr/lib/&amp;lt;application name&amp;gt;/current/config/
&lt;span class=&quot;lineno&quot;&gt;22&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; failed uid &amp;lt;user&amp;gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; alert
&lt;span class=&quot;lineno&quot;&gt;23&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; failed gid &amp;lt;user&amp;gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; alert
&lt;span class=&quot;lineno&quot;&gt;24&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; failed permission &lt;span class=&quot;m&quot;&gt;0775&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; alert
&lt;span class=&quot;lineno&quot;&gt;25&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; does not exist &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; alert
&lt;span class=&quot;lineno&quot;&gt;26&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;27&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Check if the unicorn configuration file is present&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;28&lt;/span&gt; check &amp;lt;application name&amp;gt;-conf-file-unicorn with path /usr/lib/&amp;lt;application name&amp;gt;/current/config/unicorn.rb
&lt;span class=&quot;lineno&quot;&gt;29&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; failed uid &amp;lt;user&amp;gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; alert
&lt;span class=&quot;lineno&quot;&gt;30&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; failed gid &amp;lt;user&amp;gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; alert
&lt;span class=&quot;lineno&quot;&gt;31&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; failed permission &lt;span class=&quot;m&quot;&gt;0775&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; alert
&lt;span class=&quot;lineno&quot;&gt;32&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; does not exist &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; alert
&lt;span class=&quot;lineno&quot;&gt;33&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; changed checksum &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; alert
&lt;span class=&quot;lineno&quot;&gt;34&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;35&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# --- --- --- --- --- --- --- --- --- --- --- ---&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;36&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Logs&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;37&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Check if the log file is present&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;38&lt;/span&gt; check file &amp;lt;application name&amp;gt;-log-file path /var/log/&amp;lt;application name&amp;gt;/access.log
&lt;span class=&quot;lineno&quot;&gt;39&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; failed uid &amp;lt;user&amp;gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; alert
&lt;span class=&quot;lineno&quot;&gt;40&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; failed gid &amp;lt;user&amp;gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; alert
&lt;span class=&quot;lineno&quot;&gt;41&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; failed permission &lt;span class=&quot;m&quot;&gt;0644&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; alert
&lt;span class=&quot;lineno&quot;&gt;42&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; does not exist &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; alert&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I would recommend to spend some time with Monit’s &lt;a href=&quot;http://mmonit.com/monit/documentation/monit.html&quot;&gt;documentation&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;startstop-scripts&quot;&gt;Start/Stop Scripts&lt;/h2&gt;

&lt;p&gt;We all know that each application need an start script. The reason for that is the situation when the server get’s rebooted. The application should be able to come up automatically.&lt;/p&gt;

&lt;p&gt;A good practice is to put the start/stop scripts to the same directory where the application is deployed.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;
&lt;br /&gt;The deployment should be adjusted so that it don’t interfere with Monit&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;start-scripts-for-ruby-applications&quot;&gt;Start Scripts for Ruby Applications&lt;/h3&gt;

&lt;p&gt;A sample Ruby start script template which uses &lt;a href=&quot;http://rvm.io/&quot;&gt;RVM&lt;/a&gt; is presented below:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;lineno&quot;&gt; 1&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;#!/bin/bash&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt; 3&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# &amp;lt;application name&amp;gt; Start Script&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# This script is executed by the &amp;quot;&amp;lt;user&amp;gt;&amp;quot; user&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt; 6&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Define the project directory&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;project_directory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;/home/&amp;lt;user&amp;gt;/&amp;lt;application name&amp;gt;&amp;#39;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt; 9&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Define ruby related details&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ruby_version&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;ruby-2.0.0-p195&amp;#39;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;11&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ruby_gemset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;lt;gemset name&amp;gt;&amp;#39;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;12&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;13&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;14&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Start the application server&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;15&lt;/span&gt; env &lt;span class=&quot;nv&quot;&gt;LANG&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;en_US.UTF-8&amp;#39;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;16&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;rvm_path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$HOME&lt;/span&gt;/.rvm/ &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;17&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$HOME&lt;/span&gt;/.rvm/bin/rvm-shell &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;18&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;$ruby_version@$ruby_gemset&amp;quot;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;19&lt;/span&gt; -c &lt;span class=&quot;s2&quot;&gt;&amp;quot;cd $project_directory/current; RAILS_ENV=production $project_directory/current/bin/unicorn -c $project_directory/current/config/unicorn.rb -E production -D&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And just the relevant part for a &lt;a href=&quot;http://www.sinatrarb.com/&quot;&gt;Sinatra&lt;/a&gt; application:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;# Start the application server&lt;/span&gt;
env &lt;span class=&quot;nv&quot;&gt;LANG&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;en_US.UTF-8&amp;#39;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;rvm_path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$HOME&lt;/span&gt;/.rvm/ &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$HOME&lt;/span&gt;/.rvm/bin/rvm-shell &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&amp;quot;$ruby_version@$ruby_gemset&amp;quot;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
-c &lt;span class=&quot;s2&quot;&gt;&amp;quot;cd $project_directory/current; bundle exec rackup $project_directory/current/config.ru --env staging --pid $project_directory/current/tmp/pids/sinatra.pid --port 8080  &amp;gt;&amp;gt; /var/log/&amp;lt;application name&amp;gt;/out.log 2&amp;gt;&amp;gt; /var/log/&amp;lt;application name&amp;gt;/error.log &amp;amp;&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The most important part here is the usage of &lt;code&gt;rvm-shell&lt;/code&gt; which syntax looks like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;rvm-shell &lt;span class=&quot;s2&quot;&gt;&amp;quot;&amp;lt;ruby version&amp;gt;@&amp;lt;ruby gemset&amp;gt;&amp;quot;&lt;/span&gt; -c &lt;span class=&quot;s2&quot;&gt;&amp;quot;&amp;lt;command which starts the ruby process&amp;gt;&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id=&quot;debugging-monit-configurations&quot;&gt;Debugging Monit Configurations&lt;/h2&gt;

&lt;p&gt;Now that we know where to put our configuration files let’s see how we can debug/test them.&lt;/p&gt;

&lt;p&gt;The first thing we need to do is adjust Monit’s poll cycle length in the &lt;code&gt;/etc/monit/monitrc&lt;/code&gt; file:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;daemon &lt;span class=&quot;m&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Number in seconds&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This value represents the time which Monit sleeps after each check. We need to keep it low for debugging purposes.&lt;/p&gt;

&lt;p&gt;Then add a configuration file &lt;code&gt;/etc/monit/conf.d/apps/monit-test-app.conf&lt;/code&gt; with the following content:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;check process monit-test-app with pidfile &lt;span class=&quot;s2&quot;&gt;&amp;quot;/usr/lib/monit-test-app/pids/process.pid&amp;quot;&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;/usr/lib/monit-test-app/bin/start.sh&amp;quot;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; does not exist &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; start&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Enable it by adding the following line to &lt;code&gt;/etc/monit/monitrc&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;include /etc/monit/conf.d/apps/*.conf&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Our application start script is located under &lt;code&gt;/usr/lib/monit-test-app/bin/start.sh&lt;/code&gt; and looks like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;lineno&quot;&gt; 1&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;#!/bin/bash&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt; 3&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;application_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;monit-test-app&amp;#39;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;script_dirirectory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;$( cd &amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt; dirname &lt;span class=&quot;s2&quot;&gt;&amp;quot;$0&amp;quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot; &amp;amp;&amp;amp; pwd )&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;application_dirirectory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$script_dirirectory&lt;/span&gt;/..
&lt;span class=&quot;lineno&quot;&gt; 6&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt; 7&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;log_file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;/var/log/$application_name/stdout.log&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;pid_file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;/var/run/$application_name.pid&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; -e &lt;span class=&quot;s2&quot;&gt;&amp;quot;\n`date +&amp;quot;&lt;/span&gt;%y%m%d %H:%M:%S&lt;span class=&quot;s2&quot;&gt;&amp;quot;` - start script&amp;quot;&lt;/span&gt; &amp;gt;&amp;gt; &lt;span class=&quot;nv&quot;&gt;$log_file&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;11&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;12&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$BASHPID&lt;/span&gt; &amp;gt;&amp;gt; &lt;span class=&quot;nv&quot;&gt;$pid_file&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;13&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;14&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;`date +&amp;quot;&lt;/span&gt;%y%m%d %H:%M:%S&lt;span class=&quot;s2&quot;&gt;&amp;quot;` - sleep 5s&amp;quot;&lt;/span&gt; &amp;gt;&amp;gt; &lt;span class=&quot;nv&quot;&gt;$log_file&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;15&lt;/span&gt; sleep 5
&lt;span class=&quot;lineno&quot;&gt;16&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;17&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;`date +&amp;quot;&lt;/span&gt;%y%m%d %H:%M:%S&lt;span class=&quot;s2&quot;&gt;&amp;quot;` - remove pid file&amp;quot;&lt;/span&gt; &amp;gt;&amp;gt; &lt;span class=&quot;nv&quot;&gt;$log_file&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;18&lt;/span&gt; rm &lt;span class=&quot;nv&quot;&gt;$pid_file&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;
&lt;br /&gt;Remember to make the script executable &lt;code&gt;chmod u+x /usr/lib/monit-test-app/bin/start.sh&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Finally start Monit with (if not already started):&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;service monit start &lt;span class=&quot;c&quot;&gt;# or: /etc/init.d/monit start&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And reload Monit’s configuration:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;monit reload&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Monit should start the &lt;code&gt;start.sh&lt;/code&gt; over and over again.&lt;/p&gt;

&lt;p&gt;Check if Monit does it’s job by checking the logs:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;tail -f /var/log/monit.log /var/log/monit-test-app/*
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&amp;gt; /var/log/monit.log &amp;lt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;UTC Nov &lt;span class=&quot;m&quot;&gt;27&lt;/span&gt; 08:07:57&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; error    : &lt;span class=&quot;s1&quot;&gt;&amp;#39;monit-test-app&amp;#39;&lt;/span&gt; process is not running
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;UTC Nov &lt;span class=&quot;m&quot;&gt;27&lt;/span&gt; 08:07:57&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; info     : &lt;span class=&quot;s1&quot;&gt;&amp;#39;monit-test-app&amp;#39;&lt;/span&gt; start: /usr/lib/monit-test-app/bin/start.sh

&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&amp;gt; /var/log/monit-test-app/stdout.log &amp;lt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;

&lt;span class=&quot;m&quot;&gt;141127&lt;/span&gt; 08:07:57 - start script
&lt;span class=&quot;m&quot;&gt;141127&lt;/span&gt; 08:07:57 - sleep 5s
&lt;span class=&quot;m&quot;&gt;141127&lt;/span&gt; 08:08:02 - remove pid &lt;span class=&quot;nv&quot;&gt;file&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&amp;gt; /var/log/monit.log &amp;lt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;UTC Nov &lt;span class=&quot;m&quot;&gt;27&lt;/span&gt; 08:08:27&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; error    : &lt;span class=&quot;s1&quot;&gt;&amp;#39;monit-test-app&amp;#39;&lt;/span&gt; failed to start &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;exit &lt;/span&gt;status 0&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; -- no output
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;UTC Nov &lt;span class=&quot;m&quot;&gt;27&lt;/span&gt; 08:08:37&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; error    : &lt;span class=&quot;s1&quot;&gt;&amp;#39;monit-test-app&amp;#39;&lt;/span&gt; process is not running
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;UTC Nov &lt;span class=&quot;m&quot;&gt;27&lt;/span&gt; 08:08:37&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; info     : &lt;span class=&quot;s1&quot;&gt;&amp;#39;monit-test-app&amp;#39;&lt;/span&gt; start: /usr/lib/monit-test-app/bin/start.sh&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I also like to keep an eye on Monit’s summary in a separate terminal:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;watch -n1 &lt;span class=&quot;s1&quot;&gt;&amp;#39;monit summary&amp;#39;&lt;/span&gt;
Every 1.0s: monit summary                                                                                                                                                            Thu Nov &lt;span class=&quot;m&quot;&gt;27&lt;/span&gt; 00:47:43 2014

The Monit daemon 5.10 uptime: 26m

Process &lt;span class=&quot;s1&quot;&gt;&amp;#39;monit-test-app&amp;#39;&lt;/span&gt;            Execution failed
System &lt;span class=&quot;s1&quot;&gt;&amp;#39;6934259efb99&amp;#39;&lt;/span&gt;               Running&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Sometimes it’s helpful to run Monit in a verbose mode. Simply change the init file with this command:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;sed -i &lt;span class=&quot;s1&quot;&gt;&amp;#39;s/^MONIT_OPTS=$/MONIT_OPTS=&amp;quot;-v&amp;quot;/&amp;#39;&lt;/span&gt; /etc/init.d/monit&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And restart Monit so that the change can take place:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;service monit restart&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id=&quot;useful-monit-commands&quot;&gt;Useful Monit Commands&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;List all commands&lt;/p&gt;

    &lt;pre&gt;&lt;code&gt;  monit -h
&lt;/code&gt;&lt;/pre&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Get Monit summary&lt;/p&gt;

    &lt;pre&gt;&lt;code&gt;  monit summary
&lt;/code&gt;&lt;/pre&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Disable/unmonitor a service&lt;/p&gt;

    &lt;pre&gt;&lt;code&gt;  monit unmonitor &amp;lt;service name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

    &lt;p&gt;If You want to disable the service permanently You can move the appropriate file to the &lt;code&gt;/etc/monit/conf.d/disabled&lt;/code&gt; directory and then reload Monit with&lt;/p&gt;

    &lt;pre&gt;&lt;code&gt;  monit reload
&lt;/code&gt;&lt;/pre&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;View Monit logs&lt;/p&gt;

    &lt;pre&gt;&lt;code&gt;  tail -f /var/log/monit.log
&lt;/code&gt;&lt;/pre&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Resources:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/edx/configuration/wiki/Ansible-variable-conventions-and-overriding-defaults&quot;&gt;Ansible Variables&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.tecmint.com/how-to-install-and-setup-monit-linux-process-and-services-monitoring-program/&quot;&gt;How to Install and Setup Monit&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://stackoverflow.com/questions/3356476/debugging-monit&quot;&gt;Debugging monit&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Thu, 13 Nov 2014 08:00:00 +0100</pubDate>
        <link>http://czerasz.com/2014/11/13/monitoring-systems-and-applications-with-monit/</link>
        <guid isPermaLink="true">http://czerasz.com/2014/11/13/monitoring-systems-and-applications-with-monit/</guid>
        
        <category>monit</category>
        
        <category>monitoring</category>
        
        <category>service recovery</category>
        
        <category>Gentoo</category>
        
        <category>Ubuntu</category>
        
        
      </item>
    
      <item>
        <title>Docker Tips &amp; Tricks</title>
        <description>&lt;p class=&quot;post__tldr&quot; itemprop=&quot;description&quot;&gt;This article will cover some really cool techniques and useful tricks which I learned during developing &lt;a href=&quot;https://www.docker.com/&quot; target=&quot;_blank&quot;&gt;Docker&lt;/a&gt; containers.&lt;/p&gt;

&lt;h2 id=&quot;make-use-of-dockers-cache&quot;&gt;Make Use of Docker’s Cache&lt;/h2&gt;

&lt;p&gt;Docker saves each commit (each &lt;code&gt;RUN&lt;/code&gt;) in a separate layer. Make use of this caching mechanism while planning your application containers.&lt;/p&gt;

&lt;p&gt;Let’s consider an PHP application example. The libraries for this application are installed through the &lt;a href=&quot;https://getcomposer.org/&quot;&gt;Composer&lt;/a&gt; package manager. To make use of the cache we will add the following to our &lt;code&gt;Dockerfile&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;lineno&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Install libraries through composer&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Until the composer.json&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;3&lt;/span&gt; ADD ./app/composer.json /usr/lib/composer/composer.json
&lt;span class=&quot;lineno&quot;&gt;4&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Install all Wordpress dependencies&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;6&lt;/span&gt; ENV COMPOSER_HOME &lt;span class=&quot;s1&quot;&gt;&amp;#39;/usr/lib/composer&amp;#39;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;7&lt;/span&gt; RUN &lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; /usr/lib/composer &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; composer install&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Later in our &lt;a href=&quot;#what-is-a-docker-startinit-script&quot;&gt;Docker start/init script&lt;/a&gt; (which is used while running the container) we symlink those files/libraries where they are supposed to be:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;# Create symlinks to libraries downloaded through composer&lt;/span&gt;
ln -s /usr/lib/composer/vendor /var/www/vendor&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This allows us to spin up containers really fast!&lt;/p&gt;

&lt;p&gt;If we would run &lt;code&gt;composer install&lt;/code&gt; for the first time in our &lt;a href=&quot;#what-is-a-docker-startinit-script&quot;&gt;start/init script&lt;/a&gt; the run process would take at least as long as the &lt;code&gt;composer install&lt;/code&gt;, which usually takes a lot of time.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;
&lt;br /&gt;This scenario implies that a container is build for a specific application.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Read more about this technique &lt;a href=&quot;http://bitjudo.com/blog/2014/03/13/building-efficient-dockerfiles-node-dot-js/&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;minimize-commitlayer-number-wisely&quot;&gt;Minimize Commit/Layer Number… Wisely&lt;/h2&gt;

&lt;p&gt;Remember that each commit represents a separate file system layer and that the number of commits is limited.&lt;/p&gt;

&lt;p&gt;You can check the maximum limit of commits with the following script:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;#!/bin/bash&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Remove the commit if it exists&lt;/span&gt;
docker rmi czerasz/aufs-layers-test

&lt;span class=&quot;c&quot;&gt;# Create a base container&lt;/span&gt;
docker run --name&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;aufs-layers-test-container&amp;quot;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           ubuntu /bin/mkdir /data

&lt;span class=&quot;c&quot;&gt;# Commit the changes to a new image&lt;/span&gt;
docker commit --message&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;First commit&amp;#39;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;aufs-layers-test-container&amp;#39;&lt;/span&gt; czerasz/aufs-layers-test

&lt;span class=&quot;c&quot;&gt;# Remove the container&lt;/span&gt;
docker rm &lt;span class=&quot;s1&quot;&gt;&amp;#39;aufs-layers-test-container&amp;#39;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; i in &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;1..1000&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; -e &lt;span class=&quot;s2&quot;&gt;&amp;quot;\n$i build\n&amp;quot;&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Add another commit&lt;/span&gt;
cat &lt;span class=&quot;s&quot;&gt;&amp;lt;&amp;lt;-EOF |&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;FROM czerasz/aufs-layers-test&lt;/span&gt;

&lt;span class=&quot;s&quot;&gt;RUN /bin/echo -e &amp;quot;test\n&amp;quot; &amp;gt;&amp;gt; /data/test&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;EOF&lt;/span&gt;
docker build -t czerasz/aufs-layers-test -

&lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;On my machine it returned:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;Cannot create container with more than &lt;span class=&quot;m&quot;&gt;127&lt;/span&gt; parents&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Which means that there are only &lt;code&gt;127&lt;/code&gt; commits possible.&lt;/p&gt;

&lt;p&gt;This is the reason why one should combine the &lt;code&gt;RUN&lt;/code&gt; commands in the &lt;code&gt;Dockerfile&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A real word example (which I use in &lt;a href=&quot;https://registry.hub.docker.com/u/czerasz/base/&quot;&gt;my base image&lt;/a&gt;) is presented below:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;RUN apt-get install -y build-essential &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    apt-get install -y software-properties-common &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    apt-get install -y pwgen &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
                       python-software-properties &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
                       vim &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
                       curl &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
                       wget &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
                       git &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
                       unzip &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
                       tree&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The downside of this construction is that whenever I need to remove/add a package, Docker is not able to use it’s cache feature and all packages need to be installed from the scratch. And this takes time.&lt;/p&gt;

&lt;h2 id=&quot;separate-installation-and-configuration&quot;&gt;Separate Installation and Configuration&lt;/h2&gt;

&lt;p&gt;Add configuration files in the &lt;code&gt;Dockerfile&lt;/code&gt; as late as possible. The configuration is something which changes quite often and it would revalidate the cache with each change.&lt;/p&gt;

&lt;p&gt;Analyze this Wordpress &lt;code&gt;Dockerfile&lt;/code&gt; as an example:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;# --- PHP ---&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Install PHP&lt;/span&gt;
RUN apt-get install -y php5-fpm &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
                       php5-mysql &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
                       php-apc

&lt;span class=&quot;c&quot;&gt;# Wordpress Requirements&lt;/span&gt;
RUN apt-get install -y php5-curl &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
                       php5-gd &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
                       php5-intl &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
                       php-pear &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
                       php5-imagick &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
                       php5-imap &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
                       php5-mcrypt &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
                       php5-memcache &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
                       php5-ming &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
                       php5-ps &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
                       php5-pspell &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
                       php5-recode &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
                       php5-sqlite &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
                       php5-tidy &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
                       php5-xmlrpc &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
                       php5-xsl

&lt;span class=&quot;c&quot;&gt;# --- NGINX ---&lt;/span&gt;

....

&lt;span class=&quot;c&quot;&gt;# ------ PHP-FPM CONFIGURATION ------&lt;/span&gt;
RUN mkdir -p /var/log/php5-fpm/
&lt;span class=&quot;c&quot;&gt;# Configure the php5-fpm  process and the default pool&lt;/span&gt;
ADD ./config/php-fpm/php-fpm.conf /etc/php5/fpm/php-fpm.conf
ADD ./config/php-fpm/pool.d/www.conf /etc/php5/fpm/pool.d/www.conf
ADD ./config/php-fpm/php.ini /etc/php5/fpm/php.ini&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;When we change the PHP configuration the build process will still work really fast, because the most time consuming part, the package installation is cached.&lt;/p&gt;

&lt;p&gt;I even tend to split the installation and configuration between separate &lt;code&gt;Dockerfiles&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;always-name-containers&quot;&gt;Always Name Containers&lt;/h2&gt;

&lt;p&gt;While running a new containers rigorously use the &lt;code&gt;--name=&lt;/code&gt; option. This way you will always know what this container was ment for.&lt;/p&gt;

&lt;p&gt;Some real world examples are presented below:
- &lt;code&gt;czerasz-web&lt;/code&gt;
- &lt;code&gt;czerasz-database&lt;/code&gt;
- &lt;code&gt;czerasz-data-container&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;
&lt;br /&gt;A human readable name is very useful while linking containers and while working with data containers.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;use-refreshedat-variable-for-better-cache-control&quot;&gt;Use &lt;code&gt;REFRESHED_AT&lt;/code&gt; Variable for Better Cache Control&lt;/h2&gt;

&lt;p&gt;Consider the following &lt;code&gt;Dockerfile&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;FROM ubuntu

&lt;span class=&quot;c&quot;&gt;# Set the reset cache variable&lt;/span&gt;
ENV REFRESHED_AT 2014-11-01

...&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;By changing the value of the &lt;code&gt;REFRESHED_AT&lt;/code&gt; variable you are able to flush the whole cache.&lt;/p&gt;

&lt;h2 id=&quot;use-a-supervisor-for-multiple-proccesses&quot;&gt;Use a Supervisor for Multiple Proccesses&lt;/h2&gt;

&lt;p&gt;If Your container wraps multiple applications/processes use a process control system tool like &lt;a href=&quot;http://supervisord.org/&quot;&gt;&lt;code&gt;supervisord&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A sample &lt;code&gt;/etc/supervisor/conf.d/supervisord.conf&lt;/code&gt; which manages Serf, PHP-FPM, Nginx is presented below:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;supervisord&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;pidfile&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/supervisord.pid &lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;supervisord pidfile&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;default supervisord.pid&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;nodaemon&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;                &lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;start in foreground &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;default &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;program:php5-fpm&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/usr/sbin/php5-fpm -c /etc/php5/fpm
&lt;span class=&quot;nv&quot;&gt;autorestart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;program:nginx&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;nginx
&lt;span class=&quot;nv&quot;&gt;autostart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;autorestart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;program:serf&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/etc/serf/serf-start.sh
&lt;span class=&quot;nv&quot;&gt;numprocs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1
&lt;span class=&quot;nv&quot;&gt;autostart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;autorestart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;program:serf-join&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/etc/serf/serf-join.sh
&lt;span class=&quot;nv&quot;&gt;autorestart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;exitcodes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id=&quot;use-image-inheritance&quot;&gt;Use Image Inheritance&lt;/h2&gt;

&lt;p&gt;Don’t be afraid of creating Docker images. Separate your logic into different images which inherit from each other.&lt;/p&gt;

&lt;p&gt;An example is presented below:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ubuntu:latest&lt;/code&gt; → &lt;code&gt;czerasz/base&lt;/code&gt; → &lt;code&gt;czerasz/wordpress-base&lt;/code&gt; → &lt;code&gt;czerasz/czerasz-wordpress-blog&lt;/code&gt;&lt;/p&gt;

&lt;h2 id=&quot;use-data-containers&quot;&gt;Use Data Containers&lt;/h2&gt;

&lt;p&gt;A data container is a concept which is based on a simple idea to keep the data in a special container.&lt;/p&gt;

&lt;p&gt;Then by using the option &lt;code&gt;--volumes-from&lt;/code&gt; we attach the files from the data container to our application/database container.&lt;/p&gt;

&lt;p&gt;The data container’s status will be &lt;code&gt;Exited&lt;/code&gt; but don’t fear that someone will delete it by accident. Until it’s used by another container the command &lt;code&gt;docker rm data-container&lt;/code&gt; will not work.&lt;/p&gt;

&lt;p&gt;A real word example is a Wordpress Docker setup in which the uploads are kept in a separate data container. Let’s see how it works!&lt;/p&gt;

&lt;p&gt;Create data container:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;docker run -v /data/uploads &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           --name czerasz-data-container &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           -d debian chown -R www-data:www-data /data/uploads&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Create a Wordpress container and attach to it the &lt;code&gt;/data/uploads&lt;/code&gt; directory from the data container:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;docker run &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
       -v &lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;pwd&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;/app:/var/www &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
       --volumes-from czerasz-data-container &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
       --link czerasz-database:db &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
       -p 80:80 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
       --name czerasz-site &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
       -d czerasz/wordpress-base &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
       /usr/local/bin/init.sh production&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Tip&lt;/strong&gt;
&lt;br /&gt;Access data container files like this:&lt;/p&gt;

  &lt;pre&gt;&lt;code&gt;$ ls -al `docker inspect -f &#39;&#39; czerasz-data-container`
drwx------ 4 www-data www-data 4096 Nov  1 22:04 .
drwx------ 3 root     root     4096 Nov  1 20:14 ..
drwx------ 8 www-data www-data 4096 Nov  2 01:25 2014
&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;Read more about data containers &lt;a href=&quot;https://docs.docker.com/userguide/dockervolumes/&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;use-files-for-environment-variables&quot;&gt;Use Files for Environment Variables&lt;/h2&gt;

&lt;p&gt;If you have a lot of environment varaiables which need to be passed to the container use the &lt;code&gt;--env-file&lt;/code&gt; option. It allows you to import variables from a file formated like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;primary&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;mongo_1532
&lt;span class=&quot;nv&quot;&gt;number_of_replicas&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;3
...&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id=&quot;always-remove-temporary-containers&quot;&gt;Always Remove Temporary Containers&lt;/h2&gt;

&lt;p&gt;There is always a situation when you need a container just for a small tasks. It could be checking a specific command, exporting files from a data container or creating a database dump.&lt;/p&gt;

&lt;p&gt;Remember to use the &lt;code&gt;--rm&lt;/code&gt; option. It will remove the container after it’s job is done and you will never have to deal with a container necropolis after calling &lt;code&gt;docker ps -a&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A real world example (which was borrowed from &lt;a href=&quot;https://registry.hub.docker.com/_/mysql/&quot;&gt;here&lt;/a&gt;) is presented below:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;docker run -it &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
            --link some-mysql:mysql &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
            --rm mysql sh -c &lt;span class=&quot;s1&quot;&gt;&amp;#39;exec mysql -h&amp;quot;$MYSQL_PORT_3306_TCP_ADDR&amp;quot; -P&amp;quot;$MYSQL_PORT_3306_TCP_PORT&amp;quot; -uroot -p&amp;quot;$MYSQL_ENV_MYSQL_ROOT_PASSWORD&amp;quot;&amp;#39;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id=&quot;simplify-communication-by-using-the-hostname&quot;&gt;Simplify Communication by Using the Hostname&lt;/h2&gt;

&lt;p&gt;Whenever you can, use the &lt;code&gt;-h, --hostname=&quot;&quot;&lt;/code&gt; option to give your container a logical hostname. This will make networking tasks much easier.&lt;/p&gt;

&lt;p&gt;The example below shows a simple usecase. First we spin up a server container:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;docker run --hostname&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;web-server&amp;quot;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           --rm &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           --name host-communication-test-web-server &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           -it ubuntu bash

root@web-server:/# &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Response from: &amp;quot;&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;hostname&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt; &amp;gt; index.html &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; python3 -m http.server 8000&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then we spin up a client server which requests the web server:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;docker run --hostname&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;web-client&amp;quot;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           --rm &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           --link host-communication-test-web-server:web-server &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           -it ubuntu bash

root@web-client:/# apt-get update &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; apt-get install -y curl
root@web-client:/# curl -v web-server:8000/index.html
* Hostname was NOT found in DNS cache
*   Trying 172.17.1.57...
* Connected to web-server &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;172.17.1.57&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; port &lt;span class=&quot;m&quot;&gt;8000&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;c&quot;&gt;#0)&lt;/span&gt;
&amp;gt; GET /index.html HTTP/1.1
&amp;gt; User-Agent: curl/7.35.0
&amp;gt; Host: web-server:8000
&amp;gt; Accept: */*
&amp;gt;
* HTTP 1.0, assume close after body
&amp;lt; HTTP/1.0 &lt;span class=&quot;m&quot;&gt;200&lt;/span&gt; OK
&amp;lt; Server: SimpleHTTP/0.6 Python/3.4.0
&amp;lt; Date: Tue, &lt;span class=&quot;m&quot;&gt;25&lt;/span&gt; Nov &lt;span class=&quot;m&quot;&gt;2014&lt;/span&gt; 11:41:52 GMT
&amp;lt; Content-type: text/html
&amp;lt; Content-Length: 26
&amp;lt; Last-Modified: Tue, &lt;span class=&quot;m&quot;&gt;25&lt;/span&gt; Nov &lt;span class=&quot;m&quot;&gt;2014&lt;/span&gt; 11:40:14 GMT
&amp;lt;
Response from: web-server
* Closing connection 0&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;As you can see we don’t have to use any environment variables (which we by the way don’t have if no port is exposed). We can just rely on the hostname.&lt;/p&gt;

&lt;h2 id=&quot;what-is-a-docker-startinit-script&quot;&gt;What is a Docker Start/Init Script?&lt;/h2&gt;

&lt;p&gt;A Docker start/init script is simply a script which is executed when the container starts.&lt;/p&gt;

&lt;p&gt;The script can be called manually while one runs a container:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;docker run -it user_name/image_name /usr/lib/init-script-name.sh&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Or through one of the &lt;code&gt;Dockerfile&lt;/code&gt; directives:
- &lt;a href=&quot;http://docs.docker.com/reference/builder/#entrypoint&quot;&gt;&lt;code&gt;ENTRYPOINT&lt;/code&gt;&lt;/a&gt;
- &lt;a href=&quot;http://docs.docker.com/reference/builder/#cmd&quot;&gt;&lt;code&gt;CMD&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;resources&quot;&gt;Resources:&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://developerblog.redhat.com/2014/09/30/overview-storage-scalability-docker/&quot;&gt;Comprehensive Overview of Storage Scalability in Docker&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://registry.hub.docker.com/_/mysql/&quot;&gt;MySQL Docker image &lt;code&gt;README.md&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://bitjudo.com/blog/2014/03/13/building-efficient-dockerfiles-node-dot-js/&quot;&gt;Building Efficient Dockerfiles for Node.js Applications&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.docker.com/userguide/dockervolumes/&quot;&gt;Managing Data in Containers&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.docker.com/articles/networking/#container-networking&quot;&gt;Docker Networking&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Thu, 13 Nov 2014 08:00:00 +0100</pubDate>
        <link>http://czerasz.com/2014/11/13/docker-tip-and-tricks/</link>
        <guid isPermaLink="true">http://czerasz.com/2014/11/13/docker-tip-and-tricks/</guid>
        
        <category>docker</category>
        
        
      </item>
    
      <item>
        <title>Bash Script Template</title>
        <description>&lt;p class=&quot;post__tldr&quot; itemprop=&quot;description&quot;&gt;If you like &lt;a href=&quot;http://en.wikipedia.org/wiki/KISS_principle&quot; target=&quot;_blank&quot;&gt;KISS&lt;/a&gt;, there is a good chance you will enjoy this short article. I will show you how to structure projects wich use bash scripts.&lt;/p&gt;

&lt;p&gt;Almost in any project there is the need to use &lt;strong&gt;bash scripts&lt;/strong&gt; to automate work.&lt;/p&gt;

&lt;p&gt;I convert any multi-line command or multi-argument command into a script.
&lt;br /&gt;It’s easier to type &lt;code&gt;./bin/run-docker.sh&lt;/code&gt; than to type/paste the full command, like this one, into the terminal:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;docker run --name haproxy-routing &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           -p 80:80 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           -v /home/user/config:/data/haproxy/config &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           --link site-container:site &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           --link blog-container:blog &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           -d czerasz/haproxy-routing&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The convention I use, is to put all bash scripts into a &lt;code&gt;bin&lt;/code&gt; directory inside of the project:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;.
├── app.py
├── bin
│   └── ...
├── config
│   └── environment.yml
└──...&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It’s very common that I reference to files from the project level. And it’s so common, that I always start my bash scripts with the following template:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;lineno&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;#!/bin/bash&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;2&lt;/span&gt; 
&lt;span class=&quot;lineno&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;script_directory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;$( cd &amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt; dirname &lt;span class=&quot;s2&quot;&gt;&amp;quot;$0&amp;quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot; &amp;amp;&amp;amp; pwd )&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;project_directory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$script_directory&lt;/span&gt;/..&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Thanks to a cool &lt;a href=&quot;http://www.sublimetext.com/3&quot;&gt;Sublime&lt;/a&gt; package called &lt;a href=&quot;https://github.com/weslly/Nettuts-Fetch&quot;&gt;Fetch&lt;/a&gt; I can reuse this template by taking just two steps.
You can reuse my &lt;a href=&quot;https://github.com/weslly/Nettuts-Fetch&quot;&gt;Fetch&lt;/a&gt; config which looks like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;quot;files&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;quot;bash_script_template&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;https://gist.githubusercontent.com/czerasz/3046161/raw/3c2300338917de19045b5b9a500fa25cacb41297/start-script-helper.sh&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;err&quot;&gt;...&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;&lt;br /&gt;
There are projects mostly ruby based, where the &lt;code&gt;bin&lt;/code&gt; directory is occupied by gems installed with the &lt;a href=&quot;http://bundler.io/man/bundle-exec.1.html#BUNDLE-INSTALL-BINSTUBS&quot;&gt;bundle binstrubs command&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;bundle install --binstubs&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In such cases I update my &lt;code&gt;.gitignore&lt;/code&gt; with the following:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;# Ignore all gems and track only wanted scripts&lt;/span&gt;
bin/*
!bin/build-docker.sh&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

</description>
        <pubDate>Thu, 13 Nov 2014 08:00:00 +0100</pubDate>
        <link>http://czerasz.com/2014/11/13/bash-script-template/</link>
        <guid isPermaLink="true">http://czerasz.com/2014/11/13/bash-script-template/</guid>
        
        <category>bash</category>
        
        <category>template</category>
        
        <category>tips</category>
        
        
      </item>
    
  </channel>
</rss>
