Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Demystifying Kubernetes Operators with the Operator SDK: Part 1

#1
Demystifying Kubernetes Operators with the Operator SDK: Part 1

<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/12/demystifying-kubernetes-operators-with-the-operator-sdk-part-1.png" width="256" height="256" title="" alt="" /></div><div><p><span><span>You may have heard about the concept of custom Operators in Kubernetes. You may have even read about the </span><a href="https://github.com/operator-framework/operator-sdk/blob/master/doc/user-guide.md"><span>CoreOS operator-sdk</span></a><span>, or tried walking through the setup. The concept is cool: </span><span>Operators can help you extend Kubernetes functionality to include managing any stateful applications your organization uses. They can ideally help you move away from manual human intervention at runtime for things like upgrades, node recovery, and resizing a cluster. </span><span>But after reading a bit on the topic, you may secretly still be mystified as to what operators exactly do, and how all the components work together. </span></span></p>
<p><span><span>In this article, we will demystify what an operator is, and how the CoreOS operator-sdk translates its input to the code that is then run as an operator. In this step-by-step tutorial, we will create a general example of an operator, with a few bells and whistles beyond the functionality shown in the </span><a href="https://github.com/operator-framework/operator-sdk/blob/master/doc/user-guide.md"><span>operator-sdk user guide</span></a><span>. By the end, you will have a </span><span>solid foundation for how to build a custom operator that can be applied to real-world use cases. </span></span></p>
<h3><span><span>Hello Operator, could you tell me what an Operator is? </span></span></h3>
<p><span><span>To describe what an operator does, let’s go back to Kubernetes architecture for a bit. Kubernetes is essentially a </span><strong><span>desired state manager</span></strong><span>. You give it a desired state for your application (number of instances, disk space, image to use, etc.) and it attempts to maintain that state should anything get out of wack. Kubernetes uses what’s called a control plane on it’s master node. The control plane includes a number of </span><strong><span>controllers</span></strong><span><strong> </strong>whose job is to reconcile against the desired state in the following way: </span></span></p>
<ul>
<li>
<p><span><span>Monitor existing K8s objects (Pods, Deployments, etc.) to determine their state</span></span></p>
</li>
<li>
<p><span><span>Compare it to the K8s yaml spec for the object</span></span></p>
</li>
<li>
<p><span><span>If the state is not the same as the spec, the controller will attempt to remedy this</span></span></p>
</li>
</ul>
<p><span><span>A common scenario where reconciling takes place is a Pod is defined with three replicas. One goes down, and with K8s’ controller watching, it recognizes that there should be three Pods running, not two. It then works to create a new instance of the Pod. </span></span></p>
<p><span><span>This simplified diagram shows the role of controllers in Kubernetes architecture as follows.</span></span></p>
<ul>
<li>
<p><span><span>The Kubectl CLI sends an object spec (Pod, Deployment, etc.) to the API server on the Master Node to run on the cluster</span></span></p>
</li>
<li>
<p><span><span>The Master Node will schedule the object to run (not shown) </span></span></p>
</li>
<li>
<p><span><span>Once running, a Controller will continuously monitor the object and reconcile it against the spec</span></span></p>
</li>
</ul>
<p><span><span>In this way, Kubernetes works great to take much of the manual work out of maintaining the runtime for stateless applications. Yet it is limited in the number of object types (Pods, Deployments, Namespaces, Services, DaemonSets, etc.) that it will natively maintain. Each of these object types has a predetermined behavior and way of reconciling against their spec should they break, without much deviation in how they are handled. </span></span></p>
<p><span><span>Now, what if your application has a bit more complexity and you need to perform a custom operation to bring it to a desired running state? </span></span></p>
<p><span><span>Think of a stateful application. You have a database application running on several nodes. If a majority of nodes go down, you’ll need to reload the database from a specific snapshot following specific steps. Using existing object types and controllers in Kubernetes, this would be impossible to achieve. Or think of scaling nodes up, or upgrading to a new version, or disaster recovery for our stateful application. These kinds of operations often need very specific steps, and typically require manual intervention. </span></span></p>
<p><span><span>Enter operators. </span></span></p>
<p><span><span>Operators extend Kubernetes by allowing you to define a </span><strong><span>Custom Controller</span></strong><span> to watch your application and perform custom tasks based on its state (a perfect fit to automate maintenance of the stateful application we described above). The application you want to watch is defined in Kubernetes as a new object: a </span><a href="https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/"><span>Custom Resource</span></a><span> (CR) that has its own yaml spec and object type (in K8s, a </span><span>kind</span><span>) that is understood by the API server. That way, you can define any specific criteria in the custom spec to watch out for, and reconcile the instance when it doesn’t match the spec. The way an operator’s controller reconciles against a spec is very similar to native Kubernetes’ controllers, though it is using mostly custom components. </span></span></p>
<p><span><span>Note the primary difference in our diagram from the previous one is that the Operator is now running the custom controller to reconcile the spec. While the API Server is aware of the custom controller, the Operator runs independently, and can run either inside or outside the cluster. </span></span></p>
<div>
<table>
<tbody>
<tr>
<td><span><img alt="JMgJ3Q24igREHRCmKiX0UAhZVXH-arLxyyXWfKAm" height="20" src="http://www.sickgaming.net/blog/wp-content/uploads/2018/12/demystifying-kubernetes-operators-with-the-operator-sdk-part-1.png" width="20" /><img alt="JMgJ3Q24igREHRCmKiX0UAhZVXH-arLxyyXWfKAm" height="20" src="http://www.sickgaming.net/blog/wp-content/uploads/2018/12/demystifying-kubernetes-operators-with-the-operator-sdk-part-1.png" width="20" /></span></td>
<td>
<p><span><span>Because Operators are a powerful tool for stateful applications, we are seeing a number of </span><a href="https://github.com/coreos"><span>pre-built operators from CoreOS</span></a><span> and other contributors for things like ectd, Vault, and Prometheus. And while these are a great starting point, the value of your operator really depends on what you do with it: what your best practice is for failed states, and how the operator functionality may have to work alongside manual intervention. </span></span></p>
</td>
</tr>
</tbody>
</table>
</div>
<h3><span><span>Dial it in: Yes, I’d like to try Building an Operator</span></span></h3>
<p><span><span>Based on the above diagram, in order to create our custom Operator, we’ll need the following: </span></span></p>
<ol>
<li>
<p><span><span>A Custom Resource (CR) spec that defines the application we want to watch, as well as an API for the CR</span></span></p>
</li>
<li>
<p><span><span>A Custom Controller to watch our application</span></span></p>
</li>
<li>
<p><span><span>Custom code within the new controller that dictates how to reconcile our CR against the spec</span></span></p>
</li>
<li>
<p><span><span>An Operator to manage the Custom Controller</span></span></p>
</li>
<li>
<p><span><span>A deployment for the Operator and Custom Resource</span></span></p>
</li>
</ol>
<p><span><span>All of the above could be created by writing Go code and specs by hand, or using a tool like </span><a href="https://github.com/kubernetes-sigs/kubebuilder"><span>kubebuilder</span></a><span> to generate Kubernetes APIs. But the easiest route (and the method we’ll use here) is generating the boilerplate for these components using the </span><a href="https://github.com/operator-framework/operator-sdk/blob/master/doc/user-guide.md"><span>CoreOS operator-sdk</span></a><span>.</span><span> It allows you to generate the skeleton for the spec, the controller, and the operator, all via a convenient CLI. Once generated, you define the custom fields in the spec and write the custom code to reconcile against the spec. We’ll walk through each of these steps in the next part of the tutorial. </span></span></p>
<p>Toye Idowu is a Platform Engineer at Kenzan Media.</p>
</div>
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016