Set up your development environment

From zero to hero: Write your first Kubernetes charm > Set up your development environment

See previous: Understand your application

You understand your application and are eager to start developing your charm.

Not so fast! Like any software developer, you first need to set up your charm development environment!

There are a few things to consider:

  • When you develop, in general, it’s a good idea to do it in an isolated development environment. That way you can always quickly bounce back if anything goes wrong. Here you should know that charm developers usually use Multipass, a tool that you can install on Linux, Windows, or macOS to spin up an Ubuntu virtual machine (VM) in one line.

  • It is also a good idea to decide upfront on the tools that you are going to use. Here you should know that, while there are many ways in which you can develop a charm, the current recommendation is to use the CLI tool Charmcraft and the Python charmed operator development library Ops. If you decide to use Charmcraft, you’ll also have to take into account the fact that it is currently supported only on Linux.

  • Next, you’ll need a cloud – in the specific case of developing a charm for Kubernetes, a Kubernetes cluster. For this purpose charm developers usually use MicroK8s, a low-ops, minimal production Kubernetes that can be set up on a local workstation and is thus ideal for development.

  • Finally, you will need something to deploy, scale, integrate, etc., your charm on your Kubernetes cluster. Here you need to set up the Juju OLM (Operator Lifecycle Manager), that is, you need to install the CLI client Juju.

In this part of the tutorial we will walk you through how to set up an Ubuntu VM with Multipass, get access to Charmcraft and Ops, install MicroK8s, install Juju, and understand their role and your workflow around them in the chapters that follow.

Contents:

  1. Create your charm project directory
  2. Set up an Ubuntu VM with Multipass
  3. Install Charmcraft
  4. Get Ops
  5. Install MicroK8s
  6. Set up the Juju OLM

Create your charm project directory

On your local machine, create a project directory. On Linux, for example, you can do this as shown below.

mkdir ~/fastapi-demo

This is the directory where you will put all your charm files. Since it is on your local machine, you can also use your favorite editor to edit them.

Set up an Ubuntu VM with Multipass

The goal of this section is to help you get a Linux machine that supports installation from snap, so that you can install Charmcraft.

If you already have that, we still recommend this step as a way for you to experiment in an isolated development environment.

If you however wish to continue directly on your existing Linux machine, you may do so – just ignore the code lines where we go through Multipass.

However, please keep in mind that the instructions in this tutorial have only been tested for the scenario with Multipass.

First, on your local machine, install Multipass: Linux | macOS | Windows.

Then, open a terminal and use Multipass to launch an Ubuntu VM, as shown below (where charm-dev is the name we gave to our VM). This is the place where we will install everything else, including Charmcraft which, recall, requires Linux. That way, if anything goes wrong, you can simply delete your Multipass instance.

# Launch a VM  "charm-dev" with 8 GB RAM, 2 CPU cores, and 20 GB disk:
multipass launch -n charm-dev -m 8g -c 2 -d 20G
# >>> Launched: charm-dev

In the same terminal, use Multipass to mount your local project directory to your VM, as below. Any change you make in this directory on your host machine will be available inside the VM at the mount point, ~/fastapi-demo.

# Mount a project dir, see https://multipass.run/docs/mount-command
multipass mount ~/fastapi-demo charm-dev:~/fastapi-demo

Finally, open a shell inside the VM, as shown below. Any command you type after the last terminal prompt (ubuntu@charm-dev:~$) will be inside of this VM.

multipass shell charm-dev
# >>> ubuntu@charm-dev:~$ 

Read more: Multipass

Install Charmcraft

Inside your Ubuntu VM, configure LXD and install the Juju SDK CLI tool Charmcraft, as shown below. Charmcraft will give you access to a rich set of powerful CLI commands. You can use them to simplify the creation, building, and publication of your charm.

# Charmcraft relies on LXD. Configure LXD:
lxd init --auto
# Install Charmcraft:
sudo snap install charmcraft --classic

Read more: Charmcraft (charmcraft)

Get Ops

You don’t need to do anything to get the Python charmed operator development framework Ops at this point. Just plan on using it. That is, be ready to require, use, and import it in your code as you would any other Python library.

Read more: Ops (ops)

Install MicroK8s

Inside your Ubuntu VM, install and configure MicroK8s, as shown below. This will create and configure a small Kubernetes cloud (by default called microk8s) your local workstation.

If you’re not using a Multipass VM but rather your existing Linux:
In the instructions below, replace the ubuntu user with $USER.

# Install Microk8s from snap:
sudo snap install microk8s --channel 1.25-strict/stable

# Add the 'ubuntu' user to the MicroK8s group:
sudo usermod -a -G snap_microk8s ubuntu

# Give the 'ubuntu' user permissions to read the ~/.kube directory:
sudo chown -f -R ubuntu ~/.kube

# Create the 'microk8s' group:
newgrp snap_microk8s

# Enable the necessary MicroK8s addons:
sudo microk8s enable hostpath-storage dns

# Set up a short alias for the Kubernetes CLI:
sudo snap alias microk8s.kubectl kubectl

Read more: MicroK8s

Set up the Juju OLM

Inside your Ubuntu VM, install the Juju OLM, connect it to your MicroK8s cloud, and prepare a workspace (‘model’), as below. This is where we will test that our charm can be deployed and managed as intended.

# Install 'juju':
sudo snap install juju --channel 3.0/stable
# >>> juju (3.0/stable) 3.0.0 from Canonical✓ installed

# Since the juju package is strictly confined, you also need to manually create a path:
mkdir -p ~/.local/share

# Register your "microk8s" cloud with juju:
# Not necessary --juju recognises a MicroK8s cloud automatically, as you can see by running 'juju clouds'. 
juju clouds
# >>> Cloud      Regions  Default    Type  Credentials  Source    Description
# >>> localhost  1        localhost  lxd   0            built-in  LXD Container Hypervisor
# >>> microk8s   1        localhost  k8s   1            built-in  A Kubernetes Cluster
# (If for any reason this doesn't happen, you can register it manually using 'juju add-k8s microk8s'.)

# Install a "juju" controller into your "microk8s" cloud. 
# We'll name ours "tutorial-controller".
juju bootstrap microk8s tutorial-controller

# Create a workspace, or 'model', on this controller. 
# We'll call ours "charm-model".
# Juju will create a Kubernetes namespace "charm-model"
juju add-model charm-model

# Check status:
juju status
# >>> Model         Controller           Cloud/Region        Version  SLA          Timestamp
# >>> charm-model  tutorial-controller  microk8s/localhost  3.0.2    unsupported  16:05:03+01:00

# >>> Model "admin/charm-model" is empty.

# There's your charm model!
# This is where you will deploy your charm.

Read more: Get started with the Juju OLM

Congratulations, your development environment is ready!

Now, if you want to:

  • Continue the tutorial:
    • Use the project directory on your host machine to create and edit your charm files.
    • Use your Ubuntu VM to run Charmcraft or Juju commands.
    • To pause:
      Stop the Ubuntu VM by running multipass stop charm-dev.
    • To resume:
      Restart the Ubuntu VM: multipass start charm-dev.
      Reopen a shell into it: multipass shell charm-dev.
      Reaccess the charm project directory: cd ~/fastapi-demo.
  • Abandon the tutorial and clean up:
    • Delete the Ubuntu VM: multipass delete --purge charm-dev
    • Delete the charm project directory from your host machine.

See next: Create a minimal Kubernetes charm


Last updated a month ago.