How To Install Prometheus and Grafana On Your Cluster Using Terraform and Helm.

Nwokolo Emmanuel
7 min readApr 24, 2023

Hey! Welcome to another blog series.

In this edition, you will learn how to install Prometheus and Grafana using Terraform.

There are many ways to install Prometheus and Grafana, but we will use one of the simplest methods, Installing with Helm (a tool that helps you define, install, and upgrade applications running on Kubernetes).

However, if you prefer to install Prometheus manually, click this LINK.

Prerequisites:

  • A Kubernetes cluster running on AWS, GCP, AZURE, or anywhere.
  • An Ubuntu machine.
  • Helm installed.
  • Terraform installed.
  • kubectl installed.
  • AWS CLI installed.

If you don’t have these prerequisites installed yet, you can follow HERE to access a script I created to install them.

If you already have all this installed, then let’s go!!

1. PROVIDERS FILE.

In this section, we are going to be creating our providers’ file for us to be able to install Prometheus on our cluster.

In this file, we are calling the already existing cluster with the data module in Terraform.

We will be extracting the cluster’s name and the cluster auth from it to reference them in other parts of our codes.

Also, in the file above, we will define the kubectl, Kubernetes, and Helm providers.

We will also need to specify the location of the Kube config file for the providers listed.

We need to input the name of our cluster into the file, so anywhere you see ‘ClusterName’ replace it with the name of your cluster.

Where you see ClusterName_auth, replace the ‘ClusterName’ part as stated above, leaving the ‘_auth’ part as it is.

In the AWS region section of the file, you should input the region your cluster is running in.

2. INSTALLING PROMETHEUS.

Now that we have set our providers’ blocks, the next step is to create the Terraform script that will install Prometheus.

Before you run this script, ensure that you already have Helm & Kubernetes installed on your system.

We are defining a data resource block for the node groups created in the cluster we referenced earlier in the providers’ file above.

The other resources we need depend on the creation of the node group.

This step is an important one, especially when you are creating the eks cluster and the Prometheus deployment together or with a pipeline.

We are also provisioning a namespace with the script in the cluster.

Then, we added the link to the Helm repo, the name, and its version in the Helm release path.

We also referenced a ‘values.yaml’ file.

3. VALUES.YAML

In step 2 above, we referenced a ‘values.yaml’ file.

This values.yaml file is like a default setting that helps to define Prometheus’ behavior.

You can define configurations that meet your requirements in this file which will cause Prometheus to behave accordingly.

However, for this blog, we will use the default one that Prometheus has already defined.

This LINK contains the default file I used in this project; create a new file, name it ‘values.yaml’, and paste its content into it.

NOTE: Ensure the name is ‘values.yaml’ because that is the name specified on the helm release file above.

After this step, you should have these three files in your directory:

When all the files are ready, you can run the following commands:

$ terraform init

This command helps to initialize terraform.

terraform apply --auto-approve

This command runs your terraform script.

After the script has executed successfully, use this command to see the current namespaces:

$ kubectl get namespace

And to see the services in the Prometheus namespace, use the following command.

You should also see the Grafana service in the output.

$ kubectl get svc -n prometheus

4. ACCESSING GRAFANA & PROMETHEUS ON THE WEB

After you have installed Grafana and Prometheus with the Terraform script, you will need access to it, right?

There are a few ways to access your Grafana and Prometheus on the web.

But in this blog, we will be looking at one which is, changing the service type of each service from Cluster IP to load balancer.

We can get the service of Prometheus and Grafana in the cluster by running this command:

$ kubectl get svc -n prometheus
output

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
alertmanager-operated ClusterIP None <none> 9093/TCP,9094/TCP,9094/UDP 15m
prometheus-grafana ClusterIP 172.20.128.125 <none> 80/TCP 15m
prometheus-kube-prometheus-alertmanager ClusterIP 172.20.45.181 <none> 9093/TCP 15m
prometheus-kube-prometheus-operator ClusterIP 172.20.128.175 <none> 443/TCP 15m
prometheus-kube-prometheus-prometheus ClusterIP 172.20.79.122 <none> 9090/TCP 15m
prometheus-kube-state-metrics ClusterIP 172.20.122.220 <none> 8080/TCP 15m
prometheus-operated ClusterIP None <none> 9090/TCP 15m
prometheus-prometheus-node-exporter ClusterIP 172.20.128.240 <none> 9100/TCP 15m

You should see an output like the one above.

Then run this command to edit the Prometheus-grafana service above:

$ kubectl edit svc prometheus-grafana -n prometheus

You should see a new screen pop up as shown below:

Scroll down to the point where you see this ‘type: ClusterIP’ and change it to these exact words:

LoadBalancer

NOTE: The text editor is Vim, so you will have to click on the letter ‘I’ on your keyboard to insert the values, and when you have finished editing, you will press the ESC key and type :wq to save it.

After saving the file, you will see a load balancer created for the Prometheus-Grafana service.

If you are using AWS like me:

