Deploy Web Services on GKE Cluster with Node.js

Deploy Web Services on GKE Cluster with Node.

jsVaruni PunchihewaBlockedUnblockFollowFollowingMay 4Kubernetes is an open source system for automating deployments, scaling and managing containerized applications.

We will first briefly look at some of the concepts in Kubernetes that you may come across doing the Hands-on.

There is something called Images where you find the executable package of your application that includes your code, libraries, conf files, runtime, environment variables, etc.

By running these images we launch something called Containers.

These containers run in a container cluster which is managed using Kubernetes.

A container cluster is nothing but a group of compute engine VM (Virtual Machine) instances.

In a container cluster, there are two types of VM instances.

MasterNode instancesThus, in the above diagram, there are four VM instances, for each node instance and master.

Master is the supervising machine.

It manages the cluster.

Kublet is used to communicate with the master.

The pods contain containers.

Inside each pod, there can be multiple containers running.

All the containers inside a pod share the same underlying resources.

That means they all have the same IP address, share the same disk volumes, etc.

A service is a grouping of pods that are running on the cluster.

Hands-onFor this practical, I will be using Ubuntu 18.

04.

1 and node.

jsPrerequisites:Sign in to your Google account.

If you do not have one, sign up for a new account.

2.

Install Google Cloud SDK.

3.

Install Kubectlsudo snap install kubectl –classicGo to the Google Cloud Platform Console and create a new project.

Open the newly created project.

In the Navigation menu, click the APIs & Services and go to the Library page.

Search for Kubernetes Engine API and click EnableNow let us create a cluster.

Go to the Navigation menu, select Kubernetes Engine -> Clusters.

Then you will be prompted to Create a Cluster.

You can customize your cluster according to your needs, but for this project, I will be using all the default settings.

Click the create button, and wait till the cluster is created.

Configure kubectl command line access by running the following command:gcloud container clusters get-credentials <cluster name> — zone <zone name> –project <project ID>eg: gcloud container clusters get-credentials standard-cluster-service –zone us-central1-a –project k8s-api-service-projectOpen the gcloud shell, and typegcloud container clusters listThis will list out all the existing clusters for running containers.

In our case, we have a cluster with three nodes.

a list of existing clusters for running containersNow, in your local machine, open the terminal, go inside the folder where your application is at.

Create a text file named “Dockerfile” inside the folder you are now at.

touch DockerfileOpen the created text file and copy and paste the following commands, and save it.

FROM node:8# Create app directoryWORKDIR /usr/src/app# Install app dependencies# A wildcard is used to ensure both package.

json AND package-lock.

json are copied# where available (npm@5+)COPY package*.

json .

/RUN npm install# If you are building your code for production# RUN npm ci –only=production# Bundle app sourceCOPY .

.

CMD [ "npm", "start" ]Dockerfile is a text file that has a series of instructions on how to build your image.

It supports a simple set of commands that you need to use in your Dockerfile.

WORKDIR <path>: Sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile.

If the WORKDIR doesn’t exist, it will be created even if it’s not used in any subsequent Dockerfile instruction.

COPY <src>.

<dest>: Copy new files or directories from <src> and adds them to the filesystem of the container at the path <dest>.

RUN <command> : The command is run in a shell, which by default is /bin/sh -c on LinuxCMD [“executable”,”param1",”param2"] : Sets the command to be executed when running the image.

Similarly, create a “.

dockerignore” file using touch .

dockerignore and copy and paste the following commands.

node_modulesnpm-debug.

logIn most cases, you’ll be copying in the source code of your application into a docker image.

Typically you would do that by adding COPY src/ dest/ or similar to your Dockerfile.

That’s a great way to do it, but that’s also going to include things like your .

git/ directory or /tmp folders that belong to your project, which you really do not need for building the docker image.

Including such files will increase the docker image size unnecessarily.

We can exclude files and directories we do not need within our final image.

All you have to do is create a .

dockerignore file alongside your Dockerfile.

At this point, it’s pretty similar to what a .

gitignore file does for your git repos.

You just need to tell it what you want to ignore.

Then, build the container image for the data service application.

docker build -t <image repository name>:<tag name> .

eg: docker build -t dataserver:v2 .

To see your built image, type docker imagesNow, let’s put this image to the Docker Hub.

( If you do not have an account at docker hub, first you need to sign up there)Create a new repository.

Mine would be “apiservice”Now push the image we built into the docker hubLog in to the Docker Hub from the terminaldocker loginEnter your username and passwordGet your image ID by typing docker imagesTag your imagedocker tag <image ID> <docker hub repository name>:<tag>eg: docker tag 377026348163 varuni95/dataservice:v2Push your imagedocker push <docker hub repository name>eg: docker push varuni95/dataserviceNow let’s pull our image and create a new container from it.

Go to the Gcloud shell,kubectl run <container name> –image=<docker hub repository name>:<tag name> –port=<port number>eg: kubectl run data –image=varuni95/dataservice:v2 –port=3101Now expose the Kubernetes deployment through a load balancerkubectl expose deployment data –type=LoadBalancerGet the external IP addresskubectl get svcCopy that IP address ( 35.

232.

48.

65), and paste it there as the request URL in your api_service application (go inside api_service>routes>states_hash.

js.

Do the same for api_service>routes>states_titlecase.

js)Now let’s follow the same steps as mentioned above to expose the api servicedocker build -t apiserver:v2 .

Go to the Docker Hub and create a new repository (eg: apiservice)Get your image Id from docker imagesTag your imagedocker tag c174a3d43afa varuni95/apiservice:v2Push your imagedocker push varuni95/apiservicePull our image and run a new container in the clusterkubectl run api –image=varuni95/apiservice:v2 –port=3100Expose the deployment through a load balancerkubectl expose deployment api –type=LoadBalancerGet your external IP addressNow you should now be able to access the service by pointing your browser to this address:http://35.

184.

37.

140:3100/codeToState?code=ALhttp://35.

184.

37.

140:3100/stateToCode?state=AlabamaTry out with different US state codes ????Here is the link to the GitHub RepoVaruni-Punchihewa/kubernetesContribute to Varuni-Punchihewa/kubernetes development by creating an account on GitHub.

github.

com.. More details

Leave a Reply