클러스터 권한 제어
    • PDF

    클러스터 권한 제어

    • PDF

    Article Summary

    VPC 환경에서 이용 가능합니다.

    참고

    ncp-iam-authenticator를 사용하여 kubeconfig를 구성하면 ServiceAccount 토큰을 이용하여 권한 제어를 할 필요가 없습니다.
    IAM 인증 사용자 관리를 확인해 주십시오.

    kubeconfig 파일의 토큰 값을 변경하여 특정 네임스페이스에 대해서만 생성, 삭제, 변경이 가능하도록 권한을 설정할 수 있습니다.

    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의 resources, verbs에서 사용할 권한을 정의합니다. 위 코드 예시에서는 hello-world라는 네임스페이스의 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-data, client-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
      
      • 다른 네임스페이스 조회 (접근 불가능)
      $ 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.