NAME                                      TYPE           CLUSTER-IP       EXTERNAL-IP                                                               PORT(S)                      AGE
alertmanager-operated ClusterIP None <none> 9093/TCP,9094/TCP,9094/UDP 24m
prometheus-grafana LoadBalancer 172.20.128.125 a5ae5a761a6f243e2aa65692a14f2b79-1095997634.eu-west-2.elb.amazonaws.com 80:31801/TCP 24m
prometheus-kube-prometheus-alertmanager ClusterIP 172.20.45.181 <none> 9093/TCP 24m
prometheus-kube-prometheus-operator ClusterIP 172.20.128.175 <none> 443/TCP 24m
prometheus-kube-prometheus-prometheus ClusterIP 172.20.79.122 <none> 9090/TCP 24m
prometheus-kube-state-metrics ClusterIP 172.20.122.220 <none> 8080/TCP 24m
prometheus-operated ClusterIP None <none> 9090/TCP 24m
prometheus-prometheus-node-exporter ClusterIP 172.20.128.240 <none> 9100/TCP

Or if you are using another cloud provider it would be a public IP address.

When you put that IP address or DNS name in the browser you should get this:

The username is admin, and the password is prom-operator.

And when you are in, this is how the interface should look:

PROMETHEUS

We will follow the same steps to set up the Prometheus user interface:

$ kubectl get svc -n prometheus
NAME                                      TYPE           CLUSTER-IP       EXTERNAL-IP                                                               PORT(S)                      AGE
alertmanager-operated ClusterIP None <none> 9093/TCP,9094/TCP,9094/UDP 37m
prometheus-grafana LoadBalancer 172.20.128.125 a5ae5a761a6f243e2aa65692a14f2b79-1095997634.eu-west-2.elb.amazonaws.com 80:31801/TCP 37m
prometheus-kube-prometheus-alertmanager ClusterIP 172.20.45.181 <none> 9093/TCP 37m
prometheus-kube-prometheus-operator ClusterIP 172.20.128.175 <none> 443/TCP 37m
prometheus-kube-prometheus-prometheus ClusterIP 172.20.79.122 <none> 9090/TCP 37m
prometheus-kube-state-metrics ClusterIP 172.20.122.220 <none> 8080/TCP 37m
prometheus-operated ClusterIP None <none> 9090/TCP 37m
prometheus-prometheus-node-exporter ClusterIP 172.20.128.240 <none> 9100/TCP 37m
kubectl edit svc prometheus-kube-prometheus-prometheus -n prometheus

And you should see a new screen pop up:

Scroll down to the point where you see this ‘type: ClusterIP’ and change its value to LoadBalancer.

Save the Vim editor as illustrated above.

NOTE: the text editor is Vim so you will have to type in I to insert and when you are done you will need to press ESC and `type :wq` to save it.

NAME                                      TYPE           CLUSTER-IP       EXTERNAL-IP                                                               PORT(S)                      AGE
alertmanager-operated ClusterIP None <none> 9093/TCP,9094/TCP,9094/UDP 44m
prometheus-grafana LoadBalancer 172.20.128.125 a5ae5a761a6f243e2aa65692a14f2b79-1095997634.eu-west-2.elb.amazonaws.com 80:31801/TCP 44m
prometheus-kube-prometheus-alertmanager ClusterIP 172.20.45.181 <none> 9093/TCP 44m
prometheus-kube-prometheus-operator ClusterIP 172.20.128.175 <none> 443/TCP 44m
prometheus-kube-prometheus-prometheus LoadBalancer 172.20.79.122 a2ea161ec21704f3e9a71691dd665323-1343072495.eu-west-2.elb.amazonaws.com 9090:31206/TCP 44m
prometheus-kube-state-metrics ClusterIP 172.20.122.220 <none> 8080/TCP 44m
prometheus-operated ClusterIP None <none> 9090/TCP 44m
prometheus-prometheus-node-exporter ClusterIP 172.20.128.240 <none> 9100/TCP 44m

And now we have two load balancers running for prometheus and grafana.

Copy the Prometheus DNS name or IP address and put it in the browser and also open port 9090:

Like in the picture above, you should see the Prometheus User Interface

And with these steps, you have installed both Prometheus and Grafana on your cluster with access to their U.I.

Congratulations! See you in the next series!

Resources

NOTICE

There is a platform called THE CLOUD TOP G MASTERMIND where you can learn more about becoming a Cloud Engineer, the tools you need to build your projects, and getting your dream job.

You can get more details by clicking this LINK.

see you soon!

NOTE: If you have any questions or want to add to this blog, you can send me an E-mail. And I reply faster to people that subscribe to my newsletter!!

Conclusion

If you loved this blog post, please, give it a like, comment, and don’t forget to click the follow button.

And if you would love to get an update on the two exciting blogs I will be posting this week, then you should sign up for my newsletter right here!!

--

--

Nwokolo Emmanuel

I am a Cloud Engineer, I love sharing easy solutions to problems that I found difficult. Interested in Open Source | twitter: twitter.com/CloudTopG