[CKS] 1-14.RBAC
쿠버네티스 RBAC는 역할 기반으로 사용자 권한을 세밀하게 제어하여 클러스터 보안을 강화하고 안정적인 리소스 관리를 지원합니다.
개요
RBAC를 사용하면 특정 권한이 있는 역할을 정의하고 해당 역할을 사용자 또는 그룹에 연결하여 클러스터 내에서 안전하고 통제된 접근을 보장할 수 있습니다.
역할 생성, 사용자 연결, 구성 확인, 특정 리소스에 대한 접근 제한 등의 과정을 살펴보겠습니다.
Creating a Role
Role은 YAML 파일에 정의됩니다.
- apiVersion:
rbac.authorization.k8s.io/v1
.
- metadata.name:
- The name of your role (e.g., "developer")
- rules:
- 허용되는 API 그룹, 리소스, verb 작성
- core group에 경우 비워둘 수 있습니다.
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: developer
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["list", "get", "create", "update", "delete"]
- apiGroups: [""]
resources: ["ConfigMap"]
verbs: ["create"]
역할과 역할 바인딩은 네임스페이스로 구성됩니다. 위 예에서 메타데이터에 별도로 지정하지 않는 한, 역할은 기본 네임스페이스에 생성됩니다.
Role binding
역할에 정의된 권한을 사용자에게 부여하려면 역할 바인딩을 생성해야 합니다.
역할 바인딩은 사용자(또는 그룹)를 역할에 연결합니다.
- subjects:
- 권한을 부여할 사용자, 그룹, Service Account
- roleRef:
- 이전에 생성된 역할에 대한 참조
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: devuser-developer-binding
subjects:
- kind: User
name: dev-user
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: developer
apiGroup: rbac.authorization.k8s.io
권한 확인
kubectl auth can-i
kubectl auth can-i create deployments
# 계정을 전환하지 않고 다른 사용자(예: "dev-user")의 권한을 확인하려면 --as
kubectl auth can-i create deployments --as=dev-user
특정 리소스 제한
쿠버네티스의 RBAC를 사용하면 권한을 세부적으로 조정할 수 있습니다.
리소스 타입에 모든 권한을 부여하는 대신 resourceNames
필드를 사용하여 특정 리소스에만 권한을 부여할 수 있습니다. 예를 들어, 사용자가 blue와 orange라는 이름의 파드와만 상호 작용할 수 있도록 하려면 다음과 같이 역할을 정의합니다.
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: developer
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "create", "update"]
resourceNames: ["blue", "orange"]
- https://notes.kodekloud.com/docs/Certified-Kubernetes-Security-Specialist-CKS/Cluster-Setup-and-Hardening/RBAC
- https://learn.kodekloud.com/user/courses/certified-kubernetes-security-specialist-cks/module/eac6dac8-4481-4138-96ef-a2135f20e05e/lesson/250ed868-3d01-4c71-bd30-ec82841a7538?autoplay=true
Hands-on
- Resource Names가 kube-proxy인 configmap만 가져올 수 있음
controlplane /etc/kubernetes/manifests ➜ k describe role kube-proxy -n kube-system
Name: kube-proxy
Labels: <none>
Annotations: <none>
PolicyRule:
Resources Non-Resource URLs Resource Names Verbs
--------- ----------------- -------------- -----
configmaps [] [kube-proxy] [get]
- api groups 보는 방법
$ kubectl api-resources
NAME SHORTNAMES APIVERSION NAMESPACED KIND
bindings v1 true Binding
componentstatuses cs v1 false ComponentStatus
configmaps cm v1 true ConfigMap
endpoints ep v1 true Endpoints
events ev v1 true Event
limitranges limits v1 true LimitRange
namespaces ns v1 false Namespace
nodes no v1 false Node
persistentvolumeclaims pvc v1 true PersistentVolumeClaim
persistentvolumes pv v1 false PersistentVolume
pods po v1 true Pod
podtemplates v1 true PodTemplate
replicationcontrollers rc v1 true ReplicationController
resourcequotas quota v1 true ResourceQuota
secrets v1 true Secret
serviceaccounts sa v1 true ServiceAccount
services svc v1 true Service
mutatingwebhookconfigurations admissionregistration.k8s.io/v1 false MutatingWebhookConfiguration
validatingadmissionpolicies admissionregistration.k8s.io/v1 false ValidatingAdmissionPolicy
validatingadmissionpolicybindings admissionregistration.k8s.io/v1 false ValidatingAdmissionPolicyBinding