Set up / Tear down your test environment

This document shows how to set up a Juju test environment – complete with a sandbox (Ubuntu VM), a local cloud (LXD for machine charms and MicroK8s for Kubernetes charms), and Juju – and then how to tear it all down once you’re done playing around.

There are two ways to get all this set up: automatically or manually.

Set up / tear down automatically

Set up automatically

  1. Install Multipass.

If on Windows: Note that Multipass can only be installed on Windows 10 Pro or Enterprise. If you are using a different version, please follow the Set up / tear down manually guide, omitting the Multipass step.

For example, on Linux (assumes you have snapd):

sudo snap install multipass
  1. Use Multipass with the charm-dev blueprint to launch a Juju-ready Ubuntu VM (below my-juju-vm):
multipass launch --cpus 4 --memory 8G --disk 50G --name my-juju-vm charm-dev 

This step may take a few minutes to complete (e.g., 10 mins).

This is because the command downloads, installs, (updates,) and configures a number of packages, and the speed will be affected by network bandwidth (not just your own, but also that of the package sources).

However, once it’s done, you’ll have everything you’ll need – all in a nice isolated environment that you can clean up easily.

See more: GitHub > multipass-blueprints > charm-dev.yaml

Troubleshooting: If this fails, run multipass delete --purge my-juju-vm to clean up, then try the launch line again.

  1. Open a shell into the VM:
