[kubernetes-실습] CRD (Custom Resource Definition)

# Custom Resource Definition 생성

# CRD 생성을 위한 yaml 파일 작성
ps0107@k8smaster1:~$ vi crd.yaml 
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: crontabs.ps.example.com
spec:
  scope: Cluster
  group: ps.example.com
  version: v1
  names:
    kind: CronTab
    plural: crontabs
    singular: crontab
    shortNames:
    - ct 

# CRD 생성
ps0107@k8smaster1:~$ kubectl create -f crd.yaml                                                                                      customresourcedefinition.apiextensions.k8s.io/crontabs.ps.example.com created

# 생성 확인
ps0107@k8smaster1:~$ kubectl get crd
NAME                                          CREATED AT
bgpconfigurations.crd.projectcalico.org       2020-01-28T08:33:18Z
bgppeers.crd.projectcalico.org                2020-01-28T08:33:18Z
clusterinformations.crd.projectcalico.org     2020-01-28T08:33:18Z
crontabs.ps.example.com                       2020-02-06T06:14:19Z
felixconfigurations.crd.projectcalico.org     2020-01-28T08:33:18Z
globalnetworkpolicies.crd.projectcalico.org   2020-01-28T08:33:18Z
globalnetworksets.crd.projectcalico.org       2020-01-28T08:33:18Z
hostendpoints.crd.projectcalico.org           2020-01-28T08:33:18Z
ippools.crd.projectcalico.org                 2020-01-28T08:33:18Z
networkpolicies.crd.projectcalico.org         2020-01-28T08:33:18Z

# 생성된 객체 자세한 정보 확인
ps0107@k8smaster1:~$ kubectl describe crd crontabs.ps.example.com
Name:         crontabs.ps.example.com
Namespace:    
Labels:       <none>
Annotations:  <none>
API Version:  apiextensions.k8s.io/v1beta1
Kind:         CustomResourceDefinition
Metadata:
  Creation Timestamp:  2020-02-06T06:14:19Z
  Generation:          1
  Resource Version:    1033756
  Self Link:           /apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions/crontabs.ps.example.com
  UID:                 b8c8a2a4-5e76-4471-8d17-e9c36a24b4c1
Spec:
  Conversion:
    Strategy:  None
  Group:       ps.example.com
  Names:
    Kind:       CronTab
    List Kind:  CronTabList
    Plural:     crontabs
    Short Names:
      ct
    Singular:               crontab
  Preserve Unknown Fields:  true
  Scope:                    Cluster
  Version:                  v1
  Versions:
    Name:     v1
    Served:   true
    Storage:  true
Status:
  Accepted Names:
    Kind:       CronTab
    List Kind:  CronTabList
    Plural:     crontabs
    Short Names:
      ct
    Singular:  crontab
  Conditions:
    Last Transition Time:  2020-02-06T06:14:19Z
    Message:               no conflicts found
    Reason:                NoConflicts
    Status:                True
    Type:                  NamesAccepted
    Last Transition Time:  <nil>
    Message:               the initial names have been accepted
    Reason:                InitialNamesAccepted
    Status:                True
    Type:                  Established
  Stored Versions:
    v1
Events:  <none>

# CRD를 이용한 새로운 객체 생성
ps0107@k8smaster1:~$ vi new-crontab.yaml 
apiVersion: "ps.example.com/v1"
kind: CronTab
metadata:
  name: new-cron-obj
spec:
  cronSpec: "*/5 * * * *"
  image: example-cron-image

# 객체 생성
ps0107@k8smaster1:~$ kubectl create -f new-crontab.yaml 
crontab.ps.example.com/new-cron-obj created

# 생성된 객체 확인
ps0107@k8smaster1:~$ kubectl get CronTab
NAME           AGE
new-cron-obj   13s

# shortName으로도 확인
ps0107@k8smaster1:~$ kubectl get ct
NAME           AGE
new-cron-obj   22s

ps0107@k8smaster1:~$ kubectl describe ct
Name:         new-cron-obj
Namespace:    
Labels:       <none>
Annotations:  <none>
API Version:  ps.example.com/v1
Kind:         CronTab
Metadata:
  Creation Timestamp:  2020-02-06T06:15:03Z
  Generation:          1
  Resource Version:    1033816
  Self Link:           /apis/ps.example.com/v1/crontabs/new-cron-obj
  UID:                 7569c09f-8ceb-4eba-91ce-04dbf835c878
Spec:
  Cron Spec:  */5 * * * *
  Image:      example-cron-image
Events:       <none>

# CRD 삭제
ps0107@k8smaster1:~$ kubectl delete -f crd.yaml 
customresourcedefinition.apiextensions.k8s.io "crontabs.ps.example.com" deleted

# 삭제 후 shortName이 안되는걸 확인 가능하다.
ps0107@k8smaster1:~$ kubectl get ct
Error from server (NotFound): Unable to list "ps.example.com/v1, Resource=crontabs": the server could not find the requested resource (get crontabs.ps.example.com)