RBAC이란
역할 기반 접근 제어(role-based access control, RBAC)는 컴퓨터 시스템 보안에서 권한이 있는 사용자들에게 시스템 접근을 통제하는 한 방법이다. [출처:위키백과]
쿠버네티스에서 RBAC은 일반 사용자, 서비스 계정에 권한을 부여한 롤에 따라 리소스에 대한 권한을 제어하는 기능을 제공한다. RBAC을 적절히 사용해 쿠버네티스 리소스의 보안을 확보할 수 있다.
다른 액세스 제어 방법과 비교하여 다음과 같은 장점이 있습니다.
- 클러스터에서 리소스 및 비리소스 권한을 모두 포함합니다.
- 전체 RBAC은 여러 API 객체에 의해 수행되며 다른 API 객체와 마찬가지로 kubectl 또는 API로 조작 할 수 있습니다.
- API Server를 다시 시작하지 않고도 런타임에 조정할 수 있습니다.
User & Role & RoleBinding
1. USER
사용자 개념
일반 사용자
- 클러스터 외부에서 쿠버네티스를 조작하는 사용자
kubectl config view | grep current-context
서비스 계정
- 쿠버네티스 내부적으로 관리되며 파드가 쿠버네티스 API를 다룰 때 사용하는 사용자
apiVersion: v1 kind: ServiceAccount metadata: name: myuser namespace: default
2. Role
특정 api나 리소스에 대한 권한들을 명시해둔 규칙들의 집합
Verb
Verb | 의미 |
---|---|
create | 새로운 리소스 생성 |
get | 개별 리소스 조회 |
list | 여러건의 리소스 조회 |
update | 기존 리소스내용 전체 업데이트 |
patch | 기존 리소스중 일부 내용 변경 |
delete | 개별 리소스 삭제 |
deletecollection | 여러 리소스 삭제 |
Role
- 속한 네임스페이스에 한 곳에만 적용
kind: Role apiVersion: rbac.authorization.k8s.io/v1 metadata: namespace: default name: read-role rules: - apiGroups: [""] resources: ["pods"] resourceNames: [“mypod"] verbs: ["get", "list"]
ClusterRole
- 클러스터 전체 네임스페이스에 적용
kind: ClusterRole apiVersion: rbac.authorization.k8s.io/v1 metadata: name: read-clusterrole rules: - apiGroups: [""] resources: ["pods"] verbs: ["get", "list"]
- Grouping 가능
kind: ClusterRole apiVersion: rbac.authorization.k8s.io/v1 metadata: name: admin-aggregation aggregationRule: clusterRoleSelectors: - matchLabels: kubernetes.io/bootstrapping: rbac-defaults rules: []
3. RoleBinding
롤과 사용자를 묶어(binding)주는 역할
RoleBinding
- 속한 네임스페이스에 한 곳에만 적용
kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: read-rolebinding namespace: default subjects: - kind: ServiceAccount name: myuser apiGroup: "" roleRef: kind: Role name: read-role apiGroup: rbac.authorization.k8s.io
ClusterRoleBinding
- 클러스터 전체 네임스페이스에 적용
kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: read-clusterrolebinding subjects: - kind: ServiceAccount name: myuser namespace: default apiGroup: "" roleRef: kind: ClusterRole name: read-clusterrole apiGroup: rbac.authorization.k8s.io
실습
일반 사용자
1. 사용자 생성
> kubectl create serviceaccount myuser
생성 확인
> kubectl get serviceaccount myuser -o yaml
토큰 얻기
시크릿 조회
> kubectl get secret myuser-token-kchkm -o yaml
Base64 디코딩
echo 'ZXlKaGJHY2lPaUpTVXpJ..' | base64 -D
토큰 얻기 2
kubectl -n default describe secret $(kubectl -n default get secret | grep myuser | awk '{print $1}')
2. Role 생성
ClusterRole
kind: ClusterRole apiVersion: rbac.authorization.k8s.io/v1 metadata: name: read-clusterrole rules: - apiGroups: [""] resources: ["pods"] verbs: ["get", "list"]
kubectl apply -f read-clusterrole.yaml
3. RoleBinding 생성
kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: read-clusterrolebinding subjects: - kind: ServiceAccount name: myuser namespace: default apiGroup: "" roleRef: kind: ClusterRole name: read-clusterrole apiGroup: rbac.authorization.k8s.io
kubectl apply -f read-clusterrolebinding.yaml