DEV Community

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

Posted on

11

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.

AWS Industries LIVE! Stream

Watch AWS Industries LIVE!

Discover how cloud technology is solving real-world problems on Industries LIVE!

Learn More

Top comments (0)

Billboard image

Try REST API Generation for Snowflake

DevOps for Private APIs. Automate the building, securing, and documenting of internal/private REST APIs with built-in enterprise security on bare-metal, VMs, or containers.

  • Auto-generated live APIs mapped from Snowflake database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay