From Spring Boot to Kubernetes: A Developer’s Journey with Minikube

Ever found yourself staring at your Spring Boot application, sipping your fourth cup of coffee, and wondering, “How on earth do I get this running on Kubernetes?” Trust me, I’ve been there. Deploying applications can sometimes feel like trying to herd cats—chaotic and mildly dangerous. But fear not! Today, I’ll take you on a journey from a simple Spring Boot app to a Kubernetes deployment using Minikube. And we’ll do it together, step by step, with a dash of humor to keep things lively.

Why Kubernetes and Minikube?

Before we dive in, let’s chat about why we’re doing this. Kubernetes is the de facto standard for container orchestration. It handles scaling, load balancing, and ensures your applications run smoothly. But setting up a full-blown Kubernetes cluster can be overkill for local development. That’s where Minikube comes in—a lightweight Kubernetes implementation that runs on your local machine. It’s like having a personal Kubernetes playground!

Check out the full repository on GitHub: You can find all the code from this tutorial in my oanda-algo-trading repo, where I’ve tested and refined each step.

Prerequisites

First things first, let’s make sure we’re all on the same page with the tools we’ll need:

Minikube: Our local Kubernetes cluster.

brew install minikube

Colima: A container runtime for macOS, acting as a Docker Desktop replacement.

brew install colima

Docker: For building and pushing our application images.

brew install docker

kubectl: Kubernetes command-line tool (comes with Minikube).

Setting Up the Environment

1. Start Colima

We’ll use Colima to manage Docker without the overhead of Docker Desktop.

colima start

2. Set Docker Context to Colima

Ensure Docker commands are directed to Colima.

docker context use colima

3. Start Minikube with Docker Driver

Let’s fire up Minikube using Docker.

minikube start --driver=docker

Note: The first time you do this, it might take a while. Feel free to grab another cup of coffee—or maybe switch to tea if the caffeine shakes are setting in.

Building and Pushing Your Spring Boot App

4. Build Your Docker Image

Navigate to your Spring Boot application’s root directory and build the Docker image.

docker build -t oanda-trading-api .

5. Tag and Push to Docker Hub

Let’s tag the image for Docker Hub and push it. Replace <your-dockerhub-username> with your actual Docker Hub username.

docker tag oanda-trading-api:latest <your-dockerhub-username>/oanda-trading-api:latest

docker login

docker push <your-dockerhub-username>/oanda-trading-api:latest

Pro Tip: If Docker starts asking existential questions or throws credential errors, check your ~/.docker/config.json file and make sure the “credsStore” entry isn’t causing mischief.

Handling Secrets: Securely Passing Your API Key

6. Create a Kubernetes Secret

We all have secrets—mine is that I sometimes debug with print statements. For your application, you might have an API key that shouldn’t be exposed. Let’s store it securely.

Assuming your API key is stored in an environment variable OANDA_API_KEY, create a Kubernetes secret like this:

kubectl create secret generic oanda-api-secrets --from-literal=OANDA_API_KEY="$OANDA_API_KEY"

Remember: Secrets are like your browser history—keep them safe and out of sight!

Crafting the Kubernetes Manifests

7. deployment.yaml

Here’s how your deployment file should look:

apiVersion: apps/v1

kind: Deployment

metadata:

  name: oanda-trading-api-deployment

spec:

  replicas: 2

  selector:

    matchLabels:

      app: oanda-trading-api

  template:

    metadata:

      labels:

        app: oanda-trading-api

    spec:

      containers:

        - name: oanda-trading-api

          image: <your-dockerhub-username>/oanda-trading-api:latest

          ports:

            - containerPort: 8080

          env:

            - name: OANDA_API_KEY

              valueFrom:

                secretKeyRef:

                  name: oanda-api-secrets

                  key: OANDA_API_KEY

8. service.yaml

And your service file:

apiVersion: v1

kind: Service

metadata:

  name: oanda-trading-api-service

spec:

  type: NodePort

  selector:

    app: oanda-trading-api

  ports:

    - protocol: TCP

      port: 80

      targetPort: 8080

      nodePort: 30000

Why NodePort? It exposes the service on a port that can be accessed from your local machine. Think of it as your app waving from behind the Kubernetes curtain.

Deploying to Minikube

9. Apply Your Configurations

Time to bring it all to life!

kubectl apply -f deployment.yaml

kubectl apply -f service.yaml

10. Access Your Application

Find out where your application is running:

minikube ip

kubectl get svc

Then, in your browser or API client, navigate to:

http://<minikube-ip>:30000

If everything worked, give yourself a pat on the back. If not, don’t worry—we’ve all been there.

Troubleshooting Tips (a.k.a., “Why is this happening to me?”)

Minikube Hanging on Start: The initial image pull can take time. If it seems stuck, check your internet connection or manually pull the image:

docker pull gcr.io/k8s-minikube/kicbase:v0.0.45

Docker Credential Errors: If you see errors about docker-credential-osxkeychain, edit ~/.docker/config.json and adjust or remove the “credsStore” entry.

Resource Allocation: If Minikube or Docker misbehaves, try allocating more resources:

colima stop

colima start --cpu 4 --memory 4

Environment Variables Not Set: Ensure your OANDA_API_KEY is exported in your shell before creating the secret.

Reflections on the Journey

Deploying a Spring Boot app to Kubernetes using Minikube might seem daunting at first—like trying to assemble IKEA furniture without instructions. But once you break it down into steps, it’s quite manageable. Plus, you get the satisfaction of seeing your application running in a Kubernetes cluster right on your local machine.

Remember, every expert was once a beginner who didn’t give up. So keep experimenting, breaking things, and fixing them—that’s how we grow as developers.

Author’s Note: I hope this guide has been helpful and maybe even brought a smile to your face. You can find all the code and configurations in my oanda-algo-trading repo. Happy coding!

📚 Further Reading & Related Topics

If you’re exploring Spring Boot deployments in Kubernetes using Minikube, these related articles will provide deeper insights into cloud-native development:

• Accessing API in Minikube: A Complete Guide – Learn how to expose and interact with APIs in a Minikube environment, ensuring smooth local development and testing.

• Kubernetes Helm: Simplifying the Deployment of Your Applications – Discover how Helm charts can help streamline and manage Spring Boot application deployments in Kubernetes.

One response to “From Spring Boot to Kubernetes: A Developer’s Journey with Minikube”

  1. Why Developers Should Consider Setting Up Their Windows Machine with WSL for Development Tools – Scalable Human Blog Avatar

    […] • From Spring Boot to Kubernetes: A Developer’s Journey with Minikube – Learn how WSL can enhance containerized development, making it easier to run Kubernetes and Minikube locally. […]

    Like

Leave a comment

I’m Sean

Welcome to the Scalable Human blog. Just a software engineer writing about algo trading, AI, and books. I learn in public, use AI tools extensively, and share what works. Educational purposes only – not financial advice.

Let’s connect