Dockerized Application Deployment Using Ansible

Task Overview

This Task focuses on automating the deployment of a multi-container application using Ansible. The goal is to install Docker, configure the environment, and deploy a containerized application with Docker Compose on multiple servers. The project follows Infrastructure as Code (IaC) principles to ensure consistency and automation.

Technology Stack

Architecture Diagram

                    +----------------------+
                    |    User Request      |
                    +----------+-----------+
                               |
                               v
                      +----------------+
                      |    Nginx       |  --> Reverse Proxy
                      +----------------+
                               |
          +-------------------------------------+
          |           Docker Network           |
          +-------------------------------------+
                |                   |
         +------------+      +--------------+
         |   Flask    |      |    MySQL     |  --> Database
         +------------+      +--------------+
                |
        +---------------+
        |    Redis      |  --> Caching Layer
        +---------------+
    

Implementation Plan

1. Infrastructure Setup

2. Ansible Playbook for Docker Installation

Create an Ansible playbook to install Docker, configure the user, and deploy the application.

Ansible Inventory File (inventory.ini)

[web_servers]
server1 ansible_host=192.168.1.10 ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/id_rsa
server2 ansible_host=192.168.1.11 ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/id_rsa
    

Ansible Playbook (docker-setup.yml)

---
- name: Install Docker and Deploy Application
  hosts: web_servers
  become: yes
  tasks:
    - name: Install required packages
      apt:
        name:
          - docker.io
          - docker-compose
          - python3-pip
          - python3-docker
        state: present
        update_cache: yes

    - name: Add user to Docker group
      user:
        name: ubuntu
        groups: docker
        append: yes

    - name: Start Docker service
      systemd:
        name: docker
        state: started
        enabled: yes

    - name: Copy Docker Compose file to remote servers
      copy:
        src: ./docker-compose.yml
        dest: /home/ubuntu/docker-compose.yml

    - name: Deploy Application with Docker Compose
      command: docker-compose -f /home/ubuntu/docker-compose.yml up -d
      args:
        chdir: /home/ubuntu/

    

3. Docker Compose Configuration

The docker-compose.yml file defines the multi-container application with services for:

  1. Flask Web App
  2. Nginx Reverse Proxy
  3. MySQL Database
  4. Redis Cache

docker-compose.yml

version: '3.8'

services:
  web:
    image: myflaskapp:latest
    build: ./app
    container_name: flask_app
    restart: always
    ports:
      - "5000:5000"
    environment:
      - DATABASE_URL=mysql://user:password@db/mydb
      - REDIS_URL=redis://redis:6379
    depends_on:
      - db
      - redis

  db:
    image: mysql:5.7
    container_name: mysql_db
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
      MYSQL_DATABASE: mydb
      MYSQL_USER: user
      MYSQL_PASSWORD: password
    ports:
      - "3306:3306"
    volumes:
      - db_data:/var/lib/mysql

  redis:
    image: redis:alpine
    container_name: redis_cache
    restart: always
    ports:
      - "6379:6379"

  nginx:
    image: nginx:latest
    container_name: nginx_proxy
    restart: always
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
    depends_on:
      - web

volumes:
  db_data:

    

4. Running the Deployment

Step 1: Clone the Repository

git clone https://github.com/your-repo/dockerized-app.git
cd dockerized-app
ansible-playbook -i inventory.ini docker-setup.yml
    

Step 2: Run the Ansible Playbook

ansible-playbook -i inventory.ini docker-setup.yml

5. Verification & Testing

Enhancements & Next Steps

Conclusion

This project provides an automated way to deploy a multi-container application using Docker Compose and Ansible. It ensures scalability, consistency, and ease of management.