2020-05-24 23:13:21 +05:30
---
stage: Configure
group: Configure
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#designated-technical-writers
---
2019-12-26 22:10:19 +05:30
# Adding and removing Kubernetes clusters
2020-03-13 15:44:24 +05:30
GitLab offers integrated cluster creation for the following Kubernetes providers:
2019-12-26 22:10:19 +05:30
- Google Kubernetes Engine (GKE).
- Amazon Elastic Kubernetes Service (EKS).
2020-04-08 14:13:33 +05:30
GitLab can also integrate with any standard Kubernetes provider, either on-premise or hosted.
2020-03-13 15:44:24 +05:30
2020-07-28 23:09:34 +05:30
NOTE: **Note:**
Watch the webcast [Scalable app deployment with GitLab and Google Cloud Platform ](https://about.gitlab.com/webcast/scalable-app-deploy/ )
and learn how to spin up a Kubernetes cluster managed by Google Cloud Platform (GCP)
in a few clicks.
2019-12-26 22:10:19 +05:30
TIP: **Tip:**
2021-01-03 14:25:43 +05:30
Every new Google Cloud Platform (GCP) account receives
[$300 in credit upon sign up ](https://console.cloud.google.com/freetrial ).
In partnership with Google, GitLab is able to offer an additional $200 for new GCP
accounts to get started with GitLab's Google Kubernetes Engine Integration.
[Follow this link ](https://cloud.google.com/partners/partnercredit/?pcn_code=0014M00001h35gDQAQ#contact-form )
to apply for credit.
2019-12-26 22:10:19 +05:30
2020-03-13 15:44:24 +05:30
## Before you begin
2020-04-22 19:07:51 +05:30
Before [adding a Kubernetes cluster ](#create-new-cluster ) using GitLab, you need:
2020-03-13 15:44:24 +05:30
- GitLab itself. Either:
2020-07-28 23:09:34 +05:30
- A [GitLab.com account ](https://about.gitlab.com/pricing/#gitlab-com ).
2020-03-13 15:44:24 +05:30
- A [self-managed installation ](https://about.gitlab.com/pricing/#self-managed ) with GitLab version
2021-01-03 14:25:43 +05:30
12.5 or later. This ensures the GitLab UI can be used for cluster creation.
2020-03-13 15:44:24 +05:30
- The following GitLab access:
- [Maintainer access to a project ](../../permissions.md#project-members-permissions ) for a
project-level cluster.
- [Maintainer access to a group ](../../permissions.md#group-members-permissions ) for a
group-level cluster.
- [Admin Area access ](../../admin_area/index.md ) for a self-managed instance-level
cluster. ** (CORE ONLY)**
2019-12-26 22:10:19 +05:30
## Access controls
2021-01-03 14:25:43 +05:30
> - Restricted service account for deployment was [introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/issues/51716) in GitLab 11.5.
2019-12-26 22:10:19 +05:30
2021-01-03 14:25:43 +05:30
When creating a cluster in GitLab, you are asked if you would like to create either:
2019-12-26 22:10:19 +05:30
2021-01-03 14:25:43 +05:30
- A [Role-based access control (RBAC) ](https://kubernetes.io/docs/reference/access-authn-authz/rbac/ )
cluster, which is the GitLab default and recommended option.
- An [Attribute-based access control (ABAC) ](https://kubernetes.io/docs/reference/access-authn-authz/abac/ ) cluster.
2019-12-26 22:10:19 +05:30
GitLab creates the necessary service accounts and privileges to install and run
[GitLab managed applications ](index.md#installing-applications ). When GitLab creates the cluster,
a `gitlab` service account with `cluster-admin` privileges is created in the `default` namespace
to manage the newly created cluster.
2020-07-28 23:09:34 +05:30
The first time you install an application into your cluster, the `tiller` service
account is created with `cluster-admin` privileges in the
2021-01-03 14:25:43 +05:30
`gitlab-managed-apps` namespace. This service account is used by Helm to
2020-07-28 23:09:34 +05:30
install and run [GitLab managed applications ](index.md#installing-applications ).
2019-12-26 22:10:19 +05:30
2021-01-03 14:25:43 +05:30
Helm also creates additional service accounts and other resources for each
2019-12-26 22:10:19 +05:30
installed application. Consult the documentation of the Helm charts for each application
for details.
If you are [adding an existing Kubernetes cluster ](add_remove_clusters.md#add-existing-cluster ),
ensure the token of the account has administrator privileges for the cluster.
The resources created by GitLab differ depending on the type of cluster.
### Important notes
Note the following about access controls:
- Environment-specific resources are only created if your cluster is
[managed by GitLab ](index.md#gitlab-managed-clusters ).
2021-01-03 14:25:43 +05:30
- If your cluster was created before GitLab 12.2, it uses a single namespace for all project
2019-12-26 22:10:19 +05:30
environments.
### RBAC cluster resources
GitLab creates the following resources for RBAC clusters.
| Name | Type | Details | Created when |
|:----------------------|:---------------------|:-----------------------------------------------------------------------------------------------------------|:-----------------------|
| `gitlab` | `ServiceAccount` | `default` namespace | Creating a new cluster |
| `gitlab-admin` | `ClusterRoleBinding` | [`cluster-admin` ](https://kubernetes.io/docs/reference/access-authn-authz/rbac/#user-facing-roles ) roleRef | Creating a new cluster |
| `gitlab-token` | `Secret` | Token for `gitlab` ServiceAccount | Creating a new cluster |
2020-07-28 23:09:34 +05:30
| `tiller` | `ServiceAccount` | `gitlab-managed-apps` namespace | Installing Helm charts |
| `tiller-admin` | `ClusterRoleBinding` | `cluster-admin` roleRef | Installing Helm charts |
2019-12-26 22:10:19 +05:30
| Environment namespace | `Namespace` | Contains all environment-specific resources | Deploying to a cluster |
| Environment namespace | `ServiceAccount` | Uses namespace of environment | Deploying to a cluster |
| Environment namespace | `Secret` | Token for environment ServiceAccount | Deploying to a cluster |
| Environment namespace | `RoleBinding` | [`edit` ](https://kubernetes.io/docs/reference/access-authn-authz/rbac/#user-facing-roles ) roleRef | Deploying to a cluster |
### ABAC cluster resources
GitLab creates the following resources for ABAC clusters.
| Name | Type | Details | Created when |
|:----------------------|:---------------------|:-------------------------------------|:---------------------------|
| `gitlab` | `ServiceAccount` | `default` namespace | Creating a new cluster |
| `gitlab-token` | `Secret` | Token for `gitlab` ServiceAccount | Creating a new cluster |
2020-07-28 23:09:34 +05:30
| `tiller` | `ServiceAccount` | `gitlab-managed-apps` namespace | Installing Helm charts |
| `tiller-admin` | `ClusterRoleBinding` | `cluster-admin` roleRef | Installing Helm charts |
2019-12-26 22:10:19 +05:30
| Environment namespace | `Namespace` | Contains all environment-specific resources | Deploying to a cluster |
| Environment namespace | `ServiceAccount` | Uses namespace of environment | Deploying to a cluster |
| Environment namespace | `Secret` | Token for environment ServiceAccount | Deploying to a cluster |
2020-11-24 15:15:51 +05:30
### Security of runners
2019-12-26 22:10:19 +05:30
2020-11-24 15:15:51 +05:30
Runners have the [privileged mode ](https://docs.gitlab.com/runner/executors/docker.html#the-privileged-mode )
enabled by default, which allows them to execute special commands and run
2019-12-26 22:10:19 +05:30
Docker in Docker. This functionality is needed to run some of the
[Auto DevOps ](../../../topics/autodevops/index.md )
jobs. This implies the containers are running in privileged mode and you should,
therefore, be aware of some important details.
The privileged flag gives all capabilities to the running container, which in
turn can do almost everything that the host can do. Be aware of the
inherent security risk associated with performing `docker run` operations on
arbitrary images as they effectively have root access.
2020-11-24 15:15:51 +05:30
If you don't want to use a runner in privileged mode, either:
2019-12-26 22:10:19 +05:30
2020-11-24 15:15:51 +05:30
- Use shared runners on GitLab.com. They don't have this security issue.
- Set up your own runners using the configuration described at
[shared runners ](../../gitlab_com/index.md#shared-runners ). This involves:
2019-12-26 22:10:19 +05:30
1. Making sure that you don't have it installed via
[the applications ](index.md#installing-applications ).
2020-11-24 15:15:51 +05:30
1. Installing a runner
2019-12-26 22:10:19 +05:30
[using `docker+machine` ](https://docs.gitlab.com/runner/executors/docker_machine.html ).
2020-04-22 19:07:51 +05:30
## Create new cluster
2019-12-26 22:10:19 +05:30
2020-07-28 23:09:34 +05:30
New clusters can be created using GitLab on Google Kubernetes Engine (GKE) or
Amazon Elastic Kubernetes Service (EKS) at the project, group, or instance level:
2019-12-26 22:10:19 +05:30
2020-07-28 23:09:34 +05:30
1. Navigate to your:
- Project's ** {cloud-gear}** **Operations > Kubernetes** page, for a project-level cluster.
- Group's ** {cloud-gear}** **Kubernetes** page, for a group-level cluster.
2020-10-24 23:57:45 +05:30
- **Admin Area >** ** {cloud-gear}** **Kubernetes** page, for an instance-level cluster.
2020-07-28 23:09:34 +05:30
1. Click **Add Kubernetes cluster** .
1. Click the **Create new cluster** tab.
1. Click either **Amazon EKS** or **Google GKE** , and follow the instructions for your desired service:
- [Amazon EKS ](add_eks_clusters.md#new-eks-cluster ).
- [Google GKE ](add_gke_clusters.md#creating-the-cluster-on-gke ).
2020-03-13 15:44:24 +05:30
2019-12-26 22:10:19 +05:30
## Add existing cluster
2021-01-03 14:25:43 +05:30
If you have an existing Kubernetes cluster, you can add it to a project, group,
or instance.
2019-12-26 22:10:19 +05:30
2021-01-03 14:25:43 +05:30
Kubernetes integration isn't supported for arm64 clusters. See the issue
[Helm Tiller fails to install on arm64 cluster ](https://gitlab.com/gitlab-org/gitlab/-/issues/29838 )
for details.
2019-12-26 22:10:19 +05:30
2020-03-13 15:44:24 +05:30
### Existing Kubernetes cluster
2019-12-26 22:10:19 +05:30
2020-03-13 15:44:24 +05:30
To add a Kubernetes cluster to your project, group, or instance:
2019-12-26 22:10:19 +05:30
2020-03-13 15:44:24 +05:30
1. Navigate to your:
2020-06-23 00:09:42 +05:30
1. Project's ** {cloud-gear}** **Operations > Kubernetes** page, for a project-level cluster.
1. Group's ** {cloud-gear}** **Kubernetes** page, for a group-level cluster.
2020-10-24 23:57:45 +05:30
1. **Admin Area >** ** {cloud-gear}** **Kubernetes** page, for an instance-level cluster.
2019-12-26 22:10:19 +05:30
1. Click **Add Kubernetes cluster** .
2020-03-13 15:44:24 +05:30
1. Click the **Add existing cluster** tab and fill in the details:
2020-06-23 00:09:42 +05:30
1. **Kubernetes cluster name** (required) - The name you wish to give the cluster.
1. **Environment scope** (required) - The
2020-10-04 03:57:07 +05:30
[associated environment ](index.md#setting-the-environment-scope ) to this cluster.
2020-06-23 00:09:42 +05:30
1. **API URL** (required) -
It's the URL that GitLab uses to access the Kubernetes API. Kubernetes
exposes several APIs, we want the "base" URL that is common to all of them.
For example, `https://kubernetes.example.com` rather than `https://kubernetes.example.com/api/v1` .
Get the API URL by running this command:
```shell
kubectl cluster-info | grep 'Kubernetes master' | awk '/http/ {print $NF}'
```
2021-01-03 14:25:43 +05:30
1. **CA certificate** (required) - A valid Kubernetes certificate is needed to authenticate to the cluster. We use the certificate created by default.
2020-06-23 00:09:42 +05:30
1. List the secrets with `kubectl get secrets` , and one should be named similar to
`default-token-xxxxx` . Copy that token name for use below.
1. Get the certificate by running this command:
```shell
kubectl get secret < secret name > -o jsonpath="{['data']['ca\.crt']}" | base64 --decode
```
2021-01-03 14:25:43 +05:30
If the command returns the entire certificate chain, you must copy the Root CA
certificate and any intermediate certificates at the bottom of the chain.
A chain file has following structure:
```plaintext
-----BEGIN MY CERTIFICATE-----
-----END MY CERTIFICATE-----
-----BEGIN INTERMEDIATE CERTIFICATE-----
-----END INTERMEDIATE CERTIFICATE-----
-----BEGIN INTERMEDIATE CERTIFICATE-----
-----END INTERMEDIATE CERTIFICATE-----
-----BEGIN ROOT CERTIFICATE-----
-----END ROOT CERTIFICATE-----
```
2020-06-23 00:09:42 +05:30
1. **Token** -
GitLab authenticates against Kubernetes using service tokens, which are
scoped to a particular `namespace` .
**The token used should belong to a service account with
[`cluster-admin` ](https://kubernetes.io/docs/reference/access-authn-authz/rbac/#user-facing-roles )
privileges.** To create this service account:
1. Create a file called `gitlab-admin-service-account.yaml` with contents:
```yaml
apiVersion: v1
kind: ServiceAccount
metadata:
2020-11-24 15:15:51 +05:30
name: gitlab
2020-06-23 00:09:42 +05:30
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: gitlab-admin
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
2020-07-28 23:09:34 +05:30
- kind: ServiceAccount
2020-11-24 15:15:51 +05:30
name: gitlab
2020-07-28 23:09:34 +05:30
namespace: kube-system
2020-06-23 00:09:42 +05:30
```
1. Apply the service account and cluster role binding to your cluster:
```shell
kubectl apply -f gitlab-admin-service-account.yaml
```
2021-01-03 14:25:43 +05:30
You need the `container.clusterRoleBindings.create` permission
2020-06-23 00:09:42 +05:30
to create cluster-level roles. If you do not have this permission,
you can alternatively enable Basic Authentication and then run the
2021-01-03 14:25:43 +05:30
`kubectl apply` command as an administrator:
2020-06-23 00:09:42 +05:30
```shell
kubectl apply -f gitlab-admin-service-account.yaml --username=admin --password=< password >
```
NOTE: **Note:**
Basic Authentication can be turned on and the password credentials
can be obtained using the Google Cloud Console.
Output:
```shell
2020-11-24 15:15:51 +05:30
serviceaccount "gitlab" created
2020-06-23 00:09:42 +05:30
clusterrolebinding "gitlab-admin" created
```
2020-11-24 15:15:51 +05:30
1. Retrieve the token for the `gitlab` service account:
2020-06-23 00:09:42 +05:30
```shell
2020-11-24 15:15:51 +05:30
kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep gitlab | awk '{print $1}')
2020-06-23 00:09:42 +05:30
```
Copy the `<authentication_token>` value from the output:
2021-01-03 14:25:43 +05:30
```plaintext
2020-11-24 15:15:51 +05:30
Name: gitlab-token-b5zv4
2020-06-23 00:09:42 +05:30
Namespace: kube-system
Labels: < none >
2020-11-24 15:15:51 +05:30
Annotations: kubernetes.io/service-account.name=gitlab
2019-12-26 22:10:19 +05:30
kubernetes.io/service-account.uid=bcfe66ac-39be-11e8-97e8-026dce96b6e8
2020-06-23 00:09:42 +05:30
Type: kubernetes.io/service-account-token
Data
====
ca.crt: 1025 bytes
namespace: 11 bytes
token: < authentication_token >
```
NOTE: **Note:**
2021-01-03 14:25:43 +05:30
For GKE clusters, you need the
2020-06-23 00:09:42 +05:30
`container.clusterRoleBindings.create` permission to create a cluster
role binding. You can follow the [Google Cloud
documentation](https://cloud.google.com/iam/docs/granting-changing-revoking-access)
to grant access.
1. **GitLab-managed cluster** - Leave this checked if you want GitLab to manage namespaces and service accounts for this cluster.
See the [Managed clusters section ](index.md#gitlab-managed-clusters ) for more information.
1. **Project namespace** (optional) - You don't have to fill it in; by leaving
2021-01-03 14:25:43 +05:30
it blank, GitLab creates one for you. Also:
2020-06-23 00:09:42 +05:30
- Each project should have a unique namespace.
- The project namespace is not necessarily the namespace of the secret, if
you're using a secret with broader permissions, like the secret from `default` .
- You should **not** use `default` as the project namespace.
- If you or someone created a secret specifically for the project, usually
with limited permissions, the secret's namespace and project namespace may
be the same.
2019-12-26 22:10:19 +05:30
1. Finally, click the **Create Kubernetes cluster** button.
2021-01-03 14:25:43 +05:30
After a couple of minutes, your cluster is ready. You can now proceed
2019-12-26 22:10:19 +05:30
to install some [pre-defined applications ](index.md#installing-applications ).
#### Disable Role-Based Access Control (RBAC) (optional)
When connecting a cluster via GitLab integration, you may specify whether the
2021-01-03 14:25:43 +05:30
cluster is RBAC-enabled or not. This affects how GitLab interacts with the
2020-07-28 23:09:34 +05:30
cluster for certain operations. If you did *not* check the **RBAC-enabled cluster**
2021-01-03 14:25:43 +05:30
checkbox at creation time, GitLab assumes RBAC is disabled for your cluster
2019-12-26 22:10:19 +05:30
when interacting with it. If so, you must disable RBAC on your cluster for the
integration to work properly.
2021-01-03 14:25:43 +05:30
![RBAC ](img/rbac_v13_1.png )
2019-12-26 22:10:19 +05:30
2021-01-03 14:25:43 +05:30
CAUTION: **Caution:**
2020-07-28 23:09:34 +05:30
Disabling RBAC means that any application running in the cluster,
2019-12-26 22:10:19 +05:30
or user who can authenticate to the cluster, has full API access. This is a
[security concern ](index.md#security-implications ), and may not be desirable.
To effectively disable RBAC, global permissions can be applied granting full access:
2020-03-13 15:44:24 +05:30
```shell
2019-12-26 22:10:19 +05:30
kubectl create clusterrolebinding permissive-binding \
--clusterrole=cluster-admin \
--user=admin \
--user=kubelet \
--group=system:serviceaccounts
```
## Enabling or disabling integration
2020-07-28 23:09:34 +05:30
The Kubernetes cluster integration enables after you have successfully either created
a new cluster or added an existing one. To disable Kubernetes cluster integration:
2019-12-26 22:10:19 +05:30
2020-07-28 23:09:34 +05:30
1. Navigate to your:
- Project's ** {cloud-gear}** **Operations > Kubernetes** page, for a project-level cluster.
- Group's ** {cloud-gear}** **Kubernetes** page, for a group-level cluster.
2020-10-24 23:57:45 +05:30
- **Admin Area >** ** {cloud-gear}** **Kubernetes** page, for an instance-level cluster.
2020-07-28 23:09:34 +05:30
1. Click on the name of the cluster.
1. Click the **GitLab Integration** toggle.
1. Click **Save changes** .
2019-12-26 22:10:19 +05:30
## Removing integration
2020-07-28 23:09:34 +05:30
To remove the Kubernetes cluster integration from your project, first navigate to the **Advanced Settings** tab of the cluster details page and either:
2020-03-13 15:44:24 +05:30
- Select **Remove integration** , to remove only the Kubernetes integration.
2020-06-23 00:09:42 +05:30
- [From GitLab 12.6 ](https://gitlab.com/gitlab-org/gitlab/-/issues/26815 ), select
2020-03-13 15:44:24 +05:30
**Remove integration and resources** , to also remove all related GitLab cluster resources (for
example, namespaces, roles, and bindings) when removing the integration.
2019-12-26 22:10:19 +05:30
When removing the cluster integration, note:
- You need Maintainer [permissions ](../../permissions.md ) and above to remove a Kubernetes cluster
integration.
- When you remove a cluster, you only remove its relationship to GitLab, not the cluster itself. To
2020-03-13 15:44:24 +05:30
remove the cluster, you can do so by visiting the GKE or EKS dashboard, or using `kubectl` .
2019-12-26 22:10:19 +05:30
## Learn more
To learn more on automatically deploying your applications,
read about [Auto DevOps ](../../../topics/autodevops/index.md ).
2021-01-03 14:25:43 +05:30
## Troubleshooting
### There was a problem authenticating with your cluster. Please ensure your CA Certificate and Token are valid
If you encounter this error while adding a Kubernetes cluster, ensure you're
properly pasting the service token. Some shells may add a line break to the
service token, making it invalid. Ensure that there are no line breaks by
pasting your token into an editor and removing any additional spaces.