[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 | # nginx-deployment.yaml |
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 | kubernetes: |
Waiting for Pod Running
You can use while loop in shell to waiting for Pod status from Pending
to Running
.
1 | kubernetes: |
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/