Automate your Kubernetes cluster bootstrapping with Rancher and Ansible and speed up your pipeline
By: Alexis Sotto Maior
Tags: DevOps, Ansible, Kubernetes, K3s, Ci Cd Pipeline

An Ansible Playbook for Rancher Kubernetes Bootsrapping

Having an API in Rancher and an RKE template makes our job easier. With Ansible, we can automate the steps required to create a new Kubernetes cluster in Rancher and add some post-configurations, such as DNS entry from ingress and Helm Chart deployments. This playbook, for example, interacts with Rancher to deploy a cluster from a template.

Here is the information that we need before running our first playbook. This must be passed to Ansible as variables:

rancher_host: URL of your rancher installation. rancher_clustername: the name of your new bootstrapping cluster. rancher_user: Rancher username with “create cluster” permission. rancher_pass: Rancher password for your rancher_user. rancher_cluster_template_id: The id of your RKE Template (ex: ctr-abcd) rancher_cluster_roles: A list of roles that each node in your cluster will assume (controlplane, etcd and/or worker). An Ansible inventory file with the nodes that will be added to the cluster.

The steps executed by the playbook are:

Fig 1. Create Rancher Cluster Ansible Playbook Workflow

If everything goes as planned, the cluster will be ready and available under Rancher UI. The average time for a successful 3-node (Centos 7, 4 core, 6GB RAM) cluster bootstrapping is 17 minutes, but can be slightly shorter if you set up a single-node cluster. This is because of the node registration process, where each node downloads all required containers (including Rancher agent container), execute all the installation and interact with Rancher master for configuration.

This process requires, however, that you already have your Ansible Inventory ready, with VMs configured and docker installed. This is not flexible and definitely requires manual configuration. What we did in our automation is to rely on dynamic inventory and Ansible Roles to fully prepare our nodes before running this part of the playbook.

As our private cloud is VMWare-based, we used this dynamic inventory plugin, that queries vCenter and returns a list of VMs based on defined criteria (we use tags for that). Although, if you opt to create your VMs on the fly, you can also use Ansible Roles like this one. The amazing thing about dynamic inventory in Ansible is the large availability of plugins, whether you are on-premises or on public clouds, such as AWS, Azure, GCP, etc.

Let us add those new steps to the playbook’s workflow:

Fig 2. Create Rancher Cluster Ansible Playbook Workflow with dynamic node prep.

Ok, so now we have an automation that creates a Kubernetes Cluster on Rancher based on RKE Templates and that leverages dynamic inventory in Ansible to create or reuse VMs based on specific criteria. But, to get the most of it, we also want to deploy our application on this cluster, register the ingress endpoint in DNS and tell our Devs that their cluster is ready, with their new featured deployed.

Fig 3. Full Workflow with post-operation tasks.

The whole process took around 20 minutes — 17 minutes for the k8s cluster bootstrapping + 3 minutes for post-operation tasks (Do you wish a fast-track? Keep reading!)

Overall, this process, integrated with our CI/CD pipeline, not only dynamically creates the cluster but also sync Helm Chats deployed in the production of our main application and, finally, rolls out the feature that we want to test/validate with our clients. Additionally, we execute automated tasks to validate the configuration, check the performance, security and chaos testing. It has been so far a huge ally for our continuous delivery process.