AWSでオンプレのコンテナも管理できる「ECS Anywhere」を使ってみた

記事タイトルとURLをコピーする

こんにちは。AWS CLIが好きな福島です。

今回は、ECS AnywhereがGAされたので、使ってみました。

Amazon Elastic Container Service Anywhere が利用可能に

参考

Amazon ECS Anywhere の開始方法 – 一般提供の開始 | Amazon Web Services ブログ

Amazon VPC と接続可能なおうち Amazon ECS Anywhere クラスターの構築 | Amazon Web Services ブログ

ECS Anywhereとは

AWS ECSでの一機能で、オンプレのサーバや仮想マシンなどの外部のインスタンスをECSで管理できます!

アーキテクチャ

どうやって外部のインスタンスのコンテナをECSで管理しているか、気になりますよね。

結論を言うと、
・amazon-ecs-agent
・amazon-ssm-agent
を使っています。

https://d2908q01vomqb2.cloudfront.net/b3f0c7f6bb763af1be91d9e74eabfeb199dc1f1f/2021/05/28/ecs-managed-instance-1024x489-1.png

引用:Amazon VPC と接続可能なおうち Amazon ECS Anywhere クラスターの構築 | Amazon Web Services ブログ

そのため、外部のインスタンスは、インターネットにアクセスできる必要があります。

考慮事項

①外部インスタンスには、クレデンシャル情報が埋め込まれます。($HOME/.aws/credentials)
②ECS Anywhereは、ELBに対応していません。
③ネットワークモードは、awsvpcに対応していません。
④ECS Exec はサポートされていません。
⑤x86_64 と ARM64 の両方の CPU アーキテクチャがサポートされています。サポートされているOSは、以下の通りです。

  • CentOS 7、CentOS 8
  • RHEL 7
  • Fedora 32、Fedora 33
  • openSUSE Tumbleweed
  • Ubuntu 18、Ubuntu 20
  • Debian 9、Debian 10
  • SUSE Enterprise Server 15

その他の考慮事項
What is Amazon Elastic Container Service? - Amazon Elastic Container Service

使ってみた

今回は、外部のインスタンスとして、VirtualBoxを使い、Windows10上にCentOSを導入した仮想サーバを使いました。
※VirtualBoxの導入からCentOSの導入については、記載していないため、ご了承ください。

◆流れ
1. 空のクラスタの作成
2. Externalインスタンスの登録
3. External用のタスク定義作成
4. タスクの実行

1.空のクラスタの作成

1-1.「クラスターの作成」をクリックします。

f:id:swx-fukushima:20210605055656p:plain

1-2.「ネットワーキングのみ」の選択し、「次のステップ」をクリックします。

f:id:swx-fukushima:20210605051756p:plain

1-3.「クラスタ名」を任意の名前で設定し、「作成」をクリックします。

f:id:swx-fukushima:20210605052017p:plain

2. Externalインスタンスの登録

2-1. 「Externalインスタンスの登録」をクリックします。

f:id:swx-fukushima:20210605052350p:plain

2-2. デフォルトのまま、「次のステップ」をクリックします。

アクティベーションキーの有効期間は、1-1000の間で設定できます。

f:id:swx-fukushima:20210605053201p:plain

2-3. コマンドが生成されるので、コピーします。

f:id:swx-fukushima:20210605150200p:plain

2-4. CentOSでコマンドを実行します。

# curl --proto "https" -o "/tmp/ecs-anywhere-install.sh" "https://amazon-ecs-agent.s3.amazonaws.com/ecs-anywhere-install-latest.sh" && bash /tmp/ecs-anywhere-install.sh --region "ap-northeast-1" --cluster "fk-test-ecs-anywhere" --activation-id "xxxxxxxxxxxxxxxxxxxxx" --activation-code "xxxxxxxxxxxxx"

実行結果は、長いので、本ブログの最下行に記載いたしました。

2-5. コマンドが正常に完了すると、ECSのインスタンスとして認識されます。

f:id:swx-fukushima:20210605082629p:plain

Systems Managerのマネージドインスタンスとしても認識されていることが分かります。

f:id:swx-fukushima:20210605082848p:plain

3. External用のタスク定義作成

3-1. 「新しいタスク定義の作成」をクリックします。

f:id:swx-fukushima:20210605083457p:plain

3-2. EXTERNALを選択します。

f:id:swx-fukushima:20210605083401p:plain

3-3. 「タスク定義名」に任意の名前を入力します。

f:id:swx-fukushima:20210605085211p:plain

3-4. 「コンテナの追加」をクリックします。

f:id:swx-fukushima:20210605085307p:plain

3-5. 「コンテナの追加」設定を行い、「追加」をクリックします。

・コンテナ名:nginx
・イメージ:public.ecr.aws/nginx/nginx:latest
・メモリ制限:256
・ホストポート:8080
・コンテナポート:80

f:id:swx-fukushima:20210605085416p:plain

3-6. 「作成」をクリックします。

f:id:swx-fukushima:20210605085604p:plain

4. タスクの実行

4-1. 「タスクの実行」をクリックします。

f:id:swx-fukushima:20210605085717p:plain

4-2. 起動タイプ、クラスターを設定します。

f:id:swx-fukushima:20210605085815p:plain

4-3. 「タスクの実行」をクリックします。

f:id:swx-fukushima:20210605085931p:plain

4-4. しばらくするとCentos上にコンテナがデプロイされました。

f:id:swx-fukushima:20210605090041p:plain

curlコマンドでnginxにアクセスできることも確認できます!

[root@fk-test-external ~]# curl http://localhost:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@fk-test-external ~]#

終わりに

外部インスタンスへのSSMやECSエージェントの導入は、コマンド一発で実行できるので、簡単ですね~!

