Ansible is an open-source configuration management and automation software. It has been used to configure over 54,000 hosts in more than 10,000 organizations with a success rate of 99%. This makes it one of the most widely adopted DevOps tools on Earth and second only to Chef. In this blog post we’ll cover how Ansible can be configured for Kubernetes deployments using Ansible On-Demand containers.
The “kubernetes ansible playbook example” is a tutorial on how to use the Ansible Kubernetes module. It shows you how to use it and what commands to run.
If you’re managing a large number of containers, automating the administration of each containerized application is essential. How? Using the Ansible Kubernetes module with Kubernetes (K8s)! The Ansible Kubernetes module gives you access to all of the K8s APIs and lets you construct objects like Kubernetes deployments and Kubernetes services.
You’ll learn what the Ansible Kubernetes module is and how to use it to create Kubernetes objects on the Kubernetes cluster in this lesson.
Continue reading to learn how to automate application administration.
This tutorial includes detailed instructions. If you want to join in, make sure you have the following items:
Related: Ansible Setup Guide (Ubuntu, RHEL, CentOS, macOS)
- To test the Kubernetes module, you’ll need a remote Linux PC with a Kubernetes cluster built up. The remote node in this tutorial is Ubuntu 20.04.3 LTS.
Installing Kubernetes on Ubuntu: A Step-by-Step Guide
- An inventory file has been created, and one or more hosts have been set up to perform Ansible commands and playbooks. Myserver is the name of the remote Linux machine, and web is the inventory group for this lesson.
- On both the Ansible controller host and the remote node system, Python v3.6 or later is required. On an Ubuntu system, Python v3.8.10 will be used in this course.
How Do You Install Python 3.6? Related:
- On both the Ansible controller host and the remote node computer, install Python modules openshift larger than 0.6 and PyYAML greater than 3.11.
Using Ad Hoc Commands to Create Kubernetes Objects
Perhaps you simply want to rapidly generate one K8s object, such as namespace. If that’s the case, ad hoc commands will suffice. Ad hoc commands are a fast and easy method to execute a single command on a remote computer and deploy the modifications you specify.
Open your terminal on your Ansible controller, then enter the ansible command below to connect to the web host using the Kubernetes module (-m k8s). The -a parameter instructs the command to establish a namespace (namespace2) on the Kubernetes cluster.
Ansible generates a new namespace with the current state set to the state you provide, and the kind set to the kind of K8s object (Namespace) you need to manage.
web -m k8s -a ansible “name=namespace2 state=present kind=Namespace api version=v1” “name=namespace2 state=present kind=Namespace api version
When the command is finished, you’ll receive a CHANGED notification, as shown below, confirming that Ansible successfully established the namespace on the remote host’s Kubernetes cluster.
Using the Ansible ad hoc command, create the Kubernetes namespace.
Ansible Playbook for Creating Kubernetes Objects
It may be possible to build a single Kubernetes object using an ad-hoc command, but it will be difficult if you need to install numerous objects in the Kubernetes cluster. Instead of utilizing ad-hoc commands, use the ansible-playbook command to combine the Ansible Kubernetes module with a playbook.
If you’re already logged onto the Ansible controller host, do the following:
1. Create a directory named /ansible k8s module demo using the following instructions. This directory will hold the playbook as well as all of the necessary configuration files for using the Ansible Kubernetes module.
/ansible k8s module demo/mkdir cd /ansible k8s module demo/ansible k8s module demo
2. Open your preferred text editor, create a file named pod.yml in the /ansible k8s module demo directory, and copy/paste the contents of the following YAML playbook.
Using the nginx image and the labels app as nginx and tier as frontend, the plan below generates the nginx pod inside the ata-namespace.
# The pod.yaml template file is used to generate the pod API. # Version: v1 It belongs to the String kind: Pod. # Namespace: ata-namespace metadata: # Type: String It has the following properties: Dictionary name: nginx labels: app: nginx tier: frontend spec: # Its containers are of the List and Array types: – nginx name image: nginx
3. In the /ansible k8s module demo directory, create a new file named deployment.yml and paste the contents of the following YAML playbook into it.
Related:Running Your First Kubernetes Application
- The manifest file for the deployment (nginx-deployment) inside the ata-namespace with three replicas can be found below. The nginx:1.14.2 image was used to deploy the pods.
- You define the pods’ specification under the spec argument and pods’ labels inside the selector *—> matchLabels option. The deployment only takes place if pods’ labels match the deployment labels defined in the deployment metadata
apps/v1 apiVersion Deployment type # Deployment metadata name: nginx-deployment # Namespace: ata-namespace metadata: The deployment labels’ names are: app: nginx # Labels for deployments are declared. replicas: 3 # spec Specifying the necessary number of Pods selector: match # nginx application If the deployment Label matches, the pods will be launched. metadata: template # nginx application The Pods’ labels. container specifications: – image: nginx:1.14 – name: nginx 2 ports: container – Port: 80
4. Finally, under the /ansible k8s module demo directory, create a new file titled main.yml and copy/paste the contents of the following YAML playbook.
The script below provides numerous tasks for creating and managing Kubernetes objects on the Kubernetes cluster’s remote workstation.
The tasks in the playbook accomplish the following:
- Establish a namespace (ata-namespace).
- To the remote node, copy the pod.yaml and deployment.yaml files you previously produced (steps two and three).
6 Ansible Methods for Copying Files to Remote Hosts (Step by Step)
- Using the pod.yaml and deployment.yaml files, creates a pod and deployments in the Kubernetes cluster.
- Verify that the chosen namespace contains Kubernetes pods (ata-namespace).
—- – example of Ansible k8s module # In the Kubernetes cluster, creating the namespace hosts: web # Specifying the remote server on which the k8 module will handle items remote user: ubuntu # Using Ubuntu tasks as a remote user: – name: Make a k8s namespace k8s: name: ata-namespace # namespace definition kind: Namespace api version: v1 current state # When you need to create a new object, set the state to present. Copying the Pod.yaml and deployment.yaml files to the remote node for usage in creating pods and deployments in the Kubernetes cluster – name: copying file with playbook become: true copy: src: /home/ubuntu/ansible k8s module demo/pod.yaml dest: /tmp/pod.yaml # src: /home/ubuntu/ansible k8s module demo/deployment.yaml dest: /tmp/deployment.yaml Creating a Kubernetes pod in the ata-namespace using a locally saved file – name: Construct a pod k8s: state: present # When you need to create a new object, use state=present. namespace: ata-namespace # namespace definition /tmp/pod.yaml # source Name: Get an existing Pods in the kubernetes cluster k8s: api version: v1 kind: pod name: nginx namespace: ata-namespace # specifying the namespace register: web service # Using a locally saved file to create a Kubernetes deployment in the ata-namespace – name: Create a k8s deployment: When you need to create a new object, set the state to present. namespace: ata-namespace # specifying the namespace src: /tmp/deployment.yaml # The location of the source file
Using the Ansible Kubernetes Module to Deploy Kubernetes Objects
You now have everything you need to start deploying and managing Kubernetes objects. These files, however, do nothing until you execute the ansible-playbook program. To install Kubernetes objects on the remote node, you’ll use the ansible-playbook command.
To launch the playbook (main.yml) that runs the tasks to construct the Kubernetes objects on the remote host, use the command below.
ansible-playbook main.yml # Using the main.yml playbook to run the ansible-playbook command
Some tasks have a changed status, indicating that the remote host wasn’t in the right condition and had to be adjusted to perform the command. While tasks with an ok status do not need to be changed.
To manage the Kubernetes objects in the Kubernetes cluster, use the ansible-playbook command.
To manage the Kubernetes objects in the Kubernetes cluster, use the ansible-playbook command.
Now execute each of the commands below to see whether Ansible was able to construct all of the Kubernetes objects specified in the main.yml playbook.
# Checking the deployments in the “ata-namespace” namespace in the kubernetes cluster kubectl get deployments -n ata-namespace # kubectl get pod -n ata-namespace # checking the pods in the kubernetes cluster under the “ata-namespace” namespace grep kubectl get namespace | grep ata-namespace in the kubernetes cluster to check the namespace “ata-namespace”
After executing the instructions, you should get something like this.
The result verifies that the K8s module successfully created Kubernetes objects (namespace, pod, and deployment) in the Kubernetes cluster.
Verifying the Kubernetes cluster’s Kubernetes objects
You’ve learnt how to utilize the Ansible Kubernetes module in a playbook to generate and deploy Kubernetes objects during this course. With the Ansible Kubernetes module, you can quickly deal with Kubernetes objects on distant servers inside the cluster.
With the Ansible Kubernetes module, you can now automate containerized application administration. But what if you wanted to use the same setup for several servers? If that’s the case, learn how to develop Ansible templates to expand on your gained expertise.
Ansible is a powerful and flexible automation tool that can be used to manage or automate systems. The “ansible for kubernetes pdf” is a guide on how to use the Ansible Kubernetes Module with Ansible 2.0.
- kubernetes deployment using ansible
- no module named ‘kubernetes’ ansible
- ansible kubernetes inventory
- ansible kubernetes github
- ansible k8s_info