クラスター権限制御ガイド
    • PDF

    クラスター権限制御ガイド

    • PDF

    Article Summary

    Classic環境で利用できます。

    クラスター権限制御

    Ncloud Kubernetes Serviceが提供するkubeconfig.yaml認証ファイルは管理者権限を持つため、Jenkins、Github Actionなどの3rd-partyサービスを使用するときにセキュリティ問題が発生することがあります。 このサービスを利用する場合、最小限の権限に制限して使用することをお勧めします

    準備事項

    • 運用中のクラスター、コネクターのインストール、環境変数の設定が必要です。
    • クラスター生成については、クラスター生成を参照してください。
    • kubectlのインストールと環境変数の設定は、kubectlのインストールを参照してください。

    Service Account、Role、Role Binding作成

    Namespace 追加

    テストのためのスペースを作成します。

    $ kubectl --kubeconfig $KUBE_CONFIG create ns hello-world
    

    Service Account 追加

    $ cat <<EOF | kubectl --kubeconfig $KUBE_CONFIG apply -f -
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: hello-user
      namespace: hello-world
    EOF
    

    Role / Role Binding 追加

    Roleのresourcesverbsで使用する権限を定義します。 以下の設定は、hello-worldというnamespaceにpodリソースのみ制御するように制限されます。

    $ 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
    

    Service Account Token 確認

    $ kubectl --kubeconfig $KUBE_CONFIG -n hello-world describe secret $(kubectl --kubeconfig $KUBE_CONFIG -n hello-world get secret | grep hello-user2 | awk '{print $1}')
    Name:         hello-user-token-6zjw7
    Namespace:    hello-world
    Labels:       <none>
    Annotations:  kubernetes.io/service-account.name: hello-user
                  kubernetes.io/service-account.uid: 9546d5be-1b27-11ea-8845-f220cdab2dde
    
    Type:  kubernetes.io/service-account-token
    
    Data
    ====
    ca.crt:     1029 bytes
    namespace:  11 bytes
    token:      eyJhbGciOiJSU...
    

    Tokenで認証するkubeconfig-token.yaml生成

    ダウンロードしたkubeconfig.yamlkubeconfig-token.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:
        client-key-data:  LS0tLS1CRUdJTiBDRV...
        client-certificate-data: LS0tLS1CRUdJTiB...
    

    user部分を削除した後の前段階で確認したToken情報を下記のように追加します。

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

    権限確認

    Tokenを利用してKubernetes APIを呼び出す際に正常に権限を制限しているかを確認します。

    • kubeconfig-token.yamlを環境変数として宣言
    $ export KUBE_CONFIG_TOKEN=kubeconfig-token.yaml
    
    • Podオブジェクト照会(アクセス可能)
    $ kubectl --kubeconfig $KUBE_CONFIG_TOKEN -n hello-world get pod
    No resources found in hello-world namespace.
    
    • Deploymentオブジェクト照会(アクセス不可能)
    $ kubectl --kubeconfig $KUBE_CONFIG_TOKEN -n hello-world get deploy
    Error from server (Forbidden): deployments.extensions is forbidden: User "system:serviceaccount:hello-world:hello-user" cannot list resource "deployments" in API group "extensions" in the namespace "hello-world"
    
    • 他のnamespace照会(アクセス不可能)
    $ kubectl --kubeconfig $KUBE_CONFIG_TOKEN get pod
    Error from server (Forbidden): pods is forbidden: User "system:serviceaccount:hello-world:hello-user" cannot list resource "pods" in API group "" in the namespace "default"
    

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

    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.