쿠버네티스

Kubernetes : CRD

NickTop 2025. 3. 9. 00:13

CR

Custom Resource로 사용자가 쿠버네티스에 추가하는 리소스입니다

CRD란

CRD(CustomResourceDefinition)는 CR을 만드는 방법 중 하나로 어떤 형태의 사용자 정의 리소스를 만들지 정의하는 부분입니다

 

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: students.example.com        # plural이름.API그룹으로 작성
spec:
  group: example.com                # API 그룹 이름
  versions:
    - name: v1                      # 버전
      served: true                  # 이 버전을 쿠버네티스 API에서 제공할지 여부
      storage: true                 # etcd 저장 시 이 버전을 기본으로 사용할지 여부 / 
      schema:                       # CRD의 필드 정의
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                name:
                  type: string
                age:
                  type: integer
                major:
                  type: string
              required:
                - name
                - age
  scope: Namespaced                 # 리소스를 Namespace 단위로 관리할지 여부 (Cluster로 하면 모든 Namspace가 공유)
  names:
    plural: students                # 리소스 복수형 이름 (kubectl에서 사용)
    singular: student               # 리소스 단수형 이름
    kind: Student                   # kind 이름
    shortNames:
      - std                         # 짧은 이름으로 사용 (kubectl 명령어 간소화)

 

root@controlplane ~ ➜  kubectl apply -f student-crd.yaml
customresourcedefinition.apiextensions.k8s.io/students.example.com created

 

이제 custom resource를 만들 수 있다

apiVersion: example.com/v1
kind: Student
metadata:
  name: hong-gildong
spec:
  name: "홍길동"
  age: 22
  major: "컴퓨터공학"

 

root@controlplane ~ ➜  kubectl apply -f student.yaml 
student.example.com/hong-gildong created

root@controlplane ~ ➜  kubectl get std
NAME           AGE
hong-gildong   14s

 

하지만, 단순히 custom resource를 만들었다고 해서 데이터 저장외에 아무런 기능이 없다. 왜냐하면 controller-manager에 student라는 resource를 관리하는 controller가 없기 때문이다.

따라서 custom controller를 정의해야 합니다.

kubebuilder를 통해 go언어로 코드를 작성하여 controller를 배포할 수 있습니다. (본 포스팅에서는 제외하겠습니다)

 

VPA

CRD를 활용하여 resource를 만드는 가장 좋은 예시는 VPA(VerticalPodAutoscaler)입니다

HPA(HorizontalPodAutoscaler)는 기본으로 제공되지만 VPA는 기본 제공이 아닙니다

Git에서 설치하여 쓸 수 있습니다 (https://github.com/kubernetes/autoscaler.git)

git clone https://github.com/kubernetes/autoscaler.git
cd autoscaler/vertical-pod-autoscaler

 

deploy/vpa-v1-crd.yaml 경로에 CRD가 정의되어있습니다

 

스크립트로 설치하고 확인해봅시다

./hack/vpa-up.sh

 

root@controlplane ~ ➜  kubectl get crd verticalpodautoscalers.autoscaling.k8s.io
NAME                                        CREATED AT
verticalpodautoscalers.autoscaling.k8s.io   2025-03-08T15:11:42Z



'쿠버네티스' 카테고리의 다른 글

2025 CKA 자격증 합격 후기  (0) 2025.03.19
Kubernetes : RBAC (role/rolebinding/csr/serviceAccount)  (0) 2025.03.09
Kubernetes : Kustomize  (0) 2025.03.05
Kubernetes : Helm  (0) 2025.03.04
Kubernetes : Network  (0) 2025.03.03