How is the automation of application deployment in Kubernetes implemented using GitLab CI/CD?
sobes.tech AI
Answer from AI
Automating application deployment in Kubernetes with GitLab CI/CD typically involves the following steps:
-
Setting up GitLab Runner — runs pipelines and has access to the Kubernetes cluster.
-
Creating
.gitlab-ci.yml— a file that describes the stages and jobs for building, testing, and deploying. -
Building and publishing the container image — during one of the build stages, the application's Docker image is built and pushed to a registry (GitLab Container Registry or another).
-
Deploying to Kubernetes — using
kubectlor Helm in the pipeline, Kubernetes manifests are applied to update the deployment with the new image. -
Using environment variables and secrets — for storing configurations and access to the cluster.
Example of a simple .gitlab-ci.yml for deployment:
stages:
- build
- deploy
build_image:
stage: build
image: docker:latest
services:
- docker:dind
script:
- docker build -t registry.gitlab.com/your_project/image:$CI_COMMIT_SHA .
- docker push registry.gitlab.com/your_project/image:$CI_COMMIT_SHA
only:
- main
deploy_to_k8s:
stage: deploy
image: bitnami/kubectl:latest
script:
- kubectl set image deployment/your-deployment your-container=registry.gitlab.com/your_project/image:$CI_COMMIT_SHA
- kubectl rollout status deployment/your-deployment
only:
- main
Thus, when pushing to the main branch, the image is built, published, and the application in Kubernetes is updated automatically.