How to Deploy Redis Cluster on Kubernetes

How to Deploy Redis Cluster on Kubernetes


Redis is a popular NoSQL database and an in-memory data store supporting multiple abstract data structures. These include strings, lists, hashes, sets, streams, etc. Redis provides syntax for accessing mutable data structures, allowing multiple processes to read and write them in a shared way.

A Redis Cluster is a set of Redis instances that automatically shards data across nodes. Using a cluster gives users the ability to split their datasets among nodes and keep running the database even when some nodes fail.

This tutorial will show you how to deploy a Redis Cluster on Kubernetes using ConfigMap and Helm.

How to Deploy Redis Cluster on Kubernetes


Note: If you are using Minikube, you can simulate a two-node cluster by adding the --nodes option to the start command:

minikube start --nodes 2

Deploying Redis on Kubernetes with ConfigMap

The following steps explain how to configure Redis cache and a pod containing a Redis instance.

  1. Using a text editor, create a ConfigMap YAML that will store the Redis configuration.
nano redis-conf.yaml

2. Specify your Redis configuration in the data.redis-config section.

apiVersion: v1
kind: ConfigMap
  name: test-redis-config
  redis-config: |
    maxmemory 2mb
    maxmemory-policy allkeys-lru 

The example configuration above sets the maxmemory directive and tells Redis to use the maximum of 2 MB of storage for the data set. The maxmemory-policy directive defines the procedure to be applied when the memory limit is reached. allkeys-lru first removes the less recently used (LRU) keys.

3. Save the file and exit.

4. Create the ConfigMap by applying the YAML file.

kubectl apply -f redis-conf.yaml

The system confirms that the operation was successful.

Creating a redis configmap using a yaml file and kubectl apply.

5. Create a Redis pod manifest.

nano redis-pod.yaml

6. Specify your pod configuration.

apiVersion: v1
kind: Pod
  name: redis
  - name: redis
    image: redis:5.0.4
      - redis-server
      - "/redis-master/redis.conf"
    - name: MASTER
      value: "true"
    - containerPort: 6379
        cpu: "0.1"
    - mountPath: /redis-master-data
      name: data
    - mountPath: /redis-master
      name: config
    - name: data
      emptyDir: {}
    - name: config
        name: test-redis-config
        - key: redis-config
          path: redis.conf

In the example above, the manifest defines config volume and mounts it at /redis-master directory on the pod. The spec.volumes.items section then exposes the redis-config key from the previously created ConfigMap as redis.conf file.

7. Save the file and exit.

8. Apply the pod manifest with kubectl.

kubectl apply -f redis-pod.yaml

The system confirms that the pod creation was successful.

Creating a redis pod using a yaml file and kubectl apply.

9. Check pod status.

kubectl get pod
Checking if the Redis pod deployed in the previous step is running.

10. Enter the created pod with kubectl exec.

kubectl exec -it redis -- redis-cli

The Redis server console appears:

Using kubectl exec to enter the Redis pod.

11. Use the PING command to check if the server is online.

Checking the server connection with the PING command.

If the server communicates with the console, it returns PONG as the answer.

Note: Deploy Bare Metal Cloud servers to ensure high availability and maximum performance of your Redis cluster. Achieve a reliable infrastructure with servers starting at only $0.10/hour!

Deploying Redis on Kubernetes with Helm Chart

Helm provides a quick way of setting up a Redis cluster using a pre-made Helm chart.

1. Add the Helm repository containing the Redis chart you wish to install.

helm repo add [repo-name] [repo-address]

This article uses the Redis chart available in the Bitnami repository.

Adding the Bitnami Helm repository.

2. Update local Helm repositories.

helm repo update
Updating local Helm repositories.

3. Use helm install to install the chart. The basic command is as follows:

helm install redis-test bitnami/redis

Important: If you install the chart on Minikube, it is not unusual for pods to get stuck in a CrashLoopBackOff event due to unbound volume claims. To prevent this, use the --set option and set persistence.storageClass to nfs-client for both pods.

Since Redis is deployed with non-administrative volume permissions by default, the Redis pod may not be able to communicate with the server. Resolve this problem by setting volumePermissions to true.

The final helm install command should look like this:

helm install redis-test --set persistence.storageClass=nfs-client,redis.replicas.persistence.storageClass=nfs-client bitnami/redis --set volumePermissions.enabled=true
Installing Redis using Helm on Kubernetes.

4. Export the Redis password as an environment variable.

export REDIS_PASSWORD=$(kubectl get secret --namespace default redis-test -o jsonpath="{.data.redis-password}" | base64 --decode)

5. Create a Redis client pod that you will use to access the database.

kubectl run --namespace default redis-client --restart='Never'  --env REDIS_PASSWORD=$REDIS_PASSWORD  --image --command -- sleep infinity

The system confirms the creation of the redis-client pod.

Creating the redis-client pod using kubectl run.

6. Enter the client pod with kubectl exec.

kubectl exec --tty -i redis-client --namespace default -- bash

The client console appears.

Executing into the redis-client pod using kubectl exec.

7. Use the following redis-cli command to access the Redis master instance:

redis-cli -h redis-test-master -a $REDIS_PASSWORD

The console for the master instance displays.

Logging into the redis-test-master pod using redis-cli.

8. To access the replicas, use the same command, but alter the target.

redis-cli -h redis-test-replicas -a $REDIS_PASSWORD

9. Use the PING command to test the connection with the server.

Pinging the Redis server using redis-cli.

The PONG response confirms that the server is listening.


After completing this tutorial, you should know how to configure Redis cache and pods manually, and how to deploy Redis using Helm.

For more popular tools similar to Redis, read our article on the best data management software.

Đăng ký liền tay Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !

Add Comment

Click here to post a comment

Đăng ký liền tay
Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !