Deploy Spring Boot With Redis on Kubernetes: Step-by-Step Guide

Running your Spring Boot application with Redis on Kubernetes gives you a deployment that scales and survives pod restarts. Kubernetes handles the orchestration; Redis handles the fast reads. This guide walks through both halves and connects them properly—including the three configuration mistakes that break most first attempts: a wrong Service name, mismatched namespaces, and a password that the Redis pod never actually has.

Why Deploy Spring Boot and Redis on Kubernetes?

  • Scalability: scale your app and Redis independently.
  • Resilience: Kubernetes restarts failed pods and performs rolling updates.
  • Separation of concerns: Redis and your app run as distinct, manageable services.
  • Cloud-native: works on any cloud or on-premises cluster.

Prerequisites

  • A Kubernetes cluster (minikube, kind, EKS, GKE, AKS) with kubectl pointed at it.
  • docker to build the image. helm is optional.
  • A Spring Boot 2.7+ or 3.x project with spring-boot-starter-data-redis.
  • spring-boot-starter-actuator if you want the health probes used below.

The examples use a single namespace called demo for both Redis and the app. Keeping them together is the simplest way to make Service name resolution work.

kubectl create namespace demo
kubectl config set-context --current --namespace=demo

Step 1: Deploy Redis

Option 1: Helm (recommended for production)

Helm is the package manager for Kubernetes. The Bitnami chart gives you a primary/replica setup, a Service, and a credentials Secret in one command:

helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
helm install redis bitnami/redis \
  --namespace demo \
  --set auth.password=yourpassword \
  --set replica.replicaCount=2

Two names matter here. The chart creates a primary Service called redis-master, not redis, and a Secret called redis with the key redis-password. Note both—you'll need them when you configure the app.

For local development, a lighter setup is enough:

helm install redis bitnami/redis \
  --namespace demo \
  --set architecture=standalone,master.persistence.enabled=false

Option 2: Plain Kubernetes manifest

If you'd rather not use Helm, create the password Secret first:

kubectl create secret generic redis-credentials \
  --from-literal=redis-password=yourpassword

Then save this as redis.yaml. The image tag is pinned on purpose—:latest means a silent image update can restart your pods onto a version you never tested.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  labels:
    app: redis
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
        - name: redis
          image: redis:7.2-alpine
          imagePullPolicy: IfNotPresent
          command: ["sh", "-c", "exec redis-server --requirepass \"$REDIS_PASSWORD\""]
          env:
            - name: REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: redis-credentials
                  key: redis-password
          ports:
            - containerPort: 6379
              name: redis
          resources:
            requests:
              cpu: 100m
              memory: 128Mi
            limits:
              cpu: 500m
              memory: 512Mi
          readinessProbe:
            exec:
              command: ["sh", "-c", "redis-cli -a \"$REDIS_PASSWORD\" ping | grep -q PONG"]
            initialDelaySeconds: 5
            periodSeconds: 10
          livenessProbe:
            tcpSocket:
              port: 6379
            initialDelaySeconds: 15
            periodSeconds: 20
---
apiVersion: v1
kind: Service
metadata:
  name: redis-service
  labels:
    app: redis
spec:
  selector:
    app: redis
  ports:
    - name: redis
      protocol: TCP
      port: 6379
      targetPort: 6379
  type: ClusterIP
kubectl apply -f redis.yaml
kubectl rollout status deployment/redis

Step 2: Build and Push the Spring Boot Image

Use a version tag, not :latest, so rollbacks and rollouts stay predictable. A minimal Dockerfile looks like this:

