Deploy Your Application Directly From GitLab CI to Google Kubernetes Engine(GKE)

Onur Ozkan
3 min readMar 17, 2020

--

Google Cloud Platform has containerized application management called Google Kubernetes Engine(GKE). GKE Gives you production-ready managed service for containerized applications which allows you to get up and running Kubernetes Clusters with your applications in it.

In this article, we’ll create a Gitlab CI/CD pipeline that uses a Gitlab CI Runner to automate the deployment and configuration of Google Cloud GKE cluster.

Every GitLab CI/CD pipelines needs a YAML file called .gitlab-ci.yml which allows you to manage your pipeline. We have an example repository for this, so I’ll explain everything more easily. This project has a simple node.js app in it, and the pipeline automatically creates a container for every change that made and deploys it on GKE via .gitlab-ci.yml file. Let’s take a closer look.

GitLab CI file has parameters that define the job’s behavior. If we look at the top, we have a parameter called stages that has three steps. Every step has a different job and those jobs running on Gitlab Runners. By default, CI jobs runs on shared Gitlab Runners but you can install and register a runner from your own private environment. You can find more information in here.

In the first step, which is called docker build, the runner simply uses Docker Engine to create a new docker image and pushes that image to the registry. This Image starts up an HTTP server on port 3000, that server response back You're on, HOSTNAME to every request. HOSTNAME here is the server’s actual hostname. Again, this is a simple HTTP server and so far all it needs is a Docker Engine. If we want this server reachable from the outside world we need proper networking and network management which leads us to step two.

The second step is called gcloud deploy, it uses Google Cloud command-line tool on the runner and authenticates a Google Cloud account in a secure way. It uses SERVICE_ACCOUNT variable here, and that holds an environment variable which applied to environments via the runner. The Environment variables can be protected by only exposing them to protected branches or tags. Additionally, they can be masked so they are hidden in job logs. With that account information, the runner creates a Kubernetes cluster with the specs sets in .gitlab-ci.yml file. Lastly, the same runner uses Kubernetes command-line tool this time and deploys the pods specified in simple-app.yaml file which I’ll explain that file later.

The last step is called gcloud destroy I add this step because I create the repository for just test purposes, so I would like to destroy the deployment automatically when I’m done. Because of when: manual parameter, this step actually triggers only manually. To do that you can navigate to your project’s CI/CD > Pipelines. Then click Manual Job button, the jobs execute as configured.

Now we can go back to simple-app.yaml file, Kubernetes treats all aspects as an object and those objects represented by a RESTful resource. You can provide this object information to Kubernetes via a YAML file. Let’s take a look,

This YAML file has two sections, Kubernetes divide these sections with parameter called kind. So, we have two kind here, first one is kind: Deployment, the other one is kind: Service. Both sections has to have ApiVersion, Kind, Metadata and Spec parameters in them, which keep necessary information for Kubernetes objects.

This deployment gets the container image actually created in step one, then copy that image in 3 separate nodes (that’s what ReplicaSet demands in the filereplicas: 3) and expose them on port 3000 (containerPort: 3000). These three pods will be exposed to the outside world with a LoadBalancer service, that service load balancing traffic among pods Exposes the Service externally using a Google Cloud’s load balancer. With the service section in our simple-app.yaml file, GKE creates a load balancer service and redirect pod’s 3000 port to 80 port from the outside world.

There’s alot needs to be explained, I tried to keep it simple and beginner-friendly as possible, enjoy…

--

--