Skip to content

Instantly share code, notes, and snippets.

@vnkdj5
Last active September 28, 2024 19:58
Show Gist options
  • Save vnkdj5/f7a0c99f8c154f274de84395f24b74da to your computer and use it in GitHub Desktop.
Save vnkdj5/f7a0c99f8c154f274de84395f24b74da to your computer and use it in GitHub Desktop.
Redis cluster Setup via docker using shell script
#!/bin/bash
if [ "$1" = "setup" ]; then
docker network create redis-cluster
docker run -e ALLOW_EMPTY_PASSWORD=yes -e REDIS_NODES=redis1,redis2,redis3,redis4,redis5,redis6 --name redis1 --network redis-cluster -p 6379:6379 -d bitnami/redis-cluster:latest
docker run -e ALLOW_EMPTY_PASSWORD=yes -e REDIS_NODES=redis1,redis2,redis3,redis4,redis5,redis6 --name redis2 --network redis-cluster -p 6380:6379 -d bitnami/redis-cluster:latest
docker run -e ALLOW_EMPTY_PASSWORD=yes -e REDIS_NODES=redis1,redis2,redis3,redis4,redis5,redis6 --name redis3 --network redis-cluster -p 6381:6379 -d bitnami/redis-cluster:latest
docker run -e ALLOW_EMPTY_PASSWORD=yes -e REDIS_NODES=redis1,redis2,redis3,redis4,redis5,redis6 --name redis4 --network redis-cluster -p 6382:6379 -d bitnami/redis-cluster:latest
docker run -e ALLOW_EMPTY_PASSWORD=yes -e REDIS_NODES=redis1,redis2,redis3,redis4,redis5,redis6 --name redis5 --network redis-cluster -p 6383:6379 -d bitnami/redis-cluster:latest
docker run -e ALLOW_EMPTY_PASSWORD=yes -e REDIS_NODES=redis1,redis2,redis3,redis4,redis5,redis6 --name redis6 --network redis-cluster -p 6384:6379 -d bitnami/redis-cluster:latest
echo "Sleeping for 10 seconds before joining the cluster"
sleep 10
docker exec -it redis1 redis-cli --cluster create redis1:6379 redis2:6379 redis3:6379 redis4:6379 redis5:6379 redis6:6379 --cluster-replicas 1 --cluster-yes
echo "Cluster setup completed"
elif [ "$1" = "start" ]; then
docker start redis6 redis5 redis4 redis3 redis2 redis1
elif [ "$1" = "stop" ]; then
docker stop redis6 redis5 redis4 redis3 redis2 redis1
elif [ "$1" = "remove" ]; then
docker remove redis6 redis5 redis4 redis3 redis2 redis1
else
echo "Invalid argument. Usage: $0 [setup|start|stop|remove]"
exit 1
fi
#!/bin/bash
if [ "$1" = "setup" ]; then
# Use host network mode, with explicit announce IP set to localhost (127.0.0.1)
docker run -e ALLOW_EMPTY_PASSWORD=yes \
-e REDIS_PORT_NUMBER=6379 \
-e REDIS_CLUSTER_ANNOUNCE_IP=127.0.0.1 \
-e REDIS_NODES=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384 \
--name redis1 --network host -d bitnami/redis-cluster:latest
docker run -e ALLOW_EMPTY_PASSWORD=yes \
-e REDIS_PORT_NUMBER=6380 \
-e REDIS_CLUSTER_ANNOUNCE_IP=127.0.0.1 \
-e REDIS_NODES=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384 \
--name redis2 --network host -d bitnami/redis-cluster:latest
docker run -e ALLOW_EMPTY_PASSWORD=yes \
-e REDIS_PORT_NUMBER=6381 \
-e REDIS_CLUSTER_ANNOUNCE_IP=127.0.0.1 \
-e REDIS_NODES=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384 \
--name redis3 --network host -d bitnami/redis-cluster:latest
docker run -e ALLOW_EMPTY_PASSWORD=yes \
-e REDIS_PORT_NUMBER=6382 \
-e REDIS_CLUSTER_ANNOUNCE_IP=127.0.0.1 \
-e REDIS_NODES=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384 \
--name redis4 --network host -d bitnami/redis-cluster:latest
docker run -e ALLOW_EMPTY_PASSWORD=yes \
-e REDIS_PORT_NUMBER=6383 \
-e REDIS_CLUSTER_ANNOUNCE_IP=127.0.0.1 \
-e REDIS_NODES=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384 \
--name redis5 --network host -d bitnami/redis-cluster:latest
docker run -e ALLOW_EMPTY_PASSWORD=yes \
-e REDIS_PORT_NUMBER=6384 \
-e REDIS_CLUSTER_ANNOUNCE_IP=127.0.0.1 \
-e REDIS_NODES=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384 \
--name redis6 --network host -d bitnami/redis-cluster:latest
echo "Sleeping for 10 seconds before joining the cluster"
sleep 10
# Use localhost with respective ports for cluster creation
docker exec -it redis1 redis-cli --cluster create \
127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 \
127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 \
--cluster-replicas 1 --cluster-yes
echo "Cluster setup completed"
elif [ "$1" = "start" ]; then
docker start redis6 redis5 redis4 redis3 redis2 redis1
elif [ "$1" = "stop" ]; then
docker stop redis6 redis5 redis4 redis3 redis2 redis1
elif [ "$1" = "remove" ]; then
docker rm redis6 redis5 redis4 redis3 redis2 redis1
else
echo "Invalid argument. Usage: $0 [setup|start|stop|remove]"
exit 1
fi
@vnkdj5
Copy link
Author

vnkdj5 commented Mar 13, 2024

Make it executable using the
chmod +x redis_cluster.sh

Run

./redis_cluster.sh setup  ==> initial setup
./redis_cluster.sh start ==> start
./redis_cluster.sh stop  ==> stop
./redis_cluster.sh remove  ==> remove


@vnkdj5
Copy link
Author

vnkdj5 commented Mar 13, 2024

Docker single node redis

docker run -e ALLOW_EMPTY_PASSWORD=yes --name redis -p 6379:6379 bitnami/redis:latest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment