Tech Notes

Series Kubernetes & Helm from scratch · Part 8

Redis with the Bitnami Helm chart

Install a password-protected Redis for MFA codes into the data namespace with the Bitnami chart, choose the right persistence, and wire the backend to it.

Right now our login flow is broken. At the end of part 6 we deleted the throwaway Redis along with the throwaway PostgreSQL, and in part 7 we only brought PostgreSQL back. Registration works, but the moment a user logs in, the backend tries to store a six-digit MFA code in redis.data.svc.cluster.local and finds nothing there.

In this part we fix that with the same pattern as part 7: the Bitnami chart, a Secret we create ourselves, and a small overrides file in charts/redis. The interesting decision this time isn’t how to install Redis but how much persistence a cache of five-minute codes actually needs. The answer is different from PostgreSQL, and working out why is the real lesson.

What the chart creates

The Bitnami Redis chart defaults to architecture: replication: one master, three replicas and the Services to reach them. We don’t need replicas for a cache of short-lived codes, so we’ll ask for standalone. With a release called redis, that gives us this:

The backend pod in the apps namespace connects through the redis-master Service to the redis-master-0 pod in the data namespace; the pod reads its password from the redis-credentials Secret and keeps its data in an emptyDir volume that lives only as long as the pod. namespace: appsbackend podSET mfa:… EX 300 namespace: data · release redis svc/redis-masterClusterIP · 6379 svc/redis-headlessstable pod DNS pod redis-master-0StatefulSet replica redis-credentialsSecret we create first volume redis-dataemptyDir, no PVC deleted with the pod
Same shape as PostgreSQL in part 7, with one deliberate difference: the data lives in an emptyDir, not on a PersistentVolumeClaim.

The names follow the chart’s convention: the master StatefulSet and its Service get a -master suffix, so the address the backend needs is redis-master.data.svc.cluster.local, not redis.data…. That suffix is the most common reason a first Redis install “doesn’t work”.

Step 1: the Secret

As with PostgreSQL, we create the password ourselves instead of letting the chart generate one. The chart looks for a key called redis-password by default:

REDIS_PASSWORD="$(openssl rand -hex 24)"

kubectl create secret generic redis-credentials -n data \
  --from-literal=redis-password="$REDIS_PASSWORD"

-hex rather than -base64 keeps the password free of /, + and =, which saves quoting trouble in redis-cli and connection URLs later.

The backend runs in apps, so it needs its own copy. Spring Boot’s relaxed binding means the environment variable SPRING_DATA_REDIS_PASSWORD sets spring.data.redis.password without touching the application.yml from part 4. We add it to backend-secrets, carrying the existing keys across exactly as in part 7:

get() { kubectl get secret backend-secrets -n apps -o jsonpath="{.data.$1}" | base64 -d; }

kubectl create secret generic backend-secrets -n apps \
  --from-literal=JWT_SECRET="$(get JWT_SECRET)" \
  --from-literal=DB_PASSWORD="$(get DB_PASSWORD)" \
  --from-literal=SPRING_DATA_REDIS_PASSWORD="$REDIS_PASSWORD" \
  --dry-run=client -o yaml | kubectl apply -f -

The small get function reads one key back out of the current Secret, so the JWT secret and database password don’t change. The backend Deployment already loads the whole Secret with envFrom, so the new key arrives as an environment variable on the next rollout.

Step 2: decide what persistence means for an MFA code

Before writing the values file, let’s be clear about what we’re storing. Each key is mfa:<userId>, holds a six-digit code, expires after 300 seconds and is deleted with GETDEL the moment it’s used. Nothing in Redis is the only copy of anything important: if a code disappears, the user just logs in again and gets a new one.

Redis offers a few levels of durability, and the chart lets us pick any of them:

