Getty Images/iStockphoto

Deploy a Redis Cluster on Kubernetes

Redis is a powerful platform with many use cases. Follow this tutorial to run Redis on Kubernetes to simplify deployments and increase reliability and performance.

Redis is a powerful, versatile open source platform that can support a wide range of use cases, including operating as a primary database, serving as a message broker or caching website content.

When you run Redis on Kubernetes (K8), the platform becomes even more powerful, but it isn't the right setup for everyone. Installing Redis on Kubernetes can simplify deployment, and improve reliability and performance. Follow this guide to learn how to get Redis up and running on a Kubernetes cluster.

What is Redis?

Redis is an open source in-memory data store. You can use Redis for a variety of purposes:

  • Running a NoSQL-style database.
  • Brokering messages as part of a data streaming pipeline.
  • Caching data for websites to improve performance.
  • Running a series of operations to process data or enable a transaction.

Because the data that Redis stores lives in memory, accessing that data is faster than it would be when storing data on disk. This is the main benefit of Redis as compared to traditional databases or key-value stores.

Redis offers a cluster mode feature that shards data across multiple nodes to increase reliability. Not all Redis alternatives provide this feature. In cases where they do, clustering is often more complex than it is with Redis. For instance, it's possible to run a MySQL database using a clustered architecture. But this is more work because MySQL is designed to run in a non-distributed mode.

Benefits of running Redis on Kubernetes

Redis can run in a variety of environments, including directly on Windows and Linux servers. Redis can also be deployed on Kubernetes which can increase reliability and performance in some cases.

Kubernetes can automatically manage the deployment of Redis nodes (or a single node, if you choose to run Redis in non-clustered mode) across a cluster of Kubernetes nodes. If one of the nodes that hosts part of a Redis cluster fails, Kubernetes will automatically restart the Redis node on a different server.

Nodes in Redis refers to virtual servers that form a Redis cluster. In contrast, Kubernetes nodes are the physical or virtual servers that provide the infrastructure on which Kubernetes deploys workloads. So, if you run Redis on Kubernetes, you're deploying Redis nodes on top of Kubernetes nodes. These are different types of nodes, and it's important not to conflate them.

To scale Redis up or down on Kubernetes, add or remove Redis instances from a Kubernetes cluster. You can manage load balancing through Kubernetes, which allows you to route requests efficiently across multiple Redis nodes. You can manage Redis logging and monitoring through Kubernetes, too. For example, you can deploy monitoring agents alongside Redis within the same Kubernetes cluster.

The main reason why you might not want to run Redis on Kubernetes is that deployment and configuration is a bit more complicated. You can install Redis directly on a Linux or Windows server using just a few commands. But if you install it on Kubernetes, you'll need to follow some additional steps.

Prerequisites for deploying Redis on Kubernetes

Before beginning the process for deploying Redis on Kubernetes article, you'll need the following:

  • A Kubernetes cluster. This example uses K3s, but any modern Kubernetes distribution should support Redis.
  • Helm. This example installs Redis using Helm, a package manager for Kubernetes. If Helm isn't already installed on your system, refer to the Helm documentation for installation options.
  • Basic familiarity with Kubernetes. You don't need to be a K8s guru to install Redis on Kubernetes. But you should have a working knowledge of kubectl and Helm charts, (if you choose to install Redis using Helm.

Deploy Redis on Kubernetes

This example uses a Redis Helm chart provided by Bitnami that is hosted on GitHub. Alternative installation methods are available.

Step 1

Install the Redis Helm chart.

helm install my-release oci://registry-1.docker.io/bitnamicharts/redis-cluster​

Step 2

Verify that Redis pods are running. Redis pods will launch automatically after completing the Helm chart installation. To verify that the pods successfully started, run the following command.

kubectl get pods --all-namespaces

The output should contain a list of Redis pods like the following.

NAMESPACE     NAME                                      READY   STATUS              RESTARTS       AGE

default       my-release-redis-cluster-2                0/1     ContainerCreating   0              34s

default       my-release-redis-cluster-5                0/1     ContainerCreating   0              34s

default       my-release-redis-cluster-1                0/1     ContainerCreating   0              34s

default       my-release-redis-cluster-0                0/1     ContainerCreating   0              34s

default       my-release-redis-cluster-4                0/1     ContainerCreating   0              34s

default       my-release-redis-cluster-3                0/1     ContainerCreating   0              34s

In the output, the pods are still in the ContainerCreating state. If you wait a minute or two and get the list of pods again, you should see that they have transitioned to Running.

NAMESPACE     NAME                                      READY   STATUS              RESTARTS       AGE

default       my-release-redis-cluster-5                1/1     Running             0              3m16s

default       my-release-redis-cluster-1                1/1     Running             0              3m16s

default       my-release-redis-cluster-4                1/1     Running             0              3m16s

default       my-release-redis-cluster-0                1/1     Running             0              3m16s

default       my-release-redis-cluster-2                1/1     Running             0              3m16s

default       my-release-redis-cluster-3                1/1     Running             0              3m16s

The Pods need to be in the Running state before you can proceed with connecting to the Redis cluster.

Step 3

Export the cluster password as an environment variable to connect to your newly installed Redis cluster

export REDIS_PASSWORD=$(kubectl get secret --namespace "default" my-release-redis-cluster -o jsonpath="{.data.redis-password}" | base64 -d)

Step 4

Run this command to start a new container to host the Redis client. This will start the container and automatically open a Bash shell inside it.

kubectl run --namespace default my-release-redis-cluster-client --rm --tty -i --restart='Never' \

 --env REDIS_PASSWORD=$REDIS_PASSWORD \

--image docker.io/bitnami/redis-cluster:7.2.4-debian-12-r11 -- bash

Step 5

From that location, enter the following command to complete the connection to the cluster.

redis-cli -c -h my-release-redis-cluster -a $REDIS_PASSWORD

You're now at a command prompt where you can interact with your Redis cluster. For example, if you run the command CLUSTER INFO, you should see output such as the following.

my-release-redis-cluster:6379> CLUSTER INFO

cluster_state:ok

cluster_slots_assigned:16384

cluster_slots_ok:16384

cluster_slots_pfail:0

cluster_slots_fail:0

cluster_known_nodes:6

cluster_size:3

cluster_current_epoch:6

cluster_my_epoch:1

cluster_stats_messages_ping_sent:473

cluster_stats_messages_pong_sent:475

cluster_stats_messages_sent:948

cluster_stats_messages_ping_received:470

cluster_stats_messages_pong_received:473

cluster_stats_messages_meet_received:5

cluster_stats_messages_received:948

total_cluster_links_buffer_limit_exceeded:0

Alternative Redis deployment options for Kubernetes

Deploying Redis using a Helm chart is the simplest way to get Redis up and running on Kubernetes, but it's not the only method.

Alternative approaches include deploying Redis Enterprise using a Kubernetes operator or setting up Redis as a Kubernetes Service. These techniques provide more control over how you configure Redis. But if you're new to Redis and/or Kubernetes and want a fast and simple way to install a cluster, the Helm chart method is best.

Chris Tozzi is a freelance writer, research adviser, and professor of IT and society who has previously worked as a journalist and Linux systems administrator.

Dig Deeper on Containers and virtualization