DEV Community

Cover image for Creating a Helm Chart for Grafana
Sachin Kumar
Sachin Kumar

Posted on • Updated on

Creating a Helm Chart for Grafana

Welcome to my article. You will see all about the integration of Dockerfile, Helm, Grafana, etc, in this article. So let's get started without delay.

Pre-requisite

To perform this scenario you will need mentioned platform.

Kubernetes Setup

To demonstrate this scenario, first of all, we have to install the Kubernetes setup then we can move ahead for further part. So I am installing the Kubernetes cluster on the top of AWS. let's launch the instance with mentioned configuration.

  • Ubuntu Server 18.04 LTS (HVM), SSD Volume Type
  • t2.xlarge Instance type
  • Minimum Storage 20 GiB

After launching AWS Instance, connect it with the help of any remote software eg. putty, etc, or ssh protocol and then follow the following steps.

๐Ÿ”ธ login with root power.

sudo su -
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ธ Install kubectl.

curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ธ Update the instance and install docker.

sudo apt-get update -y
sudo apt-get install docker.io -y
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ธ Install curl software to install Minikube.

sudo apt-get install curl -y
Enter fullscreen mode Exit fullscreen mode

What's Minikube?

Minikube is a utility you can use to run Kubernetes (k8s) on your local machine. It creates a single node cluster contained in a virtual machine (VM). This cluster lets you demo Kubernetes operations without requiring the time and resource-consuming installation of full-blown K8s.

๐Ÿ”ธ So let's install Minikube.

curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo chmod +x minikube
sudo mv minikube /usr/local/bin/
sudo apt install conntrack
Enter fullscreen mode Exit fullscreen mode

โš ๏ธ Note: Now go into sudo if not gone.

sudo -i
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ธ Start the Minikube

minikube start --vm-driver=none
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ธ Our single node cluster is ready to use so run the below command to check the minikube status.

minikube status
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’  you will get output such as:

root@ip-172-31-39-130:~# minikube status
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured
Enter fullscreen mode Exit fullscreen mode

Hopefully, your installation will had been completed. So now time is to move towards creating a Container image for Grafana. But before creating it, let's try to understand Grafana and Dockerfile.

What's Grafana ?

Grafana is a multi-platform open source analytics and interactive visualization web application. It provides charts, graphs, and alerts for the web when connected to supported data sources and End users can create complex monitoring dashboards using interactive query builders. Grafana is divided into a front end and back end, written in TypeScript and Go, respectively.

What's Dockerfile ?

Dockerfile is a simple text file that consists of instructions to build Docker images. Mentioned below is the syntax of a Dockerfile to creating Grafana Docker image.

๐Ÿ”ธ So create a file as Dockerfile and Ensure D should be capital in Dockerfile.

vim Dockerfile
Enter fullscreen mode Exit fullscreen mode

and write the below code inside Dockerfile.

FROM centos:7
RUN yum install wget -y
RUN wget https://dl.grafana.com/oss/release/grafana-7.0.3-1.x86_64.rpm
RUN yum install grafana-7.0.3-1.x86_64.rpm -y
WORKDIR /usr/share/grafana
CMD /usr/sbin/grafana-server start && /usr/sbin/grafana-server enable && /bin/bash
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ธ After writing the code, build the docker image with the help of following command.

docker build -t username/imagename:version .
Enter fullscreen mode Exit fullscreen mode

eg. # docker build -t hackcoderr/grafana:v1 .

๐Ÿ”ธ Now login to your DockerHub Account. But if you haven't DockerHub Account then first of all create it then move ahead with below command.

docker login
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ธ After it, just push your image to DockerHub so that you can use it in the future.

docker push username/imagename:version 
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’  After running above command, the output should be similar to the following:

root@ip-172-31-39-130:~# docker push hackcoderr/grafana:v1
The push refers to repository [docker.io/hackcoderr/grafana]
4b603ec3a2e0: Pushed
a1be9f0c6dee: Pushed
9d1af48bd5b4: Pushed
174f56854903: Mounted from library/centos
v1: digest: sha256:6bd02f99b6e905582286b344980b2f83c75348876a58eb15786fd5baab04ce0b size: 1166
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’  But if you don't want to create this container image then you can simply pull my pre-created image from the Dockerhub with help of docker pull command.

