Helm charts in K8s.

Jyoti Pawar
7 min readJun 12, 2022

1.๐‡๐ž๐ฅ๐ฆ :

Helm helps you manage Kubernetes applications. Helm Charts help you define, install, and upgrade even the most complex Kubernetes application.

Charts are easy to create, version, share, and publish. so start using Helm and stop the copy-and-paste. Helm is a graduated project in the CNCF and is maintained by the Helm community.

2. ๐๐ซ๐จ๐ฆ๐ž๐ญ๐ก๐ž๐ฎ๐ฌ :

Prometheus is an open-source systems monitoring and alerting toolkit originally built at SoundCloud.

Prometheus works well for recording any purely numeric time series. It fits both machine-centric monitoring as well as monitoring of highly dynamic service-oriented architectures. In a world of microservices, its support for multi-dimensional data collection and querying is a particular strength.

3. ๐†๐ซ๐š๐Ÿ๐š๐ง๐š :

Grafana is open source visualization and analytics software. It allows you to query, visualize, alert on, and explore your metrics no matter where they are stored.

In plain English, it provides you with tools to turn your time-series database (TSDB) data into beautiful graphs and visualizations.

Now we are going to see how we can deploy Prometheus & Grafana in K8S using Helm chart.

๐Ÿ’ก For Deployment we need pod in K8S & for pod we need Docker Image. So first will create customize Docker Image for Prometheus & Grafana.

๐ŸŽฏ ๐ƒ๐จ๐œ๐ค๐ž๐ซ ๐ˆ๐ฆ๐š๐ ๐ž ๐Ÿ๐จ๐ซ ๐๐ซ๐จ๐ฆ๐ž๐ญ๐ก๐ž๐ฎ๐ฌ :

๐Ÿ‘‰ First we will create Dockerfile for the image.

# vim Dockerfile

๐Ÿ‘‰ After this, we will type our code in that file for building the docker images.

FROM centos:latestCOPY prometheus-2.28.0.linux-amd64.tar.gz  /RUN tar -xvzf /prometheus-2.28.0.linux-amd64.tar.gzEXPOSE 9090CMD prometheus-2.28.0.linux-amd64/./prometheus  --config.file=prometheus-
2.28.0.linux-amd64/prometheus.yml

๐Ÿ‘‰ Then we will build the Docker image.

# docker build -t jyotipawar/prometheus_setup:v1 .

๐Ÿ‘‰ After building , we need to login into docker hub using command line means we need to Authenticate with credentials.

# docker login

๐Ÿ‘‰ After login successful we will push image on Docker Hub.

# docker push jyotipawar/prometheus_setup:v1

๐Ÿ‘‰ Now we can use this Docker image for deployment in K8S. K8S automatically pick image from docker hub at the time of deployment, we need to simply type the image name.

๐Ÿ‘‰ You can also use this image for practicing.

# docker pull jyotipawar/prometheus_setup:v1

๐Ÿ‘‰ And whatever we do for prometheus docker image, same step you can also do for grafana. Now I am only providing code for Docker image.

FROM centosRUN yum install wget -yRUN wget https://dl.grafana.com/oss/release/grafana-8.0.6-1.x86_64.rpmRUN yum install grafana-8.0.6-1.x86_64.rpm -yCMD /usr/sbin/grafana-server --pidfile=/var/run/grafana-server.pid --config=/etc/grafana/grafana.ini --homepath=/usr/share/grafana cfg:default.paths.data=/var/lib/grafana cfg:default.paths.logs=/var/log/grafana cfg:default.paths.plugins=/var/lib/grafana/pluginsEXPOSE 3000

๐Ÿ‘‰ Now we have Done with Docker Images. Next we will create Helm Chart. For this we need helm software.

# wget https://get.helm.sh/helm-v3.6.2-linux-amd64.tar.gz

๐Ÿ‘‰ After this we need to untar helm software.

# tar -xvzf helm-v3.6.2-linux-amd64.tar.gz

๐Ÿ‘‰ Then, copy linux-amd64/helm file in the /usr/bin/

