Custom Resource Definitions in Kubernetes - The Kubernetes Workshop
Custom Resource Definitions in Kubernetes - The Kubernetes Workshop
Introduction
In previous chapters, we learned about different
Kubernetes objects, such as Pods, Deployments,
and ConfigMaps. These objects are defined and
managed by the Kubernetes API (that is, for
these objects, the API server manages their
creation and destruction, among other
operations). However, you may want to extend
the functions provided by Kubernetes to provide
a feature that is not shipped with standard
Kubernetes, and that cannot be enabled by the
built-in objects provided by Kubernetes.
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 1/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
What Is a Custom
Controller?
CRDs and CRs help you define the desired state
for your CRs. There is a need for a component
that makes sure that the state of the Kubernetes
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 2/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 3/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 4/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
kubectl api-resources
You should see the following response:
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 5/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
kubectl api-versions
You should see the following response:
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 6/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 7/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 8/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
Note
apiVersion:
"controllers.kube.book.au/v1"
kind: PodLifecycleConfig
metadata:
name: demo-pod-lifecycle
spec:
namespaceName: crddemo
podLiveForThisMinutes: 1
The preceding specification defines our target
object. As you can see, it looks just like normal
Kubernetes objects, but the specifications (the
spec section) are defined as per our
requirements. Let's dig a bit deeper into the
details.
apiVersion
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 9/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
kind
Note
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 10/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
spec
namespaceName and
podLiveForThisMinutes
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 11/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
pod-normaliser-crd.yaml
1 apiVersion:
apiextensions.k8s.io/v1beta1
2 kind: CustomResourceDefinition
3 metadata:
4 name:
podlifecycleconfigs.controllers.ku
be.book.au
5 spec:
6 group:
controllers.kube.book.au
7 version: v1
8 scope: Namespaced
9 names:
10 kind: PodLifecycleConfig
11 plural: podlifecycleconfigs
12 singular:
podlifecycleconfig
13 #1.15 preserveUnknownFields:
false
14 validation:
15 openAPIV3Schema:
16 type: object
17 properties:
18 spec:
19 type: object
20 properties:
21 namespaceName:
22 type: string
23 podLiveForThisMinut
es:
24 type: integer
Now, let's look at various components of this
CRD:
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 12/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 13/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 14/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
group:
controllers.kube.book.au
version: v1
scope: Namespaced
names:
kind: PodLifecycleConfig
plural: podlifecycleconfigs
singular: podlifecycleconfig
#1.15 preserveUnknownFields:
false
validation:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
namespaceName:
type: string
podLiveForThisMinute
s:
type: integer
3. Using the definition from the previous step,
create the CRD using the following command:
kubectl create -f pod-
normaliser-crd.yaml -n crddemo
You should see the following response:
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 15/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
Note
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 17/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
Note
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 18/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 19/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 20/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
types.go
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 21/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
26 }
...
32 type PodLifecycleConfigList
struct {
33 meta_v1.TypeMeta
`json:",inline"`
34 meta_v1.ListMeta
`json:"metadata"`
35
36 Items []PodLifecycleConfig
`json:"items"`
37 }
You can find the complete code at this link:
https://packt.live/3jXky9G.
apiVersion:
"controllers.kube.book.au/v1"
kind: PodLifecycleConfig
metadata:
name: demo-pod-lifecycle
# namespace: "crddemo"
spec:
namespaceName: crddemo
podLiveForThisMinutes: 1
Note that in types.go, we have defined objects
that can hold the full definition of this example
spec. Also, notice in types.go that
namespaceName is defined as string and
podLiveForThisMinuets is defined as int. This
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 22/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
Note
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 23/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
main.go
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 24/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
51 podlifecycleconfigfactory
:=
informers.NewSharedInformerFactory
WithOptions
(podlifecyelconfgiclient,
Second*30,
52 informers.WithNamespace(os.
Getenv(NAMESPACE_TO_WATCH)))
You can find the complete code at this link:
https://packt.live/3lXe3FM.
main.go
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 25/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 26/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
Activity Guidelines:
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 27/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 28/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
resources:
["podlifecycleconfigs"]
verbs: ["get", "list",
"watch"]
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch",
"list", "delete"]
6. Using a RoleBinding object, associate this new
Role with the default ServiceAccount in the
crddemo namespace.
7. Build and deploy the controller Pod using the
Dockerfile provided in step 2.
8. Create a Pod that runs for a long time using the
k8s.gcr.io/busybox image in the crddemo
namespace.
Watch the Pod created in the previous step and
observe whether it is being terminated by our
controller. The expected result is that the Pod
should be created, and then it should be
automatically terminated after about a minute,
as in the following screenshot:
Note
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 29/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
NAME AGE
demo-pod-lifecycle 8h
Note that it only shows the name and age of the
object. However, if you issue a command for a
native Kubernetes object, you will see a lot more
columns. Let's try that for Deployments:
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 30/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
Note
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 31/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
singular:
podlifecycleconfigadv
#1.15 preserveUnknownFields:
false
validation:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
namespaceName:
type: string
podLiveForThisMinute
s:
type: integer
additionalPrinterColumns:
- name: NamespaceName
type: string
description: The name of the
namespace this CRD is applied
to.
JSONPath:
.spec.namespaceName
- name: PodLiveForMinutes
type: integer
description: Allowed number
of minutes for the Pod to
survive
JSONPath:
.spec.podLiveForThisMinutes
- name: Age
type: date
JSONPath:
.metadata.creationTimestamp
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 32/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 33/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
Summary
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 34/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 35/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 36/37
10/16/22, 9:06 AM 19. Custom Resource Definitions in Kubernetes | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_19_Final_SZ_ePub.xhtml 37/37