🐳 Container/K8S

[K8s] RBAC(Role-Based Access Control)

heywantodo 2024. 4. 9. 15:56
728x90
반응형

[K8s] RBAC(Role-Based Access Control)

RBAC는 개별 사용자의 역할을 기반으로 컴퓨터 또는 네트워크 리소스에 대한 액세스를 규제한다.

특정 사용자(User)와 역할(Role) 두 가지를 조합해서 사용자에게 특정 권한을 부여할 수 있다.

 

API 객체

RBAC API는 Role, ClusterRole, Rolebinding 및 ClusterRoleBinding의 네가지 종류의 개체를 선언한다.

 

Role, ClusterRole

Role 및 ClusterRole 객체에서 RBAC 규칙을 정의한다.

  • Role : 단일 네임스페이스에 포함된 리소스에 대한 액세스를 정의한다. 
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: default
  name: pod-reader
rules:
- apiGroups: [""] # "" indicates the core API group
  resources: ["pods"]
  verbs: ["get", "watch", "list"]
  • ClusterRole : 전체 클러스터의 리소스에 대한 액세스를 정의한다. 
    • 네임스페이스 리소스에 대한 권한을 정의하고 개별 네임스페이스 내에서 액세스 권한을 부여
    • 네임스페이스 리소스에 대한 권한을 정의하고 모든 네임스페이스에 대한 액세스 권한을 부여
    • 클러스터 범위 리소스에 대한 권한 정의
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  # "namespace" omitted since ClusterRoles are not namespaced
  name: secret-reader
rules:
- apiGroups: [""]
  #
  # at the HTTP level, the name of the resource for accessing Secret
  # objects is "secrets"
  resources: ["secrets"]
  verbs: ["get", "watch", "list"]

 

RoleBinding, ClusterRoleBinding 

Role 및 ClusterRole 규칙을 할당한다.

  • RoleBinding : 특정 네임스페이스 내의 사용자 또는 그룹에 Role 또는 ClusterRole을 할당한다.
apiVersion: rbac.authorization.k8s.io/v1
# This role binding allows "jane" to read pods in the "default" namespace.
# You need to already have a Role named "pod-reader" in that namespace.
kind: RoleBinding
metadata:
  name: read-pods
  namespace: default
subjects:
# You can specify more than one "subject"
- kind: User
  name: jane # "name" is case sensitive
  apiGroup: rbac.authorization.k8s.io
roleRef:
  # "roleRef" specifies the binding to a Role / ClusterRole
  kind: Role #this must be Role or ClusterRole
  name: pod-reader # this must match the name of the Role or ClusterRole you wish to bind to
  apiGroup: rbac.authorization.k8s.io
  • ClusterRoleBinding : 클러스터에 포함된 모든 네임페스의 사용자 또는 그룹에 ClusterRole을 할당한다.
apiVersion: rbac.authorization.k8s.io/v1
# This cluster role binding allows anyone in the "manager" group to read secrets in any namespace.
kind: ClusterRoleBinding
metadata:
  name: read-secrets-global
subjects:
- kind: Group
  name: manager # Name is case sensitive
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: ClusterRole
  name: secret-reader
  apiGroup: rbac.authorization.k8s.io

 

kubectl로 생성하기

사용자가 포드에서 get, watch 작업을 수행할 수 있도록 하는 Role을 생성한다. 

kubectl create role pod-reader --verb=get --verb=list --verb=watch --resource=pods

사용자가 Pod에서 get 작업을 수행할 수 있도록 하는 ClusterRole을 생성한다.

kubectl create clusterrole pod-reader --verb=get,list,watch --resource=pods

특정 네임스페이스 내에서 Role 또는 ClusterRole을 부여한다.

kubectl create rolebinding bob-admin-binding --clusterrole=admin --user=bob --namespace=acme

전체 클러스터(모든 네임스페이스)에 ClusterRole을 부여한다.

kubectl create clusterrolebinding root-cluster-admin-binding --clusterrole=cluster-admin --user=root

 

서비스 계정 권한

특정 ServiceAccount에 특정 역할을 부여할 수 있다.

예를 들어 "my-namespace" 내의 읽기 전용 권한을 "my-sa" 서비스어카운트에 부여한다.

kubectl create rolebinding my-sa-view \
  --clusterrole=view \
  --serviceaccount=my-namespace:my-sa \
  --namespace=my-namespace

 

최소 권한 

이상적으로 최소의 RBAC 권한만이 사용자 및 서비스 어카운트에 부여되어야 한다.

작업에 명시적으로 필요한 권한만 사용 되어야 한다.

  • 권한은 가능하면 네임스페이스 레벨에서 부여한다. 
  • 와일드 카드를 사용한 권한 지정을 할 때, 모든 리소스에 대해서는 지양한다.
  • cluster-admin 계정이 필수로 요구되지 않을 시에는 사용을 지양한다.
  • system:masters 그룹에 사용자 추가를 지양한다.

 

참고

https://kubernetes.io/docs/reference/access-authn-authz/rbac/

728x90
반응형