Introduction
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.
Prerequisites
- A Kubernetes cluster consisting of two or more nodes
- Helm 3 installed
- kubectl 1.14 or above installed
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.
- 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
metadata:
name: test-redis-config
data:
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.
5. Create a Redis pod manifest.
nano redis-pod.yaml
6. Specify your pod configuration.
apiVersion: v1
kind: Pod
metadata:
name: redis
spec:
containers:
- name: redis
image: redis:5.0.4
command:
- redis-server
- "/redis-master/redis.conf"
env:
- name: MASTER
value: "true"
ports:
- containerPort: 6379
resources:
limits:
cpu: "0.1"
volumeMounts:
- mountPath: /redis-master-data
name: data
- mountPath: /redis-master
name: config
volumes:
- name: data
emptyDir: {}
- name: config
configMap:
name: test-redis-config
items:
- 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.
9. Check pod status.
kubectl get pod
10. Enter the created pod with kubectl exec
.
kubectl exec -it redis -- redis-cli
The Redis server console appears:
11. Use the PING command to check if the server is online.
PING
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.
2. Update local Helm repositories.
helm repo update
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
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 docker.io/bitnami/redis:6.2.5-debian-10-r63 --command -- sleep infinity
The system confirms the creation of the redis-client
pod.
6. Enter the client pod with kubectl exec
.
kubectl exec --tty -i redis-client --namespace default -- bash
The client console appears.
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.
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.
PING
The PONG
response confirms that the server is listening.
Conclusion
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.