5
6

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.

EKS で k8s クラスタの node にログインする

Last updated at Posted at 2020-10-26

TL;DR

https://github.com/mumoshu/kube-ssm-agent を利用する

これは何

AWS では EC2 インスタンスに SSM エージェントをインストールする事でインスタンスへの操作/管理を行う事ができる。kube-ssm-agent は ssm-agent を DaemonSet として動かすことで、ノードに aws ssm start-session できるようにする。

AWS では EC2 インスタンスに後から SSH 鍵をデプロイすることはできないが、この手法では後から ssm-agent をデプロイしてログインできるようにハックできる点が秀逸。手順は面倒だが、、、

事前準備

kube-ssm-agent の使い方自体は README にある通りなのだが、ssm-agent の事前準備がけっこう面倒

手元の AWS CLI に Session Manager プラグインを追加する

ref. https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html

$ curl "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/mac/sessionmanager-bundle.zip" -o "sessionmanager-bundle.zip"
$ unzip sessionmanager-bundle.zip
$ sudo ./sessionmanager-bundle/install -i /usr/local/sessionmanagerplugin -b /usr/local/bin/session-manager-plugin

SSM セッションの RunAs の設定

ref. https://docs.aws.amazon.com/ja_jp/systems-manager/latest/userguide/session-preferences-run-as.html

(1) デフォルト ssm-agent ユーザでセッションを作る場合

他の用途で ssm-agent をすでに使っていなければこちらがお手軽。

セッションマネージャの設定で RunAs サポートのチェックを外しておく、または外れていることを確認する。

image.png

(2) ユーザ名を指定してセッションを作りたい場合

セッションマネージャの設定で RunAs サポートのチェックを入れておき

image.png

AWS CLI で使う IAM に対して SSMSessionRunAs タグを設定すると、指定したタグ値のユーザ名でセッションが作られる

image.png

尚、うまく設定できていない aws ssm start-session で以下のようなエラーが出る

An error occurred (BadRequest) when calling the StartSession operation: 
   Invalid RunAs username. Set default username in Session Manager Preferences page.

AmazonSSMManagedInstanceCore 権限の付与

EKS ノードのインスタンスプロファイルに AmazonSSMManagedInstanceCore 権限を付与する

image.png

使い方

利用を開始する (apply)

$ git clone https://github.com/mumoshu/kube-ssm-agent.git
$ cd kube-ssm-agent
$ kubectl apply -f daemonset.yaml

注: 全ノードにデプロイされます。リソースが余っていないと立ち上がりません。

$ kubectl get nodes -o wide | grep ssm-agent
NAME              READY   STATUS    RESTARTS   AGE   IP              NODE                                               NOMINATED NODE   READINESS GATES
ssm-agent-54jr6   1/1     Running   0          64m   10.0.28.155   ip-10-0-28-155.ap-northeast-1.compute.internal   <none>           <none>
ssm-agent-nkw55   0/1     Pending   0          64m   <none>          <none>                                             <none>           <none>
ssm-agent-rwcvc   1/1     Running   0          64m   10.0.26.255   ip-10-0-26-255.ap-northeast-1.compute.internal   <none>           <none>

インスタンスIDを調べる

$ instance_name=ip-10-0-28-155.ap-northeast-1.compute.internal
$ aws ec2 describe-instances --filter "PrivateDnsName=${instance_name}" | jq -r '.Reservations[].Instances[].InstanceId'
i-0899aeb403d670999

インスタンスに接続する

$ AWS_DEFAULT_REGION=ap-northeast-1 aws ssm start-session --target i-0899aeb403d670999
Starting session with SessionId: naotoshi.seo-0ba49fc7f826af431

sh-4.2$ uname -a
Linux ip-10-0-28-155.ap-northeast-1.compute.internal 4.14.198-152.320.amzn2.x86_64 #1 SMP Wed Sep 23 23:57:28 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

利用を終了する (delete)

用事が終わったら DaemonSet を破棄する

$ kubectl delete -f daemonset.yaml
5
6
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
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?