Module 8: Introducing Containers and Container Services
AWS Academy Cloud Developing
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Section 1: Introduction
Module 8: Introducing Containers and Container Services
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Module objectives
At the end of this module, you should be able to do the following:
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
3
Module overview
Sections
Lab
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
4
Knowledge check
Café business requirement
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
5
Frank and Martha recently acquired a coffee bean supplier, and they would like to include the supplier’s inventory tracking system into the café's application infrastructure. Sofía is thinking about migrating the application database to containers to complete the integration.
Containers as part of developing�a cloud application
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
6
Section 2: Introducing containers
Module 8: Introducing Containers and Container Services
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Shipping containers
Before shipping containers
After shipping containers
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
8
A container is a standardized unit of software
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
9
Evolution of deployment models: �Bare-metal servers
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
10
Host server
Host OS
Libraries
Applications A, B, C
Applications compete for server resources
Library versions must be shared across applications
Inflexible hardware costs
Evolution of deployment models:�VMs
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
11
Host server
Host OS
Virtualization platform
VM
Guest OS
Libraries
Application� A
VM
Guest OS
Libraries
Application�B
VM
Guest OS
Libraries
Application� C
Isolated applications
Isolated libraries
Better resource utilization
More OS maintenance
More server space for OS
Evolution of deployment models: Containers
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
12
Host server
Host OS
Containerization platform
Shared libraries
Container
Application� A
Bins/libs
Container
Bins/libs
Application B
Container
Bins/libs
Application C
Better resource utilization
Shared OS kernel
Shared or isolated libraries
Highly portable ;�applications run identically across environments
Lightweight, efficient, and fast
Section 2 key takeaways
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
13
Section 3: Introducing Docker containers
Module 8: Introducing Containers and Container Services
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Docker container virtualization platform
Lightweight container virtualization platform
Tools to create, store, manage, and run containers
Integration with automated build, test, and deployment pipelines
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
15
Docker container benefits
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
16
Docker container components
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
17
Layer 1
Layer 2
Layer 3
Layer 4
Dockerfile
Plain text file that provides instructions to create a container image
Container image
Read-only template that is used to create writable containers
Container�Runnable instance of an image
Container registry�Private or public images that you can base other images on
Container layer
Thin read/write layer that is used to �make changes to the running container
Dockerfile simple example
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
18
# Start with the Ubuntu latest image
FROM ubuntu:latest
# Output hello world message
CMD echo "Hello World!"
Dockerfile example: �Start a Java application
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
19
# Start with open JDK version 8 image
FROM openjdk:8
# Copy the .jar file that contains your code from your system to the container
COPY /hello.jar /usr/src/hello.jar
# Call Java to run your code
CMD java –cp /usr/src/hello.jar
Org.example.App
Dockerfile example:�Common tasks
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
20
# Start with CentOS 7 image
FROM centos:7
# Update the OS and install Apache
RUN yum –y update && yum –y install httpd
# Expose port 80—the port that the web server “listens to”
EXPOSE Port 80
# Copy shell script and give it run permissions
ADD run-httpd.sh /run-httpd.sh
RUN chmod –v +x /run-httpd.sh
# Run shell script
CMD ["/run-httpd.sh"]
Each line of the Dockerfile adds a layer
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
21
# 1 Start with CentOS 7 image
FROM centos:7
# 2 Update the OS and install Apache
RUN yum –y update && yum –y install httpd
# 3 Expose port 80
EXPOSE Port 80
# 4 Copy shell script and give it run permissions
ADD run-httpd.sh /run-httpd.sh
RUN chmod –v +x /run-httpd.sh
CMD ["/run-httpd.sh"]
Image layers (read-only)
RUN yum –y update && yum –y �install httpd
EXPOSE 80
ADD run-httpd.sh /run-httpd.sh
RUN chmod –v +x /run-httpd.sh
Base: CentOS 7
1
2
3
4
4
Docker CLI commands
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
22
Command | Description |
docker build | Build an image from a Dockerfile. |
docker images | List images on the Docker host. |
docker run | Launch a container from an image. |
docker ps | List the running containers. |
docker stop | Stop a running container. |
docker start | Start a container. |
docker push | Push the image to a registry. |
docker tag | Tag an image. |
Command | Description |
docker logs | View container log output. |
docker port | List container port mappings. |
docker inspect | Inspect container information. |
docker exec | Run a command in a container. |
docker rm | Remove one or more containers. |
docker rmi | Remove one or more images from the host. |
docker update | Dynamically update the container configuration. |
docker commit | Create a new image from a container's changes. |
Example of docker build command
Build an image from a Dockerfile in the current directory, and name the image node_app
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
23
docker build --tag node_app .
Task
Docker command
Sending build context to Docker daemon 9.007MB
Step 1/7 : FROM node:11-alpine
11-alpine: Pulling from library/node
…
Successfully built a5886f101e12
Successfully tagged node_app:latest
Example output
Example of docker images command
List the images that your Docker client is aware of
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
24
docker images
Task
Docker command
Example output
REPOSITORY | TAG | IMAGE ID | CREATED | SIZE |
<none> | node_app:latest | a5886f101e12 | 18 seconds ago | 82.7MB |
| | | | |
Example of docker run command
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
25
docker run -d --name node_app_1 -p 8000:80 node_app
Tasks
Docker command
Example output
5ed1ea04bcb58194100f71b2e7cd0aecab182313692ed833a6a700664994785f
docker ps
CONTAINER ID | IMAGE | COMMAND | CREATED | STATUS | PORTS |
5ed1ea04bcb5 | node_app | "docker-entrypoint.s…" | 9 seconds ago | Up 7 seconds | 0.0.0.0:8000->80/tcp |
Example of docker exec command
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
26
docker exec -it node_app_1 sh
Tasks
Docker command
Example output
/usr/src/app #
/usr/src/app # ls
�Dockerfile README.md app index.js network.template node_modules package-lock.json package.json public views
/usr/src/app # exit
Example of docker stop and docker rm commands
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
27
docker stop node_app_1 && docker rm node_app_1
Tasks
Docker command
Example output
node_app_1
node_app_1
Section 3 key takeaways
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
28
Lab 8.1: Migrating a Web Application to Docker Containers
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
29
Lab: Scenario
Recently, the café owners acquired one of their favorite coffee suppliers. The acquired coffee supplier runs an inventory tracking application on an AWS account.
In this lab, you again play the role of Sofía, and you will work to migrate the application to run on containers.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
30
Lab: Tasks
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
31
Lab: Final product
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
32
AWS Cloud9 EC2 instance (being used as a container host)
EC2 instance guest OS
Bins/libs
node app
Docker engine
Docker container
Network
connectivity
Bins/libs
mysql
Docker container
Begin Lab 8.1: Migrating a Web Application to Docker Containers
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
33
~ 90 minutes
Lab debrief: �Key takeaways
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
34
Section 4: Using containers for microservices
Module 8: Introducing Containers and Container Services
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Comparing monolithic�and microservice architectures
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
36
Storefront UI
Account service
Cart service
Shipping service
Data access service
Storefront UI (Node.js)
Account service (Node.js)
Cart service (Python)
Shipping service (Java)
Monolithic
Microservices
Database
Load balancer
Browser
Load balancer
Browser
Microservices and containers
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
37
Microservices design | Container characteristics |
|
|
|
|
|
|
|
|
Section 5: Introducing AWS container services
Module 8: Introducing Containers and Container Services
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Challenges of managing containers at scale
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
39
Container orchestration platforms
Scheduling
Placement
Service integration
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
40
Amazon ECS
Fully managed container orchestration service
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
41
Amazon Elastic Container Service (Amazon ECS)
Amazon ECR
Fully managed container registry that you can use to easily store, run, and manage container images for applications that run on Amazon ECS
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
42
Amazon Elastic Container Registry (Amazon ECR)
Amazon ECS solution architecture
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
43
Select the launch type
Amazon �ECS
AWS �Fargate
Amazon�EC2
Other �container �registry
Amazon �ECR
Pull the container �image from �a registry
Define your�application
Manage �your containers
Amazon ECS with Fargate�or Amazon EC2
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
44
Docker engines (one per OS in the cluster)
Bins/libs
App 1
Bins/libs
App 2
Bins/libs
App 3
Containers
VM guest operating systems in the Amazon ECS cluster
Container instance 1
Container instance 2
Container instance 3
AWS manages
You manage
Amazon ECS cluster backed by Fargate
Amazon ECS cluster backed by Amazon EC2
You manage
Choose Amazon EC2:
Choose Fargate:
Creating an Amazon ECR repository�and pushing an image
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
45
# Create a repository called hello-world
> aws ecr create-repository \
--repository-name hello-world \
--region us-east-1
# Build and tag an image
> docker build -t hello-world .
> docker tag hello-world:latest aws_account_id.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest
# Authenticate Docker to your Amazon ECR registry
# You can skip the `docker login` step if you have amazon-ecr-credential-helper set up
> aws ecr get-login-password --region region | docker login --username AWS --password-stdin aws_account_id.dkr.ecr.region.amazonaws.com
# Push an image to your repository
> docker push aws_account_id.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest
Amazon EKS
Managed service that runs Kubernetes on �the AWS Cloud
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
46
Amazon Elastic Kubernetes Service (Amazon EKS)
Amazon EKS
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
47
Amazon EKS
Run Kubernetes�applications
Connect to �Amazon EKS
Provision an �Amazon EKS �cluster
Amazon EC2
Deploy worker�nodes for your�Amazon EKS�cluster
Fargate
Section 5 key takeaways
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
48
Section 6: Deploying applications with�Elastic Beanstalk
Module 8: Introducing Containers and Container Services
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Elastic Beanstalk
Service for deploying and scaling web applications and services
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
50
AWS Elastic Beanstalk
Elastic Beanstalk components
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
51
Component | Description |
Application | Logical collection of Elastic Beanstalk components. Conceptually similar to a folder. |
Application version | Specific, labeled iteration of deployable code for a web application. |
Environment | Collection of AWS resources that run an application version. |
Environment tier | Designation of the type of application that the environment runs. Determines what resources Elastic Beanstalk provisions to support it. |
Environment configuration | Collection of parameters and settings that define how an environment and its associated resources behave. |
Saved configuration | Template that you can use as a starting point for creating unique environment configurations. |
Platform | Combination of an OS, programming language runtime, web server, application server, and Elastic Beanstalk components. You design and target your web application to a platform. |
Elastic Beanstalk CLI | CLI for Elastic Beanstalk. Provides interactive commands that simplify creating, updating, and monitoring environments from a local repository. |
IAM permissions in Elastic Beanstalk environments
Service role
Instance profile
User policies
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
52
IAM roles assigned during environment creation
Service role policy example
"Effect": "Allow",
"Action": [
"elasticloadbalancing:DescribeInstanceHealth",
"elasticloadbalancing:DescribeLoadBalancers",
"elasticloadbalancing:DescribeTargetHealth",
"ec2:DescribeInstances",
"ec2:DescribeInstanceStatus",
"ec2:GetConsoleOutput",
"ec2:AssociateAddress",
"ec2:DescribeAddresses",
"ec2:DescribeSecurityGroups",
"sqs:GetQueueAttributes",
"sqs:GetQueueUrl",
"autoscaling:DescribeAutoScalingGroups",
"autoscaling:DescribeAutoScalingInstances",
"autoscaling:DescribeScalingActivities",
"autoscaling:DescribeNotificationConfigurations",
"sns:Publish"
],
"Resource": [
"*"
]
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
53
AWSElasticBeanstalkEnhancedHealth
Elastic Beanstalk simplifies container deployment
Getting started with �Amazon ECS
Getting started with �Elastic Beanstalk
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
54
Multicontainer Docker platform
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
55
Elastic Beanstalk
Auto Scaling group, Amazon ECS cluster
Instance 1
Task 1-1
Instance 2
Task 1-2
app1.elasticbeanstalk.com:80
app1.elasticbeanstalk.com:9000
Elastic Load Balancing
Dockerrun.aws.json file
{
"AWSEBDockerrunVersion": 2,
"volumes": [
{
"name": "php-app",
"host": {
"sourcePath": "/var/app/current/php-app"
}
},
{
"name": "nginx-proxy-conf",
"host": {
"sourcePath": "/var/app/current/proxy/conf.d"
}
}
]
…
"containerDefinitions": [
{
"name": "php-app",
"image": "php:fpm",
"environment": [
{
"name": "Container",
"value": "PHP"
}
],
"essential": true,
"memory": 128,
"mountPoints": [
{
"sourceVolume": "php-app",
"containerPath": "/var/www/html",
"readOnly": true
}
]
}
…
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
56
Elastic Beanstalk deployment policies
All at once
Rolling
Rolling with batch
Traffic splitting
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
57
Immutable
Faster
More control
Deployment option namespaces
aws:elasticbeanstalk:command
aws:elasticbeanstalk:trafficsplitting
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
58
Example of traffic splitting�(canary testing)
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
59
Temporary Auto Scaling group
ELB
Auto Scaling group
v1
v2
option_settings:
aws:elasticbeanstalk:command:
DeploymentPolicy: TrafficSplitting
aws:elasticbeanstalk:trafficsplitting:
NewVersionPercent: "15"
EvaluationTime: "10"
Example deployment configurations
15%
Health checks: 10 minutes
Elastic Beanstalk
15%
v1
v2
Blue/green deployments�on Elastic Beanstalk
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
60
Elastic Beanstalk environment (blue)
Elastic Beanstalk environment (green)
myapp.useast-1.elasticbeanstalk.com
myapp_new.useast-1.elasticbeanstalk.com
3
Swap the CNAMEs
1
Clone the environment
2
Deploy and test in the new environment
Section 6 key takeaways
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
61
Lab 8.2: Running Containers on a Managed Service
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
62
Lab: Scenario
Sofía has containerized the coffee suppliers application, but wants to reduce the effort to maintain the application and improve its scalability.
As noted in the previous lab, Sofía wants to move the database to a managed database service rather than running it in a container.
Based on her research, she has made these decisions:
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
63
Aurora Serverless
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
64
Amazon Aurora
Fully managed relational database engine that is compatible with MySQL and PostgreSQL
Lab: Tasks
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
65
Lab: Final product
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
66
Public subnet 1
AWS
Cloud9
IDE
Region
VPC
ECR
Website image
Public subnet 2
Elastic Beanstalk
Classic
Load
Balancer
EC2 hosting
website image
EC2 hosting
website image
Deployment
Aurora Serverless
Aurora Serverless
Data
API
Begin Lab 8.2: Running Containers on a Managed Service
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
67
~ 90 minutes
Lab debrief: �Key takeaways
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
68
Module wrap-up
Module 8: Introducing Containers and Container Services
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Module summary
In summary, in this module, you learned how to do the following:
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
70
Complete the knowledge check
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
71
Sample exam question
A cloud architect wants to migrate a web application to containers. The team does not have much experience with AWS or containers, but the architect wants to get them started quickly to be able to experiment.
Which solution would be best?
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
72
Additional resources
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
73
Thank you
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. This work may not be reproduced or redistributed, in whole or in part, without prior written permission from Amazon Web Services, Inc. Commercial copying, lending, or selling is prohibited. Corrections, feedback, or other questions? Contact us at https://support.aws.amazon.com/#/contacts/aws-training. All trademarks are the property of their owners.
Containers as part of developing�a cloud application
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
75
AWS WAF �secures API endpoint
Café employees
Refresh cache per settings
CloudFront distribution
ElastiCache �for Memcached
AWS WAF �secures website
SNS
SQS
Suppliers
Coffee inventory
updates
Lambda
Step Functions �retrieves data from RDS and posts report to S3 with presigned URL
Amazon Cognito provides token for report requests
Amazon S3�
Developers
AWS Cloud9
CloudShell
AWS CLI
SDK for Python
Console
�bucket hosts café website
Café website users
Webpage �requests
Bucket policy
DynamoDB database stores products table
API Gateway REST API
Lambda function �does database lookups
Café employees
Elastic Beanstalk
ECR hosts Docker image
EC2 instance with Docker container runs coffee supplier website
Aurora Serverless on RDS stores supplier database