Content-cache

  • By Canonical IS DevOps
Channel Revision Published Runs on
latest/stable 37 30 Nov 2023
Ubuntu 22.04
latest/edge 44 30 Jan 2024
Ubuntu 22.04
juju deploy content-cache-k8s
Show information

Platform:

Charm architecture

At its core, Content-cache-k8s is a NGINX cache application that helps another charm serving static content for it.

The charm design leverages the sidecar pattern to allow multiple containers in each pod with Pebble running as the workload container’s entrypoint.

Pebble is a lightweight, API-driven process supervisor that is responsible for configuring processes to run in a container and controlling those processes throughout the workload lifecycle.

Pebble services are configured through layers, and the following containers represent each one a layer forming the effective Pebble configuration, or plan:

  1. An NGINX container with caching features, which can be used to efficiently serve static resources, as well as be the incoming point for all web traffic to the pod. This is the content cache container itself.
  2. A NGINX Prometheus Exporter container that can be used to provide statistics on web traffic.

As a result, if you run a kubectl get pods on a namespace named for the Juju model you’ve deployed the Content-cache-k8s charm into, you’ll see something like the following:

NAME                             READY   STATUS    RESTARTS   AGE
content-cache-k8s-0              3/3     Running   0          97s

This shows there are 3 containers - the two named above, as well as a container for the charm code itself.

And if you run kubectl describe pod content-cache-k8s-0, all the containers will have as Command /charm/bin/pebble. That’s because Pebble is responsible for the processes startup as explained above.

Containers

Configuration files for the containers can be found in the files directory of the charm repository and in the templates directory of the charm repository

Content cache

This container is the entry point for all web traffic to the pod (on port 80). Serves static files directly.

The workload that this container is running is defined in the Content-cache dockerfile in the charm repository.

NGINX prometheus exporter

This container runs the nginx/nginx-prometheus-exporter image.

The NGINX Prometheus Exporter is started with -nginx.scrape-uri=http://localhost:9080/stub_status so will scrape metrics from the NGINX container.

This has been configured in the NGINX container to return NGINX’s stub_status. The exporter listens on port 9113 and metrics about web traffic to the pod can be scraped by Prometheus there.

Docker images

The image defined in Content-cache dockerfile in the charm repository is published to Charmhub, the official repository of charms.

This is done by publishing a resource to Charmhub as described in the Juju SDK How-to guides.

Integrations

Grafana

Grafana is an open-source visualization tool that allows to query, visualize, alert on, and visualize metrics from mixed datasources in configurable dashboards for observability. This charm is shipped with its own Grafana dashboard and supports integration with the Grafana Operator to simplify observability.

Ingress

The Content-cache charm also supports being integrated with Ingress by using NGINX Ingress Integrator.

In this case, an existing Ingress controller is required. For more information, see Adding the Ingress Relation to a Charm.

Loki

Loki is an open-source fully-featured logging system. This charm is shipped with support for the Loki Operator to collect the generated logs.

Prometheus

Prometheus is an open-source system monitoring and alerting toolkit with a dimensional data model, flexible query language, efficient time series database, and modern alerting approach. This charm is shipped with a Prometheus exporter, alerts, and support for integrating with the Prometheus Operator to automatically scrape the targets.

Juju events

Accordingly to the Juju SDK: “an event is a data structure that encapsulates part of the execution context of a charm”.

For this charm, the following events are observed:

  1. _pebble_ready: fired on Kubernetes charms when the requested container is ready. Action: wait for the integrations, and configure the containers.
  2. config_changed: usually fired in response to a configuration change using the CLI. Action: wait for the integrations, validate the configuration, update Ingress, and restart the containers.
  3. report_visits_by_ip: fired when report-visits-by-ip action is executed. Action: Report the amount of visits grouped by IP that have visited the service ordered by amount of visits.

Charm code overview

The src/charm.py is the default entry point for a charm and has the ContentCacheCharm Python class which inherits from CharmBase.

CharmBase is the base class from which all Charms are formed, defined by Ops (Python framework for developing charms).

See more information in Charm.

The __init__ method guarantees that the charm observes all events relevant to its operation and handles them.

Take, for example, when a configuration is changed by using the CLI.

  1. User runs the command
juju config smtp_login=user1
  1. A config-changed event is emitted
  2. In the __init__ method is defined how to handle this event like this:
self.framework.observe(self.on.config_changed, self._on_config_changed)
  1. The method _on_config_changed, for its turn, will take the necessary actions such as waiting for all the relations to be ready and then configuring the containers.

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