[Kubernetes] RBAC

  • RBAC (Role-based Access Control)는 쿠버네티스 환경에서 Node 또는 네트워크 리소스 등 여러가지 접근 권한에 대한 Role 관리하는 작업 요소 이다.
  • RBAC는 rbac.authorization.k8s.io API를 사용하며, K8s 1.8 이상부터 RBAC Mode가 Stable 한다.
  • 또한, RBAC 활성화를 위한 –authorization-mode=RBAC 설정이 필요하다.

Role & ClusterRle #

  • Role : Default 라는 Namespace에 모든 Pod의 읽기권한(get, watch, list)을 설정하고 pod-read 라고 정의한다.
1apiVersion: rbac.authorization.k8s.io/v1
2kind: Role
3metadate:
4  namespaces: default
5  name: pod-read
6rules:
7 - apiGroups: [""] // "" 는 Core API Group 을 나타냄.
8   resources: ["pods"]
9   verbs: ["get", "watch", "list"]
  • ClusterRole : Cluster 의 Secret 정보에 대한 읽기 권한을 설정하고 secret-read 라고 정의함. (Node, Endpoint, Namespace, Service 등 모든 권한 설정)

  • ClusterRole은 namespace 영역이 아니기 때문에 생략된다.

1apiVersion: rbac.authorization.k8s.io/v1
2kind: ClusterRole
3metadata:
4  name: secret-read
5rules:
6- apiGroups: [""]
7  resources: ["secrets"]
8  verbs: ["get", "watch", "list"]
  • Sample (admin-manager.yaml)
1apiVersion: rbac.authorization.k8s.io/v1
2kind: ServiceAccount
3metadata:
4  name: admin-manager
5  namespace: kube-system

RoleBinding & ClusterRoleBinding #

  • RoleBinding 은 User, Team 단위의 권한 부여 기능이며, ClusterRoleBinding은 클러스터 단위의 권한 부여 기능을 나타낸다.

  • RoleBinding : Reference 라는 User 에게 Pod-read 권한 설정. 즉, Reference 라는 User는 Namespace가 Default인 모든 Pod을 읽기가 가능하다.

 1apiVersion: rbac.authorization.k8s.io/v1
 2kind: RoleBinding
 3metadata:
 4  name: read-pods
 5  namespace: default
 6subjects:
 7- kind: User
 8  name: reference
 9  apiGroup: rbac.authorization.k8s.io
10roleRef:
11  kind: Role // Role & ClusterRole 적용.
12  name: pod-reader // 바인딩할 Role & ClusterRole 이름과 일치해야 함.
13  apiGroup: rbac.authorization.k8s.io
  • ClusterRoleBinding : ManagerGroup 에게 Secret-Read 권한 설정. 즉, Manager-Group 의 모든 사용자가 모든 Namespace에서 Secret 을 읽을 수 있다.
 1apiVersion: rbac.authorization.k8s.io/v1
 2kind: ClusterRoleBinding
 3metadata:
 4  name: read-secrets-global
 5subjects:
 6- kind: Group
 7  name: manager-group
 8  apiGroup: rbac.authorization.k8s.io
 9roleRef:
10  kind: ClusterRole // Role & ClusterRole 적용
11  name: secret-read // 바인딩할 Role & ClusterRole 이름과 일치해야 함.
12  apiGroup: rbac.authorization.k8s.io
  • Sample (admin-rolebinding.yaml)
 1apiVersion: rbac.authorization.k8s.io/v1
 2kind: ClusterRoleBinding
 3metadata:
 4  name: admin-manager
 5subjects:
 6- kind: ServiceAccount
 7  name: admin-manager
 8  namespcae: kube-system
 9roleRef:
10  apiGroup: rbac.authorization.k8s.io
11  kind: ClusterRole
12  name: cluster-admin

K8s API 호출 권한 #

  • K8s api 호출시 RBAC role 에 의한 접근 불가 오류 메세지 출력되면 K8s api 호출 error example - kryoon에서 pod에 대한 정보를 요청할 때 RBAC role에 막혔다
1GET /api/v1/namespaces/{namespace}/pods/{name}/log
2
3WARNING: Failed to count the # of live instances on Kubernetes
4io.fabric8.kubernetes.client.KubernetesClientException: Failure executing: GET
5at: https://kubernetes.default/api/v1/namespaces/kryoon/pods.
6Message: Forbidden!Configured service account doesn't have access.
7Service account may have been revoked. pods is forbidden:
8User "system:serviceaccount:kryoon:default" cannot list pods in the namespace "kryoon":
9Unknown user "system:serviceaccount:kryoon:default".
  • 아래와 같이 pod(resource), log(subresource of pods)에 대한 권한을 추가해야 한다.
1apiVersion: rbac.authorization.k8s.io/v1
2kind: Role
3metadata:
4  namespace: default
5  name: pod-and-pod-logs-reader
6rules:
7- apiGroups: [""]
8  resources: ["pods", "pods/log"]
9  verbs: ["get", "list"]
Advertisement