# cp linux-amd64/helm  /usr/bin

๐Ÿ‘‰ Then, using version command we will ensure helm is installed or not.

# helm version

๐Ÿ”ฐ From here, we will start to create Helm chart.

๐Ÿ“ Helm uses a packaging format called charts. A chart is a collection of files that describe a related set of Kubernetes resources.

๐Ÿ“ Charts are created as files laid out in a particular directory tree. They can be packaged into versioned archives to be deployed.

๐Ÿ“ A chart is organized as a collection of files inside of a directory. The directory name is the name of the chart.

๐Ÿ‘‰ So here we will create directory.

# mkdir prometheus

๐Ÿ‘‰ In prometheus directory we will create Chart.yaml file.

# vim Chart.yaml
apiVersion: v1
name: prometheus
version: 0.1
appVersion: 1.1
description: This is a Helm Chart for Prometheus Setup.

๐Ÿ‘‰ All template files are stored in a chartโ€™s templates/ folder. So created templates folder in particular chart directory folder.

# mkdir templates

๐Ÿ‘‰ In templates folder we will create deployment.yaml and service.yaml file.

๐Ÿ‘‰ First we will create deployment.yaml.

# kubectl create deployment prometheus --image=jyotipawar/prometheus_setup:v1 --replicas=3 -n helm --dry-run -o yamlapiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: prometheus
name: prometheus
namespace: helm
spec:
replicas: 3
selector:
matchLabels:
app: prometheus
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: prometheus
spec:
containers:
- image: jyotipawar/prometheus_setup:v1
name: prometheus-setup-c5lds
resources: {}
status: {}

โšœ๏ธ Dry run mode allows you to run commands without side effects to test the actual command you want to run and using -o yaml create yaml code.

๐Ÿ‘‰ After this we will save that code in deployment.yaml file.

# kubectl create deployment prometheus --image=jyotipawar/prometheus_setup:v1 --replicas=3 -n helm --dry-run -o yaml > deployment.yaml

๐Ÿ‘‰ Then we will install helm package.

# helm list -n helm

# helm install prometheus prometheus/ -n helm

๐Ÿ‘‰ Now we will create service.yaml file.

# kubectl expose deployment prometheus --port=9090 --type=NodePort -n helm --dry-run -o yamlapiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: prometheus
app.kubernetes.io/managed-by: Helm
name: prometheus
namespace: helm
spec:
ports:
- port: 9090
protocol: TCP
targetPort: 9090
selector:
app: prometheus
type: NodePort
status:
loadBalancer: {}

๐Ÿ‘‰ After this save this file.

# kubectl expose deployment prometheus --port=9090 --type=NodePort -n helm --dry-run -o yaml > service.yaml

๐Ÿ‘‰ After expose deployment, we already install prometheus package. But after changing some we will upgrade that package. For this we need to change Chart.yaml file. In that file we need to change version. Initially version was 0.1 then we will change to 0.2.

# vim Chart.yamlapiVersion: v1
name: prometheus
version: 0.2
appVersion: 1.1
description: This is a Helm Chart for Prometheus Setup.

๐Ÿ‘‰ After changing version we will upgrage prometheus package.

# helm upgrade prometheus prometheus/ -n helm

โœ… Now we done with prometheus , now we will start creating package for grafana.

๐Ÿ‘‰ Again we need deployment for grafana.

# kubectl create deployment grafana --image=jyotipawar/grafana_setup:v1 --replicas=3 -n helm --dry-run -o yamlapiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: grafana
name: grafana
namespace: helm
spec:
replicas: 3
selector:
matchLabels:
app: grafana
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: grafana
spec:
containers:
- image: jyotipawar/grafana_setup:v1
name: grafana-setup-2w8sv
resources: {}
status: {}

๐Ÿ‘‰ Then we will save code in file.

# kubectl create deployment grafana --image=jyotipawar/grafana_setup:v1 --replicas=3 -n helm --dry-run -o yaml > deployment.yaml

