Building Kubernetes Operators with Operator SDK
Key Points
- Kubernetes natively manages scalability and fault‑tolerance for stateless apps, but stateful workloads (e.g., databases) require extra handling such as leader election and backup/recovery.
- Operators extend Kubernetes by introducing custom resources, letting you manage stateful applications with the same `kubectl apply` workflow used for built‑in resources.
- The Operator SDK supplies four development options—wrapping Helm charts, converting Ansible playbooks, or writing Java‑ or Go‑based operators—so you can build an operator in the language or format that fits your existing tooling.
- Creating an operator mainly involves defining custom spec and status fields (e.g., `size` and `nodes`) and implementing a control loop that reconciles those fields to maintain the desired state.
Full Transcript
# Building Kubernetes Operators with Operator SDK **Source:** [https://www.youtube.com/watch?v=UmIomb8aMkA](https://www.youtube.com/watch?v=UmIomb8aMkA) **Duration:** 00:06:44 ## Summary - Kubernetes natively manages scalability and fault‑tolerance for stateless apps, but stateful workloads (e.g., databases) require extra handling such as leader election and backup/recovery. - Operators extend Kubernetes by introducing custom resources, letting you manage stateful applications with the same `kubectl apply` workflow used for built‑in resources. - The Operator SDK supplies four development options—wrapping Helm charts, converting Ansible playbooks, or writing Java‑ or Go‑based operators—so you can build an operator in the language or format that fits your existing tooling. - Creating an operator mainly involves defining custom spec and status fields (e.g., `size` and `nodes`) and implementing a control loop that reconciles those fields to maintain the desired state. ## Sections - [00:00:00](https://www.youtube.com/watch?v=UmIomb8aMkA&t=0s) **Introducing Kubernetes Operators for Stateful Apps** - The speaker explains that while Kubernetes natively manages stateless workloads, stateful applications such as databases need custom operators and resources—demonstrated through a YAML custom resource and standard kubectl commands. ## Full Transcript
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 stateful
application might be something like a
database stateful application have extra
steps that need to occur when you need
to restart them so for example you might
need to do 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 allows you to extend
kubernetes with custom
resources so we're going to show how to
build an operator um 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 uh 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
is mcash D but uh the beauty of this is
we're going to use the same Cube control
commands Cube control apply dasf 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 um 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 that tool kit is 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 laying around the operator
SDK can take those Helm charts and turn
them into an operator if you already
have anable playbooks the uh operator
SDK can take those anible playbooks and
turn those into an operator and if you
need to do custom you need to just start
from scratch you don't have anything
laying around it provides a Java based
plugin 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 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 um one of which is we need
to add our custom
fields and then we're going to need to
add what's called a control
Loop so first let's talk about custom
Fields we've gotten this example over
here two uh 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
it to do in this case it's its size
which is the number of mcash D
applications we want and then nodes is
in the status section and that's sort of
the output that's recording 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 at CD 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 key
thing is you've got to add the
intelligence of your of of your operator
that's the piece that's going to worry
about trying to do what you needed 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 uh whatever needs to be
done is going to be done in What's
called the control Loop now the beauty
of control loops um this 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 PED 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
uh you know because you know I want to
go from 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 mcds 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 reconcile 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 Fields
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 our
reconcile Loop but once we have all that
filled in we can then go back to the
operator SDK it allow 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 operator's deployed it's going
to run just just like any other part of
uh the resources that are running in
your kubernetes environment are you able
to follow of that if not and you need
more information on operators and also
want information on open shift and
kubernetes check out my latest book
hybrid Cloud apps with open shift and
kubernetes that can be found in the
links
below thanks for watching if you enjoyed
this content please like the video and
subscribe to our Channel