MrJazsohanisharma

Deploying a Spring Boot Redis in Kubernetes

Blog ads

Deploying a Spring Boot App with Redis in a Kubernetes Cluster

Running your Spring Boot application with Redis on Kubernetes unlocks scalable, resilient, and cloud-native deployments. Kubernetes orchestrates your containers, while Redis provides high-performance caching or data storage. In this guide, you’ll learn step-by-step how to deploy both a Spring Boot app and Redis in a Kubernetes cluster, and connect them following best practices.

Why Deploy Spring Boot and Redis on Kubernetes?

  • Scalability: Easily scale your app and Redis independently.
  • Resilience: Kubernetes restarts failed pods and manages rolling updates.
  • Separation of Concerns: Redis and your app run as distinct, manageable services.
  • Cloud-Native: Works seamlessly on any cloud or on-premises Kubernetes cluster.

Step 1: Deploy Redis on Kubernetes

Option 1: Use Helm (Recommended for Production)

Helm is a package manager for Kubernetes. To deploy Redis with Helm:

helm repo add bitnami https://charts.bitnami.com/bitnami
helm install redis bitnami/redis

This command deploys Redis with sensible defaults. You can customize replicas, passwords, and persistence using --set flags.
For example, to set a password and replica count:

helm install redis bitnami/redis --set auth.password=yourpassword,replica.replicaCount=2

Option 2: Use a Kubernetes Manifest

Create a redis-deployment.yaml file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
        - name: redis
          image: redis:latest
          ports:
            - containerPort: 6379
---
apiVersion: v1
kind: Service
metadata:
  name: redis-service
spec:
  selector:
    app: redis
  ports:
    - protocol: TCP
      port: 6379
      targetPort: 6379

Apply it with:

kubectl apply -f redis-deployment.yaml

Step 2: Deploy Your Spring Boot Application

First, build and dockerize your Spring Boot app. Push the image to a registry accessible by your cluster (Docker Hub, ECR, GCR, etc.).

docker build -t your-dockerhub-username/springboot-app:latest .
docker push your-dockerhub-username/springboot-app:latest

Create a springboot-deployment.yaml manifest:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: springboot-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: springboot-app
  template:
    metadata:
      labels:
        app: springboot-app
    spec:
      containers:
        - name: springboot-app
          image: your-dockerhub-username/springboot-app:latest
          ports:
            - containerPort: 8080
          env:
            - name: SPRING_REDIS_HOST
              value: "redis-service"
            - name: SPRING_REDIS_PORT
              value: "6379"
---
apiVersion: v1
kind: Service
metadata:
  name: springboot-service
spec:
  selector:
    app: springboot-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: LoadBalancer

Apply it:

kubectl apply -f springboot-deployment.yaml

Step 3: Configure Spring Boot to Use Redis Service

In your application.properties or application.yml:

spring.redis.host=${SPRING_REDIS_HOST:redis-service}
spring.redis.port=${SPRING_REDIS_PORT:6379}
spring.redis.password= # Add if using Redis auth

Important: Do not use localhost as the Redis host in Kubernetes. Use the Redis service name (e.g., redis-service), which Kubernetes DNS resolves to the correct pod IP.
This ensures your Spring Boot app connects to Redis over the cluster network, not its own localhost.

Step 4: Verify the Setup

  • Check pods: kubectl get pods
  • Check services: kubectl get svc
  • Access your Spring Boot app using the external IP or NodePort.
  • Test Redis operations via your app’s API or logs.

Best Practices

  • Use StatefulSet for Redis clusters or master-slave setups for persistence and scaling.
  • Store sensitive data (like Redis passwords) in Kubernetes Secrets.
  • Use resource requests/limits for both app and Redis pods.
  • Monitor health with liveness and readiness probes.
  • For production, consider Redis Helm charts or managed Redis services.

Conclusion

Deploying Spring Boot with Redis on Kubernetes is the foundation for scalable, cloud-native applications. Use Kubernetes Services for reliable communication, externalize configuration, and follow best practices for security and resilience. With this setup, your team is ready to build and scale modern microservices with confidence.

ads

Previous Post Next Post