Three persistence options compared: a PVC with AOF keeps data through container restarts and pod deletion; an emptyDir with AOF keeps data through container restarts but not pod deletion; no persistence keeps data through neither. Optioncontainer restartpod deletedPVC PVC + AOFchart defaultkeptkept8Gi emptyDir + AOFour choice for MFA codeskeptlostnone No persistenceappendonly nolostlostnone
An emptyDir outlives a crashed container but not the pod, which is about the right lifetime for codes that expire in five minutes anyway.
  • PVC + AOF is the chart default. Redis appends every write to a file on a PersistentVolume, so data survives anything short of losing the volume. That’s right for Redis as a primary store or a queue, and overkill for us.
  • emptyDir + AOF writes the same append-only file, but to a volume that belongs to the pod. If the Redis process crashes and the kubelet restarts the container, the file is still there and Redis replays it. If the pod is deleted or rescheduled, it starts empty.
  • No persistence keeps everything in memory only. It’s the fastest, but even a container restart loses every code.

We’ll take the middle option. It costs nothing in storage, leaves no PVC behind to clean up, and only loses codes in the rare case of the pod moving, which in the worst case means a user logs in twice.

Step 3: the overrides file

charts/redis/values-dev.yaml:

architecture: standalone          # one master, no replicas or Sentinel

auth:
  enabled: true
  existingSecret: redis-credentials
  existingSecretPasswordKey: redis-password

master:
  persistence:
    enabled: false                # emptyDir instead of a PVC
  configuration: |-
    maxmemory 96mb
    maxmemory-policy volatile-ttl
  resources:
    requests: { cpu: 50m, memory: 64Mi }
    limits: { memory: 128Mi }

Line by line:

  • architecture: standalone turns off the default replicas. With replication we’d also get a redis-replicas Service and three more pods.
  • auth.existingSecret and existingSecretPasswordKey point the chart at our Secret. When existingSecret is set, the chart ignores auth.password, so a password can’t end up in this file.
  • master.persistence.enabled: false swaps the PVC for an emptyDir. We leave the chart’s AOF setting (appendonly yes) alone, so the file is still written, just to the pod’s own volume.
  • maxmemory 96mb makes Redis manage its own memory below the container’s 128Mi limit. Without it, Redis would grow until the kernel kills the container, which is a much worse failure than a refused write.
  • maxmemory-policy volatile-ttl says what to do at that limit: evict keys that have an expiry, shortest remaining time first. Every key we write has a TTL, so the first to go are codes that were about to expire anyway.
  • resources replaces the chart’s built-in preset with numbers we chose, as we did for PostgreSQL.

Step 4: lint, preview, install

The chart is an OCI artefact in the same registry as PostgreSQL. Pin the version, pull a local copy so helm lint has a directory to work on, and check the rendered output before installing:

REDIS_CHART=oci://registry-1.docker.io/bitnamicharts/redis
REDIS_CHART_VERSION="$(helm show chart "$REDIS_CHART" | awk '/^version:/ {print $2}')"
echo "$REDIS_CHART_VERSION"   # record it in charts/redis/README.md

helm pull "$REDIS_CHART" --version "$REDIS_CHART_VERSION" --untar --untardir /tmp/charts
helm lint /tmp/charts/redis -f charts/redis/values-dev.yaml

helm template redis /tmp/charts/redis -n data -f charts/redis/values-dev.yaml \
  | grep -nE "kind: (StatefulSet|Service|PersistentVolumeClaim)|redis-credentials|emptyDir"

The grep should show one StatefulSet, two Services, no PersistentVolumeClaim, an emptyDir and our Secret’s name. Then install:

helm install redis "$REDIS_CHART" \
  --version "$REDIS_CHART_VERSION" \
  --namespace data \
  -f charts/redis/values-dev.yaml

kubectl get pods,svc,pvc -n data

After a few seconds redis-master-0 should be 1/1 Running next to postgresql-0, and the only PVC in the namespace is still PostgreSQL’s.

