2020-05-07 21:09:26 +00:00
---
stage: Monitor
group: APM
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-07-25 14:12:45 +00:00
# Kubernetes clusters
2017-10-05 15:24:51 +00:00
2020-05-22 03:08:28 +00:00
> - [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/issues/35954) in GitLab 10.1 for projects.
> - [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/issues/34758) in
2019-07-29 10:23:29 +00:00
> GitLab 11.6 for [groups](../../group/clusters/index.md).
2020-05-22 03:08:28 +00:00
> - [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/issues/39840) in
2019-07-29 10:23:29 +00:00
> GitLab 11.11 for [instances](../../instance/clusters/index.md).
2018-01-12 16:23:10 +00:00
2018-02-13 13:32:34 +00:00
## Overview
2019-07-25 14:12:45 +00:00
Using the GitLab project Kubernetes integration, you can:
- Use [Review Apps ](../../../ci/review_apps/index.md ).
2020-03-13 15:09:21 +00:00
- Run [pipelines ](../../../ci/pipelines/index.md ).
2019-07-25 14:12:45 +00:00
- [Deploy ](#deploying-to-a-kubernetes-cluster ) your applications.
2020-06-30 18:09:13 +00:00
- Detect and [monitor Kubernetes ](#monitoring-your-kubernetes-cluster ).
2019-07-25 14:12:45 +00:00
- Use it with [Auto DevOps ](#auto-devops ).
- Use [Web terminals ](#web-terminals ).
- Use [Deploy Boards ](#deploy-boards-premium ). ** (PREMIUM)**
- Use [Canary Deployments ](#canary-deployments-premium ). ** (PREMIUM)**
2020-06-30 18:09:13 +00:00
- View [Logs ](#viewing-pod-logs ).
2019-07-25 14:12:45 +00:00
- Run serverless workloads on [Kubernetes with Knative ](serverless/index.md ).
2020-06-30 18:09:13 +00:00
Besides integration at the project level, Kubernetes clusters can also be
integrated at the [group level ](../../group/clusters/index.md ) or
[GitLab instance level ](../../instance/clusters/index.md ).
## Setting up
2020-05-11 18:09:55 +00:00
### Supported cluster versions
2020-06-30 18:09:13 +00:00
GitLab is committed to support at least two production-ready Kubernetes minor
versions at any given time. We regularly review the versions we support, and
provide a four-month deprecation period before we remove support of a specific
version. The range of supported versions is based on the evaluation of:
2020-05-11 18:09:55 +00:00
- Our own needs.
- The versions supported by major managed Kubernetes providers.
- The versions [supported by the Kubernetes community ](https://kubernetes.io/docs/setup/release/version-skew-policy/#supported-versions ).
Currently, GitLab supports the following Kubernetes versions:
2020-05-28 15:08:02 +00:00
- 1.16
2020-05-11 18:09:55 +00:00
- 1.15
- 1.14
- 1.13 (deprecated, support ends on November 22, 2020)
- 1.12 (deprecated, support ends on September 22, 2020)
NOTE: **Note:**
Some GitLab features may support versions outside the range provided here.
2020-06-30 18:09:13 +00:00
### Adding and removing clusters
2019-07-25 14:12:45 +00:00
2020-06-30 18:09:13 +00:00
See [Adding and removing Kubernetes clusters ](add_remove_clusters.md ) for details on how
to:
2019-07-25 14:12:45 +00:00
2020-06-30 18:09:13 +00:00
- Create a cluster in Google Cloud Platform (GCP) or Amazon Elastic Kubernetes Service
(EKS) using GitLab's UI.
- Add an integration to an existing cluster from any Kubernetes platform.
2017-11-16 13:07:57 +00:00
2020-07-08 12:09:33 +00:00
### Multiple Kubernetes clusters
2019-07-25 14:12:45 +00:00
2020-07-08 12:09:33 +00:00
> - Introduced in [GitLab Premium](https://about.gitlab.com/pricing/) 10.3
2020-07-17 21:09:23 +00:00
> - [Moved](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/35094) to GitLab Core in 13.2.
2019-07-25 14:12:45 +00:00
2020-07-08 12:09:33 +00:00
You can associate more than one Kubernetes cluster to your
2020-06-30 18:09:13 +00:00
project. That way you can have different clusters for different environments,
like dev, staging, production, and so on.
2019-07-25 14:12:45 +00:00
2020-06-30 18:09:13 +00:00
Simply add another cluster, like you did the first time, and make sure to
2020-08-13 00:10:06 +00:00
[set an environment scope ](#setting-the-environment-scope ) that will
2020-06-30 18:09:13 +00:00
differentiate the new cluster with the rest.
2019-07-25 14:12:45 +00:00
2020-08-13 00:10:06 +00:00
#### Setting the environment scope
2019-07-25 14:12:45 +00:00
2020-06-30 18:09:13 +00:00
When adding more than one Kubernetes cluster to your project, you need to differentiate
them with an environment scope. The environment scope associates clusters with [environments ](../../../ci/environments/index.md ) similar to how the
[environment-specific variables ](../../../ci/variables/README.md#limit-the-environment-scopes-of-environment-variables ) work.
2019-07-25 14:12:45 +00:00
2020-06-30 18:09:13 +00:00
The default environment scope is `*` , which means all jobs, regardless of their
environment, will use that cluster. Each scope can only be used by a single
cluster in a project, and a validation error will occur if otherwise.
Also, jobs that don't have an environment keyword set will not be able to access any cluster.
2019-07-29 10:23:29 +00:00
2020-06-30 18:09:13 +00:00
For example, let's say the following Kubernetes clusters exist in a project:
2017-10-05 15:24:51 +00:00
2020-06-30 18:09:13 +00:00
| Cluster | Environment scope |
| ----------- | ----------------- |
| Development | `*` |
| Production | `production` |
2019-07-25 14:12:45 +00:00
2020-06-30 18:09:13 +00:00
And the following environments are set in
[`.gitlab-ci.yml` ](../../../ci/yaml/README.md ):
2018-12-07 04:55:19 +00:00
2020-06-30 18:09:13 +00:00
```yaml
stages:
2020-07-03 09:08:53 +00:00
- test
- deploy
2019-07-25 14:12:45 +00:00
2020-06-30 18:09:13 +00:00
test:
stage: test
script: sh test
2019-07-25 14:12:45 +00:00
2020-06-30 18:09:13 +00:00
deploy to staging:
stage: deploy
script: make deploy
environment:
name: staging
url: https://staging.example.com/
2019-12-20 09:24:38 +00:00
2020-06-30 18:09:13 +00:00
deploy to production:
stage: deploy
script: make deploy
environment:
name: production
url: https://example.com/
```
2019-12-20 09:24:38 +00:00
2020-06-30 18:09:13 +00:00
The result will then be:
2020-01-28 03:08:39 +00:00
2020-06-30 18:09:13 +00:00
- The Development cluster details will be available in the `deploy to staging`
job.
- The production cluster details will be available in the `deploy to production`
job.
- No cluster details will be available in the `test` job because it doesn't
define any environment.
2019-12-20 09:24:38 +00:00
2020-06-30 18:09:13 +00:00
## Configuring your Kubernetes cluster
2019-07-25 14:12:45 +00:00
2019-10-29 03:06:28 +00:00
After [adding a Kubernetes cluster ](add_remove_clusters.md ) to GitLab, read this section that covers
important considerations for configuring Kubernetes clusters with GitLab.
2019-07-25 14:12:45 +00:00
### Security implications
2018-03-07 18:58:08 +00:00
CAUTION: **Important:**
The whole cluster security is based on a model where [developers ](../../permissions.md )
are trusted, so **only trusted users should be allowed to control your clusters** .
The default cluster configuration grants access to a wide set of
functionalities needed to successfully build and deploy a containerized
2018-12-14 12:31:09 +00:00
application. Bear in mind that the same credentials are used for all the
2018-03-07 18:58:08 +00:00
applications running on the cluster.
2019-07-25 14:12:45 +00:00
### GitLab-managed clusters
2019-05-03 01:05:53 +00:00
2020-02-06 15:09:11 +00:00
> - [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/22011) in GitLab 11.5.
> - Became [optional](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/26565) in GitLab 11.11.
2019-05-03 01:05:53 +00:00
You can choose to allow GitLab to manage your cluster for you. If your cluster is
managed by GitLab, resources for your projects will be automatically created. See the
2019-10-29 03:06:28 +00:00
[Access controls ](add_remove_clusters.md#access-controls ) section for details on which resources will
2019-05-03 01:05:53 +00:00
be created.
If you choose to manage your own cluster, project-specific resources will not be created
automatically. If you are using [Auto DevOps ](../../../topics/autodevops/index.md ), you will
need to explicitly provide the `KUBE_NAMESPACE` [deployment variable ](#deployment-variables )
that will be used by your deployment jobs, otherwise a namespace will be created for you.
2019-12-10 12:07:55 +00:00
#### Important notes
Note the following with GitLab and clusters:
- If you [install applications ](#installing-applications ) on your cluster, GitLab will
create the resources required to run these even if you have chosen to manage your own
cluster.
- Be aware that manually managing resources that have been created by GitLab, like
namespaces and service accounts, can cause unexpected errors. If this occurs, try
[clearing the cluster cache ](#clearing-the-cluster-cache ).
2019-05-03 01:05:53 +00:00
2019-11-26 09:08:36 +00:00
#### Clearing the cluster cache
2020-05-22 03:08:28 +00:00
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/31759) in GitLab 12.6.
2019-11-26 09:08:36 +00:00
If you choose to allow GitLab to manage your cluster for you, GitLab stores a cached
version of the namespaces and service accounts it creates for your projects. If you
modify these resources in your cluster manually, this cache can fall out of sync with
your cluster, which can cause deployment jobs to fail.
To clear the cache:
1. Navigate to your project’ s **Operations > Kubernetes** page, and select your cluster.
1. Expand the **Advanced settings** section.
1. Click **Clear cluster cache** .
2019-07-25 14:12:45 +00:00
### Base domain
2019-01-28 19:42:17 +00:00
2020-02-06 15:09:11 +00:00
> [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/24580) in GitLab 11.8.
2019-01-28 19:42:17 +00:00
2019-03-01 15:45:17 +00:00
NOTE: **Note:**
You do not need to specify a base domain on cluster settings when using GitLab Serverless. The domain in that case
will be specified as part of the Knative installation. See [Installing Applications ](#installing-applications ).
Specifying a base domain will automatically set `KUBE_INGRESS_BASE_DOMAIN` as an environment variable.
If you are using [Auto DevOps ](../../../topics/autodevops/index.md ), this domain will be used for the different
stages. For example, Auto Review Apps and Auto Deploy.
2019-10-02 12:06:04 +00:00
The domain should have a wildcard DNS configured to the Ingress IP address. After Ingress has been installed (see [Installing Applications ](#installing-applications )),
2019-03-01 15:45:17 +00:00
you can either:
2019-01-28 19:42:17 +00:00
2019-03-01 15:45:17 +00:00
- Create an `A` record that points to the Ingress IP address with your domain provider.
- Enter a wildcard DNS address using a service such as nip.io or xip.io. For example, `192.168.1.1.xip.io` .
2019-01-28 19:42:17 +00:00
2017-11-16 17:17:27 +00:00
## Installing applications
2019-10-31 15:06:41 +00:00
GitLab can install and manage some applications like Helm, GitLab Runner, Ingress,
2020-04-01 00:08:09 +00:00
Prometheus, and so on, in your project-level cluster. For more information on
2019-10-31 15:06:41 +00:00
installing, upgrading, uninstalling, and troubleshooting applications for
your project cluster, see
2019-10-02 12:06:04 +00:00
[GitLab Managed Apps ](../../clusters/applications.md ).
2019-04-03 09:01:44 +00:00
2020-06-30 18:09:13 +00:00
## Auto DevOps
Auto DevOps automatically detects, builds, tests, deploys, and monitors your
applications.
To make full use of Auto DevOps (Auto Deploy, Auto Review Apps, and
Auto Monitoring) you will need the Kubernetes project integration enabled.
[Read more about Auto DevOps ](../../../topics/autodevops/index.md )
2020-07-15 09:09:34 +00:00
NOTE: **Note:**
2020-06-30 18:09:13 +00:00
Kubernetes clusters can be used without Auto DevOps.
2019-07-25 14:12:45 +00:00
## Deploying to a Kubernetes cluster
2018-12-14 12:31:09 +00:00
2019-07-31 08:54:18 +00:00
A Kubernetes cluster can be the destination for a deployment job. If
- The cluster is integrated with GitLab, special
[deployment variables ](#deployment-variables ) are made available to your job
and configuration is not required. You can immediately begin interacting with
the cluster from your jobs using tools such as `kubectl` or `helm` .
- You don't use GitLab's cluster integration you can still deploy to your
cluster. However, you will need configure Kubernetes tools yourself
2020-05-05 18:09:43 +00:00
using [environment variables ](../../../ci/variables/README.md#custom-environment-variables )
2019-07-31 08:54:18 +00:00
before you can interact with the cluster from your jobs.
2018-12-14 12:31:09 +00:00
2019-07-25 14:12:45 +00:00
### Deployment variables
2018-01-12 17:18:09 +00:00
The Kubernetes cluster integration exposes the following
2019-04-08 12:32:38 +00:00
[deployment variables ](../../../ci/variables/README.md#deployment-environment-variables ) in the
2018-02-13 13:32:34 +00:00
GitLab CI/CD build environment.
| Variable | Description |
| -------- | ----------- |
| `KUBE_URL` | Equal to the API URL. |
2019-10-29 03:06:28 +00:00
| `KUBE_TOKEN` | The Kubernetes token of the [environment service account ](add_remove_clusters.md#access-controls ). |
2019-11-05 03:06:09 +00:00
| `KUBE_NAMESPACE` | The namespace associated with the project's deployment service account. In the format `<project_name>-<project_id>-<environment>` . For GitLab-managed clusters, a matching namespace is automatically created by GitLab in the cluster. |
2018-11-08 03:31:23 +00:00
| `KUBE_CA_PEM_FILE` | Path to a file containing PEM data. Only present if a custom CA bundle was specified. |
| `KUBE_CA_PEM` | (**deprecated**) Raw PEM data. Only if a custom CA bundle was specified. |
2018-12-05 09:32:52 +00:00
| `KUBECONFIG` | Path to a file containing `kubeconfig` for this deployment. CA bundle would be embedded if specified. This config also embeds the same token defined in `KUBE_TOKEN` so you likely will only need this variable. This variable name is also automatically picked up by `kubectl` so you won't actually need to reference it explicitly if using `kubectl` . |
2019-03-11 01:47:01 +00:00
| `KUBE_INGRESS_BASE_DOMAIN` | From GitLab 11.8, this variable can be used to set a domain per cluster. See [cluster domains ](#base-domain ) for more information. |
2018-02-13 13:32:34 +00:00
2020-02-28 00:09:08 +00:00
NOTE: **Note:**
2018-11-08 03:31:23 +00:00
Prior to GitLab 11.5, `KUBE_TOKEN` was the Kubernetes token of the main
service account of the cluster integration.
2019-08-09 00:29:24 +00:00
NOTE: **Note:**
If your cluster was created before GitLab 12.2, default `KUBE_NAMESPACE` will be set to `<project_name>-<project_id>` .
2020-02-28 00:09:08 +00:00
### Custom namespace
2020-05-22 03:08:28 +00:00
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/27630) in GitLab 12.6.
2020-02-28 00:09:08 +00:00
The Kubernetes integration defaults to project-environment-specific namespaces
of the form `<project_name>-<project_id>-<environment>` (see [Deployment
variables](#deployment-variables)).
2019-11-05 03:06:09 +00:00
2020-02-28 00:09:08 +00:00
For **non** -GitLab-managed clusters, the namespace can be customized using
2020-05-15 18:07:52 +00:00
[`environment:kubernetes:namespace` ](../../../ci/environments/index.md#configuring-kubernetes-deployments )
2020-02-28 00:09:08 +00:00
in `.gitlab-ci.yml` .
2019-11-05 03:06:09 +00:00
2020-07-16 06:09:33 +00:00
NOTE: **Note:**
When using a [GitLab-managed cluster ](#gitlab-managed-clusters ), the
2020-02-28 00:09:08 +00:00
namespaces are created automatically prior to deployment and [can not be
2020-05-22 03:08:28 +00:00
customized](https://gitlab.com/gitlab-org/gitlab/-/issues/38054).
2019-11-05 03:06:09 +00:00
2020-06-30 18:09:13 +00:00
### Integrations
#### Canary Deployments **(PREMIUM)**
Leverage [Kubernetes' Canary deployments ](https://kubernetes.io/docs/concepts/cluster-administration/manage-deployment/#canary-deployments )
and visualize your canary deployments right inside the Deploy Board, without
the need to leave GitLab.
[Read more about Canary Deployments ](../canary_deployments.md )
#### Deploy Boards **(PREMIUM)**
GitLab's Deploy Boards offer a consolidated view of the current health and
status of each CI [environment ](../../../ci/environments/index.md ) running on Kubernetes,
displaying the status of the pods in the deployment. Developers and other
teammates can view the progress and status of a rollout, pod by pod, in the
workflow they already use without any need to access Kubernetes.
[Read more about Deploy Boards ](../deploy_boards.md )
#### Viewing pod logs
GitLab makes it easy to view the logs of running pods in connected Kubernetes
clusters. By displaying the logs directly in GitLab, developers can avoid having
to manage console tools or jump to a different interface.
[Read more about Kubernetes logs ](kubernetes_pod_logs.md )
#### Web terminals
> Introduced in GitLab 8.15.
When enabled, the Kubernetes integration adds [web terminal ](../../../ci/environments/index.md#web-terminals )
support to your [environments ](../../../ci/environments/index.md ). This is based
on the `exec` functionality found in Docker and Kubernetes, so you get a new
shell session within your existing containers. To use this integration, you
should deploy to Kubernetes using the deployment variables above, ensuring any
deployments, replica sets, and pods are annotated with:
- `app.gitlab.com/env: $CI_ENVIRONMENT_SLUG`
- `app.gitlab.com/app: $CI_PROJECT_PATH_SLUG`
`$CI_ENVIRONMENT_SLUG` and `$CI_PROJECT_PATH_SLUG` are the values of
the CI variables.
You must be the project owner or have `maintainer` permissions to use terminals.
Support is limited to the first container in the first pod of your environment.
2019-07-25 14:12:45 +00:00
### Troubleshooting
2018-12-05 09:32:52 +00:00
2019-06-25 08:16:14 +00:00
Before the deployment jobs starts, GitLab creates the following specifically for
the deployment job:
- A namespace.
- A service account.
2018-12-05 09:32:52 +00:00
2019-03-28 09:28:27 +00:00
However, sometimes GitLab can not create them. In such instances, your job will fail with the message:
2019-03-20 05:07:12 +00:00
2020-05-20 03:08:04 +00:00
```plaintext
2019-04-03 06:31:04 +00:00
This job failed because the necessary resources were not successfully created.
2019-03-28 09:28:27 +00:00
```
2019-03-20 05:07:12 +00:00
2019-04-10 06:38:27 +00:00
To find the cause of this error when creating a namespace and service account, check the [logs ](../../../administration/logs.md#kuberneteslog ).
2018-12-05 09:32:52 +00:00
2019-06-28 08:39:12 +00:00
Reasons for failure include:
2018-12-05 09:32:52 +00:00
2019-06-28 08:39:12 +00:00
- The token you gave GitLab does not have [`cluster-admin` ](https://kubernetes.io/docs/reference/access-authn-authz/rbac/#user-facing-roles )
2019-03-28 09:28:27 +00:00
privileges required by GitLab.
- Missing `KUBECONFIG` or `KUBE_TOKEN` variables. To be passed to your job, they must have a matching
2020-05-15 18:07:52 +00:00
[`environment:name` ](../../../ci/environments/index.md#defining-environments ). If your job has no
2019-03-28 09:28:27 +00:00
`environment:name` set, it will not be passed the Kubernetes credentials.
2018-12-05 09:32:52 +00:00
2020-07-16 06:09:33 +00:00
NOTE: **Note:**
2019-06-28 08:39:12 +00:00
Project-level clusters upgraded from GitLab 12.0 or older may be configured
in a way that causes this error. Ensure you deselect the
[GitLab-managed cluster ](#gitlab-managed-clusters ) option if you want to manage
namespaces and service accounts yourself.
2020-06-30 18:09:13 +00:00
## Monitoring your Kubernetes cluster
Automatically detect and monitor Kubernetes metrics. Automatic monitoring of
[NGINX Ingress ](../integrations/prometheus_library/nginx.md ) is also supported.
[Read more about Kubernetes monitoring ](../integrations/prometheus_library/kubernetes.md )
2020-07-13 18:09:16 +00:00
### Visualizing cluster health
2018-12-14 12:31:09 +00:00
2020-07-13 18:09:16 +00:00
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/4701) in [GitLab Ultimate](https://about.gitlab.com/pricing/) 10.6.
2020-07-17 21:09:23 +00:00
> - [Moved](https://gitlab.com/gitlab-org/gitlab/-/issues/208224) to GitLab Core in 13.2.
2018-12-14 12:31:09 +00:00
When [Prometheus is deployed ](#installing-applications ), GitLab will automatically monitor the cluster's health. At the top of the cluster settings page, CPU and Memory utilization is displayed, along with the total amount available. Keeping an eye on cluster resources can be important, if the cluster runs out of memory pods may be shutdown or fail to start.
2019-05-05 12:29:36 +00:00
![Cluster Monitoring ](img/k8s_cluster_monitoring.png )