Juju 101 - Debugging [11/12]

Overview

Duration: 3:00

Before you get started!

Welcome to the world of operators! In this series of tutorials, we will walk you through all the necessary steps to explore and learn operators through some basic examples. Starting with just your laptop, you will learn how to use operators for sophisticated application management at scale, across multiple Kubernetes clusters, cloud virtual machines and ultimately bare metal.

This tutorial assumes you have successfully completed the previous tutorial - Logs.

Explore other tutorials >

What are operators?

Operators are pieces of software designed to drive other software. They encapsulate the operational code of applications, shifting the burden of lifecycle management from configuration files and manual steps to an operator instance. Operators are a form of automation that is gaining popularity in the Kubernetes domain where traditional configuration management does not work. However, operators work with traditional virtual and bare metal machines as well.

Learn more about operators >

What is Juju OLM?

Juju is a universal Operator Lifecycle Manager (OLM) which provides services to operators. It provides resources for operators, deploys them, manages their lifecycle, delivers configuration updates, etc. Juju OLM is universal, meaning that it supports container substrates like Kubernetes as well as traditional machine substrates like bare metal, VMware, OpenStack, or public cloud instances.

Learn more about Juju OLM >

In this tutorial, you will learn how to:

  • Debug operator code

You will only need:

  • A machine with 8 GB of RAM

  • Microk8s and Juju installed

  • Juju controller bootstrapped on MicroK8s

  • The Prometheus and Grafana operators deployed to Microk8s

Debug operator code

Duration: 7:00

Juju OLM provides a set of services for operators, including operator code debugging capabilities.

In the following steps, we will debug the XYZ operator using Juju CLI.

First, let’s see what the current status of the model is:


$ juju status
Model       Controller  Cloud/Region        Version  SLA          Timestamp
monitoring  mk8s        microk8s/localhost  2.9.10   unsupported  09:38:52+01:00

App             Version                 Status  Scale  Charm           Store     Channel  Rev  OS          Address        Message
prometheus-k8s  prom/prometheus:latest  active      1  prometheus-k8s  charmhub  stable     1  kubernetes  10.152.183.44

Unit               Workload  Agent  Address     Ports     Message
prometheus-k8s/0*  active    idle   10.1.98.47  9090/TCP

We can use juju debug-code command to launch a TMUX session to debug hooks and actions. Let’s run it against the prometheus-k8s/0 unit and capture the code once the “config-changed” hook is executing:

$ juju debug-code --at=hook prometheus-k8s/0 config-changed

A TMUX session is launched. You should see the following output:

root@prometheus-operator-0:/var/lib/juju# ERROR EOF

From another terminal window let’s run the following command:

$ juju config prometheus-k8s port=19090

This command, as we already know, changes the value of the “port” option for the “prometheus” application through the operator. As a result, the “config-changed” hook gets triggered for execution.

In the terminal window with the TMUX session, you should see the following output


2021-02-08 10:24:40,768 DEBUG Operator Framework 1.1.0 up and running.
2021-02-08 10:24:40,822 DEBUG Legacy hooks/config-changed does not exist.
2021-02-08 10:24:40,873 DEBUG Using controller storage: JUJU_VERSION=2.9-rc5
2021-02-08 10:24:40,973 DEBUG Initializing Charm
2021-02-08 10:24:41,126 DEBUG Emitting Juju event config_changed.
Starting pdb to debug charm operator.
Run `h` for help, `c` to continue, or `exit`/CTRL-d to abort.
Future breakpoints may interrupt execution again.
More details at https://discourse.jujucharms.com/t/debugging-charm-hooks
> /var/lib/juju/agents/unit-prometheus-0/charm/src/charm.py(43)_on_config_changed()
-> self._configure_pod()
(Pdb)

As you can see pdb is started to debug the operator code. It catches “config-changed” hook at line 43 of /var/lib/juju/agents/unit-prometheus-0/charm/src/charm.py file, calling the “self._configure_pod() method”:

self._configure_pod()

In order to exit pdb execute the following command:

exit

In order to exit the TMUX session execute the following command:

exit

Next steps

Duration: 2:00

Congratulations! You have reached the end of this tutorial.

You can now move to the next tutorial - “12. Code structure” - or explore other tutorials.

In this tutorial you have learnt how to:

  • Debug operator code

Where to go from here?