Production-Grade Kubernetes Cluster Deployment for a Spring Boot and MongoDB Application Using Kops

Production-Grade Kubernetes Cluster Deployment for a Spring Boot and MongoDB Application Using Kops

ยท

3 min read

Introduction

Overview

Deploying applications on Kubernetes in a production-grade environment requires careful planning, automation, and best practices. In this project, I will set up a highly available Kubernetes cluster using Kops and deploy a Spring Boot application with MongoDB.

Why Use Kops?

Kops (Kubernetes Operations) is an open-source tool that simplifies Kubernetes cluster deployment and management on AWS. It provides a declarative way to manage Kubernetes clusters, making it a nice choice for production environments and also saves cost.

Tech Stack

  • Kubernetes (K8s) - Container orchestration

  • Kops - Kubernetes cluster management

  • AWS - Cloud infrastructure

  • Spring Boot - Java-based backend service

  • MongoDB - NoSQL database

1. Setting Up the Kubernetes Cluster with Kops

Prerequisites

Before we begin, ensure you have the following:

Step 1: Create an IAM role from AWS Console

For this project we will use Admin-Access for the permission. Then Attach this IAM role to the Ubuntu instance.

from Console, Select Ubuntu Instance --> Actions --> Instance Settings --> Attach IAM Role --> Select the role which You Created. --> Save.

Step 2a: Create an S3 Bucket for Kops State Storage

aws s3 mb s3://ejay-s3-bucket --region us-east-2

Step 2b: Expose environment Variable

vi .bashrc

export NAME=kops.k8s.local
export KOPS_STATE_STORE=s3://ejay-s3-bucket

source .bashrc

Step 3: Create sshkeys

ssh-keygen -t rsa -b 4096

Step 4: Create the Kubernetes Cluster

kops create cluster \
--zones us-east-2a \
--networking weave \
--master-size t2.medium \
--node-size t2.medium \
--node-count=2 \
${NAME}

Step 5: Create an SSH public key secret

  create secret --name ${NAME} sshpublickey admin -i ~/.ssh/id_rsa.pub

Step 6: Initialize the Cluster

kops update cluster ${NAME} --yes

Step 7: Export the kubeconfig file to manage your kubernetes cluster from a remote server

 kops export kubecfg $NAME --admin

Step 8: Validate the Cluster

kops validate cluster

Miscellaneous steps

To ssh into any of the clusters

ssh -i ~/.ssh/<keyName> ubuntu@ipAddress

To Delete Cluster

kops delete cluster --name=${NAME} --state=${KOPS_STATE_STORE} --yes

2. Deploying Spring Boot and MongoDB on Kubernetes

Step 1: Create Kubernetes namespace environment

Apply the deployment

kubectl apply -f namespace.yml

Set Default Namespace in the Current Context

kubectl config set-context --current --namespace=prod

Step 2: Create Kubernetes Deployment for SpringBoot

Apply the SpringBoot deployment

kubectl apply -f springboot_deployment.yml

Step 3: Expose SpringBoot with a Service

Create a springboot_service.yml file

Apply the SpringBoot service

kubectl apply -f springboot_service.yml

Step 4: Deploy a Horizontal Pod AutoScaler (HPA)

hpa_deployment.yml file

Apply the hpa deployment

kubectl apply -f hpa_deployment.yml

Step 5: Deploy MongoDB on Kubernetes

mongodb_deployment.yml file

Apply the mongodb deployment

kubectl apply -f mongodb_deployment.yml

Step 6: Expose MongoDB with a Service

Create a mongodb_service.yml file

Apply the mongodb service

kubectl apply -f mongodb_service.yml

Step 7: Verify Deployment

Check the status of all deployments and verify all deployments are properly configured and running

kubectl get all -o wide

Step 8exi: Access the Application from a Web Browser.

After all deployments are properly configured and running as seen on the image above, using the Public-IP of any of the clusters and the NodePort (30000), access the application.

We have successfully accessed our application externally.

Conclusion

Deploying a production-grade Kubernetes cluster using Kops is a crucial step toward ensuring application scalability, reliability, and high availability. By integrating Spring Boot and MongoDB, we have created a robust backend system that can handle real-world workloads efficiently.

ย