Nodejs app + Kubernetes for beginners

Hello everyone. We are going to start a Kubernetes series. In this series, we will learn the basics of Kubernetes and try to deploy our sample microservice code with Kubernetes.

Disclaimer :) If you don’t have prior knowledge of Docker, please refer to this blog. It will help you to understand Kubernetes more precisely.

What we are going to achieve in this article

  1. What is Kubernetes? Why and when should we use it?
  2. The flow of the entire service.
  3. Create a basic microservice.
  4. Add Kubernetes necessary file and explain about these files.
  5. Run this Kubernetes and access this on localhost.

Step 1: What is Kubernetes? Why and when should we use it?

I assume that if you came here, you probably have some idea about Kubernetes. But even if you don’t know about Kubernetes, please follow the resources below, which will give you a clear idea about Kubernetes.

Step 2: Entire Flow.

Let's understand this flow. So, we can make our Kubernetes manifest file accordingly.

  • MongoDB: MongoDB can communicate with user service only. MongoDB is not authorised to talk with the gateway service.
  • User: User service can communicate with Gateway Service and MongoDB service but can not share from the internet.
  • Gateway: Gateway service can communicate to the internet and then the gateway will talk to the user service.

Step 3: Create a Basic microservice

  1. User
  2. Gateway
  3. MongoDB

Step 4: Add Kubernetes necessary file and explain about these files

  • In the Basic-Kubernetes branch, there are three folders.

  • Each folder has 2 .yaml files other than code
    1. deployment.yaml: This file is responsible for running a build docker images
    2. service.yaml: This file is responsible for exposing the code outside the world or even in the same network.

  • Explanation of MongoDB folder
    1. Mongodb is an independent service. As shown in the above diagram user service is dependent on MongoDB.
    2. So, our aim will be to run MongoDB successfully on Kubernetes.
    3. Before that let's look at the deployment file and service file of MongoDB.

deployment.yaml

                apiVersion: apps/v1 # Take version of deployement
kind: Deployment # Kind of Kubernetes file

# Define metadata of deployment file
metadata:
  name: mongodb-deployment
  labels:
    app: mongodb

# Specifications of deployment file
spec:
  replicas: 1 # How many replicas are we trying to create.
  selector:
    matchLabels:
      app: mongodb
  template:
    metadata:
      labels:
        app: mongodb
    
    # Specification of image. Docker image information
    spec:
      containers:
      - name: mongo
        image: mongodb #Image name which is taking from docker hub
            

service.yaml
1. In this file we need to look at ClusterIP
2. ClusterIP is used to expose any pod inside the same cluster.
3. We used here type as ClusterIP because MongoDB service no need to expose outside the Cluster.

                apiVersion: v1
# Kind of Kubernetes file
kind: Service

# Metadata of service file
metadata:
  name: mongodb-service
spec:
  type: ClusterIP
  ports:
    - protocol:
      port: 27017
      targetPort: 27017
  selector:
    app: mongodb
            

4. Now let's run the MongoDB service by running the following command

                cd mongodb
kubectl apply -f service.yaml -f deployment.yaml
            

5. Now our container is created and the service also.

Explanation of User folder

  1. User folder is where all user-related logic resides. So, this user service can communicate with the MongoDB service.
    2. This service is also going to use internally. So, that is why this service type is ClusterIP.

deployment.yaml

                apiVersion: apps/v1
kind: Deployment # Kind of Kubernetes file

# Define metadata of deployment file
metadata:
  name: user-deployment
  labels:
    app: user

# Specifications of deployment file
spec:
  replicas: 1 # How many replicas are we trying to create.
  selector:
    matchLabels:
      app: user
  template:
    metadata:
      labels:
        app: user
    
    # Specification of image. Docker image information
    spec:
      containers:
      - name: user
        image: aiirakesh/user:latest
        ports:
        - containerPort: 8000
        env:
          - name: MONGODB_HOST
            value: "mongodb-service.default"
            

service.yaml

                apiVersion: v1
# Kind of Kubernetes file
kind: Service

# Metadata of service file
metadata:
  name: user-service
spec:
  type: ClusterIP
  ports:
    - protocol:
      port: 8000
      targetPort: 8000
  selector:
    app: user
            

Let's run user service

                cd user
kubectl apply -f deployment.yaml -f service.yaml
            

Now if we see we have 2 services and deployments are running

Explanation of Gateway folder

  1. A gateway where our routes are mentioned. Which is going to expose us to the external world.
  2. Since it is going to communicate to internet. That is why this service type is kept as LoadBalancer.

deployment.yaml

                apiVersion: apps/v1
kind: Deployment # Kind of Kubernetes file

# Define metadata of deployment file
metadata:
  name: gateway-deployment
  labels:
    app: gateway

# Specifications of deployment file
spec:
  replicas: 1 # How many replicas are we trying to create.
  selector:
    matchLabels:
      app: gateway
  template:
    metadata:
      labels:
        app: gateway
    
    # Specification of image. Docker image information
    spec:
      containers:
      - name: gateway
        image: aiirakesh/gateway:latest
        ports:
        - containerPort: 8000
        env:
          - name: MONGODB_HOST
            value: "gateway-service.default:8001"  # This is provided like this formate <nameOfService>.<namespace>:<port>
            

service.yaml

                apiVersion: v1
# Kind of Kubernetes file
kind: Service

# Metadata of service file
metadata:
  name: gateway-service
spec:
  type: LoadBalancer
  ports:
    - protocol:
      port: 8001
      targetPort: 3000
  selector:
    app: gateway
            

Let’s run user service

                cd gateway/
kubectl apply -f service.yaml -f deployment.yaml
            

Now if we see we have 3services and 3 deployments are running

NOTE:

In the above picture, if we see in services part. Gateway service can be accessed from localhost:8001.

Next, article we will deploy the same application on AWS EKS. Which is managed service by AWS

Feedback: Thank you for reading this article. I hope you understood the basics of deploying your application from scratch on Kubernetes. Please feel free to ask questions or give suggestions to improve the content quality.


Pawfives by
Pawfives by:

Only registered users can post comments. Please, login or signup.

Start blogging about your favorite technologies and get more readers

Join other developers and claim your FAUN account now!

Avatar

RAKESH KUMAR SHARMA

@airrakesh
Simplifying human life | 👨🏽‍💻
Stats
15

Influence

408

Total Hits

2

Posts