RBAC 설정

Kubernetes Role-Based Access Control의 설계와 구현 완벽 가이드


Kubernetes Role-Based Access Control의 설계와 구현 완벽 가이드

목표: 최소 권한 원칙에 따라 안전한 권한 체계를 구축할 수 있는 수준


🏗️ RBAC 개요

[User/ServiceAccount] ──> [Role/ClusterRole] ──> [Resources]


                         [RoleBinding/ClusterRoleBinding]

핵심 개념:

  • Who: User, Group, ServiceAccount
  • What: Role (수행할 수 있는 동작)
  • Where: Scope (Namespace 또는 Cluster 전체)

📋 Role vs ClusterRole

특징RoleClusterRole
범위특정 NamespaceCluster 전체
리소스Namespace 리소스 (Pod, Service 등)Cluster 리소스 (Node, PV 등)
사용RoleBindingClusterRoleBinding 또는 RoleBinding

🎯 Role 예시

Pod 읽기 전용

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

Deployment 관리

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: production
  name: deployment-manager
rules:
- apiGroups: ["apps"]
  resources: ["deployments"]
  verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
- apiGroups: ["apps"]
  resources: ["deployments/scale"]
  verbs: ["get", "update", "patch"]

🌐 ClusterRole 예시

노드 읽기

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

관리자 (admin)

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: custom-admin
rules:
- apiGroups: ["*"]
  resources: ["*"]
  verbs: ["*"]

🔗 Binding

RoleBinding

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: read-pods
  namespace: development
subjects:
- kind: User
  name: jane
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: pod-reader
  apiGroup: rbac.authorization.k8s.io

ClusterRoleBinding

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: read-nodes-global
subjects:
- kind: Group
  name: ops-team
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: ClusterRole
  name: node-reader
  apiGroup: rbac.authorization.k8s.io

👤 ServiceAccount

기본 사용

# ServiceAccount 생성
kubectl create serviceaccount my-sa -n default

# Pod에 연결
kubectl run my-pod --image=nginx --serviceaccount=my-sa

토큰 생성 (Kubernetes 1.24+)

# 토큰 생성
kubectl create token my-sa -n default

# Secret에 저장 (레거시)
kubectl apply -f - <<EOF
apiVersion: v1
kind: Secret
metadata:
  name: my-sa-token
  annotations:
    kubernetes.io/service-account.name: my-sa
type: kubernetes.io/service-account-token
EOF

🛡️ 보안 모범 사례

1. 최소 권한 원칙

# ❌ 나쁜 예: 모든 것에 모든 권한
rules:
- apiGroups: ["*"]
  resources: ["*"]
  verbs: ["*"]

# ✅ 좋은 예: 필요한 것만
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "list"]

2. 네임스페이스 격리

# 개발자는 development 네임스페이스만
kind: Role
metadata:
  namespace: development
  name: developer

3. 서비스 어카운트 제한

# 자동 마운트 비활성화
apiVersion: v1
kind: ServiceAccount
metadata:
  name: my-sa
automountServiceAccountToken: false

📋 체크리스트

  • Role/ClusterRole 차이 이해
  • 리소스와 동작(verbs) 매핑
  • Binding으로 사용자/SA 연결
  • ServiceAccount 토큰 관리
  • 최소 권한 원칙 적용
  • 권한 테스트 (auth can-i)

🔗 관련 노트

  • 02-10-ServiceAccount — Pod 권한
  • 02-04-NetworkPolicy — TLS, mTLS
  • 03-03-보안-하드닝 — 전체 보안

마지막 업데이트: 2026-06-01