이 포스팅은 아래의 유튜브 채널 "따배"를 따라서 학습한 내용입니다.
[관련 이론]
- 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) 사전 필요 설정
: namespace "apps"가 만들어져 있어야 함
[root@k8s-master ~]# k create namespace apps
namespace/apps created
[Question]
- Create a new ClusterRole named deployment-clusterrole,
which only allows to create the following resource types: Deployment StatefulSet DaemonSet
- Create a new ServiceAccount named cicd-token in the existing in the existing namespace apps.
- Bind the new Cluster Role deployment-clusterrole to the new ServiceAccount cicd-token,
limited to the namespace apps.
[Solve]
(1) service account 생성
[root@k8s-master ~]# k create serviceaccount cicd-token -n apps
serviceaccount/cicd-token created
[root@k8s-master ~]#
(2) cluster role 생성
[root@k8s-master ~]# kubectl create clusterrole deployment-clusterrole --verb=create --resource=deployment,statefulset,daemonset -n apps
clusterrole.rbac.authorization.k8s.io/deployment-clusterrole created
(3) cluster role binding 생성
[root@k8s-master ~]# kubectl create clusterrolebinding deployment-clusterrole-binding --clusterrole=deployment-clusterrole --serviceaccount=apps:cicd-token -n apps
clusterrolebinding.rbac.authorization.k8s.io/deployment-clusterrole-binding created
[root@k8s-master ~]#
[root@k8s-master ~]# k get clusterrolebindings.rbac.authorization.k8s.io deployment-clusterrole-binding
NAME ROLE AGE
deployment-clusterrole-binding ClusterRole/deployment-clusterrole 14s
[root@k8s-master ~]# k describe clusterrolebindings.rbac.authorization.k8s.io deployment-clusterrole-binding
Name: deployment-clusterrole-binding
Labels: <none>
Annotations: <none>
Role:
Kind: ClusterRole
Name: deployment-clusterrole
Subjects:
Kind Name Namespace
---- ---- ---------
ServiceAccount cicd-token apps
[root@k8s-master ~]#
[root@k8s-master ~]# k describe clusterrole deployment-clusterrole
Name: deployment-clusterrole
Labels: <none>
Annotations: <none>
PolicyRule:
Resources Non-Resource URLs Resource Names Verbs
--------- ----------------- -------------- -----
daemonsets.apps [] [] [create]
deployments.apps [] [] [create]
statefulsets.apps [] [] [create]
[root@k8s-master ~]#
[사용 커맨드 정리]
k create serviceaccount cicd-token -n apps
kubectl create clusterrole deployment-clusterrole --verb=create --resource=deployment,statefulset,daemonset -n apps
kubectl create clusterrolebinding deployment-clusterrole-binding --clusterrole=deployment-clusterrole --serviceaccount=apps:cicd-token -n apps
k get clusterrolebindings.rbac.authorization.k8s.io deployment-clusterrole-binding
k describe clusterrole deployment-clusterrole
k describe clusterrolebindings.rbac.authorization.k8s.io deployment-clusterrole-binding
'Compute > kubernetis' 카테고리의 다른 글
[따배씨] 30. Network Policy / CKA 시험 문제 학습 (0) | 2025.02.01 |
---|---|
[따배씨] 29. Kube-DNS / CKA 시험 문제 학습 (0) | 2025.02.01 |
[따배씨] 27. ServiceAccount Role Binding / CKA 시험 문제 학습 (0) | 2025.02.01 |
[따배씨] 26. User Cluster Role Binding / CKA 시험 문제 학습 (0) | 2025.02.01 |
[따배씨] 25. User Role Binding / CKA 시험 문제 학습 (0) | 2025.02.01 |