Skip to content
Latchkey

How to Deploy to Kubernetes With the GitLab Agent in GitLab CI/CD

The GitLab agent grants CI jobs a kubeconfig context over a secure tunnel, so no cluster secret lives in the project.

Once the agent is installed and ci_access is granted to your project, jobs get a context named path/to/project:agent-name. Select it with kubectl config use-context and deploy.

Steps

  • Install the GitLab agent and grant ci_access to your project.
  • Use kubectl config use-context <project>:<agent> in the job.
  • Apply your manifests with kubectl.

Pipeline

.gitlab-ci.yml
deploy:
  image:
    name: bitnami/kubectl:latest
    entrypoint: [""]
  script:
    - kubectl config use-context my-group/my-app:prod-agent
    - kubectl apply -f k8s/
    - kubectl rollout status deployment/web

Gotchas

  • The context name is <project-full-path>:<agent-name>; a typo gives a context-not-found error.
  • The project must be listed under the agent's ci_access config or the tunnel is unavailable.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →