0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AWSのECSでコンテナにWEBターミナル接続

Posted at

ECSを使用するとデバッグ環境でコンテナに接続するのが面倒なので、コンテナにWebターミナルであるGoTTYを入れてみる方法を考えてみました。
あくまでもデバッグ目的で使用します。

自分のアプリのDockerファイルにgottyを設定

Dockerfileのgotty設定部分 (alpineを使用しています)

# GoTTY
RUN addgroup -S gotty && adduser -S -g gotty gotty \
    && mkdir -p /gotty \
    && chown -R gotty /gotty && chgrp -R gotty /gotty
RUN wget -qO- https://github.com/yudai/gotty/releases/download/v0.0.12/gotty_linux_amd64.tar.gz | tar zx -C /gotty/
RUN echo 'port = "8080"' >> /home/gotty/.gotty
RUN echo 'credential = "myacc:pass"' >> /home/gotty/.gotty
RUN chown -R gotty /gotty/gotty /home/gotty

# Supervisor
RUN echo '[supervisord]' >> /etc/supervisord.conf
RUN echo 'nodaemon=true' >> /etc/supervisord.conf
RUN echo '[program:myapp]' >> /etc/supervisord.conf
RUN echo 'command=/myworks/myapp'   >> /etc/supervisord.conf
RUN echo '[program:gotty]' >> /etc/supervisord.conf
RUN echo 'command=/gotty/gotty --config "/home/gotty/.gotty" -w /bin/ash' >> /etc/supervisord.conf
RUN echo 'autostart=true' >> /etc/supervisord.conf
# RUN echo 'user=gotty' >> /etc/supervisord.conf

EXPOSE 80 8080

CMD ["/usr/bin/supervisord", "-c", "/etc/supervisord.conf"]

portでターミナルのポート
credentialでベーシック認証のアカウントとパスワード
ただ確認したいだけであれば"user=gotty"の部分のコメントを外してください。

ECSのタスク定義

コンテナから自分のアプリの80とターミナルの8080を通したいのですが、私が使用していたecs-cliのバージョンではマルチポートの設定がうまくできなかったのでawsクライアントでタスク定義からやります。
targetGroupArnは自分のターゲットグループARNを入れてください。

  • ポート定義ファイル"multiport.json"を作成
{
"loadBalancers": [
{
"containerName": "myapp",
"containerPort": 80,
"targetGroupArn": "arn:aws:elasticloadbalancing:ap-northeast-1:*********:targetgroup/mycluster/*****"
},
{
"containerName": "myapp",
"containerPort": 8080,
"targetGroupArn": "arn:aws:elasticloadbalancing:ap-northeast-1:*********:targetgroup/mycluster/****"
}
]
}
  • タスク定義ファイル"task-definition.json"を作成
{
    "memory": "512", 
    "networkMode": "bridge", 
    "family": "mytask", 
    "placementConstraints": [], 
    "cpu": "256", 
    "executionRoleArn": "arn:aws:iam:*************", 
    "volumes": [], 
    "requiresCompatibilities": [
        "EC2"
    ], 
    "taskRoleArn": "arn:aws:iam::*************", 
    "containerDefinitions": [
        {
            "links": [], 
            "image": "********.dkr.ecr.ap-northeast-1.amazonaws.com/myapp/develop:1.0.0", 
            "hostname": "myhost", 
            "environment": [
            ], 
            "extraHosts": [], 
            "dnsServers": [], 
            "volumesFrom": [], 
            "dnsSearchDomains": [], 
            "mountPoints": [], 
            "logConfiguration": {
                "logDriver": "awslogs", 
                "options": {
                    "awslogs-region": "ap-northeast-1", 
                    "awslogs-stream-prefix": "docker", 
                    "awslogs-group": "/myapp/group"
                }
            }, 
            "dockerSecurityOptions": [], 
            "entryPoint": [], 
            "linuxParameters": {
                "devices": [], 
                "capabilities": {}
            }, 
            "essential": true, 
            "readonlyRootFilesystem": false, 
            "name": "myapp", 
            "dockerLabels": {}, 
            "privileged": false, 
            "portMappings": [
                {
                    "protocol": "tcp", 
                    "containerPort": 80, 
                    "hostPort": 0
                }, 
                {
                    "protocol": "tcp", 
                    "containerPort": 8080, 
                    "hostPort": 8080
                } 
            ], 
            "command": [], 
            "cpu": 0, 
            "ulimits": []
        }
    ] 
}

タスクの定義とECSサービス起動

revision=`aws ecs register-task-definition --cli-input-json file://task-definition.json | jq '.taskDefinition.revision'`
echo "revision:${revision}"
aws ecs create-service --cluster ${CLUSTER_NAME} --service ${SERVICE_NAME} --task-definition mytask:${revision} --deployment-configuration maximumPercent=${MAX_PARCENT},minimumHealthyPercent=${MIN_PARCENT} --desired-count 1 --launch-type EC2 --cli-input-json file://multiport.json

ブラウザでアクセス

指定した8080ポートにブラウザでアクセスするとターミナルが立ち上がります。

gotty.png

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?