クラスタ権限制御
    • PDF

    クラスタ権限制御

    • PDF

    Article Summary

    VPC環境で利用できます。

    参考

    ncp-iam-authenticatorを使用して kubeconfigを構成すると、ServiceAccountトークンを利用して権限を制御する必要がありません。

    IAM認証ユーザーの管理を確認してください。

    Kubeconfigファイルのトークン値を変更して特定の Namespaceに対してのみ作成、削除、変更を行えるように権限を設定します。

    Ncloud Kubernetes Serviceで提供する基本 kubeconfig認証ファイルは管理者のすべての権限を持つため、ユーザーが Jenkinsや GitHub Actionといった 3rd-partyサービスを使用する場合にセキュリティ問題が発生するおそれがあります。このような問題は、kubeconfigファイルを修正して権限を最小化することで防げます。

    kubeconfigファイルのトークン値変更

    kubeconfigファイルのトークン値を変更してクラスタ権限を制限する方法を、ユースケースにて説明します。

    kubeconfigファイルのトークン値を変更する方法は、次の通りです。

    1. 以下のコマンドを順に実行して Namespaceと Service Accountを作成します。

      $ kubectl --kubeconfig $KUBE_CONFIG create ns hello-world
      
      $ cat <<EOF | kubectl --kubeconfig $KUBE_CONFIG apply -f -
      apiVersion: v1
      kind: ServiceAccount
      metadata:
        name: hello-user
        namespace: hello-world
      EOF
      
    2. 以下のコマンドを実行して Roleと Role Bindingを追加し、kubeconfig設定を kubeconfig.yamlファイルでダウンロードします。

      $ cat <<EOF | kubectl --kubeconfig $KUBE_CONFIG apply -f -
      apiVersion: rbac.authorization.k8s.io/v1
      kind: Role
      metadata:
        namespace: hello-world
        name: hello-role
      rules:
      - apiGroups: [""]
        resources: ["pods"] # Objectの指定
        verbs: ["get", "list", "watch", "create", "update", "patch", "delete"] # Action制御 
      ---
      apiVersion: rbac.authorization.k8s.io/v1
      kind: RoleBinding
      metadata:
        namespace: hello-world
        name: hello-rb
      subjects:
      - kind: ServiceAccount
        name: hello-user
        namespace: hello-world
      roleRef:
        kind: Role 
        name: hello-role
        apiGroup: rbac.authorization.k8s.io
      EOF
      
      • Roleの resourcesverbsで使用する権限を定義します。上記のサンプルコードでは、 hello-worldという Namespaceの Podリソースのみ制御するように制限します。
    3. 以下のコマンドを実行して Service Accountの Secretを作成します。

      $ cat <<EOF | kubectl --kubeconfig $KUBE_CONFIG apply -f -
      apiVersion: v1
      kind: Secret
      metadata:
        name: hello-user-default
        namespace: hello-world
        annotations:
          kubernetes.io/service-account.name: hello-user
      type: kubernetes.io/service-account-token
      EOF
      
      参考

      Kubernetes 1.24からは Service Account作成時に自動で default secretを作成しません。使用しているバージョンが1.24以降である場合、ステップ3で関連 secretの作成が必要です。以前のバージョンである場合は、自動で作成されるため、当該ステップを行う必要はありません。

    4. 以下のコマンドを実行して Service Accountのトークンを確認します。

      $ kubectl --kubeconfig $KUBE_CONFIG -n hello-world describe secret $(kubectl --kubeconfig $KUBE_CONFIG -n hello-world get secret | grep hello-user | awk '{print $1}')
      
    5. ダウンロードした kubeconfig.yamlファイルをコピーして kubeconfig-token.yamlファイルで保存し、そのファイルの user 部分の client-certificate-dataclient-key-data の値を以下のように削除します。

      $ cp kubeconfig.yaml kubeconfig-token.yaml
      $ vi kubeconfig-token.yaml
      apiVersion: v1
      clusters:
      ...
      users:
      - name: kubernetes-admin
        user:
      
    6. 前のステップで確認したトークン情報を、以下のように user の値として追加します。

      $ cat kubeconfig-token.yaml
      apiVersion: v1
      clusters:
      ...
      users:
      - name: kubernetes-admin
        user:
          token:  eyJhbGciOiJSU... # Service Account Tokenの追加
      

    権限制御確認

    上記で作成した kubeconfig-token.yamlファイルを基準に、トークンを利用して Kubernetes APIを呼び出す際に正常に権限を制限するか確認する方法は、次の通りです。

    1. 以下のコマンドを実行して kubeconfig-token.yamlを環境変数として宣言します。

      $ export KUBE_CONFIG_TOKEN=kubeconfig-token.yaml
      
    2. 以下のコマンドをそれぞれ実行してアクセスの有無を確認します。

      • Podオブジェクトの照会(アクセス可能)
      $ kubectl --kubeconfig $KUBE_CONFIG_TOKEN -n hello-world get pod
      
      • Deploymentオブジェクトの照会(アクセス不可)
      $ kubectl --kubeconfig $KUBE_CONFIG_TOKEN -n hello-world get deploy
      
      • 他の Namespaceの照会(アクセス不可)
      $ kubectl --kubeconfig $KUBE_CONFIG_TOKEN get pod
      

    この記事は役に立ちましたか?

    Changing your password will log you out immediately. Use the new password to log back in.
    First name must have atleast 2 characters. Numbers and special characters are not allowed.
    Last name must have atleast 1 characters. Numbers and special characters are not allowed.
    Enter a valid email
    Enter a valid password
    Your profile has been successfully updated.