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_accessto 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/webGotchas
- 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_accessconfig or the tunnel is unavailable.
Related guides
How to Build and Push to the GitLab Container Registry in GitLab CI/CDBuild an image and push it to the GitLab Container Registry from CI/CD, authenticating with the built-in CI_R…
How to Set Up Environments and Deployments in GitLab CI/CDTrack deployments in GitLab CI/CD with the environment keyword, recording deployed versions and exposing a li…