---
title: MicroK8sまたはK3sのOIDC連携にCognitoを使うメモ
tags: ["Kubernetes", "MicroK8s", "OIDC", "Cognito", "K3s"]
categories: ["Dev", "CaaS", "Kubernetes"]
date: 2025-02-10T04:26:17Z
updated: 2025-02-10T04:28:43Z
---

MicroK8sの[OIDC連携](https://microk8s.io/docs/oidc-dex)にCognitoを使う。K3sの例も記載する。

MicroK8sのインストールは[こちら](/entries/782)を参照。

CognitoのUser Poolは作成済みの前提で、クライアントを作成する。

"Create app client"をクリック。

<img width="1787" alt="image" src="https://github.com/user-attachments/assets/2e89ec2e-c70c-4409-8255-985f5468180a" />

"Application Type"は"Traditional web application"で"Name your application"に`microk8s`を入力。"Return URL"に`http://localhost:8000` ([kubelogin](https://github.com/int128/kubelogin)のコールバックURL) を設定して、"Create app client"ボタンをクリック。
(スクリーンショットは`http://localhost:18000`になっているが、`http://localhost:8000`の間違い。)
<img width="1792" alt="image" src="https://github.com/user-attachments/assets/4d389998-cf4e-4246-b24f-75427a427c65" />

必須ではないが、ローカルで`8000`ポートが使用済みの場合ののフォールバックポートとして`18000`が使われるので、Callback URLに`http://localhost:18000`を追加しておいた方が無難。

<img width="1428" alt="image" src="https://github.com/user-attachments/assets/ca1289f4-0bb6-4feb-9f15-ed2662cab1c7" />

"App client information"パネルから"Client ID"と"Client secret"をコピー。

<img width="1431" alt="image" src="https://github.com/user-attachments/assets/038b5323-588c-41c4-a5dd-8e314b650879" />

User Poolの"Overview"パネルから"Token signing key URL"の`/.well-known/jwks.json`の前までのパスをコピー。

<img width="1791" alt="image" src="https://github.com/user-attachments/assets/a970650d-798d-432f-aded-be050b5e71e4" />


### MicroK8sの場合

MicroK8sのControlplaneをインストールしてあるサーバーにログインして、`/var/snap/microk8s/current/args/kube-apiserver`を修正する。

変数は環境に合わせて修正し、次のコマンドを実行。

```
OIDC_ISSUER_URL=https://cognito-idp.ap-northeast-1.amazonaws.com/ap-northeast-1_tv3NHZAKO
OIDC_CLIENT_ID=3ftgo86lgdb2mb0fr0jho761q9
OIDC_USERNAME_CLAIM=email
OIDC_GROUPS_CLAIM=cognito:groups

cat <<EOF | sudo tee -a /var/snap/microk8s/current/args/kube-apiserver
--oidc-issuer-url=${OIDC_ISSUER_URL}
--oidc-client-id=${OIDC_CLIENT_ID}
--oidc-username-claim=${OIDC_USERNAME_CLAIM}
--oidc-username-prefix=oidc:
--oidc-groups-claim=${OIDC_GROUPS_CLAIM}
--oidc-groups-prefix=oidc:
EOF
```

MicroK8sを再起動。

```
sudo snap restart microk8s
```

### K3sの場合


`/etc/rancher/k3s/config.yaml`の先頭に`kube-apiserver-arg:`が記述されていることを確認し、

```bash
OIDC_ISSUER_URL=https://cognito-idp.ap-northeast-1.amazonaws.com/ap-northeast-1_tv3NHZAKO
OIDC_CLIENT_ID=3ftgo86lgdb2mb0fr0jho761q9
OIDC_USERNAME_CLAIM=email
OIDC_GROUPS_CLAIM=cognito:groups

cat <<EOF | sudo tee -a /etc/rancher/k3s/config.yaml
- "oidc-issuer-url=${OIDC_ISSUER_URL}"
- "oidc-client-id=${OIDC_CLIENT_ID}"
- "oidc-username-claim=${OIDC_USERNAME_CLAIM}"
- "oidc-username-prefix=oidc:"
- "oidc-groups-claim=${OIDC_GROUPS_CLAIM}"
- "oidc-groups-prefix=oidc:"
EOF
```

K3sを再起動

```bash
sudo systemctl restart k3s
```

### RBACの設定


Cognito上のユーザーが属している管理ユーザー用のグループが`platform-engineer`とする。このグループに`cluster-admin`のClusterRoleを割り当てるために、次のコマンドを実行。

```
kubectl create clusterrolebinding cluster-admin-platform-engineer --clusterrole=cluster-admin --group=oidc:platform-engineer
```

### kubeloginの設定

https://github.com/int128/kubelogin#getting-started より、kubeloginをインストールして、次のコマンドで現在の`kubectl`のコンテキストに`congnito`のユーザーを追加する。

```
OIDC_ISSUER_URL=https://cognito-idp.ap-northeast-1.amazonaws.com/ap-northeast-1_tv3NHZAKO
OIDC_CLIENT_ID=3ftgo86lgdb2mb0fr0jho761q9
OIDC_CLIENT_SECRET=********

kubectl config set-credentials cognito \
  --exec-api-version=client.authentication.k8s.io/v1beta1 \
  --exec-command=kubectl \
  --exec-arg=oidc-login \
  --exec-arg=get-token \
  --exec-arg=--oidc-issuer-url=${OIDC_ISSUER_URL} \
  --exec-arg=--oidc-client-id=${OIDC_CLIENT_ID} \
  --exec-arg=--oidc-client-secret=${OIDC_CLIENT_SECRET}
 
kubectl config set-context --current --user=cognito
```

次のコマンドを実行。
```
kubectl auth whoami
```

Cognitoのログイン画面が立ち上がるので、ログインする。

<img width="1912" alt="image" src="https://github.com/user-attachments/assets/f21bf2db-4cd5-4d52-93d9-af0f27f233c8" />

ログインが成功すると、

<img width="1912" alt="image" src="https://github.com/user-attachments/assets/8762f74d-eb70-4e44-8457-9312f5637f9a" />

次のようにUsernameとGroupsがマッピングされる。

```
$ kubectl auth whoami
ATTRIBUTE   VALUE
Username    oidc:makingx@gmail.com
Groups      [oidc:platform-engineer system:authenticated]
```
