
Redis® is an open-source in-memory data store that can work as a quick-response database. It's capable of handling millions of queries per second with high availability and scalability. A Redis® Cluster is a group of Redis® instances used to achieve high availability, improved fault tolerance, and horizontal scaling in data management systems.
A Redis® cluster uses a full mesh topology where every node interconnects with the main node using a TCP connection. In Kubernetes, pods interconnect to each other making it possible to deploy Redis for high availability within a cluster.
Benefits of deploying Redis® in a Kubernetes cluster include:
This article explains how to deploy a Redis® Cluster on a Vultr Kubernetes Engine (VKE) cluster.
Before you begin
By default, Kubernetes adds all cluster components such as services, pods and ConfigMaps to the default namespace. By creating a separate namespace, you are able to manage pods and services more efficiently in the cluster. In this section, create the Redis® namespace as described in the steps below.
Create a new namespace for the Redis® cluster.
Verify that the new namespace is available.
Output:
A storage class is a Kubernetes resource that allows you to reserve disk space or attach volumes from a cloud provider to your cluster. By default, pods do not store the data permanently. When a pod gets deleted or restarts, data inside the pods is permanently lost.
In this section, mount a Vultr Block Storage instance to your Kubernetes cluster to store data permanently, and avoid any data loss as described below.
Using a text editor such as Nano, create a new storage class YAML file.
Add the following configurations to the file.
Save and close the file.
Apply the storage class to your cluster.
Verify that the storage class is available.
Your output should look like the below:
As displayed in the above output, a new storage class redis-storage is available and it's attached to a Vultr Block Storage volume.
Data durability is essential for Redis® deployments. Persistent volumes store Redis® data on the cluster to achieve data durability in case a pod restarts. A Persistent volume requests a specified amount of storage from the Kubernetes cluster, and it's dynamically provisioned by a StorageClass.
In this section, create a persistent volume as described below.
Create a new manifest file to configure three persistent volumes using the vultr-block-storage provisioner.
Add the following configurations to the file.
Save and close the file.
The above configuration creates three Persistent Volumes with a size of 10 GB using the vultr-block-storage provisioner. ReadWriteOnce means that the PVC is only mounted as read-write by a single node at a time.
Apply the configuration to the cluster
Verify the new persistent volumes
Your output should look like the one below:
The ConfigMap is a key-value store in a Kubernetes cluster in which you can define the Redis® configuration information. In this section, create a ConfigMap for the Redis® cluster as described below.
Create a new ConfigMap YAML file
Add the following configurations to the file. Replace your_secure_password with a secure password for your cluster
Save and close the file
In the above configuration, the master and slavepasswords must be the same to establish a connection in the Redis® cluster
Apply your configuration to the Kubernetes cluster
Verify that the ConfigMap is available in the Redis® namespace
Output:
To view the full Redis® code for a ConfigMap, visit the GitHub repository to fork or download the file.
A StatefulSet deploys stateful applications and clustered applications that save data to persistent storage. It's suitable for deploying Redis® and other applications that require persistent identities and stable hostnames. In this section, create a StatefulSet as below.
Create a new StatefulSet YAML file to scale the Redis® cluster
Add the following configurations to the file
Save and close the file
Apply the above StatefulSet configuration to deploy the Redis® cluster
Verify the list of running pods in the cluster
Output:
As displayed in the above output, all Redis® cluster pods are available and running.
To access Redis® internally on your cluster, create a headless service object in the Kubernetes cluster to access the application internally as described below.
Create a new headless service resource file
Add the following configurations to the file
Save and close the file
Apply the service resource to the Kubernetes cluster
Verify that the Redis® service is running
Output:
You have deployed the Redis cluster with three pods named redis-0, redis-1, and redis-2. The pod redis-0 acts as a master while other pods work as slaves in the cluster.
View the master pod redis-0 logs.
Output:
To view detailed information about the Redis® master node, use the describe command as below
Output:
Connect to the redis-0 pod to fetch the replication information
Log in to the Redis® shell
Authenticate with the replica using your master password
Verify the available replication information
Your output should look like the one below:
Exit the Redis® replica instance
To test the Redis® replication process, write sample data on the master pod and verify that the same data replicates on the slave pods
Connect to the master pod redis-0
Log in to Redis® using Redis® CLI
Enter the master password to gain full access to the Redis® cluster
Add some data to the master node
Verify the added data
Output:
Connect to the slave pod redis-1
Log in to the Redis® shell
Authenticate using the slave password you created earlier
Verify that data successfully replicates from the master node
Your output should look like the one below.
In this article, you have deployed a Redis® cluster to a Vultr Kubernetes Engine (VKE) cluster. You have added a key-value store on the master node and verified the replicated data on the slave node. For more information about Redis® cluster, visit the official documentation.
0 Comments
Be the first to comment and share your perspective with the community.