在aws上部署一个ecs集群服务;

  • ecs 是亚马逊自研了一个容器集群管理服务;
  • 费用预算较低,选择使用了Fargate类型;
  • 把镜像发布到ecr 服务;
  • 在aws进行CI/CD,使用CodeDeploy服务;
    • CodeCommit: 代码托管服务;
    • CodeBuild: 把代码编译放入基础镜像,生成待发布的镜像;
    • CodeDeploy: 把代码部署出去;
    • CodePipeline: 流水线,把上面三个步骤串联起来,自动化发布代码到ecs上面; 本文使用了蓝绿部署方式;
  • aws 上面的中文文档看起来是用机器翻译了,非常不适合阅读,会造成理解的歧义;建议直接阅读英文的说明文档; 或者使用google搜索解决方案;
  • 使用到的服务:
    • ecs (容器服务)
    • ec2 (负载均衡)
    • ecr(镜像管理)
    • codedeploy(自动化发布)

开始进行开发:

  • 如何 使用 codedeply 来发布代码;
  • docker tag centos-6.8:latest 785247703.dkr.ecr.ap-southeast-1.amazonaws.com/centos-6.8:latest
  • $(aws ecr get-login —no-include-email —region ap-southeast-1) ;如果出错;则到keychian里面 lock login keychain ,and unlock login keychain,password is you mac password;
  • docker push 785247703.dkr.ecr.ap-southeast-1.amazonaws.com/centos-6.8:latest
  • task json 写入: /www/
  • aws ecs register-task-definition —cli-input-json file://taskdef.json
    • 碰到问题1: An error occurred (AccessDeniedException) when calling the RegisterTaskDefinition operation: User: arn:aws:iam::785247703:user/MAC-User-cli is not authorized to perform: ecs:RegisterTaskDefinition on resource: * 问题1的解决方式: 把如下这段新增策略并赋予 mac-cli-user 用户;
  1. {
  2. "Version": "2012-10-17",
  3. "Statement": [
  4. {
  5. "Effect": "Allow",
  6. "Action": [
  7. "ecs:RegisterTaskDefinition",
  8. "ecs:ListTaskDefinitions",
  9. "ecs:DescribeTaskDefinition"
  10. ],
  11. "Resource": [
  12. "*"
  13. ]
  14. }
  15. ]
  16. }
  1. * 碰到问题2 An error occurred (AccessDeniedException) when calling the RegisterTaskDefinition operation: User: arn:aws:iam::785247703:user/MAC-User-cli is not authorized to perform: iam:PassRole on resource: arn:aws:iam::785247703:role/ecsTaskExecutionRole
  2. * 问题2的解决方式:把如下这段新增给一个新的策略名称,并赋予 mac-cli-user 用户;
  1. {
  2. "Version": "2012-10-17",
  3. "Statement": [{
  4. "Effect": "Allow",
  5. "Action": [
  6. "iam:GetRole",
  7. "iam:PassRole"
  8. ],
  9. "Resource": "arn:aws:iam::<account-id>:role/ecsTaskExecutionRole" //这里要改成实际的用户的id和要赋予的权限;
  10. }]
  11. }

碰到的问题

总结:实践aws的codepipeline的流程;实现codebuild、codedeploy全自动化;