Learning Library

← Back to Library

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
0:00so you're probably familiar with how 0:02kubernetes is great at giving you 0:04scalability and fault tolerance for your 0:06applications so for 0:14example let's say you've got three 0:17copies of your app and one of them 0:22fails the beauty of kubernetes is it's 0:25going to automatically start up a new 0:27app for you 0:31and everything is 0:32wonderful now the truth is kubernetes 0:36works great for what are called 0:37stateless applications stateless 0:39applications are like web servers and it 0:42can handle that out of the box 0:43incredibly well but what if you have a 0:46stateful application stateful 0:49application might be something like a 0:50database stateful application have extra 0:54steps that need to occur when you need 0:56to restart them so for example you might 0:58need to do leader election we might need 1:00to do backup and recovery so if you need 1:03kubernetes to support those types of 1:06applications we need to use a wonderful 1:08extension mechanism called operators and 1:11operators allows you to extend 1:13kubernetes with custom 1:16resources so we're going to show how to 1:18build an operator um but first let's 1:21talk about 1:25usage so how would we use an operator 1:29well the beauty of operators and 1:31kubernetes is that they're seamless and 1:33so they work just like regular 1:36resources so uh you're going to build a 1:38yaml and you can see I've got a yaml 1:40here and this is now a yaml for the 1:43custom resource the example we're using 1:45is mcash D but uh the beauty of this is 1:49we're going to use the same Cube control 1:51commands Cube control apply dasf and the 1:54name of the file and those same commands 1:57that you would use for regular 1:58kubernetes resources will work for the 2:00custom operators so usage just feels 2:03like the rest of 2:05kubernetes um now we need to talk about 2:08how do we build an operator how are we 2:10going to build an operator well there's 2:12a wonderful 2:18toolkit that tool kit is the operator 2:21SDK and the beauty of the operator SDK 2:23is it gives you four different ways to 2:26build your operators if you already have 2:28Helm charts laying around the operator 2:30SDK can take those Helm charts and turn 2:32them into an operator if you already 2:34have anable playbooks the uh operator 2:36SDK can take those anible playbooks and 2:38turn those into an operator and if you 2:41need to do custom you need to just start 2:43from scratch you don't have anything 2:44laying around it provides a Java based 2:47plugin approach for building an operator 2:49and it also provides a go based approach 2:52so you can use any of those of your 2:53favorite languages to to build a custom 2:57operator now you're going to want to 2:59know what are the big steps I've got to 3:01do it seems like a lot of work to build 3:03an 3:03operator Well turns out there's sort of 3:06two key steps um one of which is we need 3:10to add our custom 3:16fields and then we're going to need to 3:18add what's called a control 3:22Loop so first let's talk about custom 3:25Fields we've gotten this example over 3:27here two uh two custom fields one is 3:30size and one is nodes one is in the spec 3:33section and one is in the status section 3:35think of the spec section as the 3:38parameters that you're passing into your 3:40custom resource sort of telling it what 3:42it to do in this case it's its size 3:45which is the number of mcash D 3:47applications we want and then nodes is 3:50in the status section and that's sort of 3:51the output that's recording status of 3:54the custom resource so we've got two 3:56Fields there and we need to tell 3:58kubernetes about those Fields because 4:01kubernetes stores all of your 4:03information whether it's a custom 4:04resource or a built-in resource and a 4:07database called at CD and we've got to 4:09give it the necessary information we're 4:11going to declare the fields and the 4:13operator SDK is going to generate what 4:16are called custom resource definitions 4:18and that's the magic that's going to get 4:20your custom Fields into the etcd 4:23database with everything else that 4:24kubernetes stores so once you've got 4:27your custom Fields done the key key 4:29thing is you've got to add the 4:30intelligence of your of of your operator 4:33that's the piece that's going to worry 4:35about trying to do what you needed to do 4:38so in the examples we talked about 4:39before maybe you need to do some backup 4:41and Recovery steps maybe you need to do 4:43leader election uh whatever needs to be 4:45done is going to be done in What's 4:46called the control Loop now the beauty 4:49of control loops um this is these are 4:51used all over kubernetes and they all 4:54work the same way 5:00they're going to start by declaring what 5:02is my desired State that's going to be 5:05sort of the input PED in here's what I 5:08want and then what's going to happen at 5:10that point is we're going to evaluate 5:13well what is my current actual State and 5:16uh you know because you know I want to 5:17go from my actual state to what is the 5:19desired state that was declared and so 5:23that's what we call the 5:25reconciliation so reconciliation there 5:28is where we're going to worry about what 5:30do we what steps do we need to take to 5:32get close to that desired State maybe 5:35there weren't enough mcds that were 5:37there maybe we need to add one maybe we 5:39need to start one up so that's what's 5:41going to happen in that reconcile State 5:43and then in the last step we're going to 5:44report back the 5:46status and that's the full reconcile 5:49Loop so now we've got our custom Fields 5:52taken care of thanks to the operator SDK 5:54it gives us a lot of skeleton code we do 5:56need to fill in our control loop our 5:58reconcile Loop but once we have all that 6:00filled in we can then go back to the 6:02operator SDK it allow us to generate a 6:05new image of this operator this custom 6:08operator that we've created and it'll 6:10also enable us to deploy that operator 6:13once that operator's deployed it's going 6:14to run just just like any other part of 6:18uh the resources that are running in 6:19your kubernetes environment are you able 6:22to follow of that if not and you need 6:24more information on operators and also 6:26want information on open shift and 6:28kubernetes check out my latest book 6:30hybrid Cloud apps with open shift and 6:32kubernetes that can be found in the 6:34links 6:35below thanks for watching if you enjoyed 6:38this content please like the video and 6:40subscribe to our Channel