Observability is a crucial aspect of managing Kubernetes operators effectively. By integrating Kube-Prometheus, you can gain valuable insights into your operator’s health, monitor resource usage, and set up alerting rules to improve reliability. In this guide, we’ll explore how to use Jsonnet Bundler (jb) to integrate Kube-Prometheus into your Kubernetes operator in an efficient and scalable manner.
What is jb (Jsonnet Bundler)?
Jsonnet Bundler (jb) is a package manager for Jsonnet, a powerful templating language used to manage Kubernetes configurations. With jb, you can easily install and manage Kube-Prometheus, a comprehensive monitoring stack that includes Prometheus Operator, Alertmanager, Grafana, and ServiceMonitors.
Why Use jb for Kube-Prometheus?
Simplifies Kube-Prometheus installation and management.
Automates Kubernetes manifest generation from Jsonnet.
Allows easy customization of monitoring configurations.
Prerequisites
Before proceeding, ensure you have the following installed:
A Kubernetes cluster (Minikube, Kind, or a cloud-based cluster)
kubectl (CLI tool for interacting with Kubernetes)
go (Required for operator development)
jsonnet and jsonnet-bundler (jb)
Installing jb and jsonnet
If you haven’t installed Jsonnet Bundler, install it with:
go install github.com/jsonnet-bundler/jsonnet-bundler/cmd/jb@latest
Verify installation:
jb --version
If jsonnet is not installed, install it using:
brew install jsonnet # MacOS
sudo apt install jsonnet # Ubuntu/Debian
Step 1: Initialize jb in Your Operator Project
Navigate to your operator project directory and initialize jb:
cd my-operator # Navigate to your operator project root
jb init # Initialize Jsonnet Bundler
This creates a jsonnetfile.json file, which tracks dependencies.
Step 2: Add Kube-Prometheus as a Dependency
Install Kube-Prometheus as a Jsonnet dependency using jb:
jb install github.com/prometheus-operator/kube-prometheus/jsonnet/kube-prometheus@main
This command will:
Fetch the Kube-Prometheus package from GitHub.
Store it in the vendor/ directory.
Update jsonnetfile.lock.json with the package version.
Verify the dependency installation:
ls vendor/kube-prometheus
You should see Jsonnet files for dashboards, alerting rules, and ServiceMonitors.
Step 3: Download and Update Example Jsonnet File
Instead of manually generating manifests, we can download an example Jsonnet configuration file and a build script for easier customization.
Download the Example Jsonnet File
curl -o example.jsonnet https://raw.githubusercontent.com/prometheus-operator/kube-prometheus/main/example.jsonnet
Download the Build Script
curl -o build.sh https://raw.githubusercontent.com/prometheus-operator/kube-prometheus/main/build.sh
chmod +x build.sh
Customize example.jsonnet
Open example.jsonnet and update the namespace where Prometheus and Alertmanager will be deployed, and define which namespaces Prometheus should watch:
local kp =
(import 'kube-prometheus/main.libsonnet') +
{
values+:: {
common+: {
namespace: 'monitoring',
},
prometheus+: {
namespaces: ['monitoring'],
}
},
};
Step 4: Build and Apply the Manifests
Generate Kubernetes YAML manifests using the build.sh script:
./build.sh example.jsonnet
Note: If you see the error --: gojsontoyaml: command not found, install the required tool:
go install github.com/brancz/gojsontoyaml@latest
Apply the Kube-Prometheus Stack to Kubernetes:
kubectl apply --server-side -f manifests/setup/
kubectl apply -f manifests/
This will deploy:
Prometheus Operator
Prometheus instance
Alertmanager
Grafana dashboards
ServiceMonitors for monitoring Kubernetes components
Step 5: Verify Monitoring Setup
1. Port-forward Prometheus to Access the UI
kubectl port-forward svc/prometheus-k8s 9090:9090 -n monitoring
2. Query Metrics in Prometheus UI
Open http://localhost:9090 in a browser.
Search for your custom metrics (e.g., my_operator_reconcile_count).
3. View Logs in Prometheus Pod
kubectl logs -l app=prometheus -n monitoring
Conclusion
By following this guide, we have successfully:
✅ Integrated Kube-Prometheus into our Kubernetes operator project.
✅ Downloaded and customized an example Jsonnet configuration.
✅ Used the build.sh script to generate and apply Kubernetes manifests.
✅ Configured ServiceMonitor to track our operator’s metrics.
With this setup, you now have a fully functioning Prometheus monitoring stack that provides deep insights into your operator’s performance and health. 🚀
Have questions or need help? Drop a comment below! 👇
Top comments (0)