FROM eclipse-temurin:17-jre
WORKDIR /app
COPY target/*.jar app.jar
ENTRYPOINT ["java", "-XX:MaxRAMPercentage=75", "-jar", "app.jar"]
docker build -t your-dockerhub-username/springboot-app:1.0.0 .
docker push your-dockerhub-username/springboot-app:1.0.0

If the registry is private, create an image pull secret and reference it under imagePullSecrets in the Deployment.

Step 3: Deploy the Spring Boot App

Save this as springboot-deployment.yaml. The two values you may need to change are marked in the notes after the code.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: springboot-app
  labels:
    app: 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:1.0.0
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 8080
              name: http
          env:
            - name: SPRING_DATA_REDIS_HOST
              value: "redis-service"
            - name: SPRING_DATA_REDIS_PORT
              value: "6379"
            - name: SPRING_DATA_REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: redis-credentials
                  key: redis-password
                  optional: true
          resources:
            requests:
              cpu: 250m
              memory: 512Mi
            limits:
              cpu: "1"
              memory: 1Gi
          readinessProbe:
            httpGet:
              path: /actuator/health/readiness
              port: 8080
            initialDelaySeconds: 20
            periodSeconds: 10
          livenessProbe:
            httpGet:
              path: /actuator/health/liveness
              port: 8080
            initialDelaySeconds: 40
            periodSeconds: 20
---
apiVersion: v1
kind: Service
metadata:
  name: springboot-service
  labels:
    app: springboot-app
spec:
  selector:
    app: springboot-app
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 8080
  type: LoadBalancer

If you used Helm, change two things: set SPRING_DATA_REDIS_HOST to redis-master, and point the Secret reference at name: redis with key: redis-password. The chart's names differ from the manifest's, and this is where most people get stuck.

The optional: true on the secret means the pod still starts if the Secret is missing—handy for a passwordless dev cluster, where you can drop the password variable entirely.

The probes need actuator plus one property:

management.endpoint.health.probes.enabled=true
kubectl apply -f springboot-deployment.yaml
kubectl rollout status deployment/springboot-app

Step 4: Configure Spring Boot

Set the connection properties in application.properties. The prefix changed in Spring Boot 2.4 and the old one is gone in 3.x:

# Spring Boot 3.x
spring.data.redis.host=${SPRING_DATA_REDIS_HOST:localhost}
spring.data.redis.port=${SPRING_DATA_REDIS_PORT:6379}
spring.data.redis.password=${SPRING_DATA_REDIS_PASSWORD:}

# Spring Boot 2.x — use this prefix instead
# spring.redis.host=${SPRING_DATA_REDIS_HOST:localhost}
# spring.redis.port=${SPRING_DATA_REDIS_PORT:6379}
# spring.redis.password=${SPRING_DATA_REDIS_PASSWORD:}

Never use localhost inside a pod. It points at the app's own container, not Redis. Use the Service name, which Kubernetes DNS resolves to the pod IP. The localhost default above only exists so the app still runs on your laptop outside the cluster.

Step 5: Verify the Setup

  • Check pods: kubectl get pods -o wide
  • Check services: kubectl get svc
  • Stream app logs: kubectl logs -l app=springboot-app -f
  • Confirm DNS resolution from a throwaway pod: kubectl run tmp --rm -it --image=busybox:1.36 -- nslookup redis-service
  • Check Redis directly: kubectl exec deploy/redis -- redis-cli -a "$REDIS_PASSWORD" ping
  • Get the endpoint: kubectl get svc springboot-service, then call your API

The busybox pod is deliberate—Slim JRE images often lack getent or nslookup, so running the check from your app container can fail for the wrong reason.

Best Practices

  • Use a StatefulSet for real persistence. A Deployment writing to an empty volume loses data on every restart. Use a StatefulSet with a PersistentVolumeClaim, or a managed Redis service.
  • Keep Redis and the app in the same namespace, or use the fully qualified name redis-service.demo.svc.cluster.local. Short Service names don't resolve across namespaces.
  • Store passwords in Secrets and inject them as environment variables or mounted files. Never inline them in a manifest you commit.
  • Set resource requests and limits on every container. Without them the scheduler can't pack pods reliably, and memory pressure may evict your app.
  • Add readiness and liveness probes so traffic only reaches healthy pods and stuck containers get restarted.
  • Give the JVM room. Use a container-aware base image, or set -XX:MaxRAMPercentage=75 so the heap leaves space for the rest of the container and avoids OOMKills.
  • Lock Redis down. If you add a NetworkPolicy, allow ingress on 6379 only from the app's pods.

Conclusion

Deploying Spring Boot with Redis on Kubernetes comes down to matching names. The Service name must match the host in your app config, the Secret name and key must match what your Deployment references, and both workloads need to live in the same namespace. Get those right, pin your image tags, and add probes and resource limits—then your first rollout works instead of crash-looping.

Comments