Exercise
Create a CustomResourceDefinition named
applications.company.comthat defines an Application resource with the following properties:- Group:
company.com - Kind:
Application - Plural:
applications - Singular:
application - Short names:
app,apps - Scope:
Namespaced
- Group:
The Application CRD should have a schema with these fields under
spec:name(string, required)version(string, required)replicas(integer, minimum: 1, maximum: 10)
Apply the CRD to your cluster and verify it’s been created
Check that Kubernetes recognizes the new Application resource type
Create an Application resource named
webappwith the following specifications:- name: “my-web-app”
- version: “1.2.0”
- replicas: 3
List all Application resources and get the details of the
webappresourceUpdate the
webappresource to change the version to “1.3.0”Delete the Application resource and the CRD
Documentation
https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/
Solution
- Create a CustomResourceDefinition named
applications.company.com
This CRD is defined in the following specification.
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: applications.company.com
spec:
group: company.com
scope: Namespaced
names:
kind: Application
plural: applications
singular: application
shortNames:
- app
- apps
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
required:
- name
- version
properties:
name:
type: string
version:
type: string
replicas:
type: integer
minimum: 1
maximum: 102-3. Apply the CRD to your cluster and verify it’s been created
kubectl apply -f application-crd.yaml
kubectl get crd applications.company.com- Check that Kubernetes recognizes the new Application resource type
$ k api-resources | grep application
applications app,apps company.com/v1 true Application- Create an Application resource named
webapp
Create the following specification:
apiVersion: company.com/v1
kind: Application
metadata:
name: webapp
spec:
name: "my-web-app"
version: "1.2.0"
replicas: 3Apply it:
$ kubectl apply -f webapp.yaml
application.company.com/webapp created- List all Application resources and get the details of the
webappresource
kubectl get applications
kubectl get application webapp -o yamlYou can also use the shortname app as defined in the CRD.:
kubectl get apps
kubectl describe app webapp- Update the
webappresource to change the version to “1.3.0”
kubectl patch application webapp --type='merge' -p='{"spec":{"version":"1.3.0"}}'Or you can edit directly:
kubectl edit application webappThen, verify the change is taken into account:
kubectl get application webapp -o yaml- Delete the Application resource and the CRD
kubectl delete application webapp
kubectl delete crd applications.company.com