# Clusters

## Introduction

You can add your existing Kubernetes clusters and environments on the `Clusters and Environments` section. You must have a [super admin](https://docs.dashboard.devtron.ai/authorization/user-permissions#assign-super-admin-permissions) access to add a cluster.

![Figure 1: Clusters and Environments in Devtron](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/cluster-and-environments/cluster-db.jpg)

***

## Add Kubernetes Cluster

Use this option to add a managed or on-premise Kubernetes cluster.

1. Go to **Global Configurations** → **Clusters & Environments**.
2. Click the **Add Cluster** button on the top-right corner.

![Figure 2: Adding a Cluster](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/cluster-and-environments/add-clusters-db.jpg)

3. You can choose to add your Kubernetes cluster using either of the following methods:

* [Server URL & Bearer Token](#add-clusters-using-server-url--bearer-token)
* [Kubeconfig](#add-clusters-using-kubeconfig)

![Figure 3: Adding a Kubernetes Cluster](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/cluster-and-environments/add-kubernetes-cluster-db.jpg)

### Add Clusters Using Server URL & Bearer Token

{% hint style="info" %}

#### Note

Refer [Get Cluster Credentials](#get-cluster-credentials) to know the process of getting Server URL and bearer token.
{% endhint %}

To add a Kubernetes cluster on Devtron using a Server URL and bearer token, provide the information in the following fields:

| Field          | Description                                                                                                                                                  |
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `Name`         | Enter a name of your cluster                                                                                                                                 |
| `Server URL`   | <p>Server URL of a cluster.<br>Note: We recommended to use a <a href="#benefits-of-self-hosted-url">self-hosted URL</a> instead of cloud hosted URL.<br></p> |
| `Bearer Token` | Bearer token of a cluster                                                                                                                                    |

![Figure 4: Entering Cluster Credentials](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/cluster-and-environments/add-cluster-cred-db.jpg)

### Add Clusters Using Kubeconfig

To add clusters using kubeconfig, follow these steps:

1. First, navigate to the global configurations menu, and then go to "clusters and environment" section.
2. Click on the `Add cluster` button. In the options provided, choose the `From kubeconfig` option.
3. Next, either paste the kubeconfig file or browse for it and select the appropriate file.
4. Afterward, click on the `Get cluster` button. This action will display the cluster details alongside the kubeconfig.

![Figure 5: Using Kubeconfig](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/cluster-and-environments/add-cluster-kubeconfig-db.jpg)

5. Select the desired cluster and click on `Save` to successfully add the cluster to Devtron.

![Figure 6: Saving Cluster](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/cluster-and-environments/kubeconfig-save-cluster.jpg)

{% hint style="info" %}

#### Note

Please ensure that the kubeconfig file you use has `admin permissions`. It is crucial for Devtron to have the necessary administrative privileges; otherwise, it may encounter failures or disruptions during deployments and other operations. Admin permission is essential to ensure the smooth functioning of Devtron and to prevent any potential issues that may arise due to insufficient privileges.
{% endhint %}

***

***

## Add Environment

Once you have added your cluster in the `Clusters & Environments`, you can add the environment by clicking `Add environment`.

A new environment window pops up.

| Field              | Description                                                                                                                                                                                                                                                   |
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Environment Name` | Enter a name of your environment.                                                                                                                                                                                                                             |
| `Enter Namespace`  | <p>Enter a namespace corresponding to your environment.<br><strong>Note</strong>: If this namespace does not already exist in your cluster, Devtron will create it. If it exists already, Devtron will map the environment to the existing namespace.<br></p> |
| `Environment Type` | <p>Select your environment type:</p><ul><li><code>Production</code></li><li><code>Non-production</code></li></ul><p>Note: Devtron shows deployment metrics (DORA metrics) for environments tagged as <code>Production</code> only.</p>                        |

Click **Save** and your environment will be created.

![Figure 11: Adding an Environment in Cluster](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/cluster-and-environments/add-environment-db.jpg)

***

## Update Environment

* You can also update an environment by clicking the environment.
* You can change `Production` and `Non-Production` options only.
* You cannot change the `Environment Name` and `Namespace Name`.
* Make sure to click **Update** to update your environment.

***

## Extras

### Get Cluster Credentials

{% hint style="info" %}

#### Prerequisites

`kubectl` must be installed on the bastion.
{% endhint %}

{% hint style="info" %}

#### Note

We recommend to use a self-hosted URL instead of cloud hosted URL. Refer the benefits of [self-hosted URL](#benefits-of-self-hosted-url).
{% endhint %}

You can get the **Server URL** and **Bearer Token** by running the following command depending on the cluster provider:

{% tabs %}
{% tab title="k8s Cluster Providers" %}
If you are using EKS, AKS, GKE, Kops, Digital Ocean managed Kubernetes, run the following command to generate the server URL and bearer token:

```bash
curl -O https://raw.githubusercontent.com/devtron-labs/utilities/main/kubeconfig-exporter/kubernetes_export_sa.sh && bash kubernetes_export_sa.sh cd-user  devtroncd
```

{% endtab %}

{% tab title="Microk8s Cluster" %}
If you are using a **`microk8s cluster`**, run the following command to generate the server URL and bearer token:

```bash
curl -O https://raw.githubusercontent.com/devtron-labs/utilities/main/kubeconfig-exporter/kubernetes_export_sa.sh && sed -i 's/kubectl/microk8s kubectl/g' \
kubernetes_export_sa.sh && bash kubernetes_export_sa.sh cd-user \
devtroncd
```

{% endtab %}
{% endtabs %}

![Figure 12: Generating Cluster Credentials](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/cluster-and-environments/generate-cluster-credentials.png)

### Benefits of Self-hosted URL

* Disaster Recovery:
  * It is not possible to edit the server URL of a cloud specific provider. If you're using an EKS URL (e.g. `*****.eu-west-1.elb.amazonaws.com`), it will be a tedious task to add a new cluster and migrate all the services one by one.
  * But in case of using a self-hosted URL (e.g. `clear.example.com`), you can just point to the new cluster's server URL in DNS manager and update the new cluster token and sync all the deployments.
* Easy Cluster Migrations:
  * In case of managed Kubernetes clusters (like EKS, AKS, GKE etc) which is a cloud provider specific, migrating your cluster from one provider to another will result in waste of time and effort.
  * On the other hand, migration for a self-hosted URL is easy as the URL is of single hosted domain independent of the cloud provider.