$ multipass shell my-juju-vm
Welcome to Ubuntu 22.04.4 LTS (GNU/Linux 5.15.0-100-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/pro

  System information as of Mon Mar 18 17:11:59 CET 2024

  System load:  0.0               Processes:             117
  Usage of /:   5.6% of 28.89GB   Users logged in:       1
  Memory usage: 3%                IPv4 address for ens3: 10.238.98.63
  Swap usage:   0%

 * Strictly confined Kubernetes makes edge and IoT secure. Learn how MicroK8s
   just raised the bar for easy, resilient and secure K8s cluster deployment.

   https://ubuntu.com/engage/secure-kubernetes-at-the-edge

Expanded Security Maintenance for Applications is not enabled.

16 updates can be applied immediately.
1 of these updates is a standard security update.
To see these additional updates run: apt list --upgradable

Enable ESM Apps to receive additional future security updates.
See https://ubuntu.com/esm or run: sudo pro status


Last login: Mon Mar 18 16:09:16 2024 from 10.238.98.1
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

ubuntu@my-juju-vm:~$ 

  1. (Optional:) Verify that the VM has indeed come pre-equipped with everything you’ll need:

Verify that you have Juju, MicroK8s (for machine charms) / LXD (for machine charms), a MicroK8s / LXD cloud (microk8s / localhost), a controller on that cloud (microk8s / lxd), and a workload model on that controller (welcome-k8s / welcome-lxd) by switching to the workload model:


Expand to see the instructions for MicroK8s
ubuntu@my-juju-vm:~$ juju switch microk8s:welcome-k8s

Expand to see the instructions for LXD
ubuntu@my-juju-vm:~$ juju switch lxd:welcome-lxd

Done!

  • Going forward:

    • Use the Multipass VM shell to run all commands.
  • At any point:

    • To exit the shell, press mod key + C or type exit.
    • To stop the VM after exiting the VM shell, run multipass stop my-juju-vm.
    • To restart the VM and re-open a shell into it, type multipass shell my-juju-vm.

Tear down automatically

Delete the Multipass VM (below, my-juju-vm):

multipass delete --purge my-juju-vm

Uninstall Multipass.

Set up / tear down manually

Set up manually

(Optional) Set up an Ubuntu VM with Multipass

  1. Install Multipass: Linux | macOS | Windows. On Linux (assumes you have snapd):
sudo snap install multipass
  1. Use Multipass to launch an Ubuntu VM (below, my-juju-vm):
multipass launch --cpus 4 --memory 8G --disk 30G --name my-juju-vm
  1. Open a shell into the VM:
multipass shell my-juju-vm

Set up your cloud

Depending on whether you want to develop a Kubernetes / machine charm, you will have to set up the MicroK8s / LXD localhost cloud. For example, on Linux:


Expand to set up your MicroK8s cloud
# Install MicroK8s package:
$ sudo snap install microk8s --channel 1.28-strict

# Add your user to the `microk8s` group for unprivileged access:
$ sudo adduser $USER snap_microk8s

# Give your user permissions to read the ~/.kube directory:
$ sudo chown -f -R $USER ~/.kube

# Wait for MicroK8s to finish initialising:
$ sudo microk8s status --wait-ready

# Enable the 'storage' and 'dns' addons:
# (required for the Juju controller)
$ sudo microk8s enable hostpath-storage dns

# Alias kubectl so it interacts with MicroK8s by default:
$ sudo snap alias microk8s.kubectl kubectl

# Ensure your new group membership is apparent in the current terminal:
# (Not required once you have logged out and back in again)
$ newgrp snap_microk8s

Expand to set up your LXD cloud
# LXD should already be there from the Charmcraft setup step; in case not:
$ lxd init --auto
$ lxc network set lxdbr0 ipv6.address none

Set up Juju

On your Ubuntu VM, install Juju. It will automatically recognise your local LXD / MicroK8s cloud. Bootstrap a controller into LXD / MicroK8s, then create a model:

# Install Juju:
sudo snap install juju --channel 3.5/stable
# >>> juju (3.5/stable) 3.5.4 from Canonical✓ installed

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

# For MicroK8s, if you are working with an existing snap installation, and it is not strictly confined: 
# (https://microk8s.io/docs/strict-confinement), you must also:
#
# # Share the MicroK8s config with Juju:
# sudo sh -c "microk8s config | tee /var/snap/juju/current/microk8s/credentials/client.config"
#
# # Give the current user permission to this file:
# sudo chown -f -R $USER:$USER /var/snap/juju/current/microk8s/credentials/client.config

# Register your MicroK8s / LXD cloud with Juju:
# Not necessary --juju recognises a local MicroK8s / LXD 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'.)

# Replace <cloud> with 'microk8s' or 'localhost' 
# to bootstrap a Juju controller into your  MicroK8s / LXD cloud. 
# We'll name our controller "my-controller".
juju bootstrap <cloud> my-controller

# Create a workspace, or 'model', on this controller. 
# We'll call ours "my-model".
# (In Kubernetes this corresponds to a namespace "my-model".)
juju add-model my-model

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

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

# There's your charm model!

Tear down manually

  1. Tear down Juju:
# Destroy any models you've created:
$ juju destroy-model my-model

# Destroy any controllers you've created:
$ juju destroy-controller my-controller

# Uninstall juju. For example:
$ sudo snap remove juju
  1. Tear down the MicroK8s cloud:
# Reset Microk8s:
$ sudo microk8s reset

# Uninstall Microk8s:
$ sudo snap remove microk8s

# Remove your user from the snap_microk8s group:
$ sudo gpasswd -d $USER snap_microk8s


Contributors: @deezzir, @gbeuzeboc , @gzanchi, @ismailkayi, @jnsgruk , @kos.tsakalozos , @rbarry , @facundo , @saviq , @sed-i , @shrishtikarkera, @tmihoc , @zxhdaze, @swetha1654

1 Like

Hi! I’ve just added some instructions for tearing down manually, please let me know if there is any doc bug.

1 Like

@zxhdaze I’ve reordered the steps (tear down juju, then microk8s), but other than that it was good – thanks!

1 Like

I found a typo in the documentation. So I would like to kindly report it. In the “4. (Optional:)” section, the heading following it- “Verify that the VM has indeed come pre-equipped with you’ll need” should be “all you need” in the end of the statement.

Thanks!

1 Like

Another typo in the doc at the end of Set up Automatically section:

  • Going forward:
    • Use the Multipass VM shell to run all commands.
  • At any point:
    • To exit the shell, press mod key + C or type exit.
    • To stop the VM after exiting the VM shell, run multipass stop charm-dev-vm.
    • To restart the VM and re-open a shell into it, type multipass shell charm-dev-vm.

The block refers to a charm-dev-vm, while the actual name of the VM created in the tutorial with multipass is my-juju-vm. Thanks

1 Like

Fixed, thanks! (Also added you to the list of contributors at the end of the doc.)

Fixed, thanks! (Also added you to the list of contributors at the end of the doc.)

Hi, i found this particular documentation when i was trying to do the following tutorial- https://juju.is/docs/juju/tutorial#:~:text=See%20more%3A%20Set%20up%20your%20test%20environment%20automatically%20>%20steps%201-2

I followed the document and installed juju 3.1. But when i tried to install the postgresql-k8s charm on top of it, i got the following error -

Located charm “postgresql-k8s” in charm-hub, revision 381 Deploying “postgresql-k8s” from charm-hub charm “postgresql-k8s”, revision 381 in channel 14/stable on ubuntu@22.04/stable ERROR Charm feature requirements cannot be met:

  • charm requires at least one of the following:
    • charm requires all of the following:
      • charm requires feature “juju” (version < 3.0.0) but model currently supports version 3.1.10
    • charm requires all of the following:
      • charm requires feature “juju” (version >= 3.4.3) but model currently supports version 3.1.10
    • charm requires all of the following:
      • charm requires feature “juju” (version >= 3.5.1) but model currently supports version 3.1.10

Is it okay if i update the document to install the juju version 3.4?

Yes, please. In fact, I think it would be best if this how-to guide didn’t recommend any particular version. (The tutorial right now requires version 3.4 but we can just tell people that, as they follow this how-to guide, they should install version 3.4.) Thanks! PS Please also add your name to the list of contributors on the bottom of the doc.

1 Like

how do you suggest that we tell people to install 3.4 version?

In Get started with Juju (link should take you to the setup section). Specifically, in the See more note. But: I’d like to check which version the automatic path installs now, so leave that to me. For now, if you want, you can update this doc to remove the channel bit or give the command with the explicit channel bit in it as an alternative.