Deploying Prometheus And Grafana in Kubernetes

kanav Gupta
3 min readAug 5, 2020

--

Task :-

  1. Deploy them as pods on top of Kubernetes by creating resources Deployment, Replica Sets, pods and Services .
  2. And make their data to be remain persistent .
  3. 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

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response