のべラボ.blog

Tech Blog | AWS や サーバーレスやコンテナ などなど

Amazon Linux 2 に rbac-lookup をインストールする

Amazon Linux 2 に rbac-lookup をインストールする時に、やや試行錯誤が必要だったので正しくインストールする手順をメモしておきます。

rbac-lookupは、Kubernetes 環境の ロールやクラスターロールとバインドしているユーザーやサービスアカウントの情報をシンプルに表示してくれるコマンドラインツールです。

rbac-lookup.docs.fairwinds.com

rbac-lookup は、Linuxbrew コマンドが使用できる環境であれば容易にインストールできるはずなのですが、Amazon Linux 2 ではデフォルトでは brewが使えません。

よって、まず brew が使用できるようにします。

(実は、ここが一番試行錯誤したところです。)

まず前提として、Kubernetes クラスタに接続できる Amazon Linux 2 の環境にログインします。

以降は、OSユーザーが ssm-user という前提でコマンドを記載します。

まず、Development Tools グループを指定して必要なパッケージ群をインストールします。

sudo yum groupinstall 'Development Tools' -y

次に、Linuxbrew をインストールします。

sh -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)"

途中で次のように表示されるので、Enter キーを押します。

Press RETURN/ENTER to continue or any other key to abort:

この後、少し時間がかかりますが、brew のインストールが完了しますので、PATH を通しておきましょう。

PATH を通すためのコマンドは、brew のインストール完了時にも表示されています。

次のコマンドは、OSユーザーが ssm-user であることを前提にしています。

echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> /home/ssm-user/.bash_profile
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"

念のため、brew doctor を実行して brew が正しく使用できるか確認しておきましょう。

brew doctor

次のように表示されれば OK です。

Your system is ready to brew.

これでようやく、rbac-lookup をインストールできます。

brew install FairwindsOps/tap/rbac-lookup

インストール完了後、さっそく rbac-lookup を試してみましょう。

次のコマンドでは、Kubernetes のグループにバインドされているロールを表示します。

rbac-lookup -k group

結果の例です。

SUBJECT                   SCOPE          ROLE
eks:kube-proxy-windows    cluster-wide   ClusterRole/system:node-proxier
system:authenticated      cluster-wide   ClusterRole/eks:podsecuritypolicy:privileged
system:authenticated      cluster-wide   ClusterRole/system:basic-user
system:authenticated      cluster-wide   ClusterRole/system:discovery
system:authenticated      cluster-wide   ClusterRole/system:public-info-viewer
system:bootstrappers      cluster-wide   ClusterRole/eks:node-bootstrapper
system:masters            cluster-wide   ClusterRole/cluster-admin
system:monitoring         cluster-wide   ClusterRole/system:monitoring
system:node-proxier       cluster-wide   ClusterRole/system:node-proxier
system:nodes              cluster-wide   ClusterRole/eks:node-bootstrapper
system:serviceaccounts    cluster-wide   ClusterRole/system:service-account-issuer-discovery
system:unauthenticated    cluster-wide   ClusterRole/system:public-info-viewer

無事に rbac-lookup を実行できましたね!

今回は、EC2 インスタンスAWS Systems Manager のセッションマネージャーを使用してアクセスしたので、OS ユーザーは ssm-user となっています。

再度セッションマネージャーで接続した後は、rbac-lookup コマンドの PATH を通すために、.bash_profile の内容を反映させる必要があります。

これを手っ取り早く行うには、 sudo su - ssm-user を実行するとよいでしょう。

rbac-lookup はすごく便利なのですが、如何せん、brew でのインストールが必要になります。

macOS 環境であれば簡単にインストールできるのですが、Amazon Linux 2 では、brew 自体のインストールが必要なため、少し手間取りました。

この記事がどなたかの参考になれば嬉しいです!


/* -----codeの行番号----- */