# How to install Prometheus and Grafana on Kubernetes with Helm

Installing Prometheus and Grafana on Kubernetes using Helm is a straightforward way to set up robust monitoring and visualization tools for your cluster. Helm charts simplify deployment, making configuring and managing these tools easy.

[ad-logs-small]

## Prerequisites
1. A running Kubernetes cluster.
2. `kubectl` CLI configured to access your cluster.
3. Helm installed on your local system.

---

## Step 1: Add the Helm repository
Add the Prometheus Community Helm charts repository:

```bash
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
```

---

## Step 2: Install Prometheus
1. Install the Prometheus Helm chart:

   ```bash
   helm install prometheus prometheus-community/prometheus --namespace monitoring --create-namespace
   ```

   - `prometheus`: The release name.
   - `--namespace monitoring`: Creates a namespace for Prometheus.

2. Verify the installation:

   ```bash
   kubectl get all -n monitoring
   ```

   You should see resources like pods, services, and deployments related to Prometheus.

3. Access Prometheus:
   - Get the Prometheus server service details:

     ```bash
     kubectl get svc -n monitoring
     ```

   - Use `kubectl port-forward` to access it locally:

     ```bash
     kubectl port-forward svc/prometheus-server -n monitoring 9090:80
     ```

   - Access Prometheus at `http://localhost:9090`.

---

## Step 3: Install Grafana
1. Install the Grafana Helm chart:

   ```bash
   helm install grafana prometheus-community/grafana --namespace monitoring
   ```

2. Verify the installation:

   ```bash
   kubectl get all -n monitoring
   ```

   You should see Grafana-related pods, services, and deployments.

3. Access Grafana:
   - Get the Grafana admin password:

     ```bash
     kubectl get secret --namespace monitoring grafana -o jsonpath="{.data.admin-password}" | base64 --decode
     ```

   - Forward the Grafana service port:

     ```bash
     kubectl port-forward svc/grafana -n monitoring 3000:80
     ```

   - Open Grafana in your browser at `http://localhost:3000` and log in with:
     - Username: `admin`
     - Password: The value retrieved above.

---

## Step 4: Connect Prometheus to Grafana
1. Open Grafana and go to **Configuration > Data Sources**.
2. Click **Add Data Source** and select **Prometheus**.
3. Configure the Prometheus URL:
   - If Prometheus is accessible within the cluster, use `http://prometheus-server:80`.
   - Otherwise, use `http://localhost:9090` if you are using port forwarding.
4. Click **Save & Test** to verify the connection.

---

## Step 5: Import pre-built dashboards
1. In Grafana, go to **Dashboards > Import**.
2. Enter a dashboard ID from Grafana's dashboard repository, such as `1860` for a Kubernetes cluster monitoring dashboard.
3. Select the Prometheus data source and click **Import**.

---

## Step 6: Customize configurations (optional)
Customize Prometheus and Grafana configurations using Helm values:

1. Create a `values.yaml` file:

   ```yaml
   prometheus:
     alertmanager:
       enabled: true
     server:
       persistentVolume:
         size: 10Gi

   grafana:
     adminPassword: "customPassword"
     persistence:
       enabled: true
       size: 5Gi
   ```

2. Install with custom values:

   ```bash
   helm install prometheus prometheus-community/prometheus -f values.yaml --namespace monitoring
   helm install grafana prometheus-community/grafana -f values.yaml --namespace monitoring
   ```

---

## Step 7: Monitor your cluster
- Access pre-configured dashboards in Grafana for metrics like resource usage, pod health, and cluster performance.
- Use Prometheus for querying detailed metrics directly.

[ad-uptime]
