iwaseasahi / christchurches-map

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

素の rails を ECS 化する #784

Closed iwaseasahi closed 3 years ago

iwaseasahi commented 3 years ago

背景

https://github.com/iwaseasahi/christchurches-map/issues/782

上記 issue で ECS 化にチャレンジしましたが、全くわからない状態で ECS にするのは難易度が高いので、まずは素の rails を ECS 化してきます。

目的

ECS を理解する

完成条件

rails new したばかりの rails を ECS で動かすこと

iwaseasahi commented 3 years ago

参考

iwaseasahi commented 3 years ago

docker image を作成します。

$ docker build -t ecs-rails-sample -f docker/rails/Dockerfile .
iwaseasahi commented 3 years ago

https://ap-northeast-1.console.aws.amazon.com/ecr/repositories/private/205997696747/ecs-rails-sample?region=ap-northeast-1

iwaseasahi commented 3 years ago

ECS でタスクを実行しました。 https://ap-northeast-1.console.aws.amazon.com/ecs/home?region=ap-northeast-1#/clusters/ecs-rails-sample-cluster/tasks/1a0a1b1e8cae49e2a09e7808ca1a839c/details

http://18.183.107.155:3000/

上記の URL でアクセルできると予想していたのですが、できず。

iwaseasahi commented 3 years ago

cloudwatch を見る限り、puma は起動できています。 https://ap-northeast-1.console.aws.amazon.com/cloudwatch/home?region=ap-northeast-1#logsV2:log-groups/log-group/$252Fecs$252Fecs-rails-sample-task/log-events/ecs$252Fecs-rails-sample$252F1a0a1b1e8cae49e2a09e7808ca1a839c

スクリーンショット 2021-09-07 22 44 18
iwaseasahi commented 3 years ago

-b 0.0.0.0というオプションをつけると、仮想マシンが持っている全てのネットワークインターフェースにバインディングされるため、仮想環境外部からアクセスできる。

https://qiita.com/Masato338/items/f162394fbc37fc490dfb

ということなので、http://18.183.107.155:3000/ でアクセスできるはず。

iwaseasahi commented 3 years ago

セキュリティグループのインバウンドで 3000 port を開けたらアクセスできました。

https://ap-northeast-1.console.aws.amazon.com/ec2/v2/home?region=ap-northeast-1#SecurityGroup:group-id=sg-0cb02bbf5b2d2495b

スクリーンショット 2021-09-07 23 06 57

http://18.183.107.155:3000/

スクリーンショット 2021-09-07 23 07 50
iwaseasahi commented 3 years ago

さらに nginx を追加していきます。

iwaseasahi commented 3 years ago

https://qiita.com/polar_bear_tech/items/87f5795346d246a1e07c

iwaseasahi commented 3 years ago

https://qiita.com/eighty8/items/0288ab9c127ddb683315

iwaseasahi commented 3 years ago

これが /etc/nginx/nginx.conf の中身です。

nginx_1  | 
nginx_1  | user  nginx;
nginx_1  | worker_processes  auto;
nginx_1  | 
nginx_1  | error_log  /var/log/nginx/error.log notice;
nginx_1  | pid        /var/run/nginx.pid;
nginx_1  | 
nginx_1  | 
nginx_1  | events {
nginx_1  |     worker_connections  1024;
nginx_1  | }
nginx_1  | 
nginx_1  | 
nginx_1  | http {
nginx_1  |     include       /etc/nginx/mime.types;
nginx_1  |     default_type  application/octet-stream;
nginx_1  | 
nginx_1  |     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
nginx_1  |                       '$status $body_bytes_sent "$http_referer" '
nginx_1  |                       '"$http_user_agent" "$http_x_forwarded_for"';
nginx_1  | 
nginx_1  |     access_log  /var/log/nginx/access.log  main;
nginx_1  | 
nginx_1  |     sendfile        on;
nginx_1  |     #tcp_nopush     on;
nginx_1  | 
nginx_1  |     keepalive_timeout  65;
nginx_1  | 
nginx_1  |     #gzip  on;
nginx_1  | 
nginx_1  |     include /etc/nginx/conf.d/*.conf;
nginx_1  | }
iwaseasahi commented 3 years ago

docker-compose の volumes について https://www.ogis-ri.co.jp/otc/hiroba/technical/docker/part4.html

iwaseasahi commented 3 years ago

Unixドメインソケット https://ascii.jp/elem/000/001/415/1415088/

iwaseasahi commented 3 years ago

Use volumes https://docs.docker.com/storage/volumes/

iwaseasahi commented 3 years ago
$ docker build -t ecs-rails-sample-nginx -f docker/nginx/Dockerfile .
iwaseasahi commented 3 years ago

ECS に deploy してみましたが、以下のエラーになりました。 https://ap-northeast-1.console.aws.amazon.com/cloudwatch/home?region=ap-northeast-1#logsV2:log-groups/log-group/$252Fecs$252Fecs-rails-sample-task/log-events/ecs$252Fecs-rails-sample-nginx$252F1ab2e7f7f4c548e1a990a66cdbb0c460

おそらくローカルでも発生した volumes の問題だと思われます。

iwaseasahi commented 3 years ago

https://docs.aws.amazon.com/ja_jp/AmazonECS/latest/developerguide/using_data_volumes.html

iwaseasahi commented 3 years ago

http://35.76.111.26/

Ruby-on-Rails

iwaseasahi commented 3 years ago

ちゃんと 3000 port も閉じています。 http://35.76.111.26:3000/

iwaseasahi commented 3 years ago

500 エラーも表示できました。 http://35.76.111.26/500.html

iwaseasahi commented 3 years ago

無事に素の rails を ECS 化できたので close します。