Kubernetes/Trouble Shooting

🛠️ K8S Trouble Shooting : RBAC 및 인증 관련 오류 해결하기

ygtoken 2025. 3. 2. 19:48
728x90

 

Kubernetes에서는 보안 및 권한 관리를 위해 Role-Based Access Control(RBAC)을 사용합니다.

하지만 권한 부족, 잘못된 서비스 계정 설정, 인증서 문제로 인해 여러 가지 오류가 발생할 수 있습니다.

이 글에서는 Kubernetes 환경에서 발생할 수 있는 대표적인 RBAC 및 인증 오류 5가지를 정리하고, 각각의 원인과 해결 방법을 설명합니다. 🚀

 


 

1️⃣ “Forbidden: pods is forbidden: User…”

 

🔎 오류 설명

 

이 오류는 사용자가 특정 리소스에 대한 액세스 권한이 없을 때 발생합니다.

주로 RBAC(Role-Based Access Control) 설정 문제로 인해 발생합니다.

 

🔧 해결 방법

 

1) 현재 사용자의 권한 확인

 

사용자가 특정 리소스에 대해 수행할 수 있는 동작을 확인합니다.

kubectl auth can-i list pods

 

네임스페이스에서 특정 리소스를 조회하려면 다음 명령어를 사용합니다.

kubectl auth can-i list pods -n <namespace>

 

2) RBAC 역할(Role) 확인

 

현재 사용자가 속한 역할을 확인합니다.

kubectl get rolebinding -n <namespace>
kubectl get clusterrolebinding

 

3) 사용자에게 올바른 권한 부여

 

권한이 부족한 경우, RoleBinding 또는 ClusterRoleBinding을 추가해야 합니다.

kubectl create rolebinding <binding-name> --clusterrole=view --user=<user> -n <namespace>

 

 


 

2️⃣ “requested access to the resource is denied”

 

🔎 오류 설명

 

이 오류는 컨테이너가 프라이빗 레지스트리에서 이미지를 가져올 권한이 없을 때 발생합니다.

프라이빗 레지스트리를 사용하거나 인증이 필요한 경우 발생할 수 있습니다.

 

🔧 해결 방법

 

1) 프라이빗 레지스트리 인증 확인

 

프라이빗 레지스트리에 로그인합니다.

docker login <your-registry>

 

2) Kubernetes에서 imagePullSecrets 추가

 

프라이빗 레지스트리 인증을 위한 imagePullSecrets를 생성합니다.

kubectl create secret docker-registry myregistrykey \
  --docker-server=<your-registry> \
  --docker-username=<your-username> \
  --docker-password=<your-password> \
  --namespace=<namespace>

 

이후 Pod 또는 Deployment 설정에서 imagePullSecrets를 추가합니다.

spec:
  imagePullSecrets:
    - name: myregistrykey

 


 

3️⃣ “cannot create serviceaccount…”

 

🔎 오류 설명

 

이 오류는 Kubernetes에서 서비스 계정(ServiceAccount)을 생성할 수 없을 때 발생합니다.

RBAC 정책에 의해 서비스 계정 생성이 제한될 수 있습니다.

 

🔧 해결 방법

 

1) 서비스 계정 생성 권한 확인

 

현재 사용자가 서비스 계정을 생성할 수 있는지 확인합니다.

kubectl auth can-i create serviceaccount

 

2) 서비스 계정 생성 시도

 

새로운 서비스 계정을 생성합니다.

kubectl create serviceaccount <sa-name> -n <namespace>

 

3) RBAC 정책 수정

 

서비스 계정을 생성할 수 있도록 Role 또는 ClusterRole을 설정해야 합니다.

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: service-account-role
  namespace: <namespace>
rules:
  - apiGroups: [""]
    resources: ["serviceaccounts"]
    verbs: ["create", "get", "list"]

위 역할을 사용자에게 부여합니다.

kubectl create rolebinding sa-role-binding --role=service-account-role --serviceaccount=<namespace>:<sa-name> -n <namespace>

 

 


 

4️⃣ “service account cannot act as a user…”

 

🔎 오류 설명

 

이 오류는 서비스 계정이 특정 사용자처럼 동작하려 할 때 발생합니다.

RBAC 설정에 의해 특정 작업이 제한된 경우입니다.

 

🔧 해결 방법

 

1) 서비스 계정에 부여된 권한 확인

 

현재 서비스 계정이 어떤 권한을 가지고 있는지 확인합니다.

kubectl get rolebinding -n <namespace>
kubectl get clusterrolebinding

 

2) 서비스 계정에 적절한 권한 추가

 

해당 서비스 계정이 필요한 작업을 수행할 수 있도록 ClusterRoleBinding을 추가합니다.

kubectl create clusterrolebinding sa-cluster-role-binding \
  --clusterrole=cluster-admin \
  --serviceaccount=<namespace>:<sa-name>

 

3) 서비스 계정 사용 확인

 

서비스 계정을 사용하여 작업을 수행할 때, 올바르게 설정되었는지 확인합니다.

kubectl auth can-i '*' '*' --as=system:serviceaccount:<namespace>:<sa-name>

 


 

5️⃣ “operation not supported for token requests”

 

🔎 오류 설명

 

이 오류는 서비스 계정이 토큰 기반 요청을 수행할 권한이 없을 때 발생합니다.

주로 RBAC 정책이 올바르게 설정되지 않았거나, Kubernetes API 서버에서 제한된 작업을 수행하려 할 때 나타납니다.

 

🔧 해결 방법

 

1) 서비스 계정의 토큰 조회

 

해당 서비스 계정이 올바르게 토큰을 생성할 수 있는지 확인합니다.

kubectl describe serviceaccount <sa-name> -n <namespace>

 

2) 올바른 토큰 권한 부여

 

서비스 계정이 토큰을 요청할 수 있도록 ClusterRoleBinding을 설정합니다.

kubectl create clusterrolebinding token-role-binding \
  --clusterrole=system:service-account-issuer \
  --serviceaccount=<namespace>:<sa-name>

 

3) API 요청 수행 시 토큰 포함

 

서비스 계정을 사용하여 Kubernetes API에 접근할 때 올바른 토큰을 포함해야 합니다.

TOKEN=$(kubectl get secret $(kubectl get serviceaccount <sa-name> -o jsonpath='{.secrets[0].name}') -o jsonpath='{.data.token}' | base64 --decode)
curl -H "Authorization: Bearer $TOKEN" https://<kubernetes-api-server>/api/v1/namespaces

 


 

🎯 마무리

 

이 글에서는 Kubernetes에서 자주 발생하는 RBAC 및 인증 관련 오류 5가지와 그 해결 방법을 다뤘습니다.

 

✔️ 권한 부족 오류 (Forbidden: pods is forbidden: User…)

✔️ 이미지 풀 요청 거부 오류 (requested access to the resource is denied)

✔️ 서비스 계정 생성 오류 (cannot create serviceaccount…)

✔️ 서비스 계정의 사용자 권한 부족 오류 (service account cannot act as a user…)

✔️ 토큰 요청 제한 오류 (operation not supported for token requests)

 

만약 위 해결 방법으로도 문제가 해결되지 않는다면,

kubectl auth can-i, kubectl get rolebinding, kubectl describe serviceaccount 명령어 등을 활용하여 추가로 분석해보는 것이 좋습니다.

 

728x90