Skip to main content

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 " controller" >> /etc/hosts
echo " node1" >> /etc/hosts
echo " node2" >> /etc/hosts
Vagrant.configure("2") do |config|
  config.vm.define "controller" do |controller| = "ubuntu/focal64" "private_network", ip: ""
  controller.vm.hostname = "controller"
  controller.vm.provider "virtualbox" do |vb|
    vb.memory = "2048"
 controller.vm.provision "shell", inline: $script
config.vm.define "node1" do |node1| = "ubuntu/focal64" "private_network", ip: ""
  node1.vm.hostname = "node1"
  node1.vm.provider "virtualbox" do |vb|
   vb.memory = "1024"
  node1.vm.provision "shell", inline: $script
config.vm.define "node2" do |node2| = "ubuntu/focal64" "private_network", ip: ""
  node2.vm.hostname = "node2"
  node2.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
    node2.vm.provision "shell", inline: $script

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
    - name: Install docker
        update_cache: true
        cache_valid_time: 86400
        state: present
    - name: Install microk8s
        name: microk8s
        state: present
       classic: yes

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