DEV Community

Cover image for How to Deploy a Redis Cluster in Kubernetes
Dmitry Romanoff
Dmitry Romanoff

Posted on

How to Deploy a Redis Cluster in Kubernetes

Redis is a popular in-memory data structure store, often used as a database, cache, and message broker. Deploying a Redis cluster in Kubernetes can enhance its availability and scalability. In this article, we’ll guide you through the steps to deploy a Redis cluster using Kubernetes, specifically on Minikube.

Step 1: Create a Namespace

To keep our Redis deployment organized, we will create a dedicated namespace called my-redis. Run the following command:

kubectl create namespace my-redis
Enter fullscreen mode Exit fullscreen mode

Step 2: Define the Redis Deployment

Next, we’ll create a redis-deployment.yaml file that includes a ConfigMap for Redis configuration, a StatefulSet for the Redis instances, and a Service to expose the Redis cluster.

Here’s the content for redis-deployment.yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-config
  namespace: my-redis  # Specify the namespace
data:
  redis.conf: |
    bind 0.0.0.0
    protected-mode no
    appendonly yes
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis
  namespace: my-redis  # Specify the namespace
spec:
  serviceName: "redis"
  replicas: 5
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis
        image: redis:5
        command: ["redis-server", "/etc/redis/redis.conf"]
        volumeMounts:
        - name: redis-config
          mountPath: /etc/redis
        ports:
        - containerPort: 6379
      volumes:
      - name: redis-config
        configMap:
          name: redis-config
---
apiVersion: v1
kind: Service
metadata:
  name: redis
  namespace: my-redis  # Specify the namespace
spec:
  type: NodePort  # Change to NodePort for external access
  ports:
  - port: 6379
    nodePort: 30000  # You can specify a port or let Kubernetes assign one
  selector:
    app: redis
Enter fullscreen mode Exit fullscreen mode

Step 3: Apply the Deployment

Now, let’s apply the deployment configuration. Run the following command to create the resources defined in redis-deployment.yaml:

kubectl apply -f redis-deployment.yaml
Enter fullscreen mode Exit fullscreen mode

Step 4: Verify the Deployment

To check the status of your Redis cluster, you can use the following commands:

kubectl get statefulsets -n my-redis
kubectl get pods -n my-redis
kubectl get services -n my-redis
Enter fullscreen mode Exit fullscreen mode

These commands will show you the state of the StatefulSet, the individual Redis pods, and the service that exposes Redis.

Step 5: Accessing the Redis Cluster

To access the Redis cluster from outside the Minikube environment, you need to retrieve the Minikube IP. Use the following command:

minikube ip
Enter fullscreen mode Exit fullscreen mode

Once you have the Minikube IP, you can connect to your Redis cluster using the redis-cli tool. Replace with the actual IP address you obtained:

redis-cli -h <minikube-ip> -p 30000
Enter fullscreen mode Exit fullscreen mode

Conclusion

This setup allows for increased availability and scalability of your Redis instances. You can now leverage Redis for caching, session management, or as a message broker in your applications.

Top comments (0)