ã¨ããªãã¤è©¦ããç³»ã§ããã¾ããããã£ã±ã§ãã
tl;dr
ã®ç¶ãã
AWS CLI 㧠ALB ãä½æã㦠ECS ã® Service ãä½æã㦠ALB ã¨çµã¿åããã¦ã¿ããå°ãALB ãæä½ããã«ã¯ elbv2 ã¨ãããµãã³ãã³ããå©ç¨ããã
ã¡ã¢
ã¡ãã£ã¨ã¦ã³ãã¯
ããã¾ã§è©¦ãã¦ã㦠Amazon ECS 㨠AWS Application Load Balancer ã¯ä»¥ä¸ã®ãããªæ§æã«ãªã£ã¦ããã¨ç解ã
- Listner 㧠ALB ãã¤ã³ã¿ã¼ãããããã®æ¥ç¶ãä½çªãã¼ã㧠Listen ãããå®ç¾©ãã
- Path Pattern æ¯ã« Target Group ãå®ç¾©ãã
- Target Group ã«ã¯ EC2 ã¤ã³ã¹ã¿ã³ã¹ãã³ã³ãããã¶ãä¸ãããã¨ãåºæ¥ãï¼ããã¯ã¨ã³ãã¸ã®ã«ã¼ãã£ã³ã°ããã«ã¹ãã§ãã¯ã«ã¤ãã¦å®ç¾©ããï¼
ALB ã®è©³ç´°ã«ã¤ãã¦ã¯ä»¥ä¸ã® Blog è¨äºã¯ããã¥ã¡ã³ãã¨ä¸¦ãã§ã¨ã¦ãåèã«ãªãã
ã¾ããALB ã«ã¤ãã¦ã®å¶éäºé ã«ã¤ãã¦ã¯ä»¥ä¸ã®ããã¥ã¡ã³ãã«ã¯ç®ãéãã¦ããããã
試ããç°å¢
Amazon ECS ã AWS Application Load Balancer ãæä½ããç°å¢ã¨ãã¦ã¯...
% sw_vers
ProductName: Mac OS X
ProductVersion: 10.11.6
BuildVersion: 15G24b
% aws --version
aws-cli/1.10.56 Python/2.7.6 Darwin/15.6.0 botocore/1.4.46
ECS ã«ã¤ãã¦ã¯...
- ã¯ã©ã¹ã¿ä½ææ¸ã¿ï¼ã³ã³ããã¤ã³ã¹ã¿ã³ã¹ã¯ 2 å°ï¼
- Task Definition 㯠ecscompose-ecs-app:22 ãå©ç¨
- Container Name 㯠app ã¨ãã
- ã³ã³ããã¢ããªã±ã¼ã·ã§ã³ã¯ã³ã³ããå 㧠Port 4567 㧠Listen ãã
- Task ã¯
/hostname
ã¨ãã URL ã«ã¢ã¯ã»ã¹ããã¨ã³ã³ããã®ãã¹ãåãè¿ã
ãã£ããæµã
- ALB ãã¼ããã©ã³ãµã®ä½æ
- ALB ã¿ã¼ã²ããã°ã«ã¼ãã®ä½æ
- ALB ãªã¹ãã¼ã®ä½æ
- ALB ã¿ã¼ã²ããã°ã«ã¼ããæå®ã㦠ECS Service ãä½æ
ALB ã®ä½æ
- alb.json
% cat alb.json { "Name": "alb-demo", "Subnets": [ "subnet-12345678", "subnet-abcdefgh" ], "SecurityGroups": [ "sg-12345678" ] }
- run
% ELB_ARN=$(aws --debug \ elbv2 create-load-balancer \ --cli-input-json file://alb.json | jq -r '.LoadBalancers[].LoadBalancerArn')
- 確èª
% aws \
elbv2 describe-load-balancers
- 念ã®ããåé¤ï¼æ¶ãå¿ è¦ãç¡ããã°å©ããªãï¼
% aws \ elbv2 delete-load-balancer \ --load-balancer-arn ${ELB_ARN}
ã¿ã¼ã²ããã°ã«ã¼ãä½æ
- target-group.json
% cat target-group.json { "Name": "default", "Protocol": "HTTP", "Port": 80, "VpcId": "vpc-xxxxxxxx", "HealthCheckPath": "/hostname", "HealthCheckIntervalSeconds": 30, "HealthCheckTimeoutSeconds": 15, "HealthyThresholdCount": 3, "UnhealthyThresholdCount": 3, "Matcher": { "HttpCode": "200" } }
以ä¸ã®ããã« HealthCheckPort
ãæå®ããªãå ´åã«ã¯ traffic-port
ã¨ããå¤ãå
¥ããAmazon ECS ã®ã³ã³ãããã¶ãä¸ããå ´åã«ã¯ Docker ããæãã ããããã¼ãã«å¯¾ãããã«ã¹ãã§ãã¯ã¨ãªãã®ã§ HealthCheckPort
ã¯æå®ããªãã
% aws elbv2 describe-target-groups { "TargetGroups": [ { "HealthCheckPath": "/hostname", "HealthCheckIntervalSeconds": 30, "VpcId": "vpc-xxxxxxxxx", "Protocol": "HTTP", "HealthCheckTimeoutSeconds": 15, "HealthCheckProtocol": "HTTP", "LoadBalancerArns": [ "arn:aws:elasticloadbalancing:ap-northeast-1:1234567890123:loadbalancer/app/alb-demo/1234567890123456" ], "UnhealthyThresholdCount": 3, "HealthyThresholdCount": 3, "TargetGroupArn": "arn:aws:elasticloadbalancing:ap-northeast-1:1234567890123:targetgroup/default/1234567890123456", "Matcher": { "HttpCode": "200" }, "HealthCheckPort": "traffic-port", "Port": 80, "TargetGroupName": "default" } ] }
- run
% TARGET_GROUP=$(aws --debug \ elbv2 create-target-group \ --cli-input-json file://target-group.json | jq -r '.TargetGroups[].TargetGroupArn')
- 確èª
% echo ${TARGET_GROUP}
ãªã¹ãã¼ã®ä½æ
- run
% aws \ elbv2 create-listener \ --load-balancer-arn ${ELB_ARN} \ --protocol HTTP \ --port 80 \ --default-actions Type=forward,TargetGroupArn=${TARGET_GROUP}
ECS Service ã®ä½æ
- ecs-simple-service-elb.json
% cat ecs-simple-service-elb.json { "serviceName": "ecs-simple-service-elb", "taskDefinition": "ecscompose-ecs-app:22", "loadBalancers": [ { "targetGroupArn": "arn:aws:elasticloadbalancing:ap-northeast-1:xxxxxxxxxxxx:targetgroup/default/zzzzzzzzzzzzzzzz", "containerName": "app", "containerPort": 4567 } ], "desiredCount": 2, "role": "ecsServiceRole" }
- run
aws \ ecs create-service \ --cluster ecs-demo \ --service-name ecs-simple-service-elb \ --cli-input-json file://ecs-simple-service-elb.json
ããã¾ã§ã§...
- ALB ã¨ãªã¹ãã¼
- ã¿ã¼ã²ããã°ã«ã¼ãï¼ãã«ã¹ãã§ãã¯ãé©åã§ã¯ç¡ãã£ãã®ã§
unhealthy
âdraining
ãç¹°ãè¿ãã¦ãã... ï¼
- ECS Service
確èª
- run
% URL=$(aws \ elbv2 describe-load-balancers \ --query 'LoadBalancers[].DNSName' \ --output text) % echo ${URL} % curl ${URL} % curl ${URL}/hostname
- output
% curl ${URL}/hostname e80f74a9026f% % curl ${URL}/hostname af8647af702e% % curl ${URL}/hostname 5963b959e447%
ãã®ä»ã¡ã¢
ecs-cli 㧠ECS ã¯ã©ã¹ã¿ä½æ
--subnets
ã --azs
çã®è¤æ°æå®åºæ¥ããã©ã¡ã¼ã¿ã®å¤ã¯ã«ã³ãã§åºåããã¨ãã¹ãã¼ã¹ã§ã¯ç¡ãã®ã§æ³¨æãï¼aws cli ã¯ã¹ãã¼ã¹ã§åºåãã¨ããéãããããï¼
% ecs-cli up \ --keypair ${KEY_NAME} \ --capability-iam \ --size 1 \ --vpc vpc-xxxxxxxx \ --instance-type t2.micro \ --subnets subnet-xxxxxxxx,subnet-zzzzzzzz \ --azs ap-northeast-1a,ap-northeast-1c \ --security-group sg-xxxxxxxx
ecs-agent ãæ¢ã¾ãã¨ã©ããªãã®ã
ALB 㨠ECS ãé£æºãããç¶æ 㧠ecs-agent ãæ¢ãã¦ã¿ãã¨...
- èµ·åãã¦ãã Taskï¼ã³ã³ããï¼ã¯ç¶æããã
- ALB ã«ã¶ãä¸ãã£ã¦ãã Taskï¼ã³ã³ããï¼ã«ãæ£å¸¸ã«ã¢ã¯ã»ã¹åºæ¥ã
æ´ã«ãã®ç¶æ ã§ä¸ã¤ã® Task ã Stop Container ãã¦ã¿ãã¨...
- ALB ã®ãã«ã¹ãã§ãã¯ã失æãã¦åæ¢ããã³ã³ããã¯
unhealthy
âdraining
ã«ç¶æ ãå¤åããï¼ALB ããåãé¢ãããï¼ draining
ã«é·ç§»ããã¿ã¤ãã³ã°ã§ Running tasks count ã 1 ã¤æ¸ã
ããã§çåãecs-agent
ãåãã¦ããªãç¶æ
ã§ãã¯ã©ã¹ã¿ã®ç¶æ
ã¯ä½ããã®æ¹æ³ã§ç®¡çããã¦ããï¼