So you're probably familiar with how Kubernetes is great at giving you scalability and fault tolerance for your applications.
So, for example, let's say you've got three copies of your app and one of them fails.
The beauty of Kubernetes is it's going to automatically start up a new app for you.
And everything is wonderful.
Now, the truth is, Kubernetes works great for what are called stateless applications.
Stateless applications are like web servers. And it can handle that out-of-the box incredibly well.
But what if you have a stateful application?
A stateful application might be something like a database.
Stateful applications have extra steps that need to occur when you need to restart them.
So, for example, you might need a leader election.
We might need to do backup and recovery.
So if you need Kubernetes to support those types of applications,
we need to use a wonderful extension mechanism called Operators.
And operators allow you to extend Kubernetes with custom resources.
So we're going to show how to build an operator, but first, let's talk about usage.
So how would we use an operator?
Well, the beauty of operators and Kubernetes is that they're seamless.
And so they work just like regular resources.
So you're going to build a YAML-- and you can see I've got a YAML here and this is now a YAML for the custom resource.
The example we're using memcached.
But the beauty of this is we're going to use the same kubectl commands: kubectl apply -f and the name of the file.
And those same commands that you would use for regular Kubernetes resources will work for the custom operators.
So usage just feels like the rest of Kubernetes.
Now we need to talk about how do we build an operator.
How are we going to build an operator?
Well, there's a wonderful toolkit: the Operator SDK.
And the beauty of the Operator SDK is it gives you four different ways to build your operators.
If you already have Helm charts lying around, the Operator SDK can take those Helm charts and turn them into an operator.
If you already have Ansible playbooks, the Operator SDK can take those Ansible playbooks and turn those into an operator.
And if you need to do custom-- you need to start from scratch, you don't have anything lying around--
it provides a Java-based plug-in approach for building an operator, and it also provides a Go-based approach.
So you can use any of those of your favorite languages to build a custom operator.
Now, you're going to want to know: What are the big steps I've got to do?
It seems like a lot of work to build an operator.
Well, turns out there's sort of two key steps.
One of which is we need to add our custom fields.
And then we're going to need to have what's called a control.
So first, let's talk about custom fields.
We've got this example over here to two custom fields.
One is size and one is nodes.
One is in the spec section and one is in the status section.
Think of the spec section as the parameters that you're passing into your custom resource-- sort of telling it what to do.
In this case, its size, which is the number of memcached applications we want.
And then nodes is in the status section.
And that's sort of the output that's reporting the status of the custom resource.
So we've got two fields there and we need to tell Kubernetes about those fields because
Kubernetes stores all of your information, whether it's a custom resource or a built-in resource and a database called etcd.
And we've got to give it the necessary information.
We're going to declare the fields and the Operator SDK is going to generate what are called custom resource definitions.
And that's the magic that's going to get your custom fields into the etcd database with everything else that Kubernetes stores.
So once you've got your custom fields done, the key thing is you've got to add the intelligence of your operator.
That's the piece that's going to worry about trying to do what you need to do.
So in the examples we talked about before, maybe you need to do some backup and recovery steps,
maybe you need to do leader election.
Whatever needs to be done, is going to be done in what's called the control loop.
Now, the beauty of control loops is these are used all over Kubernetes and they all work the same way.
They're going to start by declaring what is my desired state.
That's going to be sort of the input passed in: here's what I want.
And then what's going to happen at that point is we're going to evaluate, "Well, what is my current actual state?".
And because I want to go through my actual state to what is the desired state that was declared.
And so that's what we call the reconciliation.
So reconciliation there is where we're going to worry about
what do we what steps do we need to take to get close to that desired state?
Maybe there weren't enough memcached's that were there.
Maybe we need to add one.
Maybe we need to start one up.
So that's what's going to happen in that reconciled state.
And then in the last step, we're going to report back the status and that's the full reconcile loop.
So now we've got our custom feels taken care of thanks to the Operator SDK.
It gives us a lot of skeleton code.
We do need to fill in our control loop or reconcile loop.
But once we have all that filled in, we can then go back to the Operator SDK.
It allows us to generate a new image of this operator, this custom operator that we've created.
And it'll also enable us to deploy that operator.
Once that operators are deployed, it's going to run just just like any other part of the resources that are running in your Kubernetes environment.
Were you able to follow all that?
If not and if you need more information on operators and also want information on OpenShift and Kubernetes,
check out my latest book, "Hybrid Cloud Apps with OpenShift and Kubernetes." It can be found in the links below.
Thanks for watching.
If you enjoyed this content, please Like the video and Subscribe to our channel.