Pythian Blog: Technical Track

Deploy Docker containers using AWS Opsworks

Introduction

This post is about how to deploy Docker containers on AWS using Opsworks and Docker Composer. For AWS and Docker, the introduction isn't required. So, let's quickly introduce Opsworks and Docker Composer.

Opsworks

Opsworks is a great tool provided by AWS, which runs Chef recipes on your Instances. If the instance is an AWS instance, you don't pay anything for using Opsworks, but you can also manage instances outside of AWS with a flat cost just by installing the Agent and registering the instance on Opsworks.

Opsworks Instances type

We have three different types of instances on Opsworks: 1. 24x7x365 Run with no stop 2. Time based Run in a predefined time. Such as work hours. 3. Load based Scale up and down according to the metrics preconfigured. You can find more details here.

Custom JSON

Opsworks provides Chef Databags (variables to be used in your recipes) via Custom JSON, and that's the key to this solution. We will manage everything just changing a JSON file. This file can become a member of your development pipeline easily.

Life cycle

Opsworks has five life cycles: 1. Setup 2. Configure 3. Deploy 4. Undeploy 5. Shutdown We will use setup, deploy, and shutdown. You can find more details about Opsworks life cycle here.

Docker Compose

Docker Compose was originally developed under the Fig project. Nowadays, the fig is deprecated, and docker-compose is a built-in component of Docker. Using docker-compose, you can manage all containers and their attributes (links, share volumes, etc.) in a Docker host. Docker-compose can only manage containers on the local host where it is deployed. It cannot orchestrate Docker containers between hosts. All configuration is specified inside of a YML file.

Chef recipes

Using Opsworks, you will manage all hosts using just one small Chef cookbook. All the magic is in translating Custom JSON file from Opsworks to YML file to be used by docker-compose. The cookbook will install all components (Docker, pip, and docker-compose), translate Custom JSON to YML file and send commands to docker-compose.

Hands ON

Let's stop talking and see things happen. We can split it into five steps:
  1. Resources creation
    1. Opsworks Stack
        1. Log into your AWS account
        2. Go to Services -> Management Tools -> Opsworks Accessing Opsworks menu
        3. Click on Add stack (if you already have stacks on Opsworks) or Add your first stack (if it's the first time you are creating stacks on opsworks)
        4. Select type Chef 12 stack Note: The Chef cookbook used in this example only supports Chef12
        5. Fill out stack information aws_opsworks_docker_image02 Note: - You can use any name as stack name - Make sure VPC selected are properly configured - This solution supports Amazon Linux and Ubuntu - Repository URL https://bitbucket.org/tnache/opsworks-recipes.git
        6. Click on advanced if you want to change something. Changing "Use OpsWorks security groups" to No can be a good idea when you need to communicate with instances which are running outside of Opsworks
        7. Click on "Add stack"
    2. Opsworks layer
        1. Click on "Add a layer"
        2. Set Name, Short name and Security groups. I will use webserver
      Note: Use a simple name because we will use this name in next steps The Name web is reserved for AWS internal use
        1. Click on "Add layer"
      aws_opsworks_docker_image03
    3. Opsworks Instance
        1. Click on "Instances" on left painel
        2. Click on "Add an instance"
        3. Select the size (instance type)
        4. Select the subnet
        5. Click on "Add instance"
      aws_opsworks_docker_image05
  2. Resources configuration
    1. Opsworks stack
        1. Click on "Stack" on left painel
        2. Click on "Stack Settings"
        3. Click on "Edit"
        4. Find Custom JSON field and paste the content of the file bellow
      custom_json_1
      1. Click on "Save"
    2. Opsworks layer
        1. Click on "Layers" on left painel
        2. Click on "Recipes"
        3. Hit docker-compose and press enter on Setup
        4. Hit docker-compose::deploy and press enter on Deploy
        5. Hit docker-compose::stop and press enter on Deploy
        6. Click on "Save"
      aws_opsworks_docker_image04
  3. Start
    1. Start instance
        1. Click on start
      aws_opsworks_docker_image06
  4. Tests Note: Wait until instance get online state
      1. Open your browser and you should be able to see It works!
      2. Checking running containers
    aws_opsworks_docker_image07
  5. Management
      1. Change custom json to file bellow (See resources configuration=>Opsworks stack)
    custom_json_2
      1. Click on "Deployments" on left painel
      2. Click on "Run Command"
      3. Select "Execute Recipes" as "Command"
      4. Hit "docker-compose::deploy" as "Recipes to execute"
      5. Click on "Execute Recipes"
    Note: Wait until deployment finish
      1. Checking running containers
    aws_opsworks_docker_image08

No Comments Yet

Let us know what you think

Subscribe by email