kubernetes

Manage role based access control (RBAC)

수빈2 2022. 7. 17. 17:04

Kubernetes API로 REST 요청을 하면, 3가지 단계를 거쳐서 수행된다

Authentictaion ( 인증 ) -> Authorization ( 권한 허가 ) -> Addmition Control

  • Authentication (인증) : 사용자가 누구인지 식별하는 것
  • Authorization ( 권한 허가 ) : 어떤 권한을 가지고 어떤 행동을 할 수 있는지?
  • Addmition Control : 관리자에 의한 추가 검증, 요청 내용 변경 등 수행

RBAC ( Role based access control ) 

RBAC란 말그대로 역할(Role)에 기반한 권한 부여 방식이다

위 단계에서 두번째인 Authorization에 해당한다.

쿠버네티스는 User 가 누군지 상관없이 어떤 역할에 권한을 부여한다.

  1. Role에 어떤 리소스에 어떤 권한을 부여한다 ( e.g. pod 조회 권한, deployment 생성 권한 )
  2. User나 Gorup 또는 Service account는 Role binding을 통해 해당 권한을 가질 수 있다

Role

  • Namespace별로 부여하고, Namespace에 포함되는 resource들에 대해서 권한 부여

Cluster Role

  • Cluster 전체의 Namespace 외 resource들에 권한 부여

Service Account

  • user 또는 pod과 같은 애플리케이션이 Service Account를 가질 수 있다

 

User / Group

  • User와 Group은 명시적으로 생성하는 것은 아니고, Authentication (인증) 단계를 거치는 사용자, 그룹을 뜻한다-
    • Context에서 인증을 통해서 권한을 가진 User 들

 

Verb

Role 또는 Cluster role에 부여되는 권한 ( 조회 / 생성 등 )

Verb 의미 Example
create 새로운 resource 생성 kubectl create deploy test
get 개별 resource 조회 kubectl get pod test
kubectl describe pod test
resource를 특정해줘야지만 가능
( kubectl get pod 하면 불가능 )
list 여러건 resource 조회 kubectl get pod
( describe 불가능 )
watch 특정 resource 지켜보기 아래 내용을 말하는건지?
watch kubectl get pod
-> 테스트 해볼 필요
update 기존 리소스 내용 전체 업데이트 kubectl replace / exchange 
read / write lock이 존재
patch 기존 리소스 중 일부 변경 kubectl apply/edit/patch
read / write lock이 없음 -> 마지막으로 업데이트 된 내용으로 적용
delete 개별 리소스 삭제 kubectl delete pod test
개별 resource 삭제
deletecollection 여러 리소스 삭제 kubectl delete 

실습해보기

role 생성 : logging namespace의 pod get, list 권한만 부여

$ cat role.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
 namespace: logging
 name: pod-reader
rules:
- apiGroups: [""] # "" indicates the core API group
 resources: ["pods"]
 verbs: ["get", "list"]
$ kubectl apply -f role.yaml
role.rbac.authorization.k8s.io/pod-reader created

role binding : logging namespace의 tester service account에 role binding

$ kubectl create rolebinding test --serviceaccount=logging:tester --
role=pod-reader -nlogging
rolebinding.rbac.authorization.k8s.io/test created
$ kubectl get rolebinding -nlogging
NAME ROLE AGE
test Role/pod-reader 10s
$ kubectl get rolebinding -nlogging test -o yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
 creationTimestamp: "2022-04-26T00:36:09Z"
 name: test
 namespace: logging
 resourceVersion: "6256848"
 uid: 707dd77b-3dac-4bd6-bb82-b80f6bb0e81f
roleRef:
 apiGroup: rbac.authorization.k8s.io
 kind: Role
 name: pod-reader
subjects:
- kind: ServiceAccount
 name: tester
 namespace: logging

 

test 유저로 조회하기 ( context에 service account 및 token 추가하여 테스트 )

$ kubectl get pod
NAME READY STATUS RESTARTS AGE
es-0 1/1 Running 0 4d22h
fluent-bit-6dmlf 1/1 Running 1 (5d17h ago) 17d
fluent-bit-h2t47 1/1 Running 1 17d
fluentd-4qglm 1/1 Running 1 17d
fluentd-4v7mk 1/1 Running 1 (5d17h ago) 17d
kibana-856c7d9b55-v96dp 1/1 Running 0 5d15h
nginx-7c658794b9-cvt8q 1/1 Running 0 4d17h
$ kubectl get pod -ndefault
Error from server (Forbidden): pods is forbidden: User "system:serviceaccount:logging:tester" cannot list 
resource "pods" in API group "" in the namespace "default"
$ kubectl create deployment nginx --image=nginx:latest --replicas=1 -nlogging
error: failed to create deployment: deployments.apps is forbidden: User "system:serviceaccount:logging:
tester" cannot create resource "deployments" in API group "apps" in the namespace "logging"

-> 다른 네임스페이스는 조회가 안된다