Jump to main content

Building the MicroK8s snap

MicroK8s is usually distributed as a snap package, to make it easy to distribute and install securely across a range of operating systems. As MicroK8s is completely open source, it is possible to modify or customise the source and build your own snap.

What you will need

  • Access to the internet (see section on oflline builds below)
  • A working LXD environment
  • The latest version of Snapcraft

Install LXD if required

The Snapcraft build process makes use of an LXD container. This needs to be of a recent, snap-based distribution of LXD.
Remove any previous versions of LXD/LXC (be sure to snapshot any running containers first!):

sudo apt-get remove lxd* -y
sudo apt-get remove lxc* -y

Then install the LXD snap package

snap install lxd

… and initiate the LXD environment.

lxd init

For more detailed install instructions, refer to the LXD documentation.

Install Snapcraft

Snapcraft is the tool which builds snap packages. For full installation instrctions, see the Snapcraft overview, but for platforms which support snaps, it can be installed simply by running:

sudo snap install snapcraft --classic

Fetch the MicroK8s source

MicroK8s source code is located on Github. To fetch a local copy:

git clone http://github.com/canonical/microk8s
cd ./microk8s/

Make modifications

At this stage you can modify any of the code in the repo. Many modifications may be limited to the ‘recipe’ used to build the snap - the snapcraft.yaml file, which can be found in the snapdirectory. You should again refer to the Snapcraft documentation for the settings found in this file. It is also useful to know there are a number of variables which can be set to influence the build process, which can be set either in the snapcraft.yaml file of the container itself - a list of these can be found in the build reference documentation.

Build the snap

As mentioned there are many settings and options for using snapcraft. the recommended method for building the MicroK8s snap is to use an LXD container. You can initiate the build with the command:

snapcraft --use-lxd

This will take some time as the build process fetches dependencies, stages the ‘parts’ of the snap and creates the snap package itself. the snap itself will be fetched from the build environment and placed in the local project directory. Note that the LXD container used for building will be stopped, but not deleted. This is in case there were any errors or artefacts you may wish to inspect.

Install the snap

The snap can then be installed locally by using the ‘–dangerous’ option. This is a safeguard to make sure the user is aware that the snap is not signed by the snap store, and is not confined.

sudo snap install microk8s_v1.27.3_amd64.snap

Note: You will not be able to install this snap if there is already a microk8ssnap installed on your system.

Iterate over changes

When you are done testing the changes you have made, you can remove the snap:

sudo snap remove microk8s

If you wish you can then make further changes. As noted previously, the LXD container used for building is not removed and will be reused by subsequent build instructions. When you are satisfied it is no longer needed, this container can be removed:

lxc delete snapcraft-microk8s

Building offline for air-gapped environments

A common use case is where MicroK8s is used in an airgapped or offline environment. To be able to build the MicroK8s snap in that environment requires that the source for the snap and all dependencies are available, many of which are normally fetched from online resources.

In this scenario, it is usually simplest to run through the above steps on a computer which does have access to these resources, and then transfer them to the environment where the build is to be made (following whatever normal procedures are required in the environment to declare these resources ‘safe’).

This directory can then be examined and copied to the offline environment. Please note that disconnecting from the source in this way obviously means you will not receive any, possibly important, updates. See also the documentation for installing in an offline environment

Last updated 9 months ago. Help improve this document in the forum.