{"id":644,"date":"2018-10-18T00:52:26","date_gmt":"2018-10-18T00:52:26","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=644"},"modified":"2018-10-18T15:12:25","modified_gmt":"2018-10-18T15:12:25","slug":"an-introduction-to-ansible-operators-in-kubernetes","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/18\/an-introduction-to-ansible-operators-in-kubernetes\/","title":{"rendered":"An introduction to Ansible Operators in Kubernetes"},"content":{"rendered":"<p>For years, Ansible has been a go-to choice for infrastructure automation. As Kubernetes adoption has skyrocketed, Ansible has continued to shine in the emerging container orchestration ecosystem.<\/p>\n<p>Ansible fits naturally into a Kubernetes workflow, using YAML to describe the desired state of the world. Multiple projects, including the <a href=\"https:\/\/opensource.com\/article\/18\/2\/automated-provisioning-kubernetes\">Automation Broker<\/a>, are adapting Ansible for use behind specific APIs. This article will focus on a new technique, created through a joint effort by the Ansible core team and the developers of Automation Broker, that uses Ansible to create Operators with minimal effort.<\/p>\n<h2>What is an Operator?<\/h2>\n<p>An\u00a0<a href=\"https:\/\/coreos.com\/operators\/\" target=\"_blank\" rel=\"noopener\">Operator<\/a><\/p>\n<p>is a Kubernetes controller that deploys and manages a service or application in a cluster. It automates human operation knowledge and best practices to keep services running and healthy. Input is received in the form of a custom resource. Let&#8217;s walk through that using a Memcached Operator as an example.<\/p>\n<p>The <a href=\"https:\/\/github.com\/operator-framework\/operator-sdk-samples\/tree\/master\/memcached-operator\" target=\"_blank\" rel=\"noopener\">Memcached Operator<\/a> can be deployed as a service running in a cluster, and it includes a custom resource definition (CRD) for a resource called Memcached. The end user creates an instance of that custom resource to describe how the Memcached Deployment should look. The following example requests a Deployment with three Pods.<\/p>\n<p>apiVersion: &#8220;cache.example.com\/v1alpha1&#8221;<br \/>\nkind: &#8220;Memcached&#8221;<br \/>\nmetadata:<br \/>\nname: &#8220;example-memcached&#8221;<br \/>\nspec:<br \/>\nsize: 3<\/p>\n<p>The Operator&#8217;s job is called reconciliation\u2014continuously ensuring that what is specified in the &#8220;spec&#8221; matches the real state of the world. This sample Operator delegates Pod management to a Deployment controller. So while it does not directly create or delete Pods, if you change the size, the Operator&#8217;s reconciliation loop ensures that the new value is applied to the Deployment resource it created.<\/p>\n<p>A mature Operator can deploy, upgrade, back up, repair, scale, and reconfigure an application that it manages. As you can see, not only does an Operator provide a simple way to deploy arbitrary services using only native Kubernetes APIs; it enables full day-two (post-deployment, such as updates, backups, etc.) management, limited only by what you can code.<\/p>\n<h2>Creating an Operator<\/h2>\n<p>The <a href=\"https:\/\/github.com\/operator-framework\/operator-sdk\/\" target=\"_blank\" rel=\"noopener\">Operator SDK<\/a> makes it easy to get started. It lays down the skeleton of a new Operator with many of the complex pieces already handled. You can focus on defining your custom resources and coding the reconciliation logic in Go. The SDK saves you a lot of time and ongoing maintenance burden, but you will still end up owning a substantial software project.<\/p>\n<p>Ansible was recently introduced to the Operator SDK as an even simpler way to make an Operator, with no coding required. To create an Operator, you merely:<\/p>\n<ul>\n<li>Create a CRD in the form of YAML<\/li>\n<li>Define what reconciliation should do by creating an Ansible role or playbook<\/li>\n<\/ul>\n<p>It&#8217;s YAML all the way down\u2014a familiar experience for Kubernetes users.<\/p>\n<h2>How does it work?<\/h2>\n<p>There is a preexisting Ansible Operator base container image that includes Ansible, <a href=\"https:\/\/github.com\/ansible\/ansible-runner\" target=\"_blank\" rel=\"noopener\">ansible-runner<\/a>, and the Operator&#8217;s executable service. The SDK helps to build a layer on top that adds one or more CRDs and associates each with an Ansible role or playbook.<\/p>\n<p>When it&#8217;s running, the Operator uses a Kubernetes feature to &#8220;watch&#8221; for changes to any resource of the type defined. Upon receiving such a notification, it reconciles the resource that changed. The Operator runs the corresponding role or playbook, and information about the resource is passed to Ansible as <a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/user_guide\/playbooks_variables.html#passing-variables-on-the-command-line\" target=\"_blank\" rel=\"noopener\">extra-vars<\/a>.<\/p>\n<h2>Using Ansible with Kubernetes<\/h2>\n<p>Following several iterations, the Ansible community has produced a remarkably easy-to-use module for working with Kubernetes. Especially if you have any experience with a Kubernetes module prior to Ansible 2.6, you owe it to yourself to have a look at the <a href=\"https:\/\/docs.ansible.com\/ansible\/2.6\/modules\/k8s_module.html\" target=\"_blank\" rel=\"noopener\">k8s module<\/a>. Creating, retrieving, and updating resources is a natural experience that will feel familiar to any Kubernetes user. It makes creating an Operator that much easier.<\/p>\n<h2>Give it a try<\/h2>\n<p>If you need to build a Kubernetes Operator, doing so with Ansible could save time and complexity. To learn more, head over to the Operator SDK documentation and work through the <a href=\"https:\/\/github.com\/operator-framework\/operator-sdk\/blob\/master\/doc\/ansible\/user-guide.md\" target=\"_blank\" rel=\"noopener\">Getting Started Guide<\/a> for Ansible-based Operators. Then join us on the <a href=\"https:\/\/groups.google.com\/forum\/#!forum\/operator-framework\" target=\"_blank\" rel=\"noopener\">Operator Framework mailing list<\/a> and let us know what you think.<\/p>\n<p><em>Michael Hrivnak will present <a href=\"https:\/\/www.usenix.org\/conference\/lisa18\/presentation\/hrivnak\" target=\"_blank\" rel=\"noopener\">Automating Multi-Service Deployments on Kubernetes<\/a> at <a href=\"https:\/\/www.usenix.org\/conference\/lisa18\" target=\"_blank\" rel=\"noopener\">LISA18<\/a>, October 29-31 in Nashville, Tennessee, USA.<\/em><\/p>\n<p><a href=\"http:\/\/lxer.com\/module\/newswire\/ext_link.php?rid=261728\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>For years, Ansible has been a go-to choice for infrastructure automation. As Kubernetes adoption has skyrocketed, Ansible has continued to shine in the emerging container orchestration ecosystem. Ansible fits naturally into a Kubernetes workflow, using YAML to describe the desired state of the world. Multiple projects, including the Automation Broker, are adapting Ansible for use &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/18\/an-introduction-to-ansible-operators-in-kubernetes\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;An introduction to Ansible Operators in Kubernetes&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-644","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/644","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=644"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/644\/revisions"}],"predecessor-version":[{"id":771,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/644\/revisions\/771"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}