Skip to content

Instantly share code, notes, and snippets.

@khansun
Last active October 11, 2025 11:04
Show Gist options
  • Select an option

  • Save khansun/75243b222f153f8f8d70c3272d942c63 to your computer and use it in GitHub Desktop.

Select an option

Save khansun/75243b222f153f8f8d70c3272d942c63 to your computer and use it in GitHub Desktop.
RedisClusterKubernetes
apiVersion: v1
kind: Secret
metadata:
name: redis-secret
type: Opaque
stringData:
REDIS_PASSWORD: "mypass"
---
apiVersion: v1
kind: Service
metadata:
name: redis-headless
labels:
app: redis
spec:
clusterIP: None
selector:
app: redis
ports:
- name: client
port: 6379
targetPort: 6379
- name: gossip
port: 16379
targetPort: 16379
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis
spec:
serviceName: redis-headless
replicas: 6
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis:7.2
args:
- "redis-server"
- "--cluster-enabled"
- "yes"
- "--cluster-config-file"
- "/data/nodes.conf"
- "--cluster-node-timeout"
- "5000"
- "--appendonly"
- "yes"
- "--requirepass"
- "$(REDIS_PASSWORD)"
- "--masterauth"
- "$(REDIS_PASSWORD)"
- "--cluster-announce-ip"
- "$(POD_IP)"
- "--cluster-announce-port"
- "6379"
- "--cluster-announce-bus-port"
- "16379"
env:
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: REDIS_PASSWORD
valueFrom:
secretKeyRef:
name: redis-secret
key: REDIS_PASSWORD
ports:
- containerPort: 6379
name: client
- containerPort: 16379
name: gossip
volumeMounts:
- name: data
mountPath: /data
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 100Mi
---
apiVersion: batch/v1
kind: Job
metadata:
name: redis-cluster-init
spec:
template:
spec:
restartPolicy: OnFailure
containers:
- name: redis-init
image: redis:7.2
env:
- name: REDIS_PASSWORD
valueFrom:
secretKeyRef:
name: redis-secret
key: REDIS_PASSWORD
command:
- sh
- -c
- |
echo "Waiting for Redis pods..."
sleep 20
yes yes | redis-cli --cluster create \
redis-0.redis-headless:6379 \
redis-1.redis-headless:6379 \
redis-2.redis-headless:6379 \
redis-3.redis-headless:6379 \
redis-4.redis-headless:6379 \
redis-5.redis-headless:6379 \
--cluster-replicas 1 -a ${REDIS_PASSWORD}

Having a redis cluster at your disposal can always come in handy!

Make sure you have a Kubernetes cluster ready.

(base) mahamudur.khan@Mac redis % kubectl get nodes

NAME             STATUS   ROLES           AGE     VERSION
docker-desktop   Ready    control-plane   4m45s   v1.32.2

We can now setup the redis cluster.

(base) mahamudur.khan@Mac redis % kubectl apply -f redis-cluster.yml 
secret/redis-secret created
service/redis-headless created
statefulset.apps/redis created
job.batch/redis-cluster-init created

Should look like this if we are successful.

(base) mahamudur.khan@Mac redis % kubectl get pods -w

NAME                       READY   STATUS    RESTARTS   AGE
redis-0                    1/1     Running   0          19s
redis-1                    1/1     Running   0          16s
redis-2                    1/1     Running   0          14s
redis-3                    1/1     Running   0          12s
redis-4                    1/1     Running   0          9s
redis-5                    1/1     Running   0          6s
redis-cluster-init-qfspk   1/1     Running   0          19s
redis-cluster-init-qfspk   0/1     Completed   0          23s
redis-cluster-init-qfspk   0/1     Completed   0          24s
redis-cluster-init-qfspk   0/1     Completed   0          25s

To access locally, we nee to forward the ports.

(base) mahamudur.khan@Mac redis % kubectl port-forward redis-0 6379:6379 &
kubectl port-forward redis-1 6380:6379 &
kubectl port-forward redis-2 6381:6379 &

On Mac, add the below mapping on /etc/hosts.

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1	localhost
255.255.255.255	broadcasthost
::1             localhost
127.0.0.1 redis-1
127.0.0.1 redis-2
127.0.0.1 redis-3

Finally, we can run the python script to test:

(base) mahamudur.khan@Mac redis % python testCluster.py
Connected to Redis cluster successfully!
Successfuly set test value 'khansun' for key 'rediscluster'
from rediscluster import RedisCluster
STARTUP_NODES = [
{"host": "127.0.0.1", "port": 6379},
{"host": "127.0.0.1", "port": 6380},
{"host": "127.0.0.1", "port": 6381},
]
REDIS_PASSWORD = "mypass"
TEST_KEY = "rediscluster"
TEST_VALUE = "khansun"
try:
rc = RedisCluster(
startup_nodes=STARTUP_NODES,
decode_responses=True,
skip_full_coverage_check=True,
password=REDIS_PASSWORD,
socket_timeout=10,
socket_connect_timeout=10
)
print("Connected to Redis cluster successfully!")
rc.set(TEST_KEY, TEST_VALUE)
print(f"Successfuly set test value '{rc.get(TEST_KEY)}' for key '{TEST_KEY}'")
except Exception as e:
print(f"Failed! {e}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment