MSA/Part4. Ch.7 Kubernetes Custom 관리 방법

02. Custom Resource Definition(CRD) 소개

Engineer-Lee 2023. 2. 8. 01:04
반응형

https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/

 

Extend the Kubernetes API with CustomResourceDefinitions

This page shows how to install a custom resource into the Kubernetes API by creating a CustomResourceDefinition. Before you begin You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster.

kubernetes.io

 

1. Custom Resource 소개

• Kubernetes에서는 Object를 직접 커스텀 정의해 사용할 수 있음
• 소스코드를 따로 수정하지 않고 API를 확장해 사용할 수 있는 인터페이스를 제공
• Custom Resource를 이용하여 Kubernetes Object를 확장 가능

apiVersion: v1
kind: List
items:
- apiVersion: "extension.example.com/v1"
  kind: test
  metadata:
    name: test-custom-resource

 

2. Custom Resource Definition(CRD)이란?

• Custom Resource를 Kubernetes etcd에 등록하기 위해서는
  CustomResourceDefinition(이하 CRD)를 사용
• CRD용 Manifest yaml 파일을 작성해놓으면 Custom Resource를 등록할 수 있음

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: test.extension.example.com
spec:
  group: extension.example.com
  versions:
    - name: v1
      # Each version can be enabled/disabled by Served flag.
      served: true
      # One and only one version must be marked as the storage version.
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            test:
              type: array
              items:
                type: string
  scope: Namespaced
  names:
    plural: test
    singular: test
    kind: test

 

3. CRD 생성

(1) CRD 생성 Manifest 경로 및 생성 명령어
- Chapter07 > Ch07_02-crd
$ kubectl apply -f test-crd.yaml


(2) CRD 생성 확인 명령어
$ kubectl get crd


(3) CRD 상세 설명 출력 명령어
$ kubectl explain test

 

4. Custom Resource 생성

(1) Custom Resource 생성 Manifest 경로 및 생성 명령어
- Chapter07 > Ch07_02-crd
$ kubectl apply -f test-custom-resource.yaml


(2) 생성된 Custom Resource 확인 명령어
$ kubectl get test

 

 

5. Custom Resource와 CRD 삭제

$ kubectl delete -f test-custom-resource.yaml

$ kubectl delete -f test-crd.yaml

 

 

이는 지금 반쪽자리 object로 operator를 생성해서 실제 특정 동작을 하는 리소스를 만들어야 한다. 

반응형