欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 文化 > aws(学习笔记第十九课) 使用ECS和Fargate进行容器开发

aws(学习笔记第十九课) 使用ECS和Fargate进行容器开发

2025/5/6 10:03:30 来源:https://blog.csdn.net/sealaugh1980/article/details/144594218  浏览:    关键词:aws(学习笔记第十九课) 使用ECS和Fargate进行容器开发

aws(学习笔记第十九课)

  • 使用ECSFargate进行容器开发

学习内容:

  • 使用本地EC2中部署docker应用
  • 使用ECSEC2模式进行容器开发
  • 使用ECSFargate模式进行容器开发

1. 使用本地EC2中部署docker应用

  1. docker整体
    在这里插入图片描述
    这里展示了docker的整体流程。

    • 开发阶段
      • 编写dockerfile,定义整个docker应用。(1. build
      • build docker image的过程中会从git repository pull业务代码(2. pull business code
      • build之后的docker image pushdocker repository(3. push
      • ec2 instance上,pull docker image from docker repository,之后执行docker image
        docker的一个目的就是Build once,Run anywhere(搭建一次,到处能用)
  2. 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!
    
  3. 启动EC2,并启动docker应用

    • 启动一个EC2实例
      在这里插入图片描述
    • 进入EC2实例,安装和启动docker
      • ssh链接实例后,开始安装docker
        sudo -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.shEXPOSE 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. 使用ECSEC2模式进行容器开发

  1. ECS的整体概念
    在这里插入图片描述

    • 首先定义task
      这个是application的核心,它定义了这个应用程序的docker image,这里docker image不太合适,应该叫ECSjson定义,也就是ECStask的静态定义。
    • 其次定义一个cluster
      有了静态定义,还需要动态定义,即执行环境。这是task的执行环境定义。
    • 最后定义service
      这里就是一个粘合剂,把task(静态定义)和cluster(动态定义)进行结合起来。
  2. 进入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定义一个ECStask
      在这里插入图片描述
    • 之后该task会被激活
      在这里插入图片描述
  3. 接着进行cluster定义
    在这里插入图片描述
    进行了各种设定之后,创建ECScluster,这个是用于执行task的执行环境
    但是,这里创建了之后,还是没有执行起来task,之后会创建taskclustser的粘合剂,即service

  4. 进行service的定义

    • 选择cluster之后,进入,并选择service进行创建。
      在这里插入图片描述
    • 按照执行的选项进行service的创建
      在这里插入图片描述
      负载均衡器这里不选择创建
  5. 访问service

    • 访问task,之后点击simple-app,确认网络绑定这里。
      在这里插入图片描述
    • 使用网络绑定的地址进行访问
      在这里插入图片描述
  6. 查看EC2

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

3. 使用ECSFargate模式进行容器开发

  1. 使用Fargate进行开发
    • 创建一个fargate的集群
      注意,这里选择AWS Fargate类型,它和EC2的区别是EC2模式,能见到EC2 instance,但是Fargate模式下,看不到EC2
      在这里插入图片描述
    • 对任务task做出修改
      默认的任务不支持Fargate,到了创建服务service的时候会报错。
      在这里插入图片描述
    • 对任务task做出修改让其适应Fargate
      在这里插入图片描述
    • 访问Fargateservice在这里插入图片描述
    • 查看EC2
      可以看到没有创建EC2,这里Fargate的模式,是不能见到外在的EC2 instance的。 在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词