<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mattias Lundberg</title>
    <description>The latest articles on DEV Community by Mattias Lundberg (@mattiaslundberg).</description>
    <link>https://dev.to/mattiaslundberg</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F202676%2F3eae518a-dd2f-46e2-a2a7-ea57afd0a27b.jpeg</url>
      <title>DEV Community: Mattias Lundberg</title>
      <link>https://dev.to/mattiaslundberg</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mattiaslundberg"/>
    <language>en</language>
    <item>
      <title>Using ansible for container orchestration</title>
      <dc:creator>Mattias Lundberg</dc:creator>
      <pubDate>Thu, 27 Feb 2020 14:34:13 +0000</pubDate>
      <link>https://dev.to/mattiaslundberg/using-ansible-for-container-orchestration-3051</link>
      <guid>https://dev.to/mattiaslundberg/using-ansible-for-container-orchestration-3051</guid>
      <description>&lt;p&gt;There exists almost as many ways to deploy applications as there exists applications. In the post we will look closer at one of them, using ansible to deploy or orchestrate applications in Docker containers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.ansible.com"&gt;Ansible&lt;/a&gt; is a platform for management of servers and what is running on them. In ansible you use one or more so called playbook to perform changes on one or more servers at the same time. A playbook has setting for which servers it should run agains and what it should do, so called tasks. A task can be to install a package or making sure a container is running.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.docker.com"&gt;Docker&lt;/a&gt; is a system for packaging and running applications in a consequent way no matter what programming language it is written in. Docker can build and run containers, a container can be seen as a lightweight virtual computer.&lt;/p&gt;

&lt;p&gt;Orchestration is automated configuration and management of multiple computer systems and software. In this post it is used to describe how and where an application is running.&lt;/p&gt;

&lt;p&gt;For orchestration of containers there are many ways to handle them. A common solution is &lt;a href="https://kubernetes.io"&gt;kubernetes&lt;/a&gt; that makes the orchestration easy. One drawback is that it adds complexity, especially when running in smaller environments. This complexity is sometimes too great to handle in smaller projects, especially if the application does not require all powerful features of other orchestrators.&lt;/p&gt;

&lt;p&gt;I will walk trough a more lightweight alternative that is ansible. The description and example is based on a real world project running in AWS and the application is small web application with a Flask/Python backend. The concepts described are applicable to any web application running in docker no matter the framework/language used. The application required a database, a loadbalancer and multiple application servers. This post will focus on the application servers. The application servers all runs the same codebase with the same configuration.&lt;/p&gt;

&lt;p&gt;The servers and supporing services is setup by &lt;a href="https://terraform.io"&gt;terraform&lt;/a&gt; and then ansible is used to install and configure them. After the creation of the server using terraform the following steps is required:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Run an ansible playbook to install docker&lt;/li&gt;
&lt;li&gt;Run an ansible playbook to start the application and add the containers to a loadbalancer&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The first step to install the base system could easily be removed by building a custom image, but kept for clarity and simplicity. The second step is what we will focus on, this is a simplified version of the playbook used to deploy the application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="nn"&gt;---&lt;/span&gt;
&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Prepare deployment&lt;/span&gt;
  &lt;span class="na"&gt;hosts&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;appplication_servers&lt;/span&gt;

  &lt;span class="na"&gt;tasks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Run database migrations&lt;/span&gt;
      &lt;span class="na"&gt;run_once&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;yes&lt;/span&gt;
      &lt;span class="na"&gt;docker_container&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;app-migrations&lt;/span&gt;
        &lt;span class="na"&gt;pull&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;yes&lt;/span&gt;
        &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;application:{{&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;version&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;}}"&lt;/span&gt;
        &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;db&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;upgrade"&lt;/span&gt;

&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Deploy to one server at the time&lt;/span&gt;
  &lt;span class="na"&gt;hosts&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;application_servers&lt;/span&gt;
  &lt;span class="na"&gt;serial&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;

  &lt;span class="na"&gt;tasks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Remove from load balancer&lt;/span&gt;
      &lt;span class="na"&gt;delegate_to&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;localhost&lt;/span&gt;
      &lt;span class="na"&gt;elb_target&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;state&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;absent&lt;/span&gt;

    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Start new docker container&lt;/span&gt;
      &lt;span class="na"&gt;docker_container&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;app&lt;/span&gt;
        &lt;span class="na"&gt;pull&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;yes&lt;/span&gt;
        &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;application:{{&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;version&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;}}"&lt;/span&gt;
        &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;8080:8080&lt;/span&gt;

    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Add to load balancer&lt;/span&gt;
      &lt;span class="na"&gt;delegate_to&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;localhost&lt;/span&gt;
      &lt;span class="na"&gt;elb_target&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;state&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;present&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Walking through the playbook we first run database schema migrations (in this case with &lt;code&gt;flask-migrate&lt;/code&gt;) then for every server three steps is performed:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Removing the server from the loadbalancer waiting for it to no logger have any running requests&lt;/li&gt;
&lt;li&gt;Restarting the container with the new version&lt;/li&gt;
&lt;li&gt;Adding the server back to the loadbalancer&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is done for one server at the time so the users won't notice anything during the deploy. The deploy is triggered using the following command: &lt;code&gt;ansible-playbook --extra-vars version=&amp;lt;tag&amp;gt; deploy.yaml&lt;/code&gt;. This can be done manually or by a CI system.&lt;/p&gt;

&lt;p&gt;Docker-images are build for every push to git by a CI system and pushed to a docker container registry where each server can download the image.&lt;/p&gt;

&lt;p&gt;When running with this approach I have not seen any major problems when performing normal development of the application. These actions include updating the code, running database migrations and adding new severs. Despite not having any problems yet this approach has some drawbacks when compared to other solutions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is hard to add new services/other types of containers&lt;/li&gt;
&lt;li&gt;It is hard to run periodic tasks&lt;/li&gt;
&lt;li&gt;The environment cannot easily be scaled automatically&lt;/li&gt;
&lt;li&gt;The application cannot automatically move containers from broken servers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For this particular application the advantages of simplicity far outweighs these limitations. But for other applications the tradeoffs might be different. It is also possible move to an orchestration platform if required without too much work to throw away if required in the future.&lt;/p&gt;

&lt;p&gt;An almost complete and running example of this can be found at github: &lt;a href="https://github.com/mattiaslundberg/ansible-orchestration"&gt;https://github.com/mattiaslundberg/ansible-orchestration&lt;/a&gt;&lt;/p&gt;

</description>
      <category>docker</category>
      <category>devops</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