docker pull hackcoderr/grafana:v1
Enter fullscreen mode Exit fullscreen mode

โš ๏ธ We will use this image in the upcoming steps when we will create deployment.yaml file in the Helm chart. But before it, let's know about Helm.

What's Helm ?

So let's try to understand what helm is?

  • Helm is package manager for Kubernetes
  • Helm packages are called Charts.
  • Helm Charts help define, install and upgrade complex Kubernetes application.
  • Helm Charts can be versioned, shared, and published.
  • Helm Charts can accept input parameter.
    • Kubectl need template engine to do this (Kubernetes, jinja etc)
  • Popular packages already available.

Now let's see how we can install Helm in the cluster.

๐Ÿ”ธ Run the below commands to install Helm.

wget https://get.helm.sh/helm-v3.5.2-linux-amd64.tar.gz
tar -xvzf helm-v3.5.2-linux-amd64.tar.gz

Enter fullscreen mode Exit fullscreen mode

๐Ÿ’  The output should be similar to the following, after running the tar command.

root@ip-172-31-39-130:~# tar -xvzf helm-v3.5.2-linux-amd64.tar.gz
linux-amd64/
linux-amd64/helm
linux-amd64/LICENSE
linux-amd64/README.md
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ธ After this, copy linux-amd64/helm file in the /usr/bin/.

cp linux-amd64/helm /usr/bin/
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’  Also, Ensure that Helm is installed or not with the help of the helm version and the output should be similar to the following:

root@ip-172-31-39-130:~# helm version
version.BuildInfo{Version:"v3.5.2", GitCommit:"167aac70832d3a384f65f9745335e9fb40169dc2", GitTreeState:"dirty", GoVersion:"go1.15.7"}
Enter fullscreen mode Exit fullscreen mode

Create a Helm Chart

let's create a new Helm Chart from the scratch. Helm created a bunch of files for you that are usually important for a production-ready service in Kubernetes. To concentrate on the most important parts, we can remove a lot of the created files. Letโ€™s go through the only required files for this example.

Helm file structure

๐Ÿ”ธ Create a Helm Chart for Grafana.

mkdir grafana
cd grafana
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’  But here we need a project file that is called Chart.yaml and contains all the metadata information.
๐Ÿ”ธ So, create this file. Also, C should be capital in the Chart.yaml.

vim Chart.yaml
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ธ Write the below code inside Chart.yaml.

apiVersion: v2
name: Grafana
description: A Helm chart for Kubernetes
type: application
version: 0.1.0
appVersion: 1.16.0
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ธ Make a templates folder inside grafana and go inside it.

mkdir templates
cd templates
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ธ Use this command to create a code of the deployment.yaml file.

kubectl create deployment grafana --image=hackcoderr/grafana:v1 --dry-run -o yaml > deployment.yaml
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’  The output should be similar to the following:

root@ip-172-31-39-130:~/grafana/templates# kubectl create deployment grafana --image=hackcoderr/grafana:v1 --dry-run -o yaml > deployment.yaml
W0420 18:01:34.362388   20835 helpers.go:557] --dry-run is deprecated and can be replaced with --dry-run=client.
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ธ Go outside the grafana directory and install the helm chart.

cd 
helm install grafana grafana/
Enter fullscreen mode Exit fullscreen mode

โš ๏ธ Here grafana is the name of Helm Chart and grafana/ is the path of the chart.

๐Ÿ’  After running above command, the output should be similar to the following:

root@ip-172-31-44-81:~# helm install grafana grafana/
NAME: grafana
LAST DEPLOYED: Tue Apr 20 18:10:44 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ธ Now again go inside grafana/templates and use the below command to create a code of the service.yaml file.

cd grafana/templates/
kubectl expose deployment grafana --port=3000 --type=NodePort --dry-run -o yaml > service.yaml
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’  The output should be similar to the following:

root@ip-172-31-39-130:~/grafana/templates# kubectl expose deployment grafana --port=3000 --type=NodePort --dry-run -o yaml > service.yaml
W0420 18:12:55.289972   23635 helpers.go:557] --dry-run is deprecated and can be replaced with --dry-run=client.
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ธ After it, run the below command for exposing the grafana pod.

