Deploying Prometheus And Grafana in Kubernetes
3 min readAug 5, 2020
Task :-
- Deploy them as pods on top of Kubernetes by creating resources Deployment, Replica Sets, pods and Services .
- And make their data to be remain persistent .
- And both of them should be exposed to the outside world .
Project Description
- ConfigMap for attaching config file with prometheus.
apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-configmap
labels:
app: prometheus
data:
prometheus.yml: |
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- PVC resource to make Prometheus data persistent
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: prometheus-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 3Gi
- Service for prometheus.
apiVersion: v1
kind: Service
metadata:
name: prometheus-service
labels:
app: prometheus
spec:
ports:
- port: 9090
selector:
app: prometheus
type: NodePort
ports:
- port: 9090
nodePort: 31005
name: prom-port
- Deployment to launch and expose Prometheus
apiVersion: apps/v1
kind: Deployment
metadata:
name: prometheus-deploy
labels:
app: prometheus
spec:
replicas: 1
selector:
matchLabels:
app: prometheus
strategy:
type: Recreate
template:
metadata:
labels:
app: prometheus
spec:
containers:
- image: vimal13/prometheus
name: prometheus
ports:
- containerPort: 9090
name: prometheus
volumeMounts:
- name: prometheus-config
mountPath: etc/prometheus/prometheus.yml
subPath: prometheus.yml
- name: prometheus-storage
mountPath: /prometheus
volumes:
- name: prometheus-config
configMap:
name: prometheus-config
- name: prometheus-storage
persistentVolumeClaim:
claimName: prometheus-vpc
Complete prometheus.yml file
apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-configmap
labels:
app: prometheus
data:
prometheus.yml: |
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: prometheus-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 3Gi
---
apiVersion: v1
kind: Service
metadata:
name: prometheus-service
labels:
app: prometheus
spec:
ports:
- port: 9090
selector:
app: prometheus
type: NodePort ports:
- port: 9090
nodePort: 31005
name: prom-port
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: prometheus-deploy
labels:
app: prometheus
spec:
replicas: 1
selector:
matchLabels:
app: prometheus
strategy:
type: Recreate
template:
metadata:
labels:
app: prometheus
spec:
containers:
- image: vimal13/prometheus
name: prometheus
ports:
- containerPort: 9090
name: prometheus
volumeMounts:
- name: prometheus-config
mountPath: etc/prometheus/prometheus.yml
subPath: prometheus.yml
- name: prometheus-storage
mountPath: /prometheus
volumes:
- name: prometheus-config
configMap:
name: prometheus-config
- name: prometheus-storage
persistentVolumeClaim:
claimName: prometheus-pvc
- Service for grafana
apiVersion: v1
kind: Service
metadata:
name: grafana-service
labels:
app: grafana
spec:
ports:
- port: 3000
selector:
app: grafana
type: NodePort
- PVC for grafana
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: grafana-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 3Gi
- Deployment resource for launch, expose and make Grafana persistent
apiVersion: apps/v1
kind: Deployment
metadata:
name: grafana-deploy
labels:
app: grafana
spec:
replicas: 1
selector:
matchLabels:
app: grafana
strategy:
type: Recreate
template:
metadata:
labels:
app: grafana
spec:
containers:
- image: grafana/grafana
name: grafana
ports:
- containerPort: 3000
name: grafana
volumeMounts:
- name: grafana-storage
mountPath: "/var/lib/grafana"
volumes:
- name: grafana-storage
persistentVolumeClaim:
claimName: grafana-pvc
Complete grafana.yml file
apiVersion: v1
kind: Service
metadata:
name: grafana-service
labels:
app: grafana
spec:
ports:
- port: 3000
selector:
app: grafana
type: NodePort
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: grafana-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 3Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: grafana-deploy
labels:
app: grafana
spec:
replicas: 1
selector:
matchLabels:
app: grafana
strategy:
type: Recreate
template:
metadata:
labels:
app: grafana
spec:
containers:
- image: grafana/grafana
name: grafana
ports:
- containerPort: 3000
name: grafana
volumeMounts:
- name: grafana-storage
mountPath: "/var/lib/grafana"
volumes:
- name: grafana-storage
persistentVolumeClaim:
claimName: grafana-pvc
- Here I use kustomization.yml file for launch the whole setup in one click
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- prometheus.yml
- grafana.yml
We have to start kubernetes

Launching all resources



