[Infrastructure as Code (IaC)] GitLab Runner, Kubernetes executor and kubectl to deploy manifest to Kubernetes (K8S)

GitLab Runner, Kubernetes executor and kubectl to deploy manifest to Kubernetes (K8S)

GitLab Runner is an application that works with GitLab CI/CD to run jobs in a pipeline.

The Kubernetes executor, when used with GitLab CI, connects to the Kubernetes API in the cluster creating a Pod for each GitLab CI Job.

The Kubernetes command-line tool, kubectl, allows you to run commands against Kubernetes clusters. You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs.

Usages

Creating a Deployment

The following is an example of a Deployment. It creates a ReplicaSet to bring up three nginx Pods:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# nginx-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80

Convert kubeconfig file to Base64 format as the Variable.

1
$ base64 -w 0 .kube/config

Use kubectl to deploy manifest to Kubernetes (K8S) in .gitlab.ci.yaml.

1
2
3
4
5
6
kubernetes:
stage: deploy
image: bitnami/kubectl:1.22-debian-10
script:
- echo ${KUBECONFIG_BASE64} | base64 -d > .kube/config
- KUBECONFIG=.kube/config kubectl apply -f deployments.yml

Waiting for Pod Running

You can use while loop in shell to waiting for Pod status from Pending to Running.

1
2
3
4
5
6
7
8
kubernetes:
stage: deploy
script:
# - APP_LABEL="<Your App Label"
# - echo ${APP_LABEL}
# - REPLICAS=3
# - echo ${REPLICAS}
- while test `KUBECONFIG=.kube/config kubectl get pods -l ${APP_LABEL} -n default | grep Running | wc -l` -ne ${REPLICAS}; do sleep 5; echo "waiting 5s for pods ${APP_LABEL} ready..."; done

FAQs

base64: invalid input

A likely reason for 76 being the default is that Base64 encoding was to provide a way to include binary files in e-mails and Usenet postings which was intended for humans using monitors with 80 characters width. Having a 76-character width as default made that usecase easier.

-w, --wrap=COLS will wrap encoded lines after COLS character (default 76). Use 0 to disable line wrapping.

1
$ base64 -w 0 .kube/config

Variable value is empty

If the variable is marked as protected, please remember to also mark the corresponding branch as protected, otherwise the obtained variable value is empty.

References

[1] The Kubernetes executor | GitLab - https://docs.gitlab.com/runner/executors/kubernetes.html

[2] GitLab CI/CD environment variables | GitLab - https://docs.gitlab.com/ee/ci/variables/

[3] The .gitlab-ci.yml file | GitLab- https://docs.gitlab.com/ee/ci/yaml/gitlab_ci_yaml.html

[4] Loops - Shell Scripting Tutorial - https://www.shellscript.sh/loops.html

[5] Test - Shell Scripting Tutorial - https://www.shellscript.sh/test.html

[6] GitLab CI/CD | GitLab - https://docs.gitlab.com/ee/ci/

[7] Iterate faster, innovate together | GitLab - https://about.gitlab.com/

[8] Base64 Decode and Encode - Online - https://www.base64decode.org/

[9] The Kubernetes executor | GitLab - https://docs.gitlab.com/runner/executors/kubernetes.html

[10] bitnami/kubectl - Docker Image | Docker Hub - https://hub.docker.com/r/bitnami/kubectl/