In this post, we learn how to use the Kubernetes plugin in the Ansible tool. In Ansible, it is difficult to manage large numbers of containers. But utilizing Kubernetes in Ansible is helpful while managing the containers. In this guide, we will learn what is the Kubernetes inventory source and how it functions in Ansible.
Google launched the easily accessible management software called Kubernetes which is also known as K8s which is used to manage the virtualized configurations over a remote set of machines with zero-downtime installation features, automated reversion, and scalability which provides the Kubernetes plugin with an extremely adaptable architecture in Ansible. Through the availability of REST APIs for the necessary functionality, Kubernetes’ primary goal is to conceal the complexities that are associated with controlling a number of containers.
The configuration of Kubernetes is based on client-server. However, by default, there is only one main server that serves as a controlling host. In Kubernetes, we can also use multi-main controllers for the configuration of Kubernetes. The K8s inventory source is occasionally necessary for control systems and configure the services on the remote host to control pods. But there are difficulties in getting specific instructions on how to do so. As a result, we implement this tutorial on how to make an Ansible Playbook that extracts the pods in a domain and produces a Kubernetes inventory source.
Prerequisites of Using the Kubernetes Inventory Source in Ansible
To use the Kubernetes inventory source in Ansible, there are stage process instructions or commands throughout this guide. Make sure that you have the necessary preparation if you want to move forward in it:
- To work in an Ansible scenario, we first set up the Ansible tool on the server and make sure that you have the Ansible version 2.11.7 so that we can easily use the Kubernetes plugin.
- To assess the Kubernetes plugin, we need a remote server device with a Kubernetes cluster built in.
- The remote host is currently configured to execute the Ansible instructions and playbooks, as well as a built inventory file. The name of the target remote device is K8s Master Node.
- The version of Python is 3.6 or above which must be installed on the device of the Ansible controller. It can also be in the target remote host device.
- The version of the openshift module of Python is 0.6 or above. We also need a pyYAML module of version 3.11 or above. Both modules must be set up in the remote server device as well as the controller device.
Here is the very first example where we implement the Kubernetes in Ansible. To do so, we use any Ansible environment to implement the Kubernetes inventory source. The following listed processes are carried out in the following scenario using the Kubernetes Inventory Plugin:
- Implementation of the new namespace in the K8s cluster in Ansible.
- Creation of nginx pod using the Ansible playbook.
- Creation of nginx deployment in Ansible.
To make the concepts and execution simple to understand, we implement the example in a number of steps.
Step 1: Implement the New Namespace in the K8s Cluster in Ansible
In the first step, we create the playbook so that we can write the content in the “.yml” format in Ansible. To create the playbook, the following is the command that we use:
Now, the “pod.yml” playbook is created and launched into the new terminal of Ansible. We start writing the script. In the playbook, we first define the API version which we use in the Ansible pod which is “v1”. The namespace that we define is “ansible-namespace”. Then, we list the metadata of the ansible-namespace. In the metadata, we use the nginx software and the label which contains a tier of value frontend in it. In the spec of the playbook, we list the name and the image that is stored in the container; both contain the nginx in it.
Step 2: Create the Manifest Document in Ansible
Now, we create another document in the same directory of Ansible which is the deployment document in the “.yml” format. To create the manifest document, use the following command in the Ansible terminal:
In the manifest document, we first define again the app version which is “app/v1”. Then, we tell what kind of file it is, whether it is a deployment file. Then, we define the namespace which is ansible-namespace. To build the implementation, two replicas inside the ansible-namespace show the number of the pod. The nginx images are 1.14.2 which are launched in the pod. The matchLabels parameter provides the tags for the pods and their specifications under the spec parameter. If somehow the tags on the pods match those specified in the configuration information, the implementation happens in the manifest document in Ansible.
Step 3: Create the Playbook for Kubernetes Implementation in Ansible
Now, we want to create another playbook in Ansible. In this playbook, we implement the functionality of Kubernetes in it. Use the following command to create the playbook:
The playbook is created in the same directory where the previous playbook and manifest document are stored in Ansible. In the playbook, we first define the functionality of the playbook that we want to implement. To build the connection, we provide the remote hosts that we want to target. Here, we target “all” hosts. Next, we define the variable to check the Python interpreter in Ansible using the path where it is stored. Now, we define the tasks in the playbook. First, we check the status of the minikube device. Next, we create a new Kubernetes namespace in the playbook. Then, duplicate the nginx_pod.yml and nginx_deployment .yml to the remote device.
Through these yml documents, we build the pod implementation in the remote device. Then, we check whether the Kubernetes pod is present or not on the specified location in the remote device.
After the playbook termination, we now build the inventory file so that we make the connection between the Ansible controller and the target remote host.
Here is the desired output where we see that the tasks are successfully placed on the target machine:
After the task is placed on the target machine, we check whether the “ansible-namespace” is in the Kubernetes cluster or not. We use the following “grep” command:
If you want to check the created pod in the namespace of the Kubernetes cluster, write the following command to check:
As you see in the previous output, we get the pods that are running on the device. Now, we check the deployment that we made in the Kubernetes cluster. Utilize the following statement to check the already made deployments:
We learned what is the Kubernetes inventory source in Ansible. We also learned how to utilize the Kubernetes inventory in Ansible. Then, we implemented an example so that we will learn more clearly about the working of Kubernetes inventory source in Ansible.