DEV Community

Rodrigo Faria
Rodrigo Faria

Posted on • Updated on

Running a Node.JS API on Kubernetes local with K3D

How to "play" with Kubernetes on your own machine? One way to achieve this could be K3D!

This tutorial will show how to run an application on a local Kubernetes inside a Ubuntu machine running on WSL2!

Prerequisites

First, we need to install:

Also, you need to have an account in Docker Hub.

Creating Node.js API

The first step is to create our API. Let's create a folder and initialize a Node.js project:

Node initialization

The next step is to install express with the command below:

npm i express
Enter fullscreen mode Exit fullscreen mode

Now open the Visual Studio Code, or another code editor, and create a folder called src and inside a file called index.js as in the picture below:

Application structure

Let's write some code to create an API that responds with Hello World in the path http://localhost:8087/api/v1/test:

const express = require('express')
const app = express()

app.get('/api/v1/test', (req, res) => {
    res.send('Hello World')
})

app.listen(8087, () => console.log('Server started on port 8087'))
Enter fullscreen mode Exit fullscreen mode

Now, let's change the package.json file to remove the test instruction created by default and add our instruction to execute the application:

package.json file

In the command line just type npm start and you can open your browser and access the application with the URL http://localhost:8087/api/v1/test.

Application running

Docker image

In order to execute our application in Kubernetes, we should create an image of our application. In this tutorial, we will create a Docker image.

The first step is to create in the root of the project a file called Dockerfile and add this content below:

FROM node:18-alpine

WORKDIR /app

COPY package*json ./

RUN npm i

COPY ./src ./src

CMD ["npm", "start"]
Enter fullscreen mode Exit fullscreen mode

Now we should use the build command to generate the image:

docker build -t rodrigoluisfaria/new-api:1.0.0 .
Enter fullscreen mode Exit fullscreen mode

In the command above you should replace rodrigoluisfaria with your username in Docker Hub.

If the command executes successfully, you can check the image generated using the command docker images:

docker images command

In order to test the image generated, we can run the command below to execute the container:

docker run -it --rm -p 8087:8087 rodrigoluisfaria/new-api:1.0.0
Enter fullscreen mode Exit fullscreen mode

After the container start, you can open the browser and test if you can access the application:

running application on Docker

The last step is to put this image in our Docker Hub repository.
For this you need to log in to the docker hub using the following command:

docker login
Enter fullscreen mode Exit fullscreen mode

On the page Docker Hub create a new repository called new-api and execute the command below:

docker push rodrigoluisfaria/new-api:1.0.0
Enter fullscreen mode Exit fullscreen mode

Remember to replace rodrigoluisfaria with your username in Docker Hub.

Cluster Kubernetes - K3D

Now let's create our cluster using K3D!
For this you just need to execute the command:

k3d cluster create -p "8087:80@loadbalancer" my-cluster
Enter fullscreen mode Exit fullscreen mode

In this command, you start a cluster and expose port 8087 of it.

Creating cluster Kubernetes

In order to test your cluster you could run some commands:

kubectl cluster-info
kubectl get nodes
kubectl get pods --all-namespaces
Enter fullscreen mode Exit fullscreen mode

Cluster info

Kubernetes

Now let's create a folder called k8s and three files inside:

In the deployment.yaml file put the content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-deployment
  labels:
    app: my-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-app
          image: rodrigoluisfaria/new-api:1.0.0
          ports:
            - containerPort: 8087
Enter fullscreen mode Exit fullscreen mode

In the service.yaml file put the content:

apiVersion: v1
kind: Service
metadata:
  name: my-app-service
spec:
  selector:
    app: my-app
  ports:
  - port: 8087
    targetPort: 8087
Enter fullscreen mode Exit fullscreen mode

In the ingress.yaml file put the content:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-app-ingress
spec:
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: my-app-service
            port:
              number: 8087
Enter fullscreen mode Exit fullscreen mode

Now let's run a kubectl commands to create those resources:

kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl apply -f ingress.yaml
Enter fullscreen mode Exit fullscreen mode

To verify the resources you could execute the commands below:

kubectl get deployments
kubectl get services
kubectl get ingress
Enter fullscreen mode Exit fullscreen mode

The result is something like this:

kubectl commands

The last step is to test! Open the browser and check if your application is running:

Application

I hope this tutorial was helpful, and if you have any comments or questions you could comment here or in my e-mail rodrigolfsi@gmail.com.

Top comments (0)