Geek Culture
Published in

Geek Culture

Deploy API on Kubernetes for Free

A 5-minute guide to create a Node backend on Kubernetes cluster

Photo by Karim MANJRA on Unsplash

Kubernetes has gained tremendous popularity and becomes the de-facto container orchestration tool because it provides load balancing, auto-scaling, automatic redeployment, health checking, and most important, vendor agnostic.

It empowers micro services shops to run zero-downtime application and became a crucial skill for developers. Nonetheless, the learning curve of Kubernetes is steep, not only because of the giant ecosystem and complex architecture but also the high monetary cost of provisioning infrastructure. Minikube is helpful to simulate how Kubernetes works on a local machine. It is the best for development but not ideal if you want to experiment on a real multi-node cluster. Kubesail is another excellent tool to provision Kubernetes cluster on self-owned hardware. It is perfect for developers who have existing bare-metal machine or IoT devices such as Raspberry Pi.

Suppose you want to experiment and learn on a real cluster with zero cost. YES! It is free. This tutorial is absolutely for you.

Are you kidding me? Let’s find out the answer by walking through a short but exciting workshop with me.

Long story short. We will create an Internet-accessible endpoint hosted on a Kubernetes cluster in 3 minutes.

Table of Content

Section 1: Create an API server

Section 2: Kubernetes configuration

Section 3: Deploy to Kubernetes cluster

Section 4: Scale up the pods

Section 1: Create an API server

This section will create a simple Node app, dockerise it, and upload the Docker image to the Okteto registry, the PAAS magic we rely on today.

Initiate project

Create a repository on Github and git clone it to your local machine.

Initiate the project to generate the package.json.

yarn init -y

express is the only dependency in this project. Let’s install it.

yarn add express

The focus of the app is to expose the skills data in CSV format as a GET endpoint. So we’ll skip the production requirement such as error handling, security, and throttling.

1) Create Node app

Create a simple Node application to expose an endpoint /skills on port 3001. We allow CORS by adding three Access-Control-Allow* headers to make browsers happy if the front-end application consumes the API response.

The endpoint reads a file skills.csv and writes the content to the HTTP response.

Run it locally with node app.js and see the result on http://localhost:3001/skills.

2) Dockerise app

Create a Dockerfile and expose port 3001 on the container.

Next, we are going to build an image. Beware the image name has to start with okteto.dev/ and followed the project name because Okteto will upload the image to its own Docker image repository. Cool! We don’t even need to upload to a Docker registry ourselves.

docker build -t okteto.dev/tutorial-node-api-k8s .

Run the container and make it accessible on port 8001 of the local machine.

docker run -d -p 8001:3001 okteto.dev/tutorial-node-api-k8s

The API lives on http://localhost:8001/skills. 🔥

Section 1 source code 👈

Section 2: Kubernetes configuration

It involves two configurations to bring up a container to a cluster. A k8s/deployment.yaml governs how a pod is created. In our case, we tell Kubernetes the location of the container image at okteto.dev/tutorial-node-api-k8sand the replicas count.

A k8s/service.yaml defines the way how the pod communicate. We need to pay attention to the annotations in line 7 that an Okteto specific property dev.okteto.com/auto-ingress is configured to true, so ingress will automatically expose a service endpoint. 💪

Simple enough?

Like many CI/CD tools, Okteto requires a pipeline to detail the steps to ship the app. We’ll create an okteto-pipeline.yaml in the project root to build the image and execute the kubectl apply command.

Now we’ve got the bare-minimal configuration. Let’s git commit and git push to the remote Github repository.

Section 2 source code 👈

Section 3: Deploy to Kubernetes cluster

Register Okteto with your Github account.

Okteto will create the default namespace using your Github account name.

Navigate to Deploy on the top left menu.

Specify the URL of the project and the branch. Click Deploy button.

Okteto is deploying the pod and the service right away, and it will create a service endpoint in a minute.

Let’s head over to https://tutorial-node-api-k8s-ng-the-engineer.cloud.okteto.net/skills. 👈

Section 4: Scale up the pods

To get a taste of the CI/CD, update replicas to 2. Commit and push the change.

Click redeploy in the Okteto dashboard. It will create a new pod in a minute.

Two pods are up and running now!

Check out the complete source code 👈

Congratulation! 🙌 You have deployed a simple backend on Kubernetes without swiping a credit card in 5 minutes.

Let’s recap what we’ve learnt in the workshop.

  1. Built a Node backend.
  2. Dockerise the Node app.
  3. Define Kubernetes configuration.
  4. Roll out the app on Kubernetes cluster with Okteto pipeline.
  5. Scaling up the cluster.

Thank you for reading ❤️!

Please give me a clap 👏 and share if you like the idea.

Don’t hesitate to talk with us at hi@valubees.co.uk if you have any thought.

Disclaimer: This article is not affiliated with Okteto.

--

--

--

A new tech publication by Start it up (https://medium.com/swlh).

Recommended from Medium

Full Stack Development | Angular Js versus React by Atilla Charles

Angular 13 | Hot Features

Architect, design & build scalable Angular applications

Top 5 Css Methodologies in 2021

Handling proxy in angular

Here are current #JOBS / Vacancies in #Nigeria:

Scratch that Brain — JavaScript

How web browsers use process & Threads

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Anthony Ng

Anthony Ng

Software Engineer

More from Medium

Deploy a React App to Brilliant Cloud with Docker & Nginx

What is OAuth 2.0 — In a simple way

Stepwise Guide to Node.js Authentication with Express and Knex

Custom Checks In Github: A Probot Experience — Part 1

Github and Probot Logos