Install Kubernetes cluster on vermin using ansible

Mohammed Hewedy
2 min readAug 9, 2020

Actually, I don’t think this is working, as I got some error:

fatal: [node1]: FAILED! => {“msg”: “The task includes an option with an undefined variable. The error was: ‘kubernetes_join_command’ is ….

In a previous post, we see how to install Kubernetes using shell scripts using vermin.

Today, we will see how to use Ansible to install Kubernetes on 3 nodes.

Three machines

First, we will need 3 machines, 1 master and 2 workers:

$ vermin create ubuntu/focal --cpus 2 --mem 2048
$ vermin create ubuntu/focal --cpus 1 --mem 2048
$ vermin create ubuntu/focal --cpus 1 --mem 2048

Note, the master node needs at least 2 cpus

Let’s list the machines:

$ vermin ps
VM NAME IMAGE CPUS MEM DISK TAGS
vm_02 ubuntu/focal 2 2048 2.7GB
vm_03 ubuntu/focal 1 2048 2.6GB
vm_04 ubuntu/focal 1 2048 2.6GB

Install Ansible Roles:

Now let’s create ansible.cfg :

[defaults]
inventory = ./inventory
host_key_checking = False
roles_path = ./roles

Now let’s install docker and Kubernetes Ansible roles:

$ ansible-galaxy install geerlingguy.docker
$ ansible-galaxy install geerlingguy.kubernetes

Build Ansible inventory

Next, let’s configure the inventory file, but before let’s get IP addresses for the VMs:

$ vermin ip vm_02
192.168.100.134
$ vermin ip vm_03
192.168.100.135
$ vermin ip vm_04
192.168.100.136

Now let’s write the inventory file (inventory):

master ansible_host=192.168.100.134 kubernetes_role=master
node1 ansible_host=192.168.100.135 kubernetes_role=node
node2 ansible_host=192.168.100.136 kubernetes_role=node
[cluster]
master
node1
node2
[cluster:vars]
ansible_user=vermin
ansible_private_key_file=~/.vermin/vermin_rsa

Now let’s ping the 3 servers:

$ ansible cluster -m ping

The Playbook

And finally, let’s create the playbook (k8s.playbook.yaml):

- hosts: cluster
become: true
tasks:
- import_role:
name: geerlingguy.docker
- import_role:
name: geerlingguy.kubernetes

Now let’s run the playbook:

$ ansible-playbook ./k8s.playbook.yaml

Now, the playbook starts running:

--

--