From gitlab to kubernetes

Victor Yeo
2 min readFeb 28, 2022

--

This article will talk about the process of taking the web application code from gitlab, creating a docker image, and deploying the docker image on kubernetes pod.

  1. In gitlab, create a gitlab-ci.yml file. If using GCR, login to GCR with the GCP service account key. See GCR documentation.
echo $GCP_SERVICE_KEY | base64 -d | docker login -u _json_key  --password-stdin https://${GCR}

2. In the same file, call docker commands to build a docker image

docker build -t ${IMAGE_NAME} .docker tag $IMAGE_NAME “${GCR}/${GCP_PROJECT}/${IMAGE_NAME}”docker push “${GCR}/${GCP_PROJECT}/${IMAGE_NAME}:latest”

3. In GCP, setup the kubernetes cluster. The actual setup is out of scope of this article. You can setup the cluster using GCP web UI, or setup using terraform.

After setup is done, go to command prompt, use gcloud to select the GCP project, and show the current k8s cluster info.

## select the project
$ gcloud config set project <project_name>
## show the k8s context and cluster info
kubectl config current-context
## show the k8s cluster list
$ gcloud container clusters list
NAME LOCATION MASTER_VERSION MASTER_IP MACHINE_TYPE NODE_VERSION NUM_NODES STATUS
k8s-dev us-east1 1.20.12-gke.1500 10.10.22.6 e2-medium 1.19.15-gke.1801 * 5 RUNNING

4. Create a helm chart, you will need to create the following files. The actual content of the helm yaml files are out of scope of this article.

<helm_chart_folder>
- templates
|- configmap.yaml
|- deployment.yaml
- Chart.yaml
- values.yaml

5. In helm chart folder, run the command below

## helm install <release_name> <folder_name> -n <namespace>
$ helm install <name> . -n <namespace>

6. Check if the k8s pod is deployed successfully

## to get the pod name
$ kubectl -n <namespace> get pods
$ kubectl -n <namespace> describe pod <podname>Name: mycompo-7bfd77747-wrzv8
Namespace: namespace
Priority: 0
Node: gke-k8s-dev-default-node-pool-15936747–3m17/10.60.40.49
Start Time: Sat, 12 Jan 2021 10:02:42 +0800
Labels: component=mycompo
pod-template-hash=5afd77747
Annotations: cni.projectcalico.org/podIP: 10.10.48.130/32
cni.projectcalico.org/podIPs: 10.10.48.130/32
rollme: rgjQT
Status: Running
IP: 10.10.48.130
IPs:
IP: 10.10.48.130
Controlled By: ReplicaSet/mycompo-5afd77747
Containers:
mycompo:
Container ID: docker://ff9dd6474fee5de07fb974d30bf4f410b49b673124cade14925daf2742d9e06a35

7. Appendix

The acronyms are listed as below.

GCR — Google Container Registry

GCP — Google Cloud Platform

GCP Web UI example is shown below. You can click “Create” button to create a new k8s cluster.

--

--

No responses yet