Configuring Redis Database in Kubernetes


2024-01-27
This document will guide you through configuring a Redis database in a Kubernetes environment. It will explain the deployment steps in Kubernetes and the corresponding configurations for Redis.

Creating Relevant Configuration Files

Before creating relevant configuration files, it is recommended to create a folder to centrally store these files. Additionally, create a namespace to store these resources to avoid conflicts with other resources.

In this example, I am creating a folder named 'database' and a namespace named 'database,' and I will create the relevant configuration files in this folder.

Copy Successful
# Create a folder
mkdir database
Copy Successful
# Create a namespace
kubectl create namespace database

You can also label nodes and specify nodeSelector in the deployment to deploy the application only on specified nodes.

In this example, I am labeling a node with the 'database' label and specifying nodeSelector in the deployment.

Copy Successful
# Label the node
# <node-name> is the node server name.
kubectl label node <node-name> worker=database

Create the storage.yaml file with the following content:

Copy Successful
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: database
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
reclaimPolicy: Retain

Create the volume.yaml file with the following content:

Copy Successful
# <path> is the path to the database storage
# Example: path: "/mnt/disks/data/database"
apiVersion: v1
kind: PersistentVolume
metadata:
name: database
labels:
app: database
spec:
storageClassName: database
capacity:
storage: 100Gi
accessModes:
- ReadWriteMany
hostPath:
path: <path>
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: database
namespace: database
labels:
app: database
spec:
storageClassName: database
accessModes:
- ReadWriteMany
resources:
requests:
storage: 100Gi

Create the config.yaml file with the following content:

Copy Successful
apiVersion: v1
kind: ConfigMap
metadata:
name: redis
namespace: database
labels:
app: redis
data:
redis-config: ""

Create the deployment.yaml file with the following content:

Copy Successful
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
namespace: database
labels:
app: redis
spec:
replicas: 1
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
nodeSelector:
worker: database
containers:
- name: redis
image: redis:latest
ports:
- containerPort: 6379
env:
- name: MASTER
value: "true"
command:
- redis-server
- "/redis-master/redis.conf"
resources:
limits:
cpu: "0.1"
volumeMounts:
- mountPath: /redis-master-data
name: data
subPath: redis
- mountPath: /redis-master
name: config
volumes:
- name: data
persistentVolumeClaim:
claimName: database
- name: config
configMap:
name: redis
items:
- key: redis-config
path: redis.conf

Create the service.yaml file with the following content:

Copy Successful
# For external connectivity, NodePort is used here. If internal connectivity is sufficient, ClusterIP can be used.
apiVersion: v1
kind: Service
metadata:
name: redis
namespace: database
labels:
app: redis
spec:
type: NodePort
ports:
- name: redis
port: 6379
nodePort: 6379
targetPort: 6379
selector:
app: redis
Copy Successful
# Deploy storage
kubectl apply -f storage.yaml
Copy Successful
# Deploy volume
kubectl apply -f volume.yaml
Copy Successful
# Deploy config
kubectl apply -f config.yaml
Copy Successful
# Deploy deployment
kubectl apply -f deployment.yaml
Copy Successful
# Deploy service
kubectl apply -f service.yaml

GCP Firewall Configuration

If you are using GCP and opening connections to the external world, you need to add a rule in the firewall to allow connections on port 6379.

After logging into the GCP platform, navigate to the left-hand menu:

Virtual Private Cloud > Firewall

0

Click the 'Create Firewall Rule' button at the top and add a rule to allow TCP port 6379 connections.

0

Test the Connection

Since NodePort is used in this example, you can connect directly from outside using the node's IP. You can test the connection using commonly used database tools such as DataGrip, Navicat, etc.