본문 바로가기

Infra/Kubernetes

Kubernetes Role binding 설정

몇 주전에 쿠버네티스를 설치만 하고 별다른 공부를 안하고 있었는데, 최근 공부를 해야할 필요성을 느껴서 당분간은 쿠버네티스에 관해서 글을 작성할 예정이다.

 

우선 쿠버네티스 설치 과정에 대한건 추후에 올리도록 하겠다.

지금은 쿠버네티스 클러스터 롤 바인딩 실습을 해보려 한다.

 

Kubernetes RBAC(Role Based Access Control)

쿠버네티스 RBAC는 클러스터 내의 작업 및 리소스에 대한 접근을 관리할 수 있다.

 

1) Role : 특정 네임스페이스에서 사용할 수 있는 권한과 규칙을 정의하며, 리소스(파드, 서비스) 및 작업(조회, 생성)에 대한 권한을 부여할 수 있다.

2) ClusterRole : 클러스터 수준에서 사용할 수 있는 권한과 규칙을 정의하며, 모든 네임스페이스에서 사용할 수 있으며 클러스터 전체에서 리소스 및 작업에 대한 권한을 부여할 수 있다.

3) Role Binding : 특정 사용자, 그룹, 서비스 계정에 Role을 매핑할 수 있다.네임스페이스 범위 내에서만 동작하며, Role과 동일한 네임스페이스에 존재해야 한다.

4) ClusterRole Binding : 특정 사용자, 그룹, 서비스 계정에 ClusterRole을 매핑할 수 있다. 모든 네임스페이스의 리소스에 대한 권한도 부여할 수 있다.(Role Binding 보다 더 큰 범위)

 

Kubernetes Role Binding 실습

실습 중 yaml 파일들에 대해서 생성 후 kubectl apply -f [yaml 파일명] 명령어로 꼭 설정이 적용되도록 하자!!

 

1. 디플로이먼트 생성(nginx-deployment.yaml)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-server
  labels:
    app: server
spec:
  replicas: 3
  selector:
    matchLabels:
      app: server
  template:
    metadata:
      name: nginx-server
      labels:
        app: server
    spec:
      containers:
        - name: server
          image: nginx:1.23.3
          ports:
            - containerPort: 80

 

2. Role 생성(rbac-role.yaml)

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  namespace: default
  name: role-dev
rules:
- apiGroups: ["", "apps"]
  resources: ["pods", "deployments"]
  verbs: ["get", "list", "edit"]

 

3. 사용자 생성(serviceaccount-dev01.yaml)

apiVersion: v1
kind: ServiceAccount
metadata:
  name: dev01
  namespace: default

 

서비스 계정 생성 후 kubectl get serviceaccount 명령어로 생성된 계정을 확인 할 수 있다.

 

4. 서비스 토큰 생성(serviceaccount-dev01-secret.yaml)

-> 쿠버네티스 1.24 버전 이전까지는 서비스 계정을 생성하면 자동으로 토큰이 발급되었는데, 보안 상의 이유로 1.24버전 이후부터는 토큰을 자동으로 생성하지 않아 별도로 Secret 오브젝트를 생성해야 한다.

apiVersion: v1
kind: Secret
metadata:
  name: dev01-secret
  namespace: default
  annotations:
    kubernetes.io/service-account.name: dev01
type: kubernetes.io/service-account-token

 

토큰 발급 후 kubectl get secret 명령어로 발급된 토큰 확인 가능

 

서비스 계정(dev01) 에 할당된 토큰에 대해 자세하게 알려면 kubectl describe secret dev01 명령어를 실행하면 되며, 토큰 값은 eyJh~~~~ 로 시작하는 부분이다.

 

5. kubectl config set-credentials [생성한 계정명] --token=[생성된 token 값]

 

6. 클러스터명 확인하기

kubectl config get-clusters

 

7. 클러스터와 사용자간 연결을 설정하는 context

kubectl config set-context [생성할 context명] --cluster=[확인한 클러스터명] --user=[생성한 계정명]

 

필자는 기존에 만들어둬서 context를 확인하는 걸로 대체한다.

 

8. Role Binding 생성(rbac-role-binding.yaml)

kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: dev-rolebinding
  namespace: default
subjects:
- kind: ServiceAccount
  name: dev01
  apiGroup: ""
roleRef:
  kind: Role
  name: role-dev
  apiGroup: rbac.authorization.k8s.io

 

9. context 확인 및 전환

위에서 미리 보여준 kubectl config get-contexts 명령어로 context를 확인해보자.

kubectl config use-context dev-user 명령어는 dev-user context로 전환한다는 명령어이다.

 

10. Role에 명시된 작업이 동작하는지 확인

-> 아래 그림과 같이 리소스(pod, deployment)에 대해서 작업(get)이 동작하지만, endpoint(ep)에 대한 리소스는 명시하지 않았기 때문에 endpoint에 대해서는 거부되는 걸 확인할 수 있다.

 

참고 블로그

https://t-okk.tistory.com/226

 

[K8S] Kubernetes 구축 - 3. RBAC 권한 설정(role 생성, role binding)

Kubernetes RBAC(역할 기반 접근통제) * RBAC(역할 기반 접근통제, Role-based Access Control) - Kubernetes 클러스터 내 사용자가 수행할 수 있는 작업을 제어하는 방법이다. - 클러스터 리소스 및 작업에 대한 접

t-okk.tistory.com

https://velog.io/@rockwellvinca/kubernetes-RBAC

 

[kubernetes] 쿠버네티스에서 권한을 부여하는 방법 (RBAC)

Kubernetes (k8s)의 Role-Based Access Control (RBAC)은 사용자, 그룹 또는 서비스 계정에 클러스터 내의 리소스에 대한 접근을 제어하는 방법이다.쿠버네티스 - RedHat

velog.io