iwaseasahi / christchurches-map

キリスト教会マップ
https://www.christchurches-map.com/
2 stars 0 forks source link

ECS を設定する #782

Closed iwaseasahi closed 3 years ago

iwaseasahi commented 3 years ago

背景

https://github.com/iwaseasahi/christchurches-map/issues/775 https://github.com/iwaseasahi/christchurches-map/issues/781

上記 issue で ECR を作成しました。 ECS を設定していきます。

目的

ECS 化

完成条件

iwaseasahi commented 3 years ago

https://aws.amazon.com/jp/fargate/?whats-new-cards.sort-by=item.additionalFields.postDateTime&whats-new-cards.sort-order=desc&fargate-blogs.sort-by=item.additionalFields.createdDate&fargate-blogs.sort-order=desc

iwaseasahi commented 3 years ago

https://docs.aws.amazon.com/ja_jp/AmazonECS/latest/userguide/what-is-fargate.html

iwaseasahi commented 3 years ago

新しい IAM を作成しました。 https://console.aws.amazon.com/iamv2/home#/users

iwaseasahi commented 3 years ago

Amazon-ECS

Amazon-ECS-起動ステータス

iwaseasahi commented 3 years ago

クラスタはサービスや起動しているタスクを総合して束ねているもの

iwaseasahi commented 3 years ago

Amazon ECS クラスター https://docs.aws.amazon.com/ja_jp/AmazonECS/latest/developerguide/clusters.html

Amazon ECS クラスターは、タスクまたはサービスの論理グループです。タスクとサービスは、クラスターに登録されているインフラストラクチャで実行されます。

iwaseasahi commented 3 years ago

Amazon ECS タスク定義 https://docs.aws.amazon.com/ja_jp/AmazonECS/latest/developerguide/task_definitions.html

Amazon ECS で Docker コンテナを実行するには、タスク定義が必要です。

iwaseasahi commented 3 years ago

複数のコンテナをグループにしたものをタスクとして定義します。タスク単位でCPUやメモリの割り当てを行います。

https://dev.classmethod.jp/articles/amazon-ecs-entrance-1/

iwaseasahi commented 3 years ago

Amazon ECS 細かい箇所を整理してみた https://qiita.com/leomaro7/items/6afd607f2df39f714a8e

iwaseasahi commented 3 years ago

pending と stop を繰り返すばかりで立ち上がらないです。 https://ap-northeast-1.console.aws.amazon.com/ecs/home?region=ap-northeast-1#/clusters/christchurches-map-cluster/tasks

iwaseasahi commented 3 years ago

いきなり ECS 化は難しいので、まずは rails new した app を ECS 化してみます。

iwaseasahi commented 3 years ago

先に作りたいものの構成図を作るのが良さそうです。

iwaseasahi commented 3 years ago

ecs は environment を staging にします。

iwaseasahi commented 3 years ago

ecs は puma で動かします。 以下のように path を指定して、puma を動かせます。 https://github.com/puma/puma#configuration-file

config/puma/<environment_name>.rb で作成すると、RAILS_ENV から自動的に環境に合った設定を使用するとのこと。

iwaseasahi commented 3 years ago

puma の起動が成功しました。

$ docker-compose run --rm web puma --environment staging
[+] Running 4/0
 ⠿ Container christchurches-map_chrome_1  Running                                                                                                                                                                                                                         0.0s
 ⠿ Container christchurches-map_db_1      Running                                                                                                                                                                                                                         0.0s
 ⠿ Container christchurches-map_redis_1   Running                                                                                                                                                                                                                         0.0s
 ⠿ Container christchurches-map_worker_1  Running                                                                                                                                                                                                                         0.0s
Puma starting in single mode...
* Puma version: 5.4.0 (ruby 3.0.0-p0) ("Super Flight")
*  Min threads: 5
*  Max threads: 5
*  Environment: staging
*          PID: 1
* Listening on unix:///usr/src/app/tmp/sockets/puma.sock
Use Ctrl-C to stop
iwaseasahi commented 3 years ago

rubymine で docker を動かす方法 https://pleiades.io/help/ruby/docker.html

iwaseasahi commented 3 years ago

session_store https://railsguides.jp/configuring.html

iwaseasahi commented 3 years ago

現状 session は cookie_store を利用しています。 なので、ECS でも cookie_store を利用します。 memcached は使用しません。

iwaseasahi commented 3 years ago

RDS を作成しました。 https://ap-northeast-1.console.aws.amazon.com/rds/home?region=ap-northeast-1#database:id=christchurches-map-ecs;is-cluster=false

iwaseasahi commented 3 years ago

elasticache redis を作成しました。 https://ap-northeast-1.console.aws.amazon.com/elasticache/home?region=ap-northeast-1#redis-group-nodes:id=christchurches-map-ecs-redis;clusters

iwaseasahi commented 3 years ago

以下のコマンドで rails の image を作成します。

$ docker build -t christchurches-map-rails -f docker/staging/rails/Dockerfile .
iwaseasahi commented 3 years ago

以下のコマンドで nginx の image を作成します。

$ docker build -t christchurches-map-nginx -f docker/staging/nginx/Dockerfile .
iwaseasahi commented 3 years ago

ローカルでアクセスできましたが、やはり ECS ではアクセスできませんでした。

Amazon ECS Exec を使って、log を確認する必要があります。 https://aws.amazon.com/jp/blogs/news/new-using-amazon-ecs-exec-access-your-containers-fargate-ec2/

iwaseasahi commented 3 years ago

こちらの手順に沿って、Amazon ECS Exec をやってみます。 https://docs.aws.amazon.com/ja_jp/AmazonECS/latest/userguide/ecs-exec.html

iwaseasahi commented 3 years ago

以下のコマンドを実行して、 "enableExecuteCommand": true になりました。

$ aws ecs update-service --cluster christchurches-map-cluster --service christchurches-map-service --enable-execute-command
iwaseasahi commented 3 years ago

以下のコマンドを実行して、コンテナに対して ExecuteCommand が使えることを確認しました。

$ aws ecs describe-tasks --cluster christchurches-map-cluster --tasks e223a78490a4422fa3a41f6e527485e1
iwaseasahi commented 3 years ago

https://zoo200.net/ecs-fargate-execute-command/

iwaseasahi commented 3 years ago

以下のコマンドで execute-command が実行できました。

$ aws ecs execute-command --cluster christchurches-map-cluster --task 5666e64175274088ae25602353c1352f --container christchurches-map-rails --interactive --command "/bin/sh"
iwaseasahi commented 3 years ago

ECS にして、一通り機能は使えるようになりました。 https://www.christchurches-map.site/