Cloud Native 10 min read

Deploy a Redis Cluster on Kubernetes with StatefulSet and Headless Service

Learn how to deploy a Redis cluster on Kubernetes using a StatefulSet and Headless Service, covering problem analysis, key concepts, configuration steps, initialization with redis-cli, and creating services for access, all without persistent volumes for simplicity.

Open Source Linux
Open Source Linux
Open Source Linux
Deploy a Redis Cluster on Kubernetes with StatefulSet and Headless Service

Problem Analysis

In essence, deploying a Redis cluster on Kubernetes is similar to deploying a regular application, but you must consider that Redis is a stateful application and requires data persistence.

Redis is a stateful application Each pod has its own cache and IP may change, so using a normal Deployment and Service causes issues; you need a StatefulSet with a Headless Service.

Data persistence Although Redis is memory‑based, it needs disk persistence. Use a shared file system with PersistentVolumes so all pods share the same storage.

Concept Introduction

1. Headless Service

A Headless Service has no ClusterIP; DNS resolves to the list of pod IPs.

2. StatefulSet

StatefulSet

is a Kubernetes resource for stateful applications, a variant of Deployment/RC with these features:

Each pod gets a stable network identity (e.g., redis‑0, redis‑1).

Pods are started and terminated in a strict order.

Pods use stable persistent storage that is not deleted with the pod.

StatefulSet must be used together with a Headless Service, which provides DNS entries like $(podname).$(headless service name).

Solution

The deployment uses a StatefulSet and a Headless Service. The following steps outline the configuration.

Configure a shared NFS file system.

Create PV and PVC.

Create ConfigMap with redis.conf.

Create Headless Service.

Create StatefulSet.

Initialize the Redis cluster.

Actual Operations

1. Create ConfigMap

Create redis.conf with the following content:

appendonly yes
cluster-enabled yes
cluster-config-file /var/lib/redis/nodes.conf
cluster-node-timeout 5000
dir /var/lib/redis
port 6379

Then run:

kubectl create configmap redis-conf --from-file=redis.conf

2. Create Headless Service

apiVersion: v1
kind: Service
metadata:
  name: redis-service
  labels:
    app: redis
spec:
  ports:
  - name: redis-port
    port: 6379
  clusterIP: None
  selector:
    app: redis
    appCluster: redis-cluster

3. Create StatefulSet

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: redis-app
spec:
  serviceName: "redis-service"
  replicas: 6
  template:
    metadata:
      labels:
        app: redis
        appCluster: redis-cluster
    spec:
      terminationGracePeriodSeconds: 20
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - redis
              topologyKey: kubernetes.io/hostname
      containers:
      - name: redis
        image: "registry.cn-qingdao.aliyuncs.com/gold-faas/gold-redis:1.0"
        command:
          - "redis-server"
        args:
          - "/etc/redis/redis.conf"
          - "--protected-mode"
          - "no"
        resources:
          requests:
            cpu: "100m"
            memory: "100Mi"
        ports:
          - name: redis
            containerPort: 6379
            protocol: "TCP"
          - name: cluster
            containerPort: 16379
            protocol: "TCP"
        volumeMounts:
          - name: "redis-conf"
            mountPath: "/etc/redis"
          - name: "redis-data"
            mountPath: "/var/lib/redis"
      volumes:
      - name: "redis-conf"
        configMap:
          name: "redis-conf"
          items:
            - key: "redis.conf"
              path: "redis.conf"
      - name: "redis-data"
        emptyDir: {}

4. Initialize the Redis cluster

Run a temporary Ubuntu pod as a manager:

kubectl run -i --tty redis-cluster-manager --image=ubuntu --restart=Never /bin/bash

Install tools, download Redis, compile, and place redis-cli in /usr/local/bin. Use nslookup to get pod IPs, then create the master nodes:

redis-cli --cluster create 172.17.0.10:6379 172.17.0.11:6379 172.17.0.12:6379

Add slave nodes with the appropriate master IDs:

redis-cli --cluster add-node 172.17.0.13:6379 172.17.0.10:6379 --cluster-slave --cluster-master-id <master-id>

Repeat for the remaining slaves.

5. Create Service for client access

apiVersion: v1
kind: Service
metadata:
  name: gold-redis
  labels:
    app: redis
spec:
  ports:
  - name: redis-port
    protocol: "TCP"
    port: 6379
    targetPort: 6379
  selector:
    app: redis
    appCluster: redis-cluster

After deploying the service, you can connect to any node with redis-cli -c and the cluster operates correctly.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

KubernetesRedisdevopsClusterStatefulSetHeadless Service
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.