Getting started with k0s

April 28, 2022

After using it in my day job on Azure, I'm spending some time playing with Kubernetes on three local computers at home. I've chosen to do this using k0s, and playing with Ansible to set it up.

This will hopefully be a small series, starting with setting up machines, moving onto getting k0s installed and working, and then putting some images into the k0s cluster.

Hardware

I have three mini-itx boards available to play with. These all have Atom processors and have 4Gb on RAM. These have all had a plain install of Debian Bullseye installed, no GUI, but with an SSH server. They're all on the local network with a static ip address.

The two commands that have been run manually on each box are to allow the main user (drumcoder) to do a sudo:

$ apt-get install sudo
$ usermod -aG sudo drumcoder

Passwordless SSH access

We also need to enable passwordless ssh login to each of these machines.

Run:

$ ssh-keygen -t rsa -b 4096 -C "your_email@domain.com"

to create a local key, accepting all the defaults.

Then do:

$ ssh-copy-id drumcoder@server_ip_address

to copy this key to each of the machines in the cluster.

Ansible Shutdown

The first script developed was a way to shut down all three machines at once, with a single command from the local network. I chose to do this using Ansible.

The following `inventory.yml file exists in the local directory, and this defines which machines are controlled by Ansible:

all:
  children:
    initial_controller:
      hosts:
        k0s-2:
    controller:
      hosts:
    worker:
      hosts:
        k0s-1:
        k0s-3:
  hosts:
    k0s-1:
      ansible_host: 192.168.1.81
    k0s-2:
      ansible_host: 192.168.1.82
    k0s-3:
      ansible_host: 192.168.1.83
  vars:
    ansible_user: drumcoder

I also have an ansible.cfg file to configure ansible to point to this inventory file:

[defaults]
nocows = True
roles_path = ./roles
inventory  = ./inventory.yml

remote_tmp = $HOME/.ansible/tmp
local_tmp  = $HOME/.ansible/tmp
pipelining = True
become = True
host_key_checking = False
deprecation_warnings = False
callback_whitelist = profile_tasks

# Good for debugging and running roles/plays separately using tags
# since facts are cached we can refference them in subsequent roles/tags
fact_caching = jsonfile
fact_caching_connection = .ansible_facts_cache
fact_caching_timeout = 28800

We can now write an ansible playbook, I put this into a file called shutdown.yml

---
- name: reboot cluster
  hosts: all
  become: true

  tasks:
   - name: shutdown hosts
     community.general.shutdown:

This can now execute the shutdown on all three machines with the following command:

$ ansible-playbook shutdown.yml --ask-become-pass

This will prompt for the sudo password, then run the shutdown on each machine.

Now we have a set of machines that we can manually turn on, and then turn off with a script, we can look at getting k0s installed on each of them using Ansible.