How to make use of environments to manage deployments in Gitlab
Here in this article we will try to setup different environment to deploy the application using the gitlab environment feature.
Test Environment
fedres.stack.com – gitlab v16.9.2-ee
k8master.stack.com – master node – v1.28.5
k8node.stack.com – worker node – v1.28.5
Procedure
Step1: Ensure Gitlab agent for Kubernetes cluster installed
As a first step you need to ensure that you have a working Gitlab Server with Gitlab agent for kubernetes cluster installed on your kubernetes cluster.
Follow “How to manage Kubernetes cluster using Gitlab CICD workflow” to complete this setup.
Step2: Ensure kubernetes namespaces exist
Here in this step we are going to create three namespace stackdev, stackpre, stackprod wherein we will deploy our job.
admin@k8master:~$ kubectl get ns
NAME STATUS AGE
...
stackdev Active 61m
stackpre Active 61m
stackprod Active 61m
Step3: Update the CICD pipeline
Now let’s try to update our CICD pipeline in such a way that we have three stages that will be executed, the first stage will deploy the job to dev environment, the second stage will deploy the job to preproduction environment and the third stage will deploy the job to production environment which is protected as it will need to be triggered manually by the user.
Here is the completed .gitlab-ci.yml file.
admin@fedres:~/hellojob$ cat .gitlab-ci.yml
stages:
- deploy_dev
- deploy_pre
- deploy_prod
deploy_dev:
stage: deploy_dev
environment:
name: development
script:
- kubectl config set clusters.gitlab.certificate-authority-data LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUQ3RENDQXRTZ0F3SUJBZ0lVWWZOR1IyS3p5OUtvcjNxOEkva1pBeDhZRVlzd0RRWUpLb1pJaHZjTkFRRUwKQlFBd1p6RUxNQWtHQTFVRUJoTUNTVTR4Q3pBSkJnTlZCQWdNQWsxSU1ROHdEUVlEVlFRSERBWk5kVzFpWVdreApEakFNQmdOVkJBb01CWE4wWVdOck1ROHdEUVlEVlFRTERBWmtaWFp2Y0hNeEdUQVhCZ05WQkFNTUVHWmxaSEpsCmN5NXpkR0ZqYXk1amIyMHdIaGNOTWpRd09UTXdNVEEwTXpNNVdoY05Nall3T1RNd01UQTBNek01V2pCbk1Rc3cKQ1FZRFZRUUdFd0pKVGpFTE1Ba0dBMVVFQ0F3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0doT0VyZmp5R3NiK3dWQkh4bmMvRgo2VXNFL3lUMDY3NlF0S0g5eWhSTjA2di9HZ2tEenZNbVBvUGlHOFIwVUx5QjFPVWpYS0tsNjBNaTZUQy9FM1IyClo4SndpRzd1VG5yNGh2WVV4aVFoVE5MdkJYdUdoM1NrUjNoVmVZWktkZTlBTzlRRmorcERBTTNuZEZqMUZNY1MKQzF2ekhzdFVNT2hxSHNCZXFMbkI3NHp5NmV6V3hGWEZLMkdDcTlXRm9RSG90cDJxQ3pCeEpiU3psdXN1bm5MSgpZaGdVTGtKS1R6Y1UzVlh3SExrRldaNFJZZ3Vmc3ozRGc1ZGFwUmpoTzhuSXVJb29CNWdnamFtWEVNRlBqNFh6Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K
- kubectl config view
- kubectl config get-contexts
- kubectl config use-context dev/kubernetes_iac:fedreskub
#- kubectl create ns stackdev
- kubectl -n stackdev apply -f job.yml
- kubectl -n stackdev get jobs
#- kubectl --insecure-skip-tls-verify get pods
deploy_pre:
stage: deploy_pre
environment:
name: preproduction
script:
- kubectl config set clusters.gitlab.certificate-authority-data LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUQ3RENDQXRTZ0F3SUJBZ0lVWWZOR1IyS3p5OUtvcjNxOEkva1pBeDhZRVlzd0RRWUpLb1pJaHZjTkFRRUwKQlFBd1p6RUxNQWtHQTFVRUJoTUNTVTR4Q3pBSkJnTlZCQWdNQWsxSU1ROHdEUVlEVlFRSERBWk5kVzFpWVdreApEakFNQmdOVkJBb01CWE4wWVdOck1ROHdEUVlEVlFRTERBWmtaWFp2Y0hNeEdUQVhCZ05WQkFNTUVHWmxaSEpsCmN5NXpkR0ZqYXk1amIyMHdIaGNOTWpRd09UTXdNVEEwTXpNNVdoY05Nall3T1RNd01UQTBNek01V2pCbk1Rc3cKQ1FZRFZRUUdFd0pKVGpFTE1Ba0dBMVVFQ0F3Q1RVZ3hEekFOQmdOVkJBY01CazExYldKaG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XNFL3lUMDY3NlF0S0g5eWhSTjA2di9HZ2tEenZNbVBvUGlHOFIwVUx5QjFPVWpYS0tsNjBNaTZUQy9FM1IyClo4SndpRzd1VG5yNGh2WVV4aVFoVE5MdkJYdUdoM1NrUjNoVmVZWktkZTlBTzlRRmorcERBTTNuZEZqMUZNY1MKQzF2ekhzdFVNT2hxSHNCZXFMbkI3NHp5NmV6V3hGWEZLMkdDcTlXRm9RSG90cDJxQ3pCeEpiU3psdXN1bm5MSgpZaGdVTGtKS1R6Y1UzVlh3SExrRldaNFJZZ3Vmc3ozRGc1ZGFwUmpoTzhuSXVJb29CNWdnamFtWEVNRlBqNFh6Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K
- kubectl config view
- kubectl config get-contexts
- kubectl config use-context dev/kubernetes_iac:fedreskub
#- kubectl create ns stackpre
- kubectl -n stackpre apply -f job.yml
- kubectl -n stackpre get jobs
#- kubectl --insecure-skip-tls-verify get pods
deploy_prod:
stage: deploy_prod
environment:
name: production
when: manual
script:
- kubectl config set clusters.gitlab.certificate-authority-data LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUQ3RENDQXRTZ0F3SUJBZ0lVWWZOR1IyS3p5OUtvcjNxOEkva1pBeDhZRVlzd0RRWUpLb1pJaHZjTkFRRUwKQlFBd1p6RUxNQWtHQTFVRUJoTUNTVTR4Q3pBSkJnTlZCQWdNQWsxSU1ROHdEUVlEVlFRSERBWk5kVzFpWVdreApEakFNQmdOVkJBb01CWE4wWVdOck1ROHdEUVlEVlFRTERBWmtaWFp2Y0hNeEdUQVhCZ05WQkFNTUVHWmxaSEpsCmN5NXpkR0ZqYXk1amIyMHdIaGNOTWpRd09UTXdNVEEwTXpNNVdoY05Nall3T1RNd01UQTBNek01V2pCbk1Rc3cKQ1FZRFZRUUdFd0pKVGpFTE1Ba0dBMVVFQ0F3Q1RVZ3hEekFOQmdOVkJBY01CazExYldKaGFURU9NQXdHQTFVRQpDZ3dGYzNSaFk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HZ2tEenZNbVBvUGlHOFIwVUx5QjFPVWpYS0tsNjBNaTZUQy9FM1IyClo4SndpRzd1VG5yNGh2WVV4aVFoVE5MdkJYdUdoM1NrUjNoVmVZWktkZTlBTzlRRmorcERBTTNuZEZqMUZNY1MKQzF2ekhzdFVNT2hxSHNCZXFMbkI3NHp5NmV6V3hGWEZLMkdDcTlXRm9RSG90cDJxQ3pCeEpiU3psdXN1bm5MSgpZaGdVTGtKS1R6Y1UzVlh3SExrRldaNFJZZ3Vmc3ozRGc1ZGFwUmpoTzhuSXVJb29CNWdnamFtWEVNRlBqNFh6Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K
- kubectl config view
- kubectl config get-contexts
- kubectl config use-context dev/kubernetes_iac:fedreskub
#- kubectl create ns stackprod
- kubectl -n stackprod apply -f job.yml
- kubectl -n stackprod get jobs
#- kubectl --insecure-skip-tls-verify get pods
Push the changes to the repository.
admin@fedres:~/hellojob$ git add .
admin@fedres:~/hellojob$ git commit -m "update cicd with env"
admin@fedres:~/hellojob$ git push -u origin main
Step4: Trigger the pipeline
Go to Build – Pipelines and trigger the pipeline.
The pipeline will deploy the job to dev and pre environment but the production deployment needs to be manually triggered.
Hit the play button in the deploy_prod stage to trigger the deployment to production environment.
Step5: Validate the deployments
Go to Operate – Environment and expand each environment see the deployments in each environment.
You can also check the jobs deployed in your kubernetes cluster in the respective namespaces as shown below.
admin@k8master:~$ k -n stackdev get jobs
NAME COMPLETIONS DURATION AGE
pi 1/1 9s 23m
admin@k8master:~$ k -n stackpre get jobs
NAME COMPLETIONS DURATION AGE
pi 1/1 9s 23m
admin@k8master:~$ k -n stackprod get jobs
NAME COMPLETIONS DURATION AGE
pi 1/1 8s 23m
Hope you enjoyed reading this article. Thank you..
Leave a Reply
You must be logged in to post a comment.