A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.gitlab.com/user/clusters/agent/ci_cd_workflow/ below:

Using GitLab CI/CD with a Kubernetes cluster

History

You can use GitLab CI/CD to safely connect, deploy, and update your Kubernetes clusters.

To do so, install an agent in your cluster. When done, you have a Kubernetes context and can run Kubernetes API commands in your GitLab CI/CD pipeline.

To ensure access to your cluster is safe:

To use GitLab CI/CD to interact with your cluster, runners must be registered with GitLab. However, these runners do not have to be in the cluster where the agent is.

Prerequisites:

Use GitLab CI/CD with your cluster

To update a Kubernetes cluster with GitLab CI/CD:

  1. Ensure you have a working Kubernetes cluster and the manifests are in a GitLab project.
  2. In the same GitLab project, register and install the GitLab agent for Kubernetes.
  3. Update your .gitlab-ci.yml file to select the agent’s Kubernetes context and run the Kubernetes API commands.
  4. Run your pipeline to deploy to or update the cluster.

If you have multiple GitLab projects that contain Kubernetes manifests:

  1. Install the GitLab agent for Kubernetes in its own project, or in one of the GitLab projects where you keep Kubernetes manifests.
  2. Authorize agent access in your GitLab projects.
  3. Optional. For added security, use impersonation.
  4. Update your .gitlab-ci.yml file to select the agent’s Kubernetes context and run the Kubernetes API commands.
  5. Run your pipeline to deploy to or update the cluster.

If you have multiple projects with Kubernetes manifests, you must authorize these projects to access the agent. You can authorize agent access for individual projects, groups, or subgroups so all projects have access. For added security, you can also use impersonation.

Authorization configuration can take one or two minutes to propagate.

Authorize your projects to access the agent

History

To authorize the GitLab project where you keep Kubernetes manifests to access the agent:

  1. On the left sidebar, select Search or go to and find the project that contains the agent configuration file (config.yaml).

  2. Edit the config.yaml file. Under the ci_access keyword, add the projects attribute.

  3. For the id, add the path to the project.

    ci_access:
      projects:
        - id: path/to/project

After making these changes:

Authorize projects in your groups to access the agent

History

To authorize all of the GitLab projects in a group or subgroup to access the agent:

  1. On the left sidebar, select Search or go to and find the project that contains the agent configuration file (config.yaml).

  2. Edit the config.yaml file. Under the ci_access keyword, add the groups attribute.

  3. For the id, add the path:

    ci_access:
      groups:
        - id: path/to/group/subgroup

After making these changes:

Authorize all projects in your GitLab instance to access the agent

History

Prerequisites:

To allow agents to be configured to authorize all projects in your GitLab instance:

  1. In the Admin area, select Settings > General, and expand the GitLab agent for Kubernetes section.
  2. Select Enable instance level authorization.
  3. Select Save changes.

To authorize the agent to access all of the GitLab projects:

  1. On the left sidebar, select Search or go to and find the project that contains the agent configuration file (config.yaml).

  2. Edit the config.yaml file. Under the ci_access keyword, add the instance attribute:

After making these changes to the agent configuration file:

Update your .gitlab-ci.yml file to run kubectl commands

In the project where you want to run Kubernetes commands, edit your project’s .gitlab-ci.yml file.

In the first command under the script keyword, set your agent’s context. Use the format <path/to/agent/project>:<agent-name>. For example:

deploy:
  image:
    name: bitnami/kubectl:latest
    entrypoint: ['']
  script:
    - kubectl config get-contexts
    - kubectl config use-context path/to/agent/project:agent-name
    - kubectl get pods

If you are not sure what your agent’s context is, run kubectl config get-contexts from a CI/CD job where you want to access the agent.

Environments that use Auto DevOps

If Auto DevOps is enabled, you must define the CI/CD variable KUBE_CONTEXT. Set the value of KUBE_CONTEXT to the context of the agent you want Auto DevOps to use:

deploy:
  variables:
    KUBE_CONTEXT: path/to/agent/project:agent-name

You can assign different agents to separate Auto DevOps jobs. For instance, Auto DevOps can use one agent for staging jobs, and another agent for production jobs. To use multiple agents, define an environment-scoped CI/CD variable for each agent. For example:

  1. Define two variables named KUBE_CONTEXT.
  2. For the first variable:
    1. Set the environment to staging.
    2. Set the value to the context of your staging agent.
  3. For the second variable:
    1. Set the environment to production.
    2. Set the value to the context of your production agent.
Environments with both certificate-based and agent-based connections

When you deploy to an environment that has both a certificate-based cluster (deprecated) and an agent connection:

To use an agent connection when certificate-based connections are present, you can manually configure a new kubectl configuration context. For example:

deploy:
  variables:
    KUBE_CONTEXT: my-context # The name to use for the new context
    AGENT_ID: 1234 # replace with your agent's numeric ID
    K8S_PROXY_URL: https://<KAS_DOMAIN>/k8s-proxy/ # For agent server (KAS) deployed in Kubernetes cluster (for gitlab.com use kas.gitlab.com); replace with your URL
    # K8S_PROXY_URL: https://<GITLAB_DOMAIN>/-/kubernetes-agent/k8s-proxy/ # For agent server (KAS) in Omnibus
    # Include any additional variables
  before_script:
    - kubectl config set-credentials agent:$AGENT_ID --token="ci:${AGENT_ID}:${CI_JOB_TOKEN}"
    - kubectl config set-cluster gitlab --server="${K8S_PROXY_URL}"
    - kubectl config set-context "$KUBE_CONTEXT" --cluster=gitlab --user="agent:${AGENT_ID}"
    - kubectl config use-context "$KUBE_CONTEXT"
  # Include the remaining job configuration
