#1

depending on your environment. Fortunately, Docker simplifies the process in both development and production.
In this guide, we’ll walk through how to set up Redis using Docker and configure it using Spring Boot’s application.yml. You’ll see how to separate dev and prod configurations using Spring profiles and manage environment variables in Kubernetes.

Step 1: Why Use Docker for Redis?

Before diving in, let’s answer the obvious: Why Docker?
  • Quick setup: Redis can be up and running in seconds.
  • No clutter: No system-level installation required.
  • Portability: The same configuration works across machines.
  • Consistency: Everyone on the team runs the same environment.

Step 2: Run Redis in Docker for Development

Start by spinning up a Redis container on your local machine:
docker run -d --name redis-dev -p 6379:6379 redis
This pulls the latest Redis image and exposes it on the default port, ready for your Spring Boot app to use.

Step 3: Add Redis Dependency in Spring Boot

You’ll need the Redis starter in your Maven project:
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
This adds support for Redis operations like RedisTemplate, caching, and more.

Step 4: Configure application-dev.yml

Now, create your application-dev.yml to use the local Redis instance:
spring:
  redis:
    host: localhost
    port: 6379
    database: 0
This configuration assumes you’re running Redis locally on the default port without a password.
To activate the dev profile, use:
SPRING_PROFILES_ACTIVE=dev
Spring Boot will load the application-dev.yml configuration when the app starts.

Step 5: Set Up Redis for Kubernetes in Production

For production, Redis typically runs as a pod or external service within your Kubernetes cluster. You should avoid hardcoding any values—instead, use ConfigMaps and environment variables for flexibility and security.

Step 5.1: Create a Kubernetes ConfigMap

Let’s create a ConfigMap that holds your Redis host and port:
apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-config
  namespace: your-namespace
data:
  redis_host: redis-service
  redis_port: "6379"
This allows you to change Redis settings without modifying your application code or rebuilding the image.

Step 5.2: Inject Environment Variables into Your Deployment

Next, make those values available to your Spring Boot app through environment variables:
containers:
  - name: your-app
    image: your-image
    env:
      - name: REDIS_HOST
        valueFrom:
          configMapKeyRef:
            name: redis-config
            key: redis_host
      - name: REDIS_PORT
        valueFrom:
          configMapKeyRef:
            name: redis-config
            key: redis_port
      - name: SPRING_PROFILES_ACTIVE
        value: prod
This ensures your app uses the right Redis configuration in production.

Step 6: Configure application-prod.yml

With the environment variables set, use placeholders in your production configuration:
spring:
  redis:
    host: ${REDIS_HOST}
    port: ${REDIS_PORT}
    database: 0
When your app runs in Kubernetes, Spring Boot automatically replaces these placeholders with the environment variable values injected by your container runtime.
If your production Redis requires authentication, simply extend the config:
spring:
  redis:
    host: ${REDIS_HOST}
    port: ${REDIS_PORT}
    password: ${REDIS_PASSWORD}
    ssl: false
And make sure REDIS_PASSWORD comes from a Kubernetes Secret.

Step 7: Manage Profiles Cleanly

Using Spring profiles keeps your development and production environments cleanly separated.
  • application-dev.yml: for local development with Docker
  • application-prod.yml: for production deployments in Kubernetes
You can toggle between them by setting the SPRING_PROFILES_ACTIVE variable at runtime. This avoids hardcoding anything and allows smooth transitions between environments.

image quote pre code