Gotcha: the Bitnami image changes from 2025 that we hit in part 7 apply here too. If the pod is stuck in ImagePullBackOff, kubectl describe pod redis-master-0 -n data shows which image it tried to pull; check the chart's README for that version before overriding image.repository.

Step 5: talk to it and test the persistence choice

A throwaway client pod with the official redis:7 image gives us redis-cli. Passing the password in REDISCLI_AUTH instead of -a keeps it out of the process list and avoids the warning redis-cli prints for -a:

kubectl run redis-client --rm -it --restart=Never -n data \
  --image=redis:7 \
  --env=REDISCLI_AUTH="$(kubectl get secret redis-credentials -n data -o jsonpath='{.data.redis-password}' | base64 -d)" \
  -- redis-cli -h redis-master

At the redis-master:6379> prompt, PING answers PONG, and CONFIG GET maxmemory-policy confirms our configuration was applied. You’ll also notice FLUSHALL and FLUSHDB return an unknown-command error: the chart disables both by default so nobody empties the cache by accident.

Now let’s prove the persistence behaves as we designed it:

  1. Write a key with a TTL SET smoke 1 EX 600, then TTL smoke to watch it count down.
  2. Restart only the container kubectl exec redis-master-0 -n data -- sh -c 'kill 1' stops the Redis process. The kubelet restarts the container in the same pod; kubectl get pod redis-master-0 -n data shows RESTARTS 1.
  3. The key survives Reconnect and run GET smoke. Redis replayed its append-only file from the emptyDir, so the value is still there.
  4. Delete the pod kubectl delete pod redis-master-0 -n data. The StatefulSet recreates it with a fresh emptyDir, and GET smoke now returns (nil). That's the trade-off we chose.

Step 6: point the backend at it

The backend still has REDIS_HOST=redis.data.svc.cluster.local from part 6. Point it at the master Service:

kubectl set env deploy/backend -n apps \
  REDIS_HOST=redis-master.data.svc.cluster.local

kubectl rollout status deploy/backend -n apps

The new pod picks up the host and, from backend-secrets, the SPRING_DATA_REDIS_PASSWORD we added in step 1. Port-forward the frontend as before, register a user and log in. The MFA screen appears, and in the redis-cli session KEYS 'mfa:*' shows the key while TTL counts down from 300. KEYS is fine on a dev cache this small; on a busy production Redis use SCAN instead, because KEYS blocks the server while it walks every key. Enter the code and the key disappears, because GETDEL read and removed it in one step.

If login fails with NOAUTH Authentication required in the backend log, the password didn’t reach Spring: check the variable name with kubectl exec deploy/backend -n apps -- env | grep REDIS. If it fails with UnknownHostException, the host is still missing the -master suffix.

How production Redis differs

For a dev cluster this is enough. For production, consider:

High availability architecture: replication with sentinel.enabled: true adds replicas and automatic failover. Clients must then ask Sentinel for the current master, so the backend's Redis configuration changes too.
Network access A NetworkPolicy so only pods in apps can reach port 6379. A password alone is a thin line of defence.
Match persistence to the data Codes and sessions can be lost; queues and anything without another copy can't. Decide per use, not per tool.
Or don't run it A managed Redis service (ElastiCache, Memorystore, Azure Cache) replaces the release; the backend only needs a new host and password.

Takeaways

  • The Bitnami Redis chart defaults to replication; architecture: standalone gives one master and a <release>-master Service, which is the name clients connect to.
  • Create the Secret yourself and pass auth.existingSecret; the default key is redis-password.
  • Choose persistence from the data, not the tool: for five-minute MFA codes an emptyDir with AOF survives a container restart and leaves no PVC behind.
  • Set maxmemory below the container limit and pick an eviction policy, so Redis refuses or evicts rather than being OOM-killed.
  • Spring Boot reads SPRING_DATA_REDIS_PASSWORD from the environment with no change to application.yml.

Next, in part 9, we stop patching the backend with kubectl set env and give it its own Helm chart, with probes on Actuator health and JVM memory sized for containers.