aws(学习笔记第十九课)
- 使用
ECS和Fargate进行容器开发
学习内容:
- 使用本地
EC2中部署docker应用 - 使用
ECS的EC2模式进行容器开发 - 使用
ECS的Fargate模式进行容器开发
1. 使用本地EC2中部署docker应用
-
docker整体

这里展示了docker的整体流程。- 开发阶段
- 编写
dockerfile,定义整个docker应用。(1.build) build docker image的过程中会从git repositorypull业务代码(2.pull business code)- 将
build之后的docker imagepush到docker repository(3.push) - 在
ec2 instance上,pull docker image from docker repository,之后执行docker image
docker的一个目的就是Build once,Run anywhere(搭建一次,到处能用)
- 编写
- 开发阶段
-
docker的概念• WHY CONTAINERS? Containers allow developers to iterate at high velocity and offer the speed to scale to meet the demands of the application. It’s first important to understand what a container is, and how it enables teams to move faster. • WHAT IS A CONTAINER? Containers provide a standard way to package your application’s code, configurations, and dependencies into a single object. Containers share an operating system installed on the server and run as resource-isolated processes, ensuring quick, reliable, and consistent deployments, regardless of environment. Whether you deploy locally on your laptop or to production, the experience will remain the same WHAT IS DOCKER? • Docker is a software platform that allows you to build, test, and deploy applications quickly. • Docker packages software into standardized units called containers that have everything the software needs to run including libraries, system tools, code, and runtime. • Whether you are running on Linux, Windows, or macOS, you can run containers! -
启动
EC2,并启动docker应用- 启动一个
EC2实例

- 进入
EC2实例,安装和启动dockerssh链接实例后,开始安装dockersudo -i yum install docker -y systemctl start docker systemctl enable docker docker info- 编写
dockerfile文件FROM ubuntu:18.04 # Install dependencies RUN apt-get update && \ apt-get -y install apache2 # Install apache and write hello world message RUN echo 'Hello World!' > /var/www/html/index.html # Configure apache RUN echo '. /etc/apache2/envvars' > /root/run_apache.sh && \ echo 'mkdir -p /var/run/apache2' >> /root/run_apache.sh && \ echo 'mkdir -p /var/lock/apache2' >> /root/run_apache.sh && \ echo '/usr/sbin/apache2 -D FOREGROUND' >> /root/run_apache.sh && \ chmod 755 /root/run_apache.sh EXPOSE 80 CMD /root/run_apache.sh - 运行
docker命令build
注意,这里需要sudo docker build -t hello-world .sudo权限执行docker命令 - 运行
docker命令开始运行sudo docker run -i -t -p 80:80 hello-world
- 访问
EC2实例的80端口

- 启动一个
2. 使用ECS的EC2模式进行容器开发
-
ECS的整体概念

- 首先定义
task
这个是application的核心,它定义了这个应用程序的docker image,这里docker image不太合适,应该叫ECS的json定义,也就是ECS的task的静态定义。 - 其次定义一个
cluster
有了静态定义,还需要动态定义,即执行环境。这是task的执行环境定义。 - 最后定义
service
这里就是一个粘合剂,把task(静态定义)和cluster(动态定义)进行结合起来。
- 首先定义
-
进入
ECS,首先进行任务定义(静态定义)- 使用
json定义任务{ "family": "yourApp-demo", "containerDefinitions": [ { "volumesFrom": [], "portMappings": [ { "hostPort": 80, "containerPort": 80 } ], "command": null, "environment": [], "essential": true, "entryPoint": null, "links": [], "mountPoints": [ { "containerPath": "/usr/local/apache2/htdocs", "sourceVolume": "my-vol", "readOnly": null } ], "memory": 300, "name": "simple-app", "cpu": 10, "image": "httpd:2.4" }, { "volumesFrom": [ { "readOnly": null, "sourceContainer": "simple-app" } ], "portMappings": [], "command": [ "/bin/sh -c \"while true; do echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p>' > top; /bin/date > date ; echo '</div></body></html>' > bottom; cat top date bottom > /usr/local/apache2/htdocs/index.html ; sleep 1; done\"" ], "environment": [], "essential": false, "entryPoint": [ "sh", "-c" ], "links": [], "mountPoints": [], "memory": 200, "name": "busybox", "cpu": 10, "image": "busybox" } ], "volumes": [ { "host": { "sourcePath": null }, "name": "my-vol" } ] } - 像如下一样,使用
json定义一个ECS的task

- 之后该
task会被激活

- 使用
-
接着进行
cluster定义

进行了各种设定之后,创建ECS的cluster,这个是用于执行task的执行环境
但是,这里创建了之后,还是没有执行起来task,之后会创建task和clustser的粘合剂,即service。 -
进行
service的定义- 选择
cluster之后,进入,并选择service进行创建。

- 按照执行的选项进行
service的创建

负载均衡器这里不选择创建
- 选择
-
访问
service- 访问
task,之后点击simple-app,确认网络绑定这里。

- 使用网络绑定的地址进行访问

- 访问
-
查看
EC2- 查看
EC2,确认该ECS的cluster创建的EC2实例。

- 强制删除这个
EC2实例
可以看到,强制删除了正在运行的唯一的EC2之后,过了一会,ECS会自动创建出另一个EC2。
*继续访问task的网络配置,还是能够访问该服务
但是由于没有使用ALB Application Load Balancer或者NLB Network Load Balancer,这里的IP地址变成其他的了。
总结一下,EC2的模式下,ECS上启动的image之后,是能够看到EC2 instance的,接下来的Fargate就看不到了,接下来体验下Fargate。

- 查看
3. 使用ECS的Fargate模式进行容器开发
- 使用
Fargate进行开发- 创建一个
fargate的集群
注意,这里选择AWS Fargate类型,它和EC2的区别是EC2模式,能见到EC2 instance,但是Fargate模式下,看不到EC2

- 对任务
task做出修改
默认的任务不支持Fargate,到了创建服务service的时候会报错。

- 对任务
task做出修改让其适应Fargate

- 访问
Fargate的service
- 查看
EC2
可以看到没有创建EC2,这里Fargate的模式,是不能见到外在的EC2 instance的。
- 创建一个



















