Kubernetes Level 02 Day 07: Deploy Grafana on Kubernetes Cluster¶
This document outlines the solution for Kubernetes Level 02 Day 07. The objective was to provision Grafana, a powerful open-source analytics and interactive visualization web application, on a Kubernetes cluster and expose its UI using a NodePort Service.
Table of Contents¶
Task Overview¶
Objective: Deploy Grafana and ensure its web interface is accessible.
- Deployment:
- Name:
grafana-deployment-xfusion - Image: Any Grafana image (e.g.,
grafana/grafana:latest)
- Name:
- Service:
- Type:
NodePort - NodePort:
32000
- Type:
Step-by-Step Solution¶
1. Define the Manifests¶
Create a single YAML file containing both the Deployment and the Service configurations. Grafana typically runs its web interface on port 3000 internally.
Command:
vi grafana-setup.yaml
Content:
apiVersion: apps/v1
kind: Deployment
metadata:
name: grafana-deployment-xfusion
labels:
app: grafana
spec:
replicas: 1
selector:
matchLabels:
app: grafana
template:
metadata:
labels:
app: grafana
spec:
containers:
- name: grafana-container
image: grafana/grafana:latest
ports:
- containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
name: grafana-service
spec:
type: NodePort
selector:
app: grafana
ports:
- port: 3000
targetPort: 3000
nodePort: 32000
2. Apply the Configuration¶
Apply the declarative configuration to the cluster.
Command:
kubectl apply -f grafana-setup.yaml
deployment.apps/grafana-deployment-xfusion created
service/grafana-service created
3. Verification and Access¶
Wait for the Grafana pod to fully initialize and become ready.
Check Pod Status:
kubectl get pods -l app=grafana
1/1 under the READY column.
Check Service Configuration:
kubectl get svc grafana-service
3000:32000/TCP.
Access the UI:
Once the pod is running, open the provided browser or web view feature in your lab environment. Access Grafana via the Node's IP address on port 32000 (e.g., http://<node-ip>:32000).
You should see the Grafana login screen. (Default credentials are usually admin / admin).
Deep Dive: Grafana Deployment¶
Port Mappings¶
Understanding the port routing is crucial for web applications in Kubernetes:
* containerPort: 3000: Grafana's default web server port inside the container.
* targetPort: 3000: Where the Service sends traffic to (matching the containerPort).
* port: 3000: The port the Service exposes internally within the cluster.
* nodePort: 32000: The external port opened on all physical nodes to allow access from outside the cluster.
Stateful vs Stateless¶
In this basic deployment, Grafana is running statelessly. If the pod restarts, any dashboards or data sources created manually through the UI will be lost because we didn't attach a Persistent Volume (PV). For production setups, a Persistent Volume must be mounted to /var/lib/grafana to ensure analytics configurations are saved permanently.