728x90

Kubernetes 클러스터에서 애플리케이션의 보안을 유지하고, 다양한 리소스에 대한 접근을 제어하는 것은 매우 중요함

이를 위해 Kubernetes는 RBAC(Role-Based Access Control)와 ServiceAccount를 제공하여, 클러스터 내에서의 접근 권한을 체계적으로 관리할 수 있음

Kubernetes RBAC(Role-Based Access Control)란?

Kubernetes의 RBAC는 사용자나 서비스가 클러스터 내에서 어떤 리소스에 접근하고, 어떤 동작을 수행할 수 있는지를 제어하는 메커니즘임

RBAC를 통해 특정 리소스에 대해 읽기, 쓰기 등의 권한을 세밀하게 설정할 수 있음

RBAC는 Role, ClusterRole, RoleBinding, ClusterRoleBinding으로 구성됨

  • Role: 특정 네임스페이스 내에서 리소스에 대한 권한을 정의함
  • ClusterRole: 클러스터 전체에서 권한을 정의함
  • RoleBinding: 네임스페이스 내에서 특정 주체(사용자, 그룹, ServiceAccount)에게 Role을 할당함
  • ClusterRoleBinding: 클러스터 전체에서 특정 주체에게 ClusterRole을 할당함

ServiceAccount란?

ServiceAccount는 Kubernetes 클러스터 내에서 파드(Pod)가 Kubernetes API 서버에 접근할 수 있도록 해주는 계정임

기본적으로 Kubernetes에서 파드는 ServiceAccount와 연동되며, 이 계정을 통해 Kubernetes API에 인증된 요청을 할 수 있음

apiVersion: v1
kind: ServiceAccount
metadata:
  name: example-serviceaccount
  namespace: example-namespace

RBAC와 ServiceAccount 연동하기

RBAC는 ServiceAccount와 함께 사용되어, 파드나 애플리케이션이 특정 리소스에 대해 어떤 권한을 가질지 제어할 수 있음

예를 들어, 특정 파드가 pods 리소스에 대한 읽기 권한만 가지도록 설정할 수 있음

이를 위해 Role과 RoleBinding을 활용할 수 있음

Role 정의

Role은 네임스페이스 내에서 리소스에 대한 권한을 정의함

아래는 pods 리소스에 대해 get, list, watch 권한을 가진 Role을 정의한 예시임

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: example-namespace
  name: example-role
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "list", "watch"]

RoleBinding 정의

RoleBinding은 특정 ServiceAccount가 위에서 정의한 Role의 권한을 사용할 수 있도록 바인딩하는 역할을 함

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: example-rolebinding
  namespace: example-namespace
subjects:
- kind: ServiceAccount
  name: example-serviceaccount
  namespace: example-namespace
roleRef:
  kind: Role
  name: example-role
  apiGroup: rbac.authorization.k8s.io

위의 예시에서 example-serviceaccount는 example-role에서 정의된 권한을 사용하여 pods 리소스에 대한 get, list, watch 동작을 수행할 수 있음

ClusterRole과 ClusterRoleBinding

Role과 RoleBinding은 네임스페이스 내에서만 작동하지만, 클러스터 전역에서 권한을 설정하고자 할 때는 ClusterRole과 ClusterRoleBinding을 사용함

ClusterRole 정의

ClusterRole은 클러스터 전체에서 리소스에 대한 권한을 정의할 수 있음

예를 들어, 클러스터 내의 모든 nodes와 pods에 대한 읽기 권한을 정의한 예시는 다음과 같음

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: example-clusterrole
rules:
- apiGroups: [""]
  resources: ["nodes", "pods"]
  verbs: ["get", "list", "watch"]

ClusterRoleBinding 정의

ClusterRoleBinding은 특정 ServiceAccount가 클러스터 전역에서 ClusterRole을 사용할 수 있도록 바인딩하는 역할을 함

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: example-clusterrolebinding
subjects:
- kind: ServiceAccount
  name: example-serviceaccount
  namespace: example-namespace
roleRef:
  kind: ClusterRole
  name: example-clusterrole
  apiGroup: rbac.authorization.k8s.io

이 예시에서 example-serviceaccount는 클러스터 전체에서 nodes와 pods 리소스에 대해 읽기 권한을 가짐

 

728x90

+ Recent posts