kubectl apply -f service.yaml
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’  To ensure your pod is working well with the below commands.

kubectl get pods
kubectl get deployment
kubectl get svc
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’  After running these commands, the output should be similar to the following:

Alt Text

You can check the list of the helm you have using the helm list command. Now we can check the pods in which slaves are running in the Kubernetes cluster.

root@ip-172-31-39-130:~# helm list
NAME    NAMESPACE       REVISION        UPDATED                                STATUS   CHART           APP VERSION
grafana default         1               2021-04-20 18:10:44.189498387 +0000 UTCdeployed Grafana-0.1.0   1.16.0
Enter fullscreen mode Exit fullscreen mode

Hopefully, Everything is working well till yet. So let's check that Grafana is working fine or not. For this, you have to take the public_ip_of_instance and port no. of your pod.

eg. 15.207.72.25:31130

Where 13.233.237.202 is the public IP of my instance that contains all the setup which I have done till yet and 31130 is the port no. of grafana pod which you can see in the above screenshot after running kubectl get svc command. So browse it.

๐Ÿ’  After browsing it, login page will pop up. So login with by-default username and password admin.

Alt Text
โš ๏ธ After logging you will get the page for changing the username and password, So you can if you want.

Alt Text

Now Grafana is ready to use. So let's enjoy โ˜บ๏ธ

Packing resources inside the Helm Chart.

So helm chart ready inside the grafana/ directory, but we canโ€™t publish it as it is. Firstly, we have to create a package for this helm chart.

๐Ÿ”ธ Create one directory named charts. Make sure this directory should be inside grafana directory.

mkdir charts/
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ธ Now, run the following command to packages the chart and store it inside the charts/ directory.

helm package /root/grafana -d charts/
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’  you will get output something like this:

root@ip-172-31-44-81:~/grafana# helm package /root/grafana -d charts/
Successfully packaged chart and saved it to: charts/Grafana-0.1.0.tgz
Enter fullscreen mode Exit fullscreen mode

Creating an index.yaml file.

For every Helm repository, we must require an index.yaml file. The index.yaml file contains the information about the chart that is present inside the current repository/directory.

๐Ÿ”ธ For generating index.yaml file inside charts/ directory, run following command.

helm repo index charts/
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’  You can see an index.yaml file generated with the details of the chart.

Alt Text

Hosting the chart with GitHub pages

Now everything is fine in our helm chart we can publish this to ArtifcatHub so that we can use it in the future when we require it.

But before we have to host this chart anywhere. So that we can publish it to ArtifactHub. So I am going to host it with the GitHub page. but before doing this, We have to push the chart to Github. So let's follow the following steps.

๐Ÿ”ธ Install git and config cluster with your GitHub account.

sudo apt-get install git -y
git config --global user.name 'username'
git config --global user.email 'usermail@gmail.com'
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ธ After installing git go inside grafana directory and then initialize it with the help of below command.

git init
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ธ Now add, commit then push this directory to GitHub.

git add .
git commit -m "give any msg according to you"
git branch -M main
git remote add origin URL
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ธ After pushing it, go to the Github repository and click on setting then GitHub Pages.
Alt Text

โš ๏ธ To activate the GitHub page, you have to select branch first which you want to activate then save it.

Alt Text

Publishing the Helm chart to ArtifactHub

Artifact Hub includes a fine-grained authorization mechanism that allows organizations to define what actions can be performed by their members. It is based on customizable authorization policies that are enforced by the Open Policy Agent. So go to your ArtifactHub Account and login to it.

๐Ÿ”ธ Now click on profile icon > control Panel> Add repository.

โš ๏ธ Make sure your repository's url should be like https://username.github.io/repository_name/chart/ when you're adding repository in ArtifactHub. Otherwise, it can create some issues related to url.

Alt Text

๐Ÿ”ธ After giving the required information to your Helm repository, click on Add. if you had provided the right information then it will create a Helm repository.

Alt Text

So, we had successfully published our Helm chart to the ArtifactHub.

Conclusion

The power of a great templating engine and the possibility of executing releases, upgrades, and rollbacks makes Helm great. On top of that comes the publicly available Helm Chart Hub that contains thousands of production-ready templates. This makes Helm a must-have tool in your toolbox if you work with Kubernetes on a bigger scale!

Top comments (0)