Environments with KAS that use self-signed certificates

If you use an environment with KAS and a self-signed certificate, you must configure your Kubernetes client to trust the certificate authority (CA) that signed your certificate.

To configure your client, do one of the following:

Restrict project and group access by using impersonation

History

By default, your CI/CD job inherits all the permissions from the service account used to install the agent in the cluster. To restrict access to your cluster, you can use impersonation.

To specify impersonations, use the access_as attribute in your agent configuration file and use Kubernetes RBAC rules to manage impersonated account permissions.

You can impersonate:

Authorization configuration can take one or two minutes to propagate.

Impersonate the agent

The agent is impersonated by default. You don’t need to do anything to impersonate it.

Impersonate the CI/CD job that accesses the cluster

To impersonate the CI/CD job that accesses the cluster, under the access_as key, add the ci_job: {} key-value.

When the agent makes the request to the actual Kubernetes API, it sets the impersonation credentials in the following way:

Property Description agent.gitlab.com/id Contains the agent ID. agent.gitlab.com/config_project_id Contains the agent’s configuration project ID. agent.gitlab.com/project_id Contains the CI project ID. agent.gitlab.com/ci_pipeline_id Contains the CI pipeline ID. agent.gitlab.com/ci_job_id Contains the CI job ID. agent.gitlab.com/username Contains the username of the user the CI job is running as. agent.gitlab.com/environment_slug Contains the slug of the environment. Only set if running in an environment. agent.gitlab.com/environment_tier Contains the tier of the environment. Only set if running in an environment.

Example config.yaml to restrict access by the CI/CD job’s identity:

ci_access:
  projects:
    - id: path/to/project
      access_as:
        ci_job: {}
Example RBAC to restrict CI/CD jobs

The following RoleBinding resource restricts all CI/CD jobs to view rights only.

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: ci-job-view
roleRef:
  name: view
  kind: ClusterRole
  apiGroup: rbac.authorization.k8s.io
subjects:
  - name: gitlab:ci_job
    kind: Group
Impersonate a static identity

For a given connection, you can use a static identity for the impersonation.

Under the access_as key, add the impersonate key to make the request using the provided identity.

The identity can be specified with the following keys:

See the official Kubernetes documentation for details.

Restrict project and group access to specific environments

History

By default, if your agent is available to a project, all of the project’s CI/CD jobs can use that agent.

To restrict access to the agent to only jobs with specific environments, add environments to ci_access.projects or ci_access.groups. For example:

ci_access:
  projects:
    - id: path/to/project-1
    - id: path/to/project-2
      environments:
        - staging
        - review/*
  groups:
    - id: path/to/group-1
      environments:
        - production

In this example:

Restrict access to the agent to protected branches

History

The availability of this feature is controlled by a feature flag. For more information, see the history. This feature is available for testing, but not ready for production use.

To restrict access to the agent to only jobs run on protected branches:

By default, protected_branches_only is set to false, and the agent can be accessed from unprotected and protected branches.

For additional security, you can combine this feature with environment restrictions.

If a project has multiple configurations, only the most specific configuration is used. For example, the following configuration grants access to unprotected branches in example/my-project, even though the example group is configured to grant access to only protected branches:

# .gitlab/agents/my-agent/config.yaml
ci_access:
  project:
    - id: example/my-project # Project of the group below
      protected_branches_only: false # This configuration supercedes the group configuration
      environments:
        - dev
  groups:
    - id: example
      protected_branches_only: true
      environments:
        - dev

For more details, see Access to Kubernetes from CI/CD.

Troubleshooting Grant write permissions to ~/.kube/cache

Tools like kubectl, Helm, kpt, and kustomize cache information about the cluster in ~/.kube/cache. If this directory is not writable, the tool fetches information on each invocation, making interactions slower and creating unnecessary load on the cluster. For the best experience, in the image you use in your .gitlab-ci.yml file, ensure this directory is writable.

Enable TLS

If you are on GitLab Self-Managed, ensure your instance is configured with Transport Layer Security (TLS).

If you attempt to use kubectl without TLS, you might get an error like:

$ kubectl get pods
error: You must be logged in to the server (the server has asked for the client to provide credentials)
Unable to connect to the server: certificate signed by unknown authority

If you use an environment with KAS and a self-signed certificate, your kubectl call might return this error:

kubectl get pods
Unable to connect to the server: x509: certificate signed by unknown authority

The error occurs because the job does not trust the certificate authority (CA) that signed the KAS certificate.

To resolve the issue, configure kubectl to trust the CA.

Validation errors

If you use kubectl versions v1.27.0 or v.1.27.1, you might get the following error:

error: error validating "file.yml": error validating data: the server responded with the status code 426 but did not return more information; if you choose to ignore these errors, turn validation off with --validate=false

This issue is caused by a bug with kubectl and other tools that use the shared Kubernetes libraries.

To resolve the issue, use another version of kubectl.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4