<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Cumi Oyemike on Medium]]></title>
        <description><![CDATA[Stories by Cumi Oyemike on Medium]]></description>
        <link>https://medium.com/@cumioyemike?source=rss-2f8a80ce8714------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*rwkO3b_pbCXbuU8e</url>
            <title>Stories by Cumi Oyemike on Medium</title>
            <link>https://medium.com/@cumioyemike?source=rss-2f8a80ce8714------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 15:18:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cumioyemike/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Build and Deploy an Nginx Load Balancing Infrastructure using Ansible and Vagrant]]></title>
            <link>https://cumioyemike.medium.com/build-and-deploy-an-nginx-load-balancing-infrastructure-using-ansible-and-vagrant-6fe8f570abd9?source=rss-2f8a80ce8714------2</link>
            <guid isPermaLink="false">https://medium.com/p/6fe8f570abd9</guid>
            <category><![CDATA[load-balancing]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[cloud]]></category>
            <category><![CDATA[nginx]]></category>
            <category><![CDATA[ansible]]></category>
            <dc:creator><![CDATA[Cumi Oyemike]]></dc:creator>
            <pubDate>Wed, 06 Oct 2021 04:13:12 GMT</pubDate>
            <atom:updated>2021-10-06T14:26:49.299Z</atom:updated>
            <content:encoded><![CDATA[<p>This article will walk you through building the infrastructure in the diagram below and deploying a simple python application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/403/1*Z_bcEY8mcPPTyK_oJ8o3QA.jpeg" /></figure><p><strong>Before we begin, let’s get a quick understanding of what load balancing is.</strong></p><p>In a hypothetical situation, you are contracted to build a web application for a small pizza delivery business. You do good work and the client is satisfied. The business gets very popular quickly causing a huge increase in the number of visits to the website, but the website suddenly begins to malfunction. After troubleshooting, you find out that the downtime is a result of the server’s inability to handle the traffic surge. How do you fix this?</p><p>This is where load balancing comes in. The solution to this problem is to increase the number of servers so that instead of user requests hitting just one over and over, there’s another server to handle the load too. Load balancers are servers that forward internet traffic to multiple servers. They distribute workload evenly across multiple servers to maximize speed and performance and prevent downtime. User requests go to the load balancer which then decides how to distribute the load across the servers.</p><p><strong>Now let’s dive in!</strong></p><p>Setup requirements for your local machine:</p><p>- <a href="https://virtualbox.org/wiki/Downloads">Virtualbox</a></p><p>- <a href="http://vagrantup.com/">Vagrant</a></p><p>- <a href="https://ansible.com/">Ansible</a></p><p>1) The first thing you need to do is create the virtual machines using vagrant. We will define three VMs for this infrastructure — the load balancer, server 1, and server 2.</p><p>Create a <strong>Vagrantfile</strong> in your project directory.</p><pre>$ touch Vagrantfile</pre><p>We will use the <em>ubuntu/bionic64 </em>box for the three VMs. Put the following code in your Vagrantfile.</p><pre>Vagrant.configure(2) do |config|</pre><pre>   config.vm.box = &quot;ubuntu/bionic64&quot;</pre><pre>     <br>   # configure loadbalancer</pre><pre>   config.vm.define &quot;load-balancer&quot; do |loadbalancer|</pre><pre>     loadbalancer.vm.network &quot;private_network&quot;, ip: &quot;192.168.33.11&quot;</pre><pre>     loadbalancer.vm.hostname = &quot;load-balancer&quot;</pre><pre>     loadbalancer.vm.network &quot;forwarded_port&quot;, guest: 80, host: 8080</pre><pre>     loadbalancer.vm.provider &quot;virtualbox&quot; do |vb|</pre><pre>       vb.memory = 512</pre><pre>       vb.customize [&quot;modifyvm&quot;, :id, &quot;--audio&quot;, &quot;none&quot;]</pre><pre>     end</pre><pre>   end</pre><pre>   # configure web servers</pre><pre>   config.vm.define &quot;server-1&quot; do |server_1|</pre><pre>     server_1.vm.network &quot;private_network&quot;, ip: &quot;192.168.33.12&quot;</pre><pre>     server_1.vm.hostname = &quot;server-1&quot;</pre><pre>     server_1.vm.provider &quot;virtualbox&quot; do |vb|</pre><pre>       vb.memory = 512</pre><pre>       vb.customize [&quot;modifyvm&quot;, :id, &quot;--audio&quot;, &quot;none&quot;]</pre><pre>     end</pre><pre>   end</pre><pre>   config.vm.define &quot;server-2&quot; do |server_2|</pre><pre>     server_2.vm.network &quot;private_network&quot;, ip: &quot;192.168.33.13&quot;</pre><pre>     server_2.vm.hostname = &quot;server-2&quot;</pre><pre>     server_2.vm.provider &quot;virtualbox&quot; do |vb|</pre><pre>       vb.memory = 512</pre><pre>       vb.customize [&quot;modifyvm&quot;, :id, &quot;--audio&quot;, &quot;none&quot;]</pre><pre>     end</pre><pre>   end</pre><pre>   <br>   # update the packages on the VMs</pre><pre>   config.vm.provision &quot;shell&quot;, inline: &lt;&lt;-SHELL</pre><pre>     apt-get update</pre><pre>   SHELL</pre><pre>end</pre><p>2) Next, launch the VMs. Open a terminal in your present working directory and run</p><pre>$ vagrant up</pre><p>3) Since we are using Ansible for configuration management, we need to write Ansible manifests to configure the VMs. These manifest files are written in a data serialization language called YAML and are called Playbooks. Create a folder in the directory called <strong>ansible</strong>.</p><pre>$ mkdir ansible</pre><p>4) Create an <strong>ansible.cfg</strong> file within the <strong>ansible</strong> folder. The <strong>ansible.cfg</strong> file contains Ansible’s default configuration.</p><pre>$ touch ansible.cfg</pre><p>Now let’s specify some default settings in the file</p><pre>[defaults]</pre><pre>gathering = smart</pre><pre>host_key_checking = false</pre><pre>inventory = ./hosts # the location of your hosts file</pre><p>5) Ansible needs to know which hosts to use when running a playbook. Hosts are declared and grouped in a file that is known as the inventory. Create a <strong>hosts</strong> file within the ansible folder and put the hosts from the Vagrantfile created earlier along with their IP addresses in it.</p><pre>$ touch hosts</pre><pre>server-1 ansible_host=192.168.33.12</pre><pre>server-2 ansible_host=192.168.33.13</pre><pre>load-balancer ansible_host=192.168.33.11</pre><p>Put the hosts into groups</p><pre>[webservers]</pre><pre>server-1</pre><pre>server-2</pre><pre>[loadbalancer]</pre><pre>load-balancer</pre><p>There is a default group in Ansible called <strong>all</strong> that contains every host. We will use this group to define variables that we want to apply to all available hosts.</p><pre>[all:vars]</pre><pre>ansible_user=vagrant</pre><pre>ansible_ssh_pass=vagrant</pre><pre>ansible_python_interpreter=/usr/bin/python3</pre><p>6) Now we create the playbooks.</p><p>First, create a general playbook that imports the other playbooks when referenced.</p><pre>touch site.yml</pre><pre>---</pre><pre>- import_playbook: playbook_nginx.yml</pre><pre>- import_playbook: playbook_web_servers.yml</pre><p><strong>nginx_playbook.yml</strong> and <strong>webservers_playbook.yml</strong> will contain automation tasks for Nginx and the web servers respectively.</p><p>7) Create the <strong>webservers_playbook.yml</strong> file</p><pre>touch nginx_playbook.yml</pre><pre>---</pre><pre>- hosts: all #Apply these tasks to host group &#39;all&#39;</pre><pre>  tasks:</pre><pre>  - name: Install pip</pre><pre>    apt:</pre><pre>      name: python3-pip</pre><pre>      state: present</pre><pre>    become: yes</pre><pre>  <br>  - name: Install Flask</pre><pre>    command: pip3 install -U Flask</pre><pre>    become: yes</pre><pre><br>  - name: Create app #Create a flask app in the home directory of     the VMs</pre><pre>    become: yes</pre><pre>    copy:</pre><pre>      dest: “/home/vagrant/hello.py”</pre><pre>      content: |</pre><pre>        from flask import Flask</pre><pre>        <br>        app = Flask(__name__)</pre><pre>        @app.route(‘/’)</pre><pre><br>        def index():</pre><pre>          return “Hello from Server”</pre><pre>        if __name__ == ‘__main__’:</pre><pre>        app.run(debug = True, host=’0.0.0.0&#39;, port=’5000&#39;)</pre><pre><br>  - name: Get Server and insert in the app # “Server” in the flask app should be the host name of the server</pre><pre>    shell: sed -i “s/IP/$(hostname)/g” /home/vagrant/hello.py</pre><pre>    become: yes</pre><pre><br>  - name: Create a service for the app #Create a service to start the flask application automatically</pre><pre>   become: yes</pre><pre>   copy:</pre><pre>    dest: “/etc/systemd/system/app.service”</pre><pre>    mode: 0664</pre><pre>    owner: vagrant</pre><pre>    content: |</pre><pre>      [Unit]</pre><pre>      Description=Flask application</pre><pre>      [Service]</pre><pre>      ExecStart =/ usr/bin/python3 /home/vagrant/hello.py</pre><pre>      [Install]</pre><pre>      WantedBy = multi-user.target</pre><pre><br>  - name: Start app #Start the application by running the service created earlier</pre><pre>    become: yes</pre><pre>    systemd:</pre><pre>      state: started</pre><pre>      daemon_reload: yes</pre><pre>      name: app.service</pre><pre>      enabled: yes</pre><p>8) We need to create an Nginx configuration file that would tell the Nginx load balancer to balance the load between server-1 and server-2.</p><pre>touch nginx.cfg</pre><p>Write the configuration settings</p><pre>upstream backend {</pre><pre>  server 192.168.33.12:5000; #server-1</pre><pre>  server 192.168.33.13:5000; #server-2</pre><pre>}</pre><pre><br>server {</pre><pre>  listen 80; #listen on port 80</pre><pre>  server_name web.app;</pre><pre>  location / {</pre><pre>    proxy_read_timeout 300s;</pre><pre>    proxy_pass <a href="http://backend;/">http://backend;</a> #pass all requests processed to the backend upstream servers</pre><pre>  }</pre><pre>}</pre><p>9) Create the <strong>nginx_playbook.yml</strong> file to automate the load balancing tasks</p><pre>touch nginx_playbook.yml</pre><p>Put the following tasks in the <strong>nginx_playbook.yml</strong> file</p><pre>---</pre><pre>- hosts: all</pre><pre>  tasks:</pre><pre>    - name: Ensure Nginx is installed and it is the latest version</pre><pre>      apt:</pre><pre>        name: nginx</pre><pre>        state: latest</pre><pre>        become: yes</pre><pre>   <br>    - name: Start Nginx</pre><pre>      service:</pre><pre>        name: nginx</pre><pre>        state: started</pre><pre>      become: yes</pre><pre>    <br>    - name: Copy the Nginx config file and restart nginx</pre><pre>      copy:</pre><pre>        src: ./nginx.cfg</pre><pre>        dest: /etc/nginx/sites-available/nginx.cfg</pre><pre>      become: yes</pre><pre>     <br>    - name: Create symlink</pre><pre>      file:</pre><pre>        src: /etc/nginx/sites-available/nginx.cfg</pre><pre>        dest: /etc/nginx/sites-enabled/default</pre><pre>        state: link</pre><pre>      become: yes</pre><pre><br>    - name: Restart nginx</pre><pre>      service:</pre><pre>        name: nginx</pre><pre>        state: restarted</pre><pre>      become: yes</pre><p>10) The playbooks have been created, but to deploy the load balancer we need to provision Ansible in the Vagrantfile. We’ll use the Ansible Provisioner for this.</p><p>Let’s write the provisioning blocks</p><pre>loadbalancer.vm.provision “ansible” do |ansible|</pre><pre>  ansible.verbose = “v”</pre><pre>  ansible.playbook = “ansible/site.yml”</pre><pre>  ansible.become = true</pre><pre>  ansible.inventory_path = “ansible/hosts”</pre><pre>end</pre><pre><br>server_1.vm.provision “ansible” do |ansible|</pre><pre>  ansible.verbose = “v”</pre><pre>  ansible.playbook = “ansible/site.yml”</pre><pre>  ansible.become = true</pre><pre>  ansible.inventory_path = “ansible/hosts”</pre><pre>end</pre><pre><br>server_2.vm.provision “ansible” do |ansible|</pre><pre>  ansible.verbose = “v”</pre><pre>  ansible.playbook = “ansible/site.yml”</pre><pre>  ansible.become = true</pre><pre>  ansible.inventory_path = “ansible/hosts”</pre><pre>end</pre><p>Your final <strong>Vagrantfile</strong> should look like this</p><pre>Vagrant.configure(2) do |config|</pre><pre>  config.vm.box = “ubuntu/bionic64”</pre><pre><br>  # configure loadbalancer</pre><pre>  config.vm.define “load-balancer” do |loadbalancer|</pre><pre>    loadbalancer.vm.network “private_network”, ip: “192.168.33.11”</pre><pre>    loadbalancer.vm.hostname = “load-balancer”</pre><pre>    loadbalancer.vm.network “forwarded_port”, guest: 80, host: 8080</pre><pre>    loadbalancer.vm.provider “virtualbox” do |vb|</pre><pre>      vb.memory = 512</pre><pre>      vb.customize [“modifyvm”, :id, “ — audio”, “none”]</pre><pre>    end</pre><pre><br>     loadbalancer.vm.provision “ansible” do |ansible|</pre><pre>       ansible.verbose = “v”</pre><pre>       ansible.playbook = “ansible/site.yml”</pre><pre>       ansible.become = true</pre><pre>       ansible.inventory_path = “ansible/hosts”</pre><pre>     end</pre><pre>   end</pre><pre>  # configure web servers</pre><pre><br>  config.vm.define “server-1” do |server_1|</pre><pre>      server_1.vm.network “private_network”, ip: “192.168.33.12”</pre><pre>      server_1.vm.hostname = “server-1”</pre><pre>      server_1.vm.provider “virtualbox” do |vb|</pre><pre>        vb.memory = 512</pre><pre>        vb.customize [“modifyvm”, :id, “ — audio”, “none”]</pre><pre>      end</pre><pre><br>      server_1.vm.provision “ansible” do |ansible|</pre><pre>        ansible.verbose = “v”</pre><pre>        ansible.playbook = “ansible/site.yml”</pre><pre>        ansible.become = true</pre><pre>        ansible.inventory_path = “ansible/hosts”</pre><pre>      end</pre><pre>  end</pre><pre><br>  <br>  config.vm.define “server-2” do |server_2|</pre><pre>    server_2.vm.network “private_network”, ip: “192.168.33.13”</pre><pre>    server_2.vm.hostname = “server-2”</pre><pre>    server_2.vm.provider “virtualbox” do |vb|</pre><pre>      vb.memory = 512</pre><pre>      vb.customize [“modifyvm”, :id, “ — audio”, “none”]</pre><pre>    end</pre><pre><br>    server_2.vm.provision “ansible” do |ansible|</pre><pre>      ansible.verbose = “v”</pre><pre>      ansible.playbook = “ansible/site.yml”</pre><pre>      ansible.become = true</pre><pre>      ansible.inventory_path = “ansible/hosts”</pre><pre>    end</pre><pre>  end</pre><pre>  <br>  # update the packages on the VMs<br>  config.vm.provision “shell”, inline: &lt;&lt;-SHELL</pre><pre>    apt-get update</pre><pre>  SHELL</pre><pre>end</pre><p>11) Deploy the load balancer by running</p><pre>vagrant up --provision</pre><p>This will take a while.</p><p><strong>Manual Tests</strong></p><p>There are two ways to manually check that the load balancer is working correctly:</p><p>- Open a web browser on your machine go to the load balancer’s IP address</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/649/1*L8NNj1-P2voGosdZAFn9cA.png" /><figcaption>Response from the first server</figcaption></figure><p>The application is first served from server-1, and when refreshed server-2 responds.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/647/1*upd5Rn_httm6RnRwHK4FxA.png" /><figcaption>Response from the second server</figcaption></figure><p><em>This alternating method of load balancing is called the Round Robin technique.</em></p><p>- Another way we can check that the load balancer works is to run the following command from the terminal</p><pre>curl <a href="http://192.168.33.11/">http://192.168.33.11</a></pre><p>The response should alternate between server-1 and server-2</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y4Pr2cMzlw-7DBDSyz-y-A.png" /><figcaption>output</figcaption></figure><p>If these tests passed, congrats! you just deployed your first load balancer.</p><p><strong>Conclusion</strong></p><p>In this tutorial, you learnt what load balancing is and how to build and deploy an Nginx load balancer using Ansible for configuration management.</p><p>Keep learning! 🤓</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6fe8f570abd9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>