Deploying a Spring Boot Application on DigitalOcean Kubernetes: A Step-by-Step Guide


To deploy a Spring Boot application on Kubernetes (using a service like DigitalOcean Kubernetes), you will follow similar steps as for any other Kubernetes deployment, but adapted for DigitalOcean’s Kubernetes service (DOKS). Below is a comprehensive guide on how to deploy a Spring Boot application on Kubernetes, specifically on DigitalOcean Kubernetes.

Prerequisites:

  1. DigitalOcean Account: Sign up for a DigitalOcean account.
  2. Spring Boot Application: You should have a Spring Boot application ready for deployment.
  3. Docker: Install Docker to containerize your Spring Boot application.
  4. kubectl: Install kubectl to interact with your Kubernetes cluster.
  5. DigitalOcean CLI (doctl): Install the DigitalOcean CLI for managing Kubernetes clusters and other DigitalOcean resources.
  6. Helm (optional): Helm is a Kubernetes package manager that simplifies deployments, though it’s not mandatory.

Step-by-Step Guide to Deploy Spring Boot Application on DigitalOcean Kubernetes


Step 1: Create a Spring Boot Application (if not done already)

If you don’t have a Spring Boot application yet, you can create one using Spring Initializr:

  1. Go to Spring Initializr.
  2. Choose the following:
    • Project: Maven (or Gradle)
    • Language: Java
    • Spring Boot version: Choose the latest stable version
    • Packaging: Jar
    • Dependencies: Spring Web, Spring Boot DevTools, and others as per your needs.
  3. Click on Generate, download the .zip file, and extract it.

Alternatively, if you have a Spring Boot application already, move to the next step.


Step 2: Create a Dockerfile for Your Spring Boot Application

In the root directory of your Spring Boot project, create a Dockerfile to containerize the application.

Example Dockerfile:

# Use the official OpenJDK base image for building and running Java applications
FROM openjdk:17-jdk-slim as builder

# Set the working directory inside the container
WORKDIR /app

# Copy the jar file from the target folder into the container
COPY target/*.jar app.jar

# Expose the port the app will run on
EXPOSE 8080

# Run the application
ENTRYPOINT ["java", "-jar", "/app/app.jar"]

After creating the Dockerfile, build the Docker image:

# Build the Docker image
docker build -t springboot-app .

# Run the Docker container locally to check
docker run -p 8080:8080 springboot-app

If the application runs successfully, it’s ready for deployment.


Step 3: Push the Docker Image to a Container Registry

To deploy the application on Kubernetes, you need to push the Docker image to a container registry. You can use Docker Hub or DigitalOcean Container Registry (DOCR).

Option 1: Using Docker Hub

  1. Log in to Docker Hub:
    docker login
  2. Tag the image and push it to Docker Hub:
    docker tag springboot-app <your-dockerhub-username>/springboot-app:v1
    docker push <your-dockerhub-username>/springboot-app:v1

Option 2: Using DigitalOcean Container Registry (DOCR)

  1. Create a DigitalOcean Container Registry:
    doctl registry create <your-registry-name> --region nyc3
  2. Log in to DOCR:
    doctl registry login
  3. Tag the image and push it to DigitalOcean Container Registry:
    docker tag springboot-app <your-registry-name>.digitalocean.app/springboot-app:v1
    docker push <your-registry-name>.digitalocean.app/springboot-app:v1

Step 4: Create a Kubernetes Cluster on DigitalOcean

  1. Create a Kubernetes Cluster on DigitalOcean:

    doctl kubernetes cluster create <your-cluster-name> --region nyc3 --size s-2vcpu-4gb --node-pool "name=default;count=3"
    
  2. Get the Kubeconfig to interact with your Kubernetes cluster:

    doctl kubernetes cluster kubeconfig save <your-cluster-name>
  3. Verify your connection to the cluster:

    kubectl get nodes

Step 5: Create Kubernetes Deployment and Service YAML Files

5.1. Deployment YAML (deployment.yaml)

Create a deployment.yaml file that defines how to run your Spring Boot application in Kubernetes.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: springboot-app-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: springboot-app
  template:
    metadata:
      labels:
        app: springboot-app
    spec:
      containers:
      - name: springboot-app
        image: <your-dockerhub-username>/springboot-app:v1  # Replace with DOCR path if using DOCR
        ports:
        - containerPort: 8080

5.2. Service YAML (service.yaml)

Create a service.yaml file to expose your application to the outside world using a LoadBalancer.

apiVersion: v1
kind: Service
metadata:
  name: springboot-app-service
spec:
  selector:
    app: springboot-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: LoadBalancer

Step 6: Deploy the Application on Kubernetes

  1. Apply the Deployment and Service to the Kubernetes cluster:

    kubectl apply -f deployment.yaml
    kubectl apply -f service.yaml
  2. Check the Deployment Status:

    kubectl get pods
    kubectl get svc

    The kubectl get svc command will show the external IP of the LoadBalancer. This is the IP you can use to access your Spring Boot application.


Step 7: Access the Application

Once the service is deployed, DigitalOcean will provision an external IP address for your service. You can check the IP address with:

kubectl get svc

Look for the EXTERNAL-IP column, and then you can access your Spring Boot application in the browser:

http://<EXTERNAL-IP>:80

Step 8: Scaling and Updating the Application

You can scale your application up or down by adjusting the number of replicas in the deployment.yaml file:

spec:
  replicas: 5  # Change the replica count as needed

After editing the file, apply the changes:

kubectl apply -f deployment.yaml

To update your application, build a new Docker image, tag it, push it to the registry, and then update the Kubernetes deployment with:

kubectl set image deployment/springboot-app-deployment springboot-app=<new-image>

Conclusion

This guide walks you through the steps to deploy a Spring Boot application on Kubernetes using DigitalOcean Kubernetes (DOKS) and DigitalOcean Container Registry (DOCR). You created Docker images, pushed them to a container registry, and deployed your application using Kubernetes manifests.

Comments

Popular posts from this blog

Spring Boot OpenAI Integration: Step-by-Step Guide

Orchestration-Based Saga Architecture and Spring Boot Microservices Implementation Guide

Spring Boot 3 + Angular 15 + Material - Full Stack CRUD Application Example