Monday, 12 May 2025

Docker vs Kubernetes, and Step-by-Step: Deploy Spring Boot on Kubernetes

1. Docker vs Kubernetes

🛠️ What Is Docker?

  • Docker is a tool to build, ship, and run applications inside containers.

  • Each container is an isolated environment (like a lightweight virtual machine).

  • Packages everything (code, libraries, OS dependencies) into one image.

bash
docker build -t myapp . docker run -p 8080:8080 myapp

⚙️ What Is Kubernetes?

  • An open-source container orchestration platform.

  • Manages deployment, scaling, load balancing, and failover of containers.

  • Works with Docker (or other container runtimes).

yaml

apiVersion: apps/v1 kind: Deployment metadata: name: myapp-deployment spec: replicas: 3 selector: matchLabels: app: myapp template: metadata: labels: app: myapp spec: containers: - name: myapp image: myapp:latest ports: - containerPort: 8080
bash
kubectl apply -f deployment.yaml

Feature Docker Kubernetes
Definition A platform for developing, shipping, and running containers. A system for orchestrating and managing multiple containers.
Purpose Package applications with dependencies into lightweight containers. Manage containerized applications across a cluster (scaling, failover, etc.)
Role Creates and runs containers. Deploys, scales, and manages containers across multiple nodes.
Scope Single container or application. Cluster of containers across many machines.
Core Tool docker, Dockerfile, docker-compose. kubectl, kubelet, YAML configs, Deployments, Services, etc.
Scaling Manual (via docker-compose scale, scripts). Built-in auto-scaling and load balancing.
Networking Container-to-container on one host. Supports cluster-wide networking, service discovery, and DNS.
Fault Tolerance Manual restart or recovery. Automatically restarts failed containers, reschedules them.
Storage Volumes and bind mounts. Persistent Volumes (PV) and Persistent Volume Claims (PVC).
Deployment docker run, docker-compose up. Uses YAML (Deployment, Service, Pod, etc.).
Best Use Case Developing, testing, and running a single container or small system. Running and managing large-scale, production-grade containerized systems.

🤝 Docker + Kubernetes

  • Docker creates containers.

  • Kubernetes runs and manages them in a cluster.

You typically use:

  • Docker to build your app container.

  • Kubernetes to deploy, scale, and manage the app in production.

🧠 Analogy

ConceptAnalogy
DockerBuilding and packaging a car
KubernetesManaging a fleet of cars
Docker ComposeManaging a few cars (manually)
Kubernetes ClusterAutomated traffic management

✅ Summary

DockerKubernetes
UseBuild and run containersDeploy and manage containers
Ideal ForLocal developmentProduction-grade deployments
Works Alone?YesNeeds containers (like Docker)
Replacement?No – Kubernetes uses DockerNot a replacement – works with Docker

Figure 1. Kubernetes cluster components.

A Kubernetes cluster consists of a control plane plus a set of worker machines, called nodes, that run containerized applications. Every cluster needs at least one worker node in order to run Pods.

The worker node(s) host the Pods that are the components of the application workload. The control plane manages the worker nodes and the Pods in the cluster. In production environments, the control plane usually runs across multiple computers and a cluster usually runs multiple nodes, providing fault-tolerance and high availability.

This document outlines the various components you need to have for a complete and working Kubernetes cluster.



2. Step-by-Step: Deploy Spring Boot on Kubernetes 

1. Build the Spring Boot App

a)Make sure your app is working locally, then build or run it using Maven or Gradle:

./mvnw clean package

b)If you want to skip tests while building

./mvnw clean package -DskipTests

This creates a target/<my-spring-boot-app-0.0.1-SNAPSHOT>.jar file.

(Replace my-spring-boot-app with your actual artifact ID)


c)Then run the JAR Locally

java -jar target/my-spring-boot-app-0.0.1-SNAPSHOT.jar

d) 📁 Example Directory After Build:
my-spring-boot-app/
├── src/
├── target/
│   ├── classes/
│   ├── my-spring-boot-app-0.0.1-SNAPSHOT.jar
│   └── ...
├── pom.xml
├── mvnw
├── mvnw.cmd
└── ...



2. Create a Dockerfile
In your project root, create a file named Dockerfile:

FROM openjdk:17
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]

3. Build and Push the Docker Image
docker build -t your-dockerhub-username/springboot-app:latest

Push it to Docker Hub (or any container registry):

docker push your-dockerhub-username/springboot-app:latest

Note: Replace your-dockerhub-username accordingly.

4. Create Kubernetes Deployment YAML
Create a file named deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: springboot-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: springboot-app
  template:
    metadata:
      labels:
        app: springboot-app
    spec:
      containers:
      - name: springboot-container
        image: your-dockerhub-username/springboot-app:latest
        ports:
        - containerPort: 8080

5. Create Kubernetes Service YAML
Create a file named service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: springboot-service
spec:
  selector:
    app: springboot-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: LoadBalancer  # Use NodePort if not using a cloud provider

6. Deploy to Kubernetes
Apply the configuration:

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

7. Verify
kubectl get pods
kubectl get deployments
kubectl get services

Look for the EXTERNAL-IP of your service (may take a minute to appear if using LoadBalancer on cloud).

8. Access the App
Open the EXTERNAL-IP in your browser. If using NodePort, access it with:

http://<Node-IP>:<NodePort>

Tips:
Use ConfigMap or Secrets for environment variables
For production, consider using Helm, Ingress, TLS, and Horizontal Pod Autoscaler.
For local Kubernetes: use Minikube, Docker Desktop, or Kind.
--------------------------------------


No comments:

Post a Comment