どなたかのお役に立てれば幸いです。

2-4の実行結果

root@192.168.3.4's password:
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 19369  100 19369    0     0  16708      0  0:00:01  0:00:01 --:--:-- 16697
Running ECS install script on centos 7
###

centos
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * epel: ftp.iij.ad.jp
 * extras: ftp.iij.ad.jp
 * updates: ftp.iij.ad.jp
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be updated
---> Package epel-release.noarch 0:7-13 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                Arch             Version           Repository      Size
================================================================================
Updating:
 epel-release           noarch           7-13              epel            15 k

Transaction Summary
================================================================================
Upgrade  1 Package

Total download size: 15 k
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : epel-release-7-13.noarch                                     1/2
  Cleanup    : epel-release-7-11.noarch                                     2/2
  Verifying  : epel-release-7-13.noarch                                     1/2
  Verifying  : epel-release-7-11.noarch                                     2/2

Updated:
  epel-release.noarch 0:7-13

Complete!
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * epel: ftp.iij.ad.jp
 * extras: ftp.iij.ad.jp
 * updates: ftp.iij.ad.jp
Package jq-1.6-2.el7.x86_64 already installed and latest version
Nothing to do

##########################
# Trying to install ssm agent ...


##########################
# Trying to verify the signature of amazon-ssm-agent package ...

/usr/bin/gpg
8108A07A9EBE248E3F1C63F254F4F56E693ECA21
gpg: key 693ECA21: "SSM Agent <ssm-agent-signer@amazon.com>" not changed
gpg: Total number processed: 1
gpg:              unchanged: 1
gpg: Signature made Tue May 18 03:08:31 2021 JST using RSA key ID 693ECA21
gpg: Good signature from "SSM Agent <ssm-agent-signer@amazon.com>"
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: 8108 A07A 9EBE 248E 3F1C  63F2 54F4 F56E 693E CA21
amazon-ssm-agent GPG verification passed. Install the amazon-ssm-agent.

# ok
##########################

Loaded plugins: fastestmirror
Failed to set locale, defaulting to C
Examining /tmp/tmp.GsCxM3ZCjc/ssm-agent.rpm: amazon-ssm-agent-3.0.1209.0-1.x86_64
Marking /tmp/tmp.GsCxM3ZCjc/ssm-agent.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package amazon-ssm-agent.x86_64 0:3.0.1209.0-1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                Arch         Version             Repository        Size
================================================================================
Installing:
 amazon-ssm-agent       x86_64       3.0.1209.0-1        /ssm-agent       108 M

Transaction Summary
================================================================================
Install  1 Package

Total size: 108 M
Installed size: 108 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : amazon-ssm-agent-3.0.1209.0-1.x86_64                         1/1
Created symlink from /etc/systemd/system/multi-user.target.wants/amazon-ssm-agent.service to /etc/systemd/system/amazon-ssm-agent.service.
  Verifying  : amazon-ssm-agent-3.0.1209.0-1.x86_64                         1/1

Installed:
  amazon-ssm-agent.x86_64 0:3.0.1209.0-1

Complete!

##########################
# Trying to Register SSM agent ...

SSM agent is already registered. Managed instance ID: mi-06a75ebca7ffac7c4

# ok
##########################


# ok
##########################

docker is already installed, skipping installation

##########################
# Trying to install ecs agent ...


##########################
# Trying to verify the signature of amazon-ecs-init package ...

/usr/bin/gpg
WARNING: dirmngr not installed on this server, not able to verify amazon-ecs-init package signature.

# ok
##########################

Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
Examining /tmp/tmp.MpMyuXP2La/amazon-ecs-init-latest.x86_64.rpm: amazon-ecs-init-1.52.2-2.x86_64
Marking /tmp/tmp.MpMyuXP2La/amazon-ecs-init-latest.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package amazon-ecs-init.x86_64 0:1.52.2-2 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package           Arch     Version      Repository                        Size
================================================================================
Installing:
 amazon-ecs-init   x86_64   1.52.2-2     /amazon-ecs-init-latest.x86_64    69 M

Transaction Summary
================================================================================
Install  1 Package

Total size: 69 M
Installed size: 69 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : amazon-ecs-init-1.52.2-2.x86_64                              1/1
  Verifying  : amazon-ecs-init-1.52.2-2.x86_64                              1/1

Installed:
  amazon-ecs-init.x86_64 0:1.52.2-2

Complete!
/etc/ecs/ecs.config already exists, preserving existing config and appending cluster name.
/var/lib/ecs/ecs.config already exists, preserving existing config and appending ECS anywhere requirements.
Created symlink from /etc/systemd/system/multi-user.target.wants/ecs.service to /usr/lib/systemd/system/ecs.service.

# ok
##########################


##########################
# Trying to wait for ECS agent to start ...

Ping ECS Agent registered successfully! Container instance arn: "arn:aws:ecs:ap-northeast-1:xxxxxxxxxxx:container-instance/fk-test-ecs-anywhere/a1cccb13f16d475f8b0cb67fd3069553"

You can check your ECS cluster here https://console.aws.amazon.com/ecs/home?region=ap-northeast-1#/clusters/fk-test-ecs-anywhere

# ok
##########################


##########################
This script installed three open source packages that all use Apache License 2.0.
You can view their license information here:
  - ECS Agent https://github.com/aws/amazon-ecs-agent/blob/master/LICENSE
  - SSM Agent https://github.com/aws/amazon-ssm-agent/blob/master/LICENSE
  - Docker engine https://github.com/moby/moby/blob/master/LICENSE
##########################

福島 和弥 (記事一覧)

2019/10 入社

AWS CLIが好きです。

AWS資格12冠。2023 Japan AWS Partner Ambassador/APN ALL AWS Certifications Engineer。