
上QQ阅读APP看书,第一时间看更新
Creating a cluster
First, we are going to create a cluster in the AWS console and see how it works.
- Go to the Amazon ECS page and click on Get started button (the only button in the screen as you haven't created any resources yet):

- Make sure that the two checkboxes are ticked before continuing. We want to deploy a sample application to ECS but also we want to store the images in ECR.
- The next screen is key: this is where we define the repository of our image, which will determine the repository URI that will be used for pushing images from our local machine using Docker.

- Just use devops-test as the repository name, and our repository URI will look very similar to the one shown in the preceding screenshot.
- Step number 2 (out of 6) is a series of commands provided by AWS to log in into ECR and push the images of our project. In this case, we are going to use a very simple application in Node.js:
var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Hello World!'); }); app.listen(3000, function () { console.log('Example app listening on port 3000!'); });
- Save the code from earlier in a file called index.js within a folder called devops-test on your local machine. As we are using express, we need to install the required dependency. Just execute the following command:
npm init
- After a few questions (just press Enter a few times and it should work), a file called package.json should be created. Now we need to install express for our program to run:
npm install --save express
- And voila! Our package.json file should have a line describing the required dependency:
{ "name": "code", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo "Error: no test specified"
&& exit 1", "start": "node index.js" }, "author": "", "license": "ISC", "dependencies": { "express": "^4.14.1" } }
- This file allows us to reinstall the dependencies whenever required without having to do it manually; it also allows us to specify a command that will be run when we execute npm start (a standard way of running a Node app using npm). Add the line and highlight it, as shown in the preceding code, as we will need it later (don't forget the semicolon from the previous line).
- Now we need to write our Dockerfile. A Dockerfile, as we will see in Chapter 3, Docker, is a file that describes what our Docker image looks like. In this case, we are going to reconstruct the steps needed to run the node application in a Docker container:
FROM node:latest RUN mkdir -p /usr/src/app WORKDIR /usr/src/app COPY package.json /usr/src/app/ RUN npm install COPY . /usr/src/app EXPOSE 3000 CMD [ "npm", "start" ]
- Don't try to understand the file; we will go deeper into this later on this book. Just save it with the name Dockerfile in the folder mentioned previously, devops-test. By now, your devops-test folder should look like this:

- Now we are ready to follow step 2 in the ECS setup. Be aware that the following image is regarding my user in AWS; your user will have different parameters, so use yours instead of copying from the preceding screenshot:

Once you finish it, a new version of the image with your app image should be installed in your private ECR.
- The next step (step 3) is creating what AWS calls a task definition, which is basically the configuration for one instance of our containers: how much memory we are going to use, which image we are going to run, and what ports we are going to expose in the container. Just leave the default memory but change the port to 3000, as it is the port that we used in the preceding example (the node application). This is typical docker parameter and we will learn more about it in the next chapter, where we will dive deeper into docker.
- Once you are ready, click on next and we will be with step 4. This step is where we are going to configure a service. By service, we mean the number of instances of our container are we going to keep alive and how are we going to expose them: using a load balancer or just in the EC2 instances that are part of the cluster. We will also be able to specify which IAM (AWS credential system) is going to be used for registering and deregistering running instances:

- We just leave everything by default except two parameters:
- The desired number of tasks: set to 2
- In the ELB section, we just select sample-app: 80 (or the option that isn't No ELB so AWS provisions an ELB for us)
- Click on the Next step, where we are going to define what our cluster is going to look like:
- The number of nodes
- The size of the nodes
- Once we are ready, just review and launch the instance. After a few minutes, our cluster should be up and running and ready to work with our deployed task.
You can access the instance of the task that we created in the load balancer provisioned by the cluster itself on the port 3000. As you can see, ECS makes the task of setting up a container cluster simple.
In this book, we are going to give special attention to Kubernetes and Docker Swarm mainly because they are platform agnostic technologies, but I believe that Amazon ECS is a very valid technology to be considered when building a new container-based system.