AWS: ECS пример с авторизацией в private registry

Автор: | 27/04/2017
 

Подготовка

Добавляем профайл:

$ aws configure --profile tag-ecs
AWS Access Key ID [None]: AKI***FJQ
AWS Secret Access Key [None]: 6T/a***4sG
Default region name [None]: eu-central-1
Default output format [None]: json

Создаём кластер:

[simterm]

$ aws ecs create-cluster --cluster-name tag-ecs-poc --profile tag-ecs
{
    "cluster": {
        "clusterArn": "arn:aws:ecs:eu-central-1:884660938610:cluster/tag-ecs-poc",
        "clusterName": "tag-ecs-poc",
        "status": "ACTIVE",
        "registeredContainerInstancesCount": 0,
        "runningTasksCount": 0,
        "pendingTasksCount": 0,
        "activeServicesCount": 0
    }
}

[/simterm]

Создаём tag-ecs-poc.sh – user-data файл, что бы добавить инстансы в созданный кластер:

#!/bin/bash
echo ECS_CLUSTER=tag-ecs-poc >> /etc/ecs/ecs.config

Находим AMI для ECS тут>>>.

Запускаем инстас:

[simterm]

$ aws ec2 run-instances --image-id ami-085e8a67 --key-name tag-ecs-poc --instance-type t2.medium --iam-instance-profile Name=ecsInstanceRole --user-data file://tag-ecs-poc.sh --profile tag-ecs

[/simterm]

Проверяем:

[simterm]

$ aws ecs list-container-instances --cluster tag-ecs-poc --profile tag-ecs
{
    "containerInstanceArns": [
        "arn:aws:ecs:eu-central-1:884660938610:container-instance/64a77a71-d9c4-4006-8ab3-024d7b5c04dc"
    ]
}

[/simterm]

Проверка

Сначала – запустим простой сервис WordPress, как описано тут>>>.

Создаём task definition – файл tag-ecs-poc-task-def.json:

{ 
  "containerDefinitions": [
    { 
      "name": "wordpress",
      "links": [
        "mysql"
      ],
      "image": "wordpress",
      "essential": true,
      "portMappings": [
        { 
          "containerPort": 80,
          "hostPort": 80
        }
      ],
      "memory": 500,
      "cpu": 10
    },
    { 
      "environment": [
        { 
          "name": "MYSQL_ROOT_PASSWORD",
          "value": "dbpassword"
        }
      ],
      "name": "mysql",
      "image": "mysql",
      "cpu": 10,
      "memory": 500,
      "essential": true
    }
 ],
 "family": "wordpress"
}

Регистрируем её:

[simterm]

$ aws ecs register-task-definition --cli-input-json file://tag-ecs-poc-task-def.json --profile tag-ecs

[/simterm]

Проверяем:

[simterm]

$ aws ecs list-task-definitions --profile tag-ecs
{
    "taskDefinitionArns": [
        "arn:aws:ecs:eu-central-1:884660938610:task-definition/wordpress:1"
    ]
}

[/simterm]

Создаём тестовый сервис – tag-ecs-poc-service.json:

{
   "cluster": "tag-ecs-poc",         
   "serviceName": "tag-ecs-poc-wp",         
   "taskDefinition": "wordpress:1",         
   "loadBalancers":[],         
   "desiredCount": 1
}

Запускаем его:

[simterm]

$ aws ecs create-service --cli-input-json file://tag-ecs-poc-service.json --profile tag-ecs

[/simterm]

Проверяем:

[simterm]

$ aws ecs list-services --cluster tag-ecs-poc --profile tag-ecs
{
    "serviceArns": [
        "arn:aws:ecs:eu-central-1:884660938610:service/tag-ecs-poc-wp"
    ]
}

[/simterm]

Можно перейти по URL EC2-интанса, проверить.

И проверяем сам Docker:

[simterm]

$ ssh [email protected] -i tag-ecs-poc.pem
$ docker ps
CONTAINER ID        IMAGE                            COMMAND                  CREATED             STATUS              PORTS                NAMES
7819935641b5        wordpress                        "docker-entrypoint.sh"   6 minutes ago       Up 6 minutes        0.0.0.0:80->80/tcp   ecs-wordpress-1-wordpress-a8beafedd3ffb4b35800
c3285e2b3912        mysql                            "docker-entrypoint.sh"   6 minutes ago       Up 6 minutes        3306/tcp             ecs-wordpress-1-mysql-a6b4c59da8b8c4ef6a00
150957c398be        amazon/amazon-ecs-agent:latest   "/agent"                 22 minutes ago      Up 22 minutes                            ecs-agent

[/simterm]

ОК, работает – убиваем этот сервис.

