Deploy Grafana and Prometheus on Kubernetes in Minutes
This guide walks you through preparing a Kubernetes cluster, creating deployment manifests, configuring Grafana and Prometheus, and verifying the monitoring setup, including code snippets and step‑by‑step commands for a seamless installation on a lightweight cloud server.
Introduction
Kubernetes is a powerful container orchestration system that lets you deploy, manage, and scale containerized applications. For monitoring and visualization, Grafana and Prometheus are useful tools. This article guides readers through installing and configuring Grafana and Prometheus on a Kubernetes cluster.
Prerequisites
Before installing, understand the basic concepts of Grafana and Prometheus.
Grafana
Grafana visualizes monitoring data, providing dashboards that help understand performance, health, and trends. When integrated with Prometheus, it can display metrics collected by Prometheus.
Prometheus
Prometheus is an open‑source monitoring and alerting system that scrapes metrics from targets, stores them locally, and offers query and alert capabilities, making it suitable for cloud‑native environments.
Prepare Kubernetes Cluster
Obtain a lightweight cloud server and set up a Kubernetes cluster (for example, using k3s).
Create Deployment Manifests
Save the following YAML as grafpro.yaml and adjust parameters as needed.
kind: Deployment
apiVersion: apps/v1
metadata:
name: &name grafpro
labels:
app: *name
spec:
selector:
matchLabels:
app: *name
template:
metadata:
labels:
app: *name
spec:
containers:
- name: grafana
image: grafana/grafana
securityContext:
runAsUser: 0
ports:
- containerPort: 3000
volumeMounts:
- name: *name
subPath: grafana
mountPath: /var/lib/grafana
- name: prometheus
image: prom/prometheus
securityContext:
runAsUser: 0
ports:
- containerPort: 9090
volumeMounts:
- name: *name
subPath: etc
mountPath: /etc/prometheus
- name: *name
subPath: prometheus
mountPath: /prometheus
volumes:
- name: *name
hostPath:
path: /srv/grafpro
type: DirectoryOrCreate
---
kind: Service
apiVersion: v1
metadata:
name: &name grafpro
labels:
app: *name
spec:
selector:
app: *name
ports:
- name: grafana
port: 3000
targetPort: 3000
- name: prometheus
port: 9090
targetPort: 9090
---
kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
name: &name grafpro
annotations:
traefik.ingress.kubernetes.io/router.entrypoints: web,websecure
spec:
rules:
- host: grafana.example.org
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: *name
port:
name: grafana
- host: prometheus.example.org
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: *name
port:
name: prometheus
tls:
- secretName: defaultSave the following shell script as grafpro.sh and modify the variables GRAFPRO_STORAGE, GRAFANA_DOMAIN, and PROMETHEUS_DOMAIN accordingly.
export GRAFPRO_STORAGE=${GRAFPRO_STORAGE:-"/srv/grafpro"}
export GRAFANA_DOMAIN=${GRAFPRO_DOMAIN:-"grafana.example.org"}
export PROMETHEUS_DOMAIN=${PROMETHEUS_DOMAIN:-"grafana.example.org"}
mkdir -p $GRAFPRO_STORAGE/etc
cat <<EOF >$GRAFPRO_STORAGE/etc/prometheus.yml
global:
scrape_interval: 5s
scrape_timeout: 5s
evaluation_interval: 15s
scrape_configs:
- job_name: prometheus
static_configs:
- targets:
- 127.0.0.1:9090
EOF
cat grafpro.yaml | \
sed "s#/srv/grafpro#$GRAFPRO_STORAGE#g" | \
sed "s#grafana.example.org#$GRAFANA_DOMAIN#g" | \
sed "s#prometheus.example.org#$PROMETHEUS_DOMAIN#g" | \
kubectl apply -f -Deploy Grafana and Prometheus
SSH into the server and run:
chmod +x grafpro.sh
./grafpro.shTest and Verify
Grafana
Open http://grafana.example.org in a browser, log in with username admin and password admin, change the password when prompted, then configure the Prometheus data source at http://127.0.0.1:9090 and import the “Prometheus 2.0 Stats” dashboard.
Prometheus
Open http://prometheus.example.org to access the query UI. In production, enable Basic Auth to protect the data.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
