Sitemap

Dockerizing and Orchestrating a Microservices Architecture with Kubernetes: Part 2

5 min readFeb 20, 2024
Press enter or click to view image in full size
Dockerizing and orchestration of node-based microservice

Let’s continue with dockerization and orchestration using Kubernetes of our microservices.

4. Dockerizing the Microservices:

What you do:

  • Create Dockerfiles for each microservice to define their respective Docker images.
  • Build and push the Docker images to a container registry (e.g., Docker Hub, Google Container Registry, or Amazon ECR).

Dockerizing the Frontend Service

  1. Create a Dockerfile:
  • In the ‘microservice-app’ directory, create a new file named ‘Dockerfile.frontend’ and add the following content:
# Use the official Node.js 14 image as the base image
FROM node:14
# Set the working directory inside the container
WORKDIR /app
# Copy package.json and package-lock.json to the working directory
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy the rest of the application code to the working directory
COPY . .
# Expose port 3000
EXPOSE 3000
# Command to run the application
CMD ["node", "frontend.js"]

2. Build the Docker Image:

  • Run the following command to build the Docker image for the frontend service:
docker build -t frontend-service -f Dockerfile.frontend .

3. Push the Docker Image to a Container Registry:

  • If you haven’t already, create an account on a container registry (e.g., Docker Hub, Google Container Registry, or Amazon ECR).
  • Log in to the container registry using the command provided by the registry.
  • Tag the Docker image with the registry URL and repository name:
docker tag frontend-service <registry-url>/<repository-name>:frontend-service
  • Push the Docker image to the container registry:
docker push <registry-url>/<repository-name>:frontend-service

Dockerizing the Backend Service

  1. Create a Dockerfile:
  • In the ‘backend’ directory, create a new file named ‘Dockerfile.backend’ and add the following content:
# Use the official Node.js 14 image as the base image
FROM node:14
# Set the working directory inside the container
WORKDIR /app
# Copy package.json and package-lock.json to the working directory
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy the rest of the application code to the working directory
COPY . .
# Expose port 4000
EXPOSE 4000
# Command to run the application
CMD ["node", "backend.js"]

2. Build the Docker Image:

  • Run the following command to build the Docker image for the backend service:
docker build -t backend-service -f Dockerfile.backend .

3. Push the Docker Image to a Container Registry:

  • If you haven’t already, create an account on a container registry (e.g., Docker Hub, Google Container Registry, or Amazon ECR).
  • Log in to the container registry using the command provided by the registry.
  • Tag the Docker image with the registry URL and repository name:
docker tag backend-service <registry-url>/<repository-name>:backend-service
  • Push the Docker image to the container registry:
docker push <registry-url>/<repository-name>:backend-service

5. Deploying Microservices to Kubernetes:

What you do:

  • Write Kubernetes deployment YAML files for each microservice to define how they should be deployed.
  • Create Kubernetes services to expose the microservices to each other and to external clients.
  • Deploy the microservices to your Kubernetes cluster.

Deploying the Frontend Service to Kubernetes

  1. Create a Kubernetes Deployment YAML File:
  • In the microservices-app directory, create a new file named frontend-deployment.yaml and add the following content:
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend-deployment
spec:
replicas: 1
selector:
matchLabels:
app: frontend
template:
metadata:
labels:
app: frontend
spec:
containers:
- name: frontend
image: <registry-url>/<repository-name>:frontend-service
ports:
- containerPort: 3000

2. Create a Kubernetes Service:

    Get Nitin Bhagat’s stories in your inbox

    Join Medium for free to get updates from this writer.

  • Create a new file named frontend-service.yaml and add the following content:
Create a Kubernetes Service:
Create a new file named frontend-service.yaml and add the following content:

3. Deploy the Frontend Service:

  • Run the following command to deploy the frontend service to your Kubernetes cluster:
kubectl apply -f frontend-deployment.yaml
kubectl apply -f frontend-service.yaml

Deploying the Backend Service to Kubernetes

  1. In the backend directory, create a new file named backend-deployment.yaml and add the following content:
apiVersion: apps/v1
kind: Deployment
metadata:
name: backend-deployment
spec:
replicas: 1
selector:
matchLabels:
app: backend
template:
metadata:
labels:
app: backend
spec:
containers:
- name: backend
image: <registry-url>/<repository-name>:backend-service
ports:
- containerPort: 4000

2. Create a Kubernetes Service:

  • Create a new file named backend-service.yaml and add the following content:
apiVersion: v1
kind: Service
metadata:
name: backend-service
spec:
selector:
app: backend
ports:
- protocol: TCP
port: 80
targetPort: 4000
type: LoadBalancer

3. Deploy the Backend Service:

  • Run the following command to deploy the backend service to your Kubernetes cluster:
kubectl apply -f backend-deployment.yaml
kubectl apply -f backend-service.yaml

6. Scaling and Load Balancing:

What you do:

  • Experiment with scaling your microservices horizontally by increasing the number of replicas.
  • Implement a load balancer (e.g., Kubernetes Service or Ingress) to distribute traffic among multiple instances of a microservice.

Scaling the Frontend Service

  1. Scale the Frontend Deployment:
  • Run the following command to scale the frontend deployment to 3 replicas:
kubectl scale deployment frontend-deployment --replicas=3

2. Verify the Scaling:

  • Run the following command to check the status of the frontend deployment:
kubectl get deployment frontend-deployment
  • You should see that the number of replicas has been increased to 3.

Load Balancing the Frontend Service

  1. Create a Kubernetes Service for Load Balancing:
  • Create a new file named frontend-loadbalancer.yaml and add the following content:
apiVersion: v1
kind: Service
metadata:
name: frontend-loadbalancer
spec:
selector:
app: frontend
ports:
- protocol: TCP
port: 80
targetPort: 3000
type: LoadBalancer

2. Deploy the Load Balancer Service:

  • Run the following command to deploy the load balancer service for the frontend:
kubectl apply -f frontend-loadbalancer.yaml

3. Verify the Load Balancer Service:

  • Run the following command to get the external IP address of the load balancer service:
kubectl get svc frontend-loadbalancer
  • You should see an external IP address assigned to the service.

Scaling the Backend Service

  1. Scale the Backend Deployment:
  • Run the following command to scale the backend deployment to 3 replicas:
kubectl scale deployment backend-deployment --replicas=3

2. Verify the Scaling:

  • Run the following command to check the status of the backend deployment:
kubectl get deployment backend-deployment
  • You should see that the number of replicas has been increased to 3.

Load Balancing the Backend Service

  1. Create a Kubernetes Service for Load Balancing:
  • Create a new file named backend-loadbalancer.yaml and add the following content:
apiVersion: v1
kind: Service
metadata:
name: backend-loadbalancer
spec:
selector:
app: backend
ports:
- protocol: TCP
port: 80
targetPort: 4000
type: LoadBalancer

2. Deploy the Load Balancer Service:

  • Run the following command to deploy the load balancer service for the backend:
kubectl apply -f backend-loadbalancer.yaml

3. Verify the Load Balancer Service:

  • Run the following command to get the external IP address of the load balancer service:
kubectl get svc backend-loadbalancer
  • You should see an external IP address assigned to the service.

--

--

Nitin Bhagat
Nitin Bhagat

Written by Nitin Bhagat

Software and Robotics enthusiast!!