Kubernetes: Install microk8s using ansible.
Published: 2023-01-12 10:46:50

Microk8s is a lightweight Kubernetes deployment by Canonical that is enterprise-grade, yet also compact enough to run on development boxes and edge devices.

MicroK8s is an open-source system for automating deployment, scaling, and management of containerized applications. It provides the functionality of core Kubernetes components, in a small footprint, scalable from a single node to a high-availability production cluster.

In this article, I will show you how to install microk8s on Ubuntu nodes that are created using Ansible.

Ansible is an open source configuration management software. It enables server configuration and application deployment through the use of infrastructure as code.

Ansible does not require any software to be pre-installed on remote systems for it to work and this is what makes it ‘agentless’. For Ansible to manage a machines, all it needs is a SSH connection. Ansible can also manage Windows systems and all it needs is a WinRM connection.

To define infrastructure as code using Ansible, you create files which is written in YAML format and these are called Ansible playbooks. You then execute the playbooks against an inventory of servers. The number of servers can range from 1 to thousands. Ansible will try execute them all in parallel

Writing Ansible Playbooks without roles:
Manual Steps Without Ansible :

```

1. Install Perquisite package
     - sudo apt-get update
     - sudo apt-get install { Name of Packages }
2. Install Docker
     - sudo apt-get install docker 
3. Install Microk8s
     - sudo apt-get install snapd
     - sudo snap install microk8s
```
Ansible creates an inventory file that is typically located at /etc/ansible/hosts. This is the default location used by Ansible. Here we can create a custom

Create inventory file.
  1. Create a Directory.
       - mkdir project 
       - cd project
   2. Create a file inventory.
       - vim inventory 
           [test]
           192.168.1.11 
           192.168.1.12
           192.168.1.13

inventory file
2. Off-line package installation

Manual installation download snap packages using snap download pkg command and after set some specific path.

---
- name: Package installation using script
  hosts: localhost
  become: yes
  tasks:
    - name: Ubuntu Package Update
      apt:
        update_cache: yes
    - name: Docker Act new packages
      shell: |
         snap ack "/home/ubuntu/ashish/docker/docker_1779.assert"
    - name: install Dockers, file on local disk
      snap:
        name: "/home/ubuntu/ashish/docker/docker_1779.snap"
        register: docker result
    - debug: var=docker_result
    - name: Microk8s Act the new packages
      shell: |
         snap ack /home/ubuntu/ashish/microk8s/microk8s_3272.assert"
    - name: install Microk8s, file on local disk
      snap:
        name: "/home/ubuntu/ashish/microk8s/microk8s_3272.snap"
        classic: yes
        register: microk8_result
    - debug: var=microk8_result
Output:

root@ashish:/home/ubuntu# ansible-playbook install.yaml
PLAY [Package installation using script]****************************
TASK [Gathering Facts] *******************************************
ok: [localhost]
TASK [Ubuntu Package Update]**************************************

ok: [localhost]
TASK [Docker Act new packages] ******************************
changed: [localhost]
TASK [install Dockers, file on local disk] ************************
changed: [localhost]
TASK [debug] *********************************
ok: [localhost] => {
    "docker_result": {
        "changed": true,
        "channel": "stable",
        "classic": false,
        "cmd": "['state', 'classic', 'channel', {'actionable_snaps': ['/home/ubuntu/ashish/docker/docker_1779.snap']}]",
        "cmd_args": [
            "/usr/bin/snap",
            "install",
            "/home/ubuntu/ashish/docker/docker_1779.snap"
        ],
        "failed": false,
        "force_lang": "C",
        "rc": 0,
        "snaps_installed": [
            "/home/ubuntu/ashish/docker/docker_1779.snap"
        ],
        "stderr": "",
        "stderr_lines": [],
        "stdout": "docker 20.10.14 from Canonical** installed\n",
        "stdout_lines": [
            "docker 20.10.14 from Canonical** installed"
        ]
    }
}
TASK [Microk8s Act the new packages] ****************************
changed: [localhost]
TASK [install Microk8s, file on local disk] ************************
changed: [localhost]
TASK [debug] ******************************
ok: [localhost] => {
    "microk8_result": {
        "changed": true,
        "channel": "stable",
        "classic": true,
        "cmd": "['state', 'classic', 'channel', {'actionable_snaps': ['/home/ubuntu/ashish/microk8s/microk8s_3272.snap']}]",
        "cmd_args": [
            "/usr/bin/snap",
            "install",
            "--classic",
            "/home/ubuntu/ashish/microk8s/microk8s_3272.snap"
        ],
        "failed": false,
        "force_lang": "C",
        "rc": 0,
        "snaps_installed": [
            "/home/ubuntu/ashish/microk8s/microk8s_3272.snap"
        ],
        "stderr": "",
        "stderr_lines": [],
        "stdout": "microk8s v1.24.0 from Canonical** installed\n",
        "stdout_lines": [
            "microk8s v1.24.0 from Canonical** installed"
        ]
    }
}
PLAY RECAP *********************************
localhost                  : ok=9    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0