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:- Resources creation
- Opsworks Stack
- Log into your AWS account
- Go to Services -> Management Tools -> Opsworks
- 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)
- Select type Chef 12 stack Note: The Chef cookbook used in this example only supports Chef12
- Fill out stack information 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
- 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
- Click on "Add stack"
- Opsworks layer
- Click on "Add a layer"
- Set Name, Short name and Security groups. I will use webserver
- Click on "Add layer"
- Opsworks Instance
- Click on "Instances" on left painel
- Click on "Add an instance"
- Select the size (instance type)
- Select the subnet
- Click on "Add instance"
- Opsworks Stack
- Resources configuration
- Opsworks stack
- Click on "Stack" on left painel
- Click on "Stack Settings"
- Click on "Edit"
- Find Custom JSON field and paste the content of the file bellow
- Click on "Save"
- Opsworks layer
- Click on "Layers" on left painel
- Click on "Recipes"
- Hit docker-compose and press enter on Setup
- Hit docker-compose::deploy and press enter on Deploy
- Hit docker-compose::stop and press enter on Deploy
- Click on "Save"
- Opsworks stack
- Start
- Tests Note: Wait until instance get online state
- Open your browser and you should be able to see It works!
- Checking running containers
- Management
- Change custom json to file bellow (See resources configuration=>Opsworks stack)
- Click on "Deployments" on left painel
- Click on "Run Command"
- Select "Execute Recipes" as "Command"
- Hit "docker-compose::deploy" as "Recipes to execute"
- Click on "Execute Recipes"
- Checking running containers
Share this
You May Also Like
These Related Stories
Windows containers: installing SQL server
Windows containers: installing SQL server
Sep 21, 2015
8
min read
Extend Oracle Enterprise Manager (OEM) Compatibility with Proxy Monitoring
Extend Oracle Enterprise Manager (OEM) Compatibility with Proxy Monitoring
Jul 6, 2023
14
min read
Benchmarking Postgres on AWS 4,000 PIOPs EBS instances
Benchmarking Postgres on AWS 4,000 PIOPs EBS instances
May 8, 2013
5
min read
No Comments Yet
Let us know what you think