Microk8s charm

MicroK8s is a snap that makes it easy to install Kubernetes on a single machine. As well as Kubernetes itself, MicroK8s includes a large suite of prepackaged addons.

With only a few commands, three or more machines running MicroK8s can be combined in a high-availability MicroK8s cluster. (A two-machine cluster is also possible, but it won’t be HA.)

That’s still a lot of typing and deploying VMs or machines, so I decided to write a charm to make the process easier. Now, with a suitable Juju controller and machine provider, those commands above become simply:

 juju deploy -n3 --constraints 'cpus=2 mem=4G' cs:~pjdc/microk8s

(4G is a reasonable minimum for MicroK8s to deploy and form a cluster. Depending on what you decide to run in k8s, you may well need more!)

When the deployment completes, you’ll note that the cluster has been formed, and you can see every Kubernetes node from each microk8s unit.

$ juju exec --application microk8s -- 'microk8s status | grep -A2 high-availability:'
- return-code: 0
  stdout: |
    high-availability: yes
      datastore master nodes: 10.15.254.233:19001 10.15.254.234:19001 10.15.254.235:19001
      datastore standby nodes: none
  unit: microk8s/0
- return-code: 0
  stdout: |
    high-availability: yes
      datastore master nodes: 10.15.254.233:19001 10.15.254.234:19001 10.15.254.235:19001
      datastore standby nodes: none
  unit: microk8s/2
- return-code: 0
  stdout: |
    high-availability: yes
      datastore master nodes: 10.15.254.233:19001 10.15.254.234:19001 10.15.254.235:19001
      datastore standby nodes: none
  unit: microk8s/1

$ juju exec --application microk8s -- microk8s kubectl get nodes
- return-code: 0
  stdout: |
    NAME                     STATUS   ROLES    AGE     VERSION
    juju-439239-microk8s-1   Ready    <none>   6m3s    v1.19.3-34+a56971609ff35a
    juju-439239-microk8s-0   Ready    <none>   8m3s    v1.19.3-34+a56971609ff35a
    juju-439239-microk8s-2   Ready    <none>   4m51s   v1.19.3-34+a56971609ff35a
  unit: microk8s/0
- return-code: 0
  stdout: |
    NAME                     STATUS   ROLES    AGE     VERSION
    juju-439239-microk8s-1   Ready    <none>   6m3s    v1.19.3-34+a56971609ff35a
    juju-439239-microk8s-0   Ready    <none>   8m3s    v1.19.3-34+a56971609ff35a
    juju-439239-microk8s-2   Ready    <none>   4m51s   v1.19.3-34+a56971609ff35a
  unit: microk8s/1
- return-code: 0
  stdout: |
    NAME                     STATUS   ROLES    AGE     VERSION
    juju-439239-microk8s-1   Ready    <none>   6m3s    v1.19.3-34+a56971609ff35a
    juju-439239-microk8s-0   Ready    <none>   8m3s    v1.19.3-34+a56971609ff35a
    juju-439239-microk8s-2   Ready    <none>   4m51s   v1.19.3-34+a56971609ff35a
  unit: microk8s/2

$ _

Now you can add the cluster to your Juju controller (in this example, named microk8s):

juju expose microk8s
juju ssh microk8s/0 microk8s config | \
    juju add-k8s microk8s --cluster-name=microk8s-cluster --controller=microk8s --skip-storage

and deploy k8s Juju charms:

$ juju deploy cs:~mattermost-charmers/mattermost
Located charm "cs:~mattermost-charmers/mattermost-9".
Deploying charm "cs:~mattermost-charmers/mattermost-9".
$ juju status
Model       Controller  Cloud/Region      Version  SLA          Timestamp
mattermost  microk8s    microk8s/default  2.8.3    unsupported  01:18:53Z

App         Version  Status   Scale  Charm       Store       Rev  OS          Address  Notes
mattermost           waiting      1  mattermost  jujucharms    9  kubernetes           Waiting for database relation

Unit           Workload  Agent  Address  Ports  Message
mattermost/0*  waiting   idle                   Waiting for database relation
$ _

The charm has a couple of additional features to support unusual or restricted environments.

Kubernetes requires nodes to be able to find each other by name. If necessary, you can use juju config microk8s manage_etc_hosts=true to tell the charm to handle this.

For microk8s to deploy successfully, it needs to be able to fetch some OCI images from the Internet. If your environment requires this access to be via a proxy, use juju config containerd_env to tell the charm how to configure this.

If access to external DNS servers is restricted, then CoreDNS will not be able to look up any names, since it defaults to Google Public DNS, and your applications may not work. Using juju config coredns_config, change the forwarders option to a value that will work for you.

The charm’s source code is currently hosted in a personal repository on Launchpad, but as work continues on the charm and if others are interested to contribute it may move to a team namespace. Please give it a try and let me know what you think.

7 Likes

This might well be the easiest way to deploy a quick k8s cluster on any cloud! Keen to get feedback from folks on how it feels.

thank you for the tut. do you if it’s possible to add worker nodes with juju?