最近在工作上在研究 AWS ECS (Amazon Elastic Container Service) 其實是在為 EKS 做準備,
因為其實 AWS 的服務很多都是舊服務連接的因此先研究舊服務也不錯 ~
在使用的過程中,
遇到一個問題就是 ECS 會自動開 ECS Instances 但是他們有機會要用到共用的磁碟,
因此可以使用的解法是 AWS EFS (Amazon Elastic File System),
但是遇到一個問題是架設 Cluster ECS 的時候沒有加入 EFS 的選項,
因此做了很多的研究發現,
要做到 ECS Instances 與 EFS 連線官方推薦的方法是 EC2 的 User data,
而 User data 要去哪裡找呢?
其實在建立 Cluster ECS 的時候會自動在 EC2 的 AUTO SCALING -> Auto Scaling Groups 多一個選項,
預設名稱應該會是 EC2ContainerService-ECS 開頭的,
可以在 Launch Configurations 設定裡面更新 User data,
官方給的 Example 如下:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Content-Type: multipart/mixed; boundary="==BOUNDARY==" | |
| MIME-Version: 1.0 | |
| --==BOUNDARY== | |
| Content-Type: text/cloud-boothook; charset="us-ascii" | |
| # Install nfs-utils | |
| cloud-init-per once yum_update yum update -y | |
| cloud-init-per once install_nfs_utils yum install -y nfs-utils | |
| # Create /efs folder | |
| cloud-init-per once mkdir_efs mkdir /efs | |
| # Mount /efs | |
| cloud-init-per once mount_efs echo -e 'fs-abcd1234.efs.us-east-1.amazonaws.com:/ /efs nfs4 nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 0 0' >> /etc/fstab | |
| mount -a | |
| --==BOUNDARY== | |
| Content-Type: text/x-shellscript; charset="us-ascii" | |
| #!/bin/bash | |
| # Set any ECS agent configuration options | |
| echo "ECS_CLUSTER=default" >> /etc/ecs/ecs.config | |
| --==BOUNDARY==-- |
主要需要注意的地方是 fs-abcd1234.efs.us-east-1.amazonaws.com 這個位置要填 EFS 網頁上給的,
與 ECS_CLUSTER 為目前 ECS cluster 的 ID
只要做到這兩項就可以完成摟 ~
在下次會介紹配合 S3 下載檔案執行 Shell 的方法。