Docker and Kubernetes Setup using Ansible

Dipto Chakrabarty
CodeChef-VIT
Published in
5 min readMar 26, 2020

This post is about how you can easily setup and configure docker and kubernetes using ansible .

We are going to create an ansible role which will manage the installation and configuration for us .

Now what are roles , I found a good definition

Roles provide a framework for fully independent, or interdependent collections of variables, tasks, files, templates, and modules. In Ansible, the role is the primary mechanism for breaking a playbook into multiple files.

Basically roles are pre-written codes and you can use in your individual playbooks , it helps you perform your tasks without writing much of the code , roles also provide a way to clean up your architecture as it separates your code into multiple files.

To create a role named kubernetes-docker you can use the command

ansible-galaxy init kubernetes-docker

Setup the roles path in your configuration file and initialize your role in that path

The roles_path determines where it will read the ansible roles you have created.

To check if role has been made or not or to check the roles in your system you can use the command

ansible-galaxy list

this lists down all the roles you have in your system.

I will be using an EC2 instance for this demonstration where neither docker or kubernetes (focusing on one node for now , multiple nodes coming up soon) is installed.

So log into your instance and check if docker or kubernetes is installed.

I am using an Ubuntu image , to check if you have any package installed you can use

dpkg -s <package name>

As you can see docker or kubernetes is not installed.

So first we install Ansible in our system and configure it , install ansible using pip

sudo pip install ansible

We start writing our playbooks , for docker installation

The first step is to add the docker repository to our apt package manager ,

to do that first we add the repository keys and then add the repository to apt.

To install docker we need to add a repository link to our package managers repository , normally to do this we use the command

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu  $(lsb_release -cs)  stable"

and we also need to add the apt keys for this repository

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add –

Every key ensures that the packages installed from that repository are authentic so that there are no unnecessary or wrong installations.

Next we need to install a couple of packages , we can do the using the package module but more importantly to install multiple packages at a single go we can with_items which is similar to loops of other languages.

Install these packages

The items written under with_items are part of a list and are iterated over one after the other , we use this to install packages one after another and significantly reduces our code.

Finally we are ready to install docker , using the apt module we install docker and then restart and enable its service using the service module.

Docker package installation and enabling service

Enabling a service ensures that your service is running even after your system restarts .

We should have docker installed now .

Now we need to install kubernetes , for kubernetes we need three main packages

  • kubeadm
  • kubelet
  • kubectl

So lets go ahead and write the kubernetes.yml file

Our first step will be to disable swap , to do that we use the mount module

Remove and Disable swap memory

First we remove the swap memory present in the system and after that we disable swap permanently (kubernetes cannot run with swap memory on pre requisite) .

Next once again we need to add the repository for kubernetes and add the keys related to it , so lets do that using the apt_key and apt_repository modules

Once done we can install the packages required and enable them

Now lets include both the files in our main.yml of the role

main.yml

Now for using our role we just have to include our role in our playbook

kubedocker.yml file

That’s it if we run our playbook we will successfully install docker and kubernetes , using roles we just had to include the role to add all the functionalities of configuring kubernetes and docker.

Configuration Complete.

Docker has been installed and started .

Kubernetes setup done.

You can checkout the repository here where I have a few more roles on server hardening also Repository.

--

--

Dipto Chakrabarty
CodeChef-VIT

Site Reliability Engineer , I talk about Devops Backend and AI. Tech Doctor making sure to diagnose and make your apps run smoothly in production.