Integrating Prometheus and Grafana
DevOps Assembly Line — Task 5
What is Prometheus?
Prometheus is an open-source tool; it is used to system monitoring and alerting.
What is Grafana?
Grafana supports querying Prometheus with beautiful personalized dashboards, graphs, and UI.
Task Overview-
Integrate Prometheus and Grafana and perform in the following way:
1. Deploy them as pods on top of Kubernetes by creating resources Deployment, Replica Set, Pods or Services
2. And make their data to remain persistent
3. And both of them should be exposed to the outside world.
Step 1
Service code for exposing the Prometheus-deployment
apiVersion: v1
kind: Service
metadata:
name: prometheus-service
labels:
app: prometheus1
spec:
ports:
- nodePort: 30851
port: 9090
targetPort: 9090
selector:
app: prometheus1type: LoadBalancer
Step 2
Persistent volume claim (PVC)
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: prometheus-pv-claim
labels:
app: prometheus1
spec:
accessModes:
- ReadWriteOnce
resources:
requests:storage: 20Gi
Step 3
Config map code for scrapping and permanenting the prometheus.yml file.
kind: ConfigMap
apiVersion: v1
metadata:
name: prometheus-script
data:
prometheus.yml: |
global:
scrap_interval: 15s
evaluation_interval: 15s
altering:
altermanagers:
- static_configs:
- targets:
rule_files:
scrap_configs:
- job_name: 'prometheus-node'
static_configs:
- targets: ['localhost:9090']
- job_name: 'testing_node'
static_configs:
- targets: ['192.168.99.103:9100']
labels:app: 'prometheus'
Step 4
Deployment code of Prometheus.
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: prometheuspod
labels:
app: prometheus1
spec:
selector:
matchLabels:
app: prometheus1
strategy:
type: Recreate
template:
metadata:
labels:
app: prometheus1
spec:
containers:
- image: vish1234/prometheus:p2
name: prometheus1
ports:
- containerPort: 9090
name: prometheus1
volumeMounts:
- name: prom-script
mountPath: /etc/prometheus/prometheus.yml
subPath: prometheus.yml
- name: prometheus-persistent-storage
mountPath: /etc/prometheus
volumes:
- name: prometheus-persistent-storage
persistentVolumeClaim:
claimName: prometheus-pv-claim
- name: prom-script
configMap:
name: prometheus-scriptdefaultMode: 0744
Step 5
We will write a service code for Grafana.
apiVersion: v1
kind: Service
metadata:
name: grafana-service
labels:
app: grafana
spec:
ports:
- port: 3000
selector:
app: grafanatype: LoadBalancer
Step 6
PVC
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: grafana-pv-claim
labels:
app: grafana
spec:
accessModes:
- ReadWriteOnce
resources:
requests:storage: 20Gi
Step 7
Deployment of Grafana
apiVersion: apps/v1
kind: Deployment
metadata:
name: grafanapod
labels:
app: grafana
spec:
selector:
matchLabels:
app: grafana
strategy:
type: Recreate
template:
metadata:
labels:
app: grafana
spec:
containers:
- image: vimal13/grafana
name: grafana
ports:
- containerPort: 3000
name: grafana
volumeMounts:
- name: grafana-persistent-storage
mountPath: /var/lib/grafana
volumes:
- name: grafana-persistent-storage
persistentVolumeClaim:claimName: grafana-pv-claim
Step 8
I have run the following commands for running both the files.
kubectl apply -f prometheus.yml
kubectl apply -f grafana.yml