Some applications need a lot of operational knowledge - not just the complexity of modelling the app in Kubernetes, but ongoing maintenance tasks. Think of a stateful application where you want to create backups of data. The application deployment is modelled in Kubernetes resources, and it would be great to model the operations in Kubernetes too.
That's what the operator pattern does. It's a loose definition for an approach where you install an application which extends Kubernetes. So in that stateful application your operator would deploy the app, and it would also create a custom DataBackup resource in the cluster. Any time you want to take a backup, you deploy a backup object, the operator sees the object and performs all the backup tasks.
One of the problems with public operators is that you take a dependency on a third-party to maintain and update their deployments. The operators we use in this lab are good examples - they don't support ARM64 processors, so if you're using Apple Silicon then you won't be able to run all the exercises.
How this lab works
Read this once - every lab in this track follows the same rules.
Where you type the commands. Open a terminal and go to the folder where you cloned this repository. On Windows use PowerShell (search for it in the Start menu), on macOS or Linux use Terminal. Every path in this lab is written from the repository root, so stay there unless a step says otherwise.
Lines starting with > are explanations, not commands. Don't paste them.
Only the text inside grey code boxes gets typed.
The 📋 sign means it's your turn. When you see it, try the step yourself first. Underneath it there is a collapsed "Not sure how?" arrow with the answer - open it after you have tried, not before. The labs are written this way on purpose: you remember what you worked out, not what you pasted.
If a command fails, that is normal. Some steps fail deliberately to show you what the error looks like. Read the sentence above the command before you assume something is broken.
When you get stuck, in this order:
- Re-read the paragraph above the command - the answer is usually there.
- Open the "Not sure how?" arrow.
- Open
hints.mdfor a nudge. - Open
solution.mdfor the full answer. Using it is fine; copying it without reading is not.
When you finish, run the Cleanup section at the bottom. Labs share ports and container names, so leftovers from one lab break the next one.
Before you start
Takes about 55 minutes.
A Kubernetes cluster must be running. Docker Desktop ships one: Settings -> Kubernetes -> Enable Kubernetes. Check it:
kubectl get nodes
You should see at least one node with STATUS of Ready. If the command
hangs or errors, the cluster is still starting - give it a minute and try
again.
Helm must be installed - it is a separate command line tool, it does not come with Docker Desktop. Check it:
helm version --short
A version number like v3.x.x means you are ready. If you get
command not found, install it first: on Windows winget install Helm.Helm,
on macOS brew install helm, or follow
helm.sh/docs/intro/install.
Run every command from the root of this repository - the paths in this lab are written relative to it.
Reference
- Custom Resources - extending Kubernetes with your own object type
- NATS Operator - a sample operator for deploying message queues
- Bitpoke MySql Operator - operator for managing MySql databases
Custom Resources
Operators typically work by adding CustomResourceDefinitions (CRDs) to the cluster.
CRDs are pretty simple in themselved, you deploy an object which describes the schema of your new resource type:
- student-crd.yaml - defines a v1 Student resource, with email and company fields; the rest of the spec tells Kubernetes to store objects in its database, and print out specific fields when objects are shown in Kubectl
You deploy CRDs in the usual way:
kubectl apply -f labs/operators/specs/crd
📋 List all the custom resource in the cluster, and print the details of the new Student CRD.
Not sure how?
kubectl get crd
kubectl describe crd students
The CRD is just the object schema. Kubernetes only stores objects if it understands the type, which is what the CRD describes.
Now your Kubernetes cluster understands Student resources, you can define them with YAML:
- edwin.yaml - describes a student who works at Microsoft
- priti.yaml - a student who works at Google
📋 Create all the Students in the labs/operators/specs/students folder, list them and print the details for Priti.
Not sure how?
These objects are standard YAML:
kubectl apply -f labs/operators/specs/students
And the resources can be accesses using the CRD name:
kubectl get students
kubectl describe student priti
If you try to apply this YAML in a cluster which doesn't have the Student CRD installed, you'll get an error.
The standard Kubectl verbs (get, delete, describe) work for all objects, including custom resources.
NATS Operator
A CRD itself doesn't do anything, it just lets you store resources in the cluster. Operators typically install CRDs and also run controllers in the cluster. A controller is just an app running in a Pod which connects to the Kubernetes API and watches for CRDs being created or updated.
When you create a new custom resource, the controller see that and takes action - which could mean creating Deployment, Services and ConfigMaps, or running any custom code you need.
NATS is a high-performance message queue which is very popular for asynchronous messaging in distributed apps. The NATS operator runs as a Deployment:
- nats/operator/10-deployment.yaml - runs a single operator Pod which installs some CRDs and runs the controller
Install the operator and look carefully at the output:
kubectl apply -f labs/operators/specs/nats/operator
The operator installs some RBAC objects and the Deployment
📋 List the custom resource types in your cluster now. You'll see some NATS types - how did these get created?
Not sure how?
kubectl get crd
Shows your custom Student resource, and also NatsCluster and NatsServiceRole resources.
There's no YAML for these CRDs, so the NATS controller running in the Pod must have created them by using the Kubernetes API in code.
You can confirm the RBAC setup gives the controller ServiceAccount permission to do that:
kubectl auth can-i create crds --as system:serviceaccount:default:nats-operator
We can use a NatsCluster object to create a clustered, highly-available message queue for applications to use:
- msgq.yaml - defines a cluster with 3 NATS servers, running version 2.5
Create the cluster resource:
kubectl apply -f labs/operators/specs/nats/cluster
A single object gets created.
📋 Print the details of your new message queue, and look at the other objects running in the default namespace. The operator logs will show how the Pods were created.
Not sure how?
The output from the CRD doesn't show much:
kubectl get natscluster -o wide
But the operator has created Pods and Services:
kubectl get all --show-labels
Check the logs and you'll see the operator is managing the Pods - there's no Deployment or ReplicaSet for the message queue Pods:
kubectl logs -l name=nats-operator
The NATS operator is unusual because it acts as a Pod controller. Typically operators build on top of Kubernetes resources, so they would use Deployments to manage Pods.
Print the details of one of the NATS Pods and you'll see it's managed by the operator:
kubectl describe po msgq-1
You'll see Controlled By: NatsCluster/msgq
📋 The NATS operator still provides high availability. Delete one of the message queue Pods and confirm it gets recreated.
Not sure how?
kubectl delete po msgq-2
kubectl get po -l app=nats
kubectl logs -l name=nats-operator
You'll see a new Pod called msgq-2 gets created, and the operator logs show it coming online.
There's not much more you can do with the NATS operator, so we'll try one which has some more features.
MySql Operator
There's a Helm chart for the Presslabs MySql operator in this repository:
- values.yaml - defines the default values for the operator; there is a lot you can tweak here
Install the operator (you'll need the Helm CLI installed):
helm install mysql-operator labs/operators/specs/mysql/operator/
# the operator pod might restart and take a few minutes to be ready:
kubectl get po -l app.kubernetes.io/name=mysql-operator -w
📋 What resources do you need to create to deploy a MySql database cluster using the operator?
Not sure?
The Helm output gives you an example of what you need:
-
a MysqlCluster object - this is a CRD installed by the operator
-
a Secret containing the admin user password for the database
You can create a replicated database cluster using these specs:
-
mysql/database/01-secret.yaml - the database password
-
mysql/database/db.yaml - the cluster set to use two MySql servers
Create the database:
kubectl apply -f labs/operators/specs/mysql/database
📋 The database Pods take a while to start up. What controller does the operator use, and what's the container configuration in the Pods?
Not sure?
List the Pods and you'll see db-mysql-0. That name should suggest that it's managed by a StatefulSet:
kubectl get statefulset
Print the Pod details and you'll see multiple containers:
kubectl describe po db-mysql-0
The container setup is pretty complext:
- two init containers which look like they set up the database environment and the MySql configuration
- the main database container which runs MySql
- three sidecar containers which export database metrics, and perform a heartbeat check between the database servers
Check the logs of the primary database server in Pod 0:
kubectl logs db-mysql-0 -c mysql
You'll see mysqld: ready for connections showing the database server is running successfully
And the logs of the secondary database server in Pod 1:
kubectl logs db-mysql-1 -c mysql
You'll see '[email protected]:3306',replication started showing the secondary is replicating data from the primary.
The operator provides a production-grade deployment of MySql, and it also sets up a CRD for creating database backups and sending them to cloud storage.
Lab
We'll make use of the operators to install infrastructure components for a demo app.
Start by deleting the existing message queue and database clusters:
kubectl delete natscluster,mysqlcluster --all
The operators are watching for resources to be deleted, and will remove all the objects they created
Now deploy a simple to-do list application:
kubectl apply -f labs/operators/specs/todo-list
The app has a website listening on http://localhost:30028 which posts messages to a queue when you create a new to-do item. A message handler listens on the same queue and creates items in the database.
Browse to the app now and you'll see an error - the components it needs don't exist yet
You'll need to create NatsCluster and MysqlCluster objects matching the config in the app to make everything work correctly.
If something goes wrong
The connection to the server ... was refused - the cluster is not
running. Enable Kubernetes in Docker Desktop and wait for it to start.
provided port is already allocated - a Service from an earlier lab still
holds that NodePort. Find it with kubectl get svc -A and delete the namespace
that lab created.
failed calling webhook ... service not found - leftovers from the
admission lab are blocking the API server, and you will not be able to
create anything, not even a namespace. That lab's Cleanup section has the two
commands that remove the webhook configurations.
Pod stuck in ContainerCreating or Pending - usually the image is still
downloading, which can take a few minutes the first time. Check with
kubectl describe pod <name> and read the Events at the bottom: they say
exactly what the Pod is waiting for.
Pod in CrashLoopBackOff - the container starts and exits. The reason is
in its logs: kubectl logs <name>. Add --previous to see the run before the
last restart.
Still stuck? The Cleanup section below resets everything, so you can start the lab again from a clean cluster.
Cleanup
Delete the basic objects and CRDs:
kubectl delete crd natsclusters.nats.io natsserviceroles.nats.io
kubectl delete all,cm,secret,crd -l kubernetes.courselabs.co=operators
The order is important, deleting CRDs deletes custom resources - make sure the controller still exists to tidy up
Delete the NATS operator:
kubectl delete -f labs/operators/specs/nats/operator
Delete the MySql CRD and operator:
kubectl delete crd -l app.kubernetes.io/name=mysql-operator
helm uninstall mysql-operator