Skip to main content
AnsibleBlogUbuntu

An Ansible MicroK8s Install of Kubernetes

By June 11, 2022September 12th, 2022No Comments

We automate the install of Kubernetes in the form of MicroK8s using Ansible on Ubuntu 20.04. In this way the MicroK8s install is correct with nothing forgotten. We also add aliases to Kubernetes kubectl in the .bashrc file and Kubernetes command completion, all with Ansible. We could, of course install MicroK8s from the command line but there a few steps.

  • Install docker
  • Snap install Microk8s
  • Add user to docker and microk8s groups
  • Add alias for kubectl
  • Add bash completion for kubectl


To be repeatable correct Ansible can be a great free tool no matter if at home or in the office. We automate the install of MicroK8s, a lightweight Kubernetes implementation, onto Ubuntu 20.04 using Ansible. In this way it is quick easy and nothing is forgotten. I am a big fan of Vagrant VirtualBox Virtual Machines. A very quck and easy way to manage machines. The machines are controlled by the Vagrantfile which, in this case, installs ansible and sets up host records if I later extend the MicroK8s cluster after the install with Ansible.

# -*- mode: ruby -*-
# vi: set ft=ruby :
$script = <<-SCRIPT
apt-get update
apt-get install -y ansible sshpass
echo "192.168.56.11 controller" >> /etc/hosts
echo "192.168.56.12 node1" >> /etc/hosts
echo "192.168.56.13 node2" >> /etc/hosts
SCRIPT
Vagrant.configure("2") do |config|
  config.vm.define "controller" do |controller|
  controller.vm.box = "ubuntu/focal64"
  controller.vm.network "private_network", ip: "192.168.56.11"
  controller.vm.hostname = "controller"
  controller.vm.provider "virtualbox" do |vb|
    vb.memory = "2048"
  end
 controller.vm.provision "shell", inline: $script
end
config.vm.define "node1" do |node1|
  node1.vm.box = "ubuntu/focal64"
  node1.vm.network "private_network", ip: "192.168.56.12"
  node1.vm.hostname = "node1"
  node1.vm.provider "virtualbox" do |vb|
   vb.memory = "1024"
  end
  node1.vm.provision "shell", inline: $script
end
config.vm.define "node2" do |node2|
  node2.vm.box = "ubuntu/focal64"
  node2.vm.network "private_network", ip: "192.168.56.13"
  node2.vm.hostname = "node2"
  node2.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
  end
    node2.vm.provision "shell", inline: $script
  end
end

This deploys 3 Ubuntu systems all with Ansible installed and correct hosts entries. In the same dirextory as the Vagrantfile we create the Ansible Playbook.

---
- name: Install Microk8s
  hosts: localhost
  gather_facts: false
  become: true
  tasks:
    - name: Install docker
      apt:
        name: docker.io
        update_cache: true
        cache_valid_time: 86400
        state: present
    - name: Install microk8s
      snap:
        name: microk8s
        state: present
       classic: yes

    - name: Add user to Docker and Microk8s groups, they will need to logout and in again
      user:
        name: '{{ lookup("env", "USER") }}'
        state: present
        groups:
          - docker
          - microk8s
        append: true
    - name: Add alias to kubectl
      become: false
      lineinfile:
        path: '{{ lookup("env", "HOME") }}/.bashrc'
        regexp: '^alias kubectl='
        line: 'alias kubectl="microk8s kubectl"'
        state: present
    - name: Add bash completion for kubectl
      become: false
      lineinfile:
        path: '{{ lookup("env", "HOME") }}/.bashrc'
       regexp: '^source \<\(kubectl'
       line: 'source <(kubectl completion bash)'
       state: present

This directory is mapped to /vagrant in the guest VM when it is started, We start it with up and use ssh to login

$ vagrant up controller
$ vagrant ssh controller

When logged in we run the Playbook

$ ansible-playbook /vagrant/microk8s.yml

Logout and back in to up date groups and run .bashrc.