๐Ÿ‘‰ Then we will install helm package.

# helm install grafana grafana/ -n helm

๐Ÿ‘‰ Now we will create service.yaml file.

# kubectl expose deployment grafana --port=3000 --type=NodePort -n helm --dry-run -o  yamlapiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: grafana
app.kubernetes.io/managed-by: Helm
name: grafana
namespace: helm
spec:
ports:
- port: 3000
protocol: TCP
targetPort: 3000
selector:
app: grafana
type: NodePort
status:
loadBalancer: {}

๐Ÿ‘‰ After this we will save that code in service.yaml file.

# kubectl expose deployment grafana --port=3000 --type=NodePort -n helm --dry-run -o yaml > service.yaml

๐Ÿ‘‰ Same thing we need to do whatever we did in prometheus Chart.yaml file. We will change version 0.1 to 0.2.

# vim Chart.yamlapiVersion: v1
name: prometheus
version: 0.2
appVersion: 1.1
description: This is a Helm Chart for Grafana Setup.

๐Ÿ‘‰ After changing version we will upgrage grafana package by the cmd below :

# helm upgrade grafana grafana/ -n helm

Now finally we will see all deployments, pods and servicce that will automatically generate using helm chart.

โ„๏ธWhatever we do till now that setup is done in client system of Kubernetes(K8S) cluster. In that cluster we have 1 Master and 1 Slave Node. And for client there is a namaspace ie. โ€œhelmโ€. K8S master provide all power/access to that namespace user.

๐Ÿ“ Now for accessing prometheus and grafana app we need slave node public ip.

โœ… Now we have done with deploy prometheus and grafana in k8s.

๐ŸŽฏ Now we will publish helm chart.

For this create one directory named charts. Make sure this directory should be inside prometheus/ directory and grafana/.

# mkdir charts

๐Ÿ‘‰ Now run the following command.

# helm package /HelmCharts/prometheus/ -d charts/# helm package /HelmCharts/grafana/ -d charts/

๐Ÿ‘‰ In charts folder it will create tar file of that perticular chart.

โšœ๏ธ This is Prometheus.

โšœ๏ธ This is Grafana.

๐Ÿ“ For every Helm repository, we must require an index.yaml file. The index.yaml file contains the information about the chart.

โšœ๏ธ This is Prometheus.

apiVersion: v1
entries:
prometheus:
- apiVersion: v1
appVersion: "1.1"
created: "2022-03-16T12:53:00.188064254+05:30"
description: This is a Helm Chart for Prometheus Setup.
digest: de54c1adceaf3d31c65d63477b96733e9989ad211709c86e43bc9cad50125237
name: prometheus
urls:
- prometheus-0.2.tgz
version: "0.2"
generated: "2022-03-16T12:53:00.18666868+05:30"

โšœ๏ธ This is Grafana.

apiVersion: v1
entries:
grafana:
- apiVersion: v1
appVersion: "1.1"
created: "2022-03-16T12:52:18.254278376+05:30"
description: This is a Helm Chart for Grafana Setup.
digest: a02be92fe10e11ce53085eab24bea9e29f068c4595f276c4c534873a998ad4dd
name: grafana
urls:
- grafana-0.2.tgz
version: "0.2"
generated: "2022-03-16T12:52:18.253523268+05:30"

๐Ÿ’ก Now we will upload package on github.

# git init # git add . # git commit -m "commit"# git branch -M master # git remote add origin URL # git push -u origin master

๐Ÿ‘‰ After pushing it, go to the Github repository and click on setting then GitHub Pages.

๐Ÿ‘‰ To activate the GitHub page, you have to select branch first which you want to activate then save it.

โšœ๏ธNow we will publish Helm Chart to ArtifactHub.

In URL it should be like this :

https://username.github.io/repository_name/charts/

And then finaly we can see our helm chart.

Thanks for reading !!

Hope it was useful !!.

--

--

Jyoti Pawar

Devops || AWS || ML || Deep learning || Python || Flask || Ansible RH294 || OpenShift DO180