How to Install microk8s Using Ansible on Ubuntu 20.04

Install Kubernetes microk8s Using Ansible on Ubuntu 20.04

Today, we’re getting hands-on with Kubernetes, the open-source wizard that makes managing containerized apps a breeze. And we’re doing it with MicroK8s, the lightweight sidekick designed for devs and operators who want to run Kubernetes right on their own machine.

But wait, we’re not stopping there! We’re adding a dash of Ansible magic to make the installation process even smoother. Ansible is like your trusty assistant for automating all things tech – from setting up your infrastructure to configuring software.

Now, let’s gear up with the basics:

Here’s what you’ll need:

  1. A computer running Ubuntu 20.04 with Ansible already on board.
  2. SSH access to the computer where you want to set up MicroK8s.
  3. Both machines should have Python3 and python3-pip installed.

Step 1: Install and Configure Ansible

To install Ansible on Ubuntu 20.04, run the following command:

sudo apt update
sudo apt install ansible

Next, we need to configure Ansible to give SSH access to the target system. To do this, we generate an SSH key pair on the Ansible control machine.


Then, copy the public key to the target system using the following command:

ssh-copy-id remote_user@target_system

This will add the public key to the authorized keys file on the target system and allow Ansible to connect via SSH.

Step 2: Create an Ansible playbook

Next, we need to create an Ansible playbook that installs MicroK8s on the target system. Create a file named install_microk8s.yaml and add the following content:

- name: Install MicroK8s
  hosts: all
  become: true
    - name: Install snapd
        name: snapd
        state: present

    - name: Install MicroK8s
      shell: snap install microk8s --classic

    - name: Allow non-root access to MicroK8s
      shell: usermod -aG microk8s $USER

    - name: Configure MicroK8s
      shell: sudo microk8s status --wait-ready

In this playbook, we first install snapd, which is required to install MicroK8s. We then use the snap module to install MicroK8s. After the installation, we add the current user to the microk8s group to allow non-root access to MicroK8s. Finally, we run the microk8s status command to configure MicroK8s.

Step 3: Run the Ansible playbook

To run the Ansible playbook, execute the following command:

ansible-playbook install_microk8s.yaml -i inventory.ini

Replace inventory.ini with the path to your Ansible inventory file. The inventory file should contain the hostname or IP address of the target system.

Once the playbook has completed successfully, you should be able to access the MicroK8s dashboard by running the following command:

microk8s dashboard-proxy