본문 바로가기
Compute/kubernetis

[따배씨] 27. ServiceAccount Role Binding / CKA 시험 문제 학습

by 조청유곽 2025. 2. 1.
반응형

이 포스팅은 아래의 유튜브 채널 "따배"를 따라서 학습한 내용입니다.  

 

 

[관련 이론]

 

 

- Kubernetes에서 service accout란? 

Kubernetes에서 **Service Account(서비스 계정)**는 Kubernetes 리소스에 접근할 수 있는 
Pod 또는 애플리케이션을 위한 특별한 계정입니다. 
일반적으로 사용자가 아닌 클러스터 내부의 워크로드(Pod, 컨테이너 등)가 Kubernetes API에 
접근할 때 사용됩니다.

 

- Service Account의 역할

Kubernetes API에 접근하기 위한 인증(Authentication) 수단을 제공합니다.
RBAC(Role-Based Access Control) 규칙을 통해 **권한 부여(Authorization)**를 설정할 수 있습니다.
기본적으로 모든 Pod는 하나의 Service Account를 사용하며, Pod 내에서 해당 계정의 토큰을 통해 
Kubernetes API에 접근할 수 있습니다.

 

- Service Account의 주요 특징

Pod와 연계:
Pod가 실행될 때, Pod에 연결된 Service Account의 인증 토큰이 자동으로 마운트됩니다.
Pod 내 애플리케이션이 이 토큰을 사용하여 Kubernetes API에 접근할 수 있습니다.

기본 Service Account:
네임스페이스마다 기본으로 default라는 이름의 Service Account가 생성됩니다.
Pod를 생성할 때 특정 Service Account를 지정하지 않으면 기본적으로 default Service Account가 
사용됩니다.

RBAC과 연계:
Service Account의 권한은 Role 또는 ClusterRole과 RoleBinding/ClusterRoleBinding을 통해 
관리됩니다.
Service Account마다 접근 권한을 세분화하여 보안 수준을 높일 수 있습니다.

 

 

[Precondition]

(1) 테스트 환경

(1.1) Rocky Linux Cluster 

: 직접 구성

[root@k8s-master ~]# k get nodes -o wide
NAME         STATUS   ROLES           AGE   VERSION   INTERNAL-IP     EXTERNAL-IP   OS-IMAGE                            KERNEL-VERSION                  CONTAINER-RUNTIME
k8s-master   Ready    control-plane   30d   v1.27.2   192.168.56.30   <none>        Rocky Linux 8.10 (Green Obsidian)   4.18.0-553.33.1.el8_10.x86_64   containerd://1.6.32
k8s-node1    Ready    <none>          30d   v1.27.2   192.168.56.31   <none>        Rocky Linux 8.8 (Green Obsidian)    4.18.0-477.10.1.el8_8.x86_64    containerd://1.6.21
k8s-node2    Ready    <none>          30d   v1.27.2   192.168.56.32   <none>        Rocky Linux 8.8 (Green Obsidian)    4.18.0-477.10.1.el8_8.x86_64    containerd://1.6.21
[root@k8s-master ~]#

 

(1.2) Ubuntu Cluster 

: kodekloud 테스트 환경 활용

controlplane ~ ➜  kubectl get nodes -o wide
NAME           STATUS   ROLES           AGE     VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION   CONTAINER-RUNTIME
controlplane   Ready    control-plane   9m6s    v1.31.0   192.6.94.6    <none>        Ubuntu 22.04.4 LTS   5.4.0-1106-gcp   containerd://1.6.26
node01         Ready    <none>          8m31s   v1.31.0   192.6.94.9    <none>        Ubuntu 22.04.4 LTS   5.4.0-1106-gcp   containerd://1.6.26

https://learn.kodekloud.com/user/courses/udemy-labs-certified-kubernetes-administrator-with-practice-tests

 

(2) 사전 필요 설정 

   : N/A


 

 

[Question]

Cluster: kubectl config use-context k8s

Create the ServiceAccount named pod-access in a new namespace called apps.

Create a Role with the name pod-role, and the RoleBinding named pod-rolebinding.

Map the Service Account from the previous step to the API resources Pods with 
the operations watch, list, get.

 

 

[Solve]

(1) namespace 및 service account 생성 

[root@k8s-master ~]# k create namespace apps

[root@k8s-master ~]# k create serviceaccount pod-access -n apps --dry-run=client -o yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  creationTimestamp: null
  name: pod-access
  namespace: apps
[root@k8s-master ~]# 
[root@k8s-master ~]# k create serviceaccount pod-access -n apps 
serviceaccount/pod-access created
[root@k8s-master ~]#

 

 

(2) Role 생성 

[root@k8s-master ~]# kubectl create role pod-role --verb=get --verb=list --verb=watch --resource=pods -n apps
role.rbac.authorization.k8s.io/pod-role created
[root@k8s-master ~]# 
[root@k8s-master ~]# k get role -n apps pod-role 
NAME       CREATED AT
pod-role   2025-01-25T23:16:50Z
[root@k8s-master ~]# 
[root@k8s-master ~]# k describe role -n apps pod-role 
Name:         pod-role
Labels:       <none>
Annotations:  <none>
PolicyRule:
  Resources  Non-Resource URLs  Resource Names  Verbs
  ---------  -----------------  --------------  -----
  pods       []                 []              [get list watch]
[root@k8s-master ~]#

 

 

(3) Role binding 생성 

[root@k8s-master ~]# kubectl create rolebinding pod-rolebinding --role=pod-role --serviceaccount=apps:pod-access --namespace=apps
rolebinding.rbac.authorization.k8s.io/pod-rolebinding created
[root@k8s-master ~]#        
[root@k8s-master ~]# k get rolebindings.rbac.authorization.k8s.io -n apps pod-rolebinding 
NAME              ROLE            AGE
pod-rolebinding   Role/pod-role   38s
[root@k8s-master ~]#
[root@k8s-master ~]# k describe rolebindings.rbac.authorization.k8s.io -n apps pod-rolebinding 
Name:         pod-rolebinding
Labels:       <none>
Annotations:  <none>
Role:
  Kind:  Role
  Name:  pod-role
Subjects:
  Kind            Name        Namespace
  ----            ----        ---------
  ServiceAccount  pod-access  apps
[root@k8s-master ~]#


 

 

[사용 커맨드 정리]

k create namespace apps

k create serviceaccount pod-access -n apps 

 

kubectl create role pod-role --verb=get --verb=list --verb=watch --resource=pods -n apps

k get role -n apps pod-role 

k describe role -n apps pod-role 

 

k get rolebindings.rbac.authorization.k8s.io -n apps pod-rolebinding

k describe rolebindings.rbac.authorization.k8s.io -n apps pod-rolebinding 

 

 

반응형