Уменьшаем кол-во выполняемых им задач до нуля:

[simterm]

$ aws ecs update-service --service tag-ecs-poc-wp --desired-count 0 --cluster tag-ecs-poc --profile tag-ecs

[/simterm]

Удаляем сервис:

[simterm]

$ aws ecs delete-service --service tag-ecs-poc-wp --cluster tag-ecs-poc --profile tag-ecs

[/simterm]

Авторизация в private registry

В роли репозитория у нас используется JFrog Artifactory.

Документация ECS по авторизации – тут>>>.

Обновляем user-data, в файл tag-ecs-poc.sh добавляем авторизацию по dockercfg:

#!/bin/bash
echo ECS_CLUSTER=tag-ecs-poc >> /etc/ecs/ecs.config
echo ECS_ENGINE_AUTH_TYPE=dockercfg >> /etc/ecs/ecs.config
echo "ECS_ENGINE_AUTH_DATA={\"https://engineering-docker.jfrog.io/v1/\":{\"auth\":\"c3Z***FND\",\"email\":\"[email protected]\"}}" >> /etc/ecs/ecs.config

Дропаем первый тестовый инстанс:

[simterm]

$ aws ec2 terminate-instances --instance-ids i-092bb29b7cacdca5d --profile tag-ecs

[/simterm]

Запускаем инстанс:

[simterm]

$ aws ec2 run-instances --image-id ami-085e8a67 --key-name tag-ecs-poc --instance-type t2.medium --iam-instance-profile Name=ecsInstanceRole --user-data file://tag-ecs-poc.sh --profile tag-ecs

[/simterm]

Удаляем тестовый task defenition (можно не удалять, а зарегистрировать с тем же именм, получить новую ревизию):

[simterm]

$ aws ecs deregister-task-definition --task-definition arn:aws:ecs:eu-central-1:884660938610:task-definition/wordpress:1 --profile tag-ec

[/simterm]

Обновляем task defenition в tag-ecs-poc-task-def.json, добавляем уже актуальные образы:

{ 
  "containerDefinitions": [
    { 
      "name": "tag-api-gateway",
      "links": [
        "tag-producers"
      ],
      "image": "engineering-docker.jfrog.io/api-gateway:production-rc_2017april-24",
      "essential": true,
      "portMappings": [
        { 
          "containerPort": 8080,
          "hostPort": 80
        }
      ],
      "memory": 500,
      "cpu": 10
    },
    { 
      "name": "tag-producers",
      "image": "engineering-docker.jfrog.io/producers:production-rc_2017april-24",
      "cpu": 10,
      "memory": 500,
      "essential": true
    }
 ],
 "family": "tag-api"
}

Регистрируем её заново:

[simterm]

$ aws ecs register-task-definition --cli-input-json file://tag-ecs-poc-task-def.json --profile tag-ecs

[/simterm]

Проверяем:

[simterm]

$ aws ecs list-task-definitions --profile tag-ecs

{
    "taskDefinitionArns": [
        "arn:aws:ecs:eu-central-1:884660938610:task-definition/tag-api:1"
    ]
}

[/simterm]

В файле tag-ecs-poc-service.json обновляем сервис:

{
   "cluster": "tag-ecs-poc",         
   "serviceName": "tag-api-ecs-poc",       
   "taskDefinition": "tag-api:1", 
   "loadBalancers":[],         
   "desiredCount": 1
}

Создаём его:

[simterm]

$ aws ecs create-service --cli-input-json file://tag-ecs-poc-service.json --profile tag-ecs

[/simterm]

Проверяем:

[simterm]

$ aws ecs list-services --cluster tag-ecs-poc --profile tag-ecs
{
    "serviceArns": [
        "arn:aws:ecs:eu-central-1:884660938610:service/tag-api-ecs-poc"
    ]
}

[/simterm]

И смотрим на самом EC2-инстансе:

[simterm]

[ec2-user@ip-172-31-23-159 ~]$ docker ps 
CONTAINER ID        IMAGE                                                                                 COMMAND                  CREATED             STATUS              PORTS                  NAMES
d842604ed55a        engineering-docker.jfrog.io/api-gateway:production-rc_2017april-24   "java -Djava.security"   12 seconds ago      Up 11 seconds       0.0.0.0:80->8080/tcp   ecs-tag-api-1-tag-api-gateway-8ae5f8f0ddcbbfc02200
b01360b079b1        engineering-docker.jfrog.io/producers:production-rc_2017april-24     "java -Djava.security"   14 seconds ago      Up 12 seconds                              ecs-tag-api-1-tag-producers-d2bcd09dec99a4a6f301

[/simterm]

Готово.