Deploy Redis On Kubernetes Using StatefulSet

Kubernetes is a powerful container orchestration tool that can be used to deploy and manage containerized applications at scale. One of the many benefits of Kubernetes is its ability to deploy StatefulSets, which are designed to manage stateful applications, such as databases, in a way that ensures data consistency and availability. In this article, we will explore how to deploy Redis on Kubernetes using a StatefulSet.

Prerequisites

Before we dive into the deployment process, there are a few prerequisites that need to be in place. First, you need to have a working Kubernetes cluster. If you don't have one setup already, there are several options available for setting up a Kubernetes cluster, including using a managed Kubernetes service such as Google Kubernetes Engine (GKE), Amazon Elastic Kubernetes Service (EKS) or Microsoft Azure Kubernetes Service (AKS).

Once you have your Kubernetes cluster set up, you will need to have the kubectl command-line tool installed on your local machine. Kubectl is the primary tool used to interact with Kubernetes clusters and is used to deploy and manage Kubernetes resources.

Lastly, you will need a Redis container image. You can either use an existing Redis image available on Docker Hub, or you can build your own image if you have specific requirements.

Deploying Redis on Kubernetes

Now that we have our prerequisites in place let's get started with deploying Redis on Kubernetes using a StatefulSet.

Step 1: Create a Redis Configuration File

Before deploying Redis using StatefulSet, we need to create a Redis configuration file. This file will contain the configuration parameters that Redis needs to run.

bind 0.0.0.0
protected-mode no

This configuration file sets Redis to bind to all network interfaces and disables the protected mode.

Step2: Create a Redis Docker Image

Now that we have our Redis configuration file, it's time to create a Docker image that includes the configuration file. We'll use a Dockerfile to build the image.

Create a file named Dockerfile with the following contents:

FROM redis:latest
COPY redis.conf /usr/local/etc/redis/redis.conf
CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]

This Dockerfile sets the base image to the latest version of Redis, copies our Redis configuration file into the image, and sets the CMD to start Redis with the configuration file.

Build the Docker image by running the following command:

$ docker build -t my-redis-image .

Step 3: Create a Redis Service

Now that we have our Redis Docker image, it's time to create a Kubernetes Service that will expose Redis to the rest of the cluster.

Create a file named redis-service.yaml with the following contents:

apiVersion: v1
kind: Service
metadata:
  name: redis
spec:
  selector:
    app: redis
  ports:
  - name: redis
    port: 6379
	

This Kubernetes Service will expose port 6379, which is the default port used by Redis.

Create the Service by running the following command:

$ kubectl apply -f redis-service.yaml

Step 4: Create a Persistent Volume Claim (PVC)

To store Redis data on Kubernetes, we need to create a Persistent Volume Claim (PVC) that will be used by the Redis StatefulSet. A PVC requests a specific amount of storage from the Kubernetes cluster, which will be dynamically provisioned by a StorageClass.

Create a file named redis-pvc.yaml with the following contents:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: redis-pvc
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

This YAML file defines a PVC named redis-pvc with a request for 1GB of storage. We are requesting the ReadWriteOnce access mode, which means that the PVC can be mounted as read-write by a single node at a time.

To create the PVC, run the following command:

$ kubectl apply -f redis-pvc.yaml

Verify that the PVC is created by running the following command:

$ kubectl get pvc

You should see an output similar to the following:

NAME        STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
redis-pvc   Bound    pvc-8d7d420b-c4a4-4c4b-8b84-2ce4e0b7c1b1   1Gi        RWO            standard       10s

Step 5: Create a Redis StatefulSet

Now that we have our Redis Docker image and Kubernetes Service, it's time to create the Redis StatefulSet.

Create a file named redis-statefulset.yaml with the following contents:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis
spec:
  serviceName: redis
  replicas: 3
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis
        image: my-redis-image
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 6379
          name: redis
	  volumes:
      - name: redis-data
      	persistentVOlumeClaim:
        claimName: redis-pvc
  volumeClaimTemplates:
  - metadata:
      name: redis-pvc
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi

This StatefulSet specifies that we want to run three replicas of Redis and that we want to use the Redis Service that we created earlier. It also specifies that we want to use the my-redis-image Docker image that we built earlier.

$ kubectl apply -f redis-statefulset.yaml

Connecting to the Redis Cluster and Adding Data

Now that we have our Redis deployment running let's connect to the cluster and add some data.

To connect to the Redis cluster, we first need to find the IP address of one of the pods. We can do this by running the following command:

$ kubectl get pods -l app=redis

This will list all of the Redis pods and their status. Copy the name of one of the pods and use it in the following command to get its IP address:

$ kubectl describe pod <pod-name> | grep IP:

Replace <pod-name> with the name of the Redis pod you want to connect to. This will output the IP address of the pod.

Next, we need to install the Redis command-line interface (CLI) on our local machine. You can do this by running the following command:

$ sudo apt-get install redis-tools

or on Arch Linux

$ sudo pacman -S redis

Once the Redis CLI is installed, we can connect to the Redis cluster by running the following command:

$ redis-cli -h <redis-ip> -p 6379

Replace <redis-ip> with the IP address of the Redis pod you want to connect to.

Once you're connected to the Redis cluster, you can add some data by running Redis commands. For example, you can set a key-value pair by running the following command:

set mykey myvalue

You can then retrieve the value of the key by running the following command:

get mykey

This will output the value of the key, which should be myvalue.

Conclusion

In this blog post, we've gone through the steps necessary to deploy Redis on Kubernetes using StatefulSets. We started by setting up a Kubernetes cluster on our local machine and creating a PresistentVolumeClaim to store Redis data. We then created a StatefulSet definition to deploy multiple Redis pods, each with its own unique hostname and persistent storage.

Finally, we verified that our deployment was successful by connecting to the Redis cluster and adding some data.

Using StatefulSets to deploy stateful applications like Redis on Kubernetes provides many benefits. It allows us to easily scale our application horizontally while maintaining data consistency and durability. It also simplifies the deployment process by abstracting away the details of managing stateful pods. With this knowledge, you should be able to deploy Redis on Kubernetes and other stateful applications using StatefulSets with confidence.

Further Reading

If you're interested in learning more about Kubernetes and stateful applications, here are some additional resources you may find useful:

References

Thank you for following along with me, and happy deploying!