[따배쿠] Custom Resource
Custom Resource
Custom Resource는 기본적으로 제공되는 리소스(예: Pod, Service, Deployment 등) 외에 사용자가 정의한 리소스
즉, Custom Resource Definition 파일에 리소스들의 필요 spec을 정의하고
Custom Resource 파일을 정의하여, 관리자가 원하는 리소스를 생성할 수 있다.
실습
# Custom Resource Definition 파일 생성
root@master:~# vi resourcedefinition.yaml
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
# name must match the spec fields below, and be in the form: <plural>.<group>
name: crontabs.stable.example.com
spec:
# group name to use for REST API: /apis/<group>/<version>
group: stable.example.com
# list of versions supported by this CustomResourceDefinition
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:
spec:
type: object
properties:
cronSpec:
type: string
image:
type: string
replicas:
type: integer
# either Namespaced or Cluster
scope: Namespaced
names:
# plural name to be used in the URL: /apis/<group>/<version>/<plural>
plural: crontabs
# singular name to be used as an alias on the CLI and for display
singular: crontab
# kind is normally the CamelCased singular type. Your resource manifests use this.
kind: CronTab
# shortNames allow shorter string to match your resource on the CLI
shortNames:
- ct
# 생성
root@master:~# kubectl apply -f resourcedefinition.yaml
customresourcedefinition.apiextensions.k8s.io/crontabs.stable.example.com created
# 확인
root@master:~# kubectl api-resources
NAME SHORTNAMES APIVERSION NAMESPACED KIND
...
crontabs ct stable.example.com/v1 true CronTab
...
group : stable.example.com/v1
resource : crontabs가 생성되었다.
# Custom Object 파일 생성
root@master:~# cat > my-crontab.yaml
apiVersion: "stable.example.com/v1"
kind: CronTab
metadata:
name: my-new-cron-object
spec:
cronSpec: "* * * * */5"
image: my-awesome-cron-imager
# 생성
root@master:~# kubectl apply -f my-crontab.yaml
crontab.stable.example.com/my-new-cron-object created
# 확인
root@master:~# kubectl get crontabs.stable.example.com
NAME AGE
my-new-cron-object 14s
# 기존 Custom Object 파일 복사
root@master:~# cp my-crontab.yaml my-crontab2.yaml
my-crontab > my-crontab2
# 새로운 Custom Object 파일 생성
root@master:~# vi my-crontab2.yaml
apiVersion: "stable.example.com/v1"
kind: CronTab
metadata:
name: my-second-cron-object
namespace: kube-system
spec:
cronSpec: "* * * * */5"
image: my-awesome-cron-image
replicas: 2
# 생성
root@master:~# kubectl apply -f my-crontab2.yaml
crontab.stable.example.com/my-second-cron-object created
# 확인
root@master:~# kubectl get crontab -n kube-system
NAME AGE
my-second-cron-object 33s
# 삭제
root@master:~# kubectl delete -f resourcedefinition.yaml
customresourcedefinition.apiextensions.k8s.io "crontabs.stable.example.com" deleted
custom resource definition을 삭제하게 되면, 그에 종속된 리소스들도 삭제되게 된다.
[참고 영상]
https://www.youtube.com/watch?v=yAcnjammQV4&list=PLApuRlvrZKohLYdvfX-UEFYTE7kfnnY36&index=12
[참고 문서]
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