Reader small image

You're reading from  The Kubernetes Workshop

Product typeBook
Published inSep 2020
PublisherPackt
ISBN-139781838820756
Edition1st Edition
Right arrow
Authors (6):
Zachary Arnold
Zachary Arnold
author image
Zachary Arnold

Zachary Arnold works as a software engineer at Ygrene Energy Fund. Zach has an experience of over 10 years in modern web development. He is an active contributor to the Open Source Kubernetes project in both SIG-Release and SIG-Docs currently focusing on security. He has been running clusters in production since Kubernetes 1.7 and has spoken at the previous 4 KubeCons. His passion areas in the project center on building highly stable Kubernetes cluster components and running workloads securely inside of Kubernetes.
Read more about Zachary Arnold

Sahil Dua
Sahil Dua
author image
Sahil Dua

Sahil Dua is a software engineer. He started using Kubernetes to run machine learning workloads. Currently, he is running various types of applications on Kubernetes. He shared his learnings as a keynote session at KubeCon Europe 2018. He is a passionate open source contributor and has contributed to some famous projects such as Git, pandas, hound, go-GitHub, and so on. He has been an open source community leader for over 2 years at DuckDuckGo.
Read more about Sahil Dua

Wei Huang
Wei Huang
author image
Wei Huang

Wei Huang: Wei works as a senior software engineer in IBM. He has over 10 years' experiences around database, data warehouse tooling, cloud, container, monitoring and devops. He started to use Kubernetes since 1.3, including extending Kubernetes LoadBalancer using CRD, networking, scheduling and monitoring. Now he is a core maintainer of Kubernetes SIG-Scheduling.
Read more about Wei Huang

Faisal Masood
Faisal Masood
author image
Faisal Masood

Faisal Masood is a cloud transformation architect at AWS. Faisal's focus is to assist customers in refining and executing strategic business goals. Faisal main interests are evolutionary architectures, software development, ML lifecycle, CD and IaC. Faisal has over two decades of experience in software architecture and development.
Read more about Faisal Masood

Mélony Qin
Mélony Qin
author image
Mélony Qin

Mélony Y. QIN, also known as CloudMelon, is the founder of CloudMelon Vis, a tech media and educational platform for technopreneurs in the cloud-native and serverless space, and a former product manager at Microsoft. With a passion for cloud-native technologies, OSS, DevOps, Kubernetes, serverless, data, and AI, Mélony has authored multiple books, including the Certified Kubernetes Administrator (CKA) Exam Guide, the Kubernetes Workshop, and Microsoft Azure Infrastructure, all published by Packt Publishing. Mélony is a member of the Association for Computing Machinery (ACM) and the Project Management Institute (PMI), leveraging her extensive experience with diverse cloud technologies to drive innovation in the cloud-native, serverless, and generative AI space. She runs the CloudMelonVis YouTube channel and Cloud-Native Innovators newsletter, read by professionals from top tech companies such as Microsoft, Google, Amazon, Dell, and Carrefour.
Read more about Mélony Qin

Mohammed Abu Taleb
Mohammed Abu Taleb
author image
Mohammed Abu Taleb

Mohammed Abu-Taleb works as a Technical Advisor at Microsoft. Working at Microsoft CSS team for troubleshooting complex issues and cases for premier customers that are using Azure Kubernetes Services (AKS). Prior that, Mohammed was a SME (subject matter expert) for the azure managed monitoring service (Azure Monitor) focusing on designing, deploying, and troubleshooting monitoring strategies for containers.
Read more about Mohammed Abu Taleb

View More author details
Right arrow

19. Custom Resource Definitions in Kubernetes

Overview

In this chapter, we will show how you can use Custom Resource Definitions (CRDs) to extend Kubernetes and add new functionality to your Kubernetes cluster. You will also learn how to define, configure, and implement a complete CRD. We will also describe various example scenarios where CRDs can be very helpful. By the end of this chapter, you will be able to define and configure a CRD and a Custom Resource (CR). You will also learn how to deploy a basic custom controller to implement the required functionality of the CR in your cluster.

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.

To build these functionalities on top of Kubernetes, we use Custom Resources (CRs). Custom Resource Definitions (CRDs) allow us to add a capability through which users can add custom objects to the Kubernetes server and use those CRs like any other native Kubernetes object. A CRD helps us to introduce our custom objects to the Kubernetes system. Once our CRD is created, it can be used like any other object in the Kubernetes server. Not only that, but we can also use the Kubernetes API...

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 system matches the desired state as defined by the CR. As you have seen in earlier chapters, the Kubernetes components that do this are called controllers. Kubernetes comes up with many of these controllers whose job is to make sure that the desired state (for example, the number of replicas of Pods defined in a Deployment) is equal to the value defined in the Deployment object. In summary, a controller is a component that watches the state of resources through the Kubernetes API server and attempts to match the current state with the desired state.

The built-in controllers that are included in a standard setup of Kubernetes are meant to work with built-in objects such as Deployments. For our CRDs and their CRs, we need to write our own custom controllers.

The Relationship between a CRD, a CR, and a Controller...

Standard Kubernetes API Resources

Let's list all the resources and APIs that are available in the Kubernetes cluster. Recall that everything we have used is defined as an API resource, and an API is a gateway through which we communicate with the Kubernetes server to work with that resource.

Get a list of all the current Kubernetes resources by using the following command:

kubectl api-resources

You should see the following response:

Figure 19.2: Standard Kubernetes API resources

