A Redis cluster provides a distributed and scalable way to store data in-memory, enabling high availability and fault tolerance. Deploying a Redis cluster on Kubernetes combines the scalability of Kubernetes with Redis’s efficient in-memory storage capabilities, creating a robust platform for data caching and processing.
This guide provides detailed instructions to install and configure a Redis cluster on Kubernetes. By following these steps, you will achieve a fully functional Redis cluster tailored for high availability and horizontal scaling.
TL;DR
To install a Redis cluster on Kubernetes:
- Deploy a Kubernetes cluster with at least 6 nodes for optimal Redis performance.
- Use a Redis Cluster Helm chart or manually configure StatefulSets and ConfigMaps.
- Configure services for cluster discovery and external access.
- Test the cluster with the Redis CLI.
Prerequisites
- Kubernetes Cluster: A working Kubernetes cluster with
kubectl
configured. - Helm Installed: If using Helm for deployment, ensure Helm is installed and configured.
- Resources: Minimum of 6 nodes in the Kubernetes cluster for a full Redis cluster setup (3 masters and 3 replicas).
- Redis CLI: The Redis command-line tool (
redis-cli
) installed on your local machine for testing and management.
Step 1: Create a Namespace for Redis
Organize the Redis cluster deployment by creating a dedicated namespace.
kubectl create namespace redis-cluster
Step 2: Deploy Redis Using Helm (Recommended)
The simplest way to deploy a Redis cluster on Kubernetes is to use a Helm chart. Helm provides pre-configured templates for Redis clusters, reducing manual setup efforts.
Add the Helm Repository:
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
Deploy the Redis Cluster
Use the following Helm command to deploy a Redis cluster with default settings:
helm install redis-cluster bitnami/redis-cluster --namespace redis-cluster
Verify the Deployment
Check the status of the Redis pods:
kubectl get pods -n redis-cluster
Expected Output:
Step 3: Deploy Redis Manually (Advanced)
If you prefer a custom setup, deploy Redis manually using StatefulSets, ConfigMaps, and Services.
Create a ConfigMap for Redis Configuration
Define the configuration file for Redis in a ConfigMap.
Command:
kubectl create configmap redis-config --from-literal=redis.conf="cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
" \
--namespace redis-cluster
Deploy Redis StatefulSet
Deploy a Redis StatefulSet with at least 6 replicas (3 masters and 3 replicas).
Example YAML (redis-statefulset.yaml):
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis-cluster
namespace: redis-cluster
spec:
serviceName: redis
replicas: 6
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis:7.0
ports:
- containerPort: 6379
volumeMounts:
- name: redis-data
mountPath: /data
env:
- name: REDIS_CONFIG_FILE
value: /redis-config/redis.conf
volumeMounts:
- name: config
mountPath: /redis-config
volumes:
- name: config
configMap:
name: redis-config
Deploy the StatefulSet:
kubectl apply -f redis-statefulset.yaml
Create a Service for Discovery
Expose the Redis pods using a headless service to enable cluster node discovery.
Example YAML (redis-service.yaml):
apiVersion: v1
kind: Service
metadata:
name: redis
namespace: redis-cluster
spec:
ports:
- port: 6379
targetPort: 6379
clusterIP: None
selector:
app: redis
Deploy the Service:
kubectl apply -f redis-service.yaml
Step 4: Connect Redis Nodes to Form a Cluster
After deploying the Redis pods, connect the nodes to form a cluster.
Steps:
1. Log into one of the Redis pods:
kubectl exec -it redis-cluster-0 -n redis-cluster -- redis-cli
2. Run the CLUSTER MEET
command to connect nodes:
CLUSTER MEET <node-ip> <port>
3. Verify the cluster status:
CLUSTER INFO
Step 5: Test the Cluster
Use the Redis CLI to interact with the cluster.
Example Commands:
- Check cluster nodes:
redis-cli -c -h <cluster-ip> CLUSTER NODES
- Set and get a value:
redis-cli -c -h <cluster-ip> SET key1 value1
redis-cli -c -h <cluster-ip> GET key1
Common Issues and Solutions
Pods Not Starting
- Cause: Insufficient resources in the Kubernetes cluster.
- Solution: Verify node capacity and ensure sufficient CPU and memory are available.
Cluster Nodes Failing to Connect
- Cause: Missing or incorrect headless service configuration.
- Solution: Verify the headless service (
redis
) exists and correctly maps to Redis pods.
Data Inconsistency
- Cause: Improper replication settings.
- Solution: Ensure that each master node has at least one replica.