In the preceding screenshot, you can see that the resources defined in Kubernetes have an APIGroup property, which defines what internal API is responsible for managing this resource. The Kind column lists the name of the resources. As we have seen earlier in this topic, for standard Kubernetes objects such as Pods, the schema or definition of a Pod object is built into Kubernetes. When you define a Pod specification to run a Pod, this could be said to be analogous to...

Why We Need Custom Resources?

As stated earlier, CRs provide a way through which we can extend the Kubernetes platform to provide functionalities that are specific to certain use cases. Here are a few use cases where you will encounter the use of CRs.

Example Use Case 1

Consider a use case in which you want to automate the provisioning of a business application or a database onto the Kubernetes cluster automatically. Abstracting away the technical details, such as configuring and deploying the application, allows teams to manage them without having an in-depth knowledge of Kubernetes. For example, you can create a CR to abstract the creation of a database. Thus, users can create a database Pod by just defining the name and size of the database in a CRD, and the controller will provision the rest.

Example Use Case 2

Consider a scenario where you have self-serving teams. Your Kubernetes platform is used by multiple teams and you would like the teams to provision namespaces...

How Our Custom Resources Are Defined

To come up with a solution for Example Use Case 3 in the previous section, we have decided that our CRD will define two fields, as mentioned in the preceding example. To accomplish this, our CR object will look as follows.

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

This is the field required by Kubernetes to group objects. Note that we put the version (v1) as part of the group key. This grouping technique helps us keep multiple versions of our object. Consider whether you want to add a new property without affecting existing users...

Summary

In this chapter, you learned about custom controllers. As per the Kubernetes glossary, a controller implements a control loop to watch the state of the cluster through the API server and makes changes in an attempt to move the current state toward the desired state.

Controllers can not only watch and manage user-defined CRs, but they can also act on resources such as Deployments or services, which are typically part of the Kubernetes controller manager. Controllers provide a way to write your own code to suit your business needs.

CRDs are the central mechanism used in the Kubernetes system to extend its capability. CRDs provide a native way to implement custom logic for the Kubernetes API server that satisfies your business requirements.

You have learned about how CRDs and controllers help provide an extension mechanism for the Kubernetes platform. You have also seen the process through which you can configure and deploy custom controllers on the Kubernetes platform...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
The Kubernetes Workshop
Published in: Sep 2020Publisher: PacktISBN-13: 9781838820756
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (6)

author image
Zachary Arnold

Zachary Arnold works as a software engineer at Ygrene Energy Fund. Zach has an experience of over 10 years in modern web development. He is an active contributor to the Open Source Kubernetes project in both SIG-Release and SIG-Docs currently focusing on security. He has been running clusters in production since Kubernetes 1.7 and has spoken at the previous 4 KubeCons. His passion areas in the project center on building highly stable Kubernetes cluster components and running workloads securely inside of Kubernetes.
Read more about Zachary Arnold

author image
Sahil Dua

Sahil Dua is a software engineer. He started using Kubernetes to run machine learning workloads. Currently, he is running various types of applications on Kubernetes. He shared his learnings as a keynote session at KubeCon Europe 2018. He is a passionate open source contributor and has contributed to some famous projects such as Git, pandas, hound, go-GitHub, and so on. He has been an open source community leader for over 2 years at DuckDuckGo.
Read more about Sahil Dua

author image
Wei Huang

Wei Huang: Wei works as a senior software engineer in IBM. He has over 10 years' experiences around database, data warehouse tooling, cloud, container, monitoring and devops. He started to use Kubernetes since 1.3, including extending Kubernetes LoadBalancer using CRD, networking, scheduling and monitoring. Now he is a core maintainer of Kubernetes SIG-Scheduling.
Read more about Wei Huang

author image
Faisal Masood

Faisal Masood is a cloud transformation architect at AWS. Faisal's focus is to assist customers in refining and executing strategic business goals. Faisal main interests are evolutionary architectures, software development, ML lifecycle, CD and IaC. Faisal has over two decades of experience in software architecture and development.
Read more about Faisal Masood

author image
Mélony Qin

Mélony Y. QIN, also known as CloudMelon, is the founder of CloudMelon Vis, a tech media and educational platform for technopreneurs in the cloud-native and serverless space, and a former product manager at Microsoft. With a passion for cloud-native technologies, OSS, DevOps, Kubernetes, serverless, data, and AI, Mélony has authored multiple books, including the Certified Kubernetes Administrator (CKA) Exam Guide, the Kubernetes Workshop, and Microsoft Azure Infrastructure, all published by Packt Publishing. Mélony is a member of the Association for Computing Machinery (ACM) and the Project Management Institute (PMI), leveraging her extensive experience with diverse cloud technologies to drive innovation in the cloud-native, serverless, and generative AI space. She runs the CloudMelonVis YouTube channel and Cloud-Native Innovators newsletter, read by professionals from top tech companies such as Microsoft, Google, Amazon, Dell, and Carrefour.
Read more about Mélony Qin

author image
Mohammed Abu Taleb

Mohammed Abu-Taleb works as a Technical Advisor at Microsoft. Working at Microsoft CSS team for troubleshooting complex issues and cases for premier customers that are using Azure Kubernetes Services (AKS). Prior that, Mohammed was a SME (subject matter expert) for the azure managed monitoring service (Azure Monitor) focusing on designing, deploying, and troubleshooting monitoring strategies for containers.
Read more about Mohammed Abu Taleb