Skip to content

Docker dev: Redis sentinel

Graeme Porteous edited this page Apr 11, 2023 · 1 revision

Opted not to commit this change as this probably won't ever be used again, but I'm documentation here just in case.

To test Redis sentinel connections in our Docker development environment we can do something like (based off 9e08bf8):

diff --git a/docker-compose.yml b/docker-compose.yml
index 18a6cdbb2..515df632f 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -11,6 +11,7 @@ services:
       - BUNDLE_PATH=/bundle/vendor
       - DATABASE_URL=postgres://postgres:password@db/
       - REDIS_URL=redis://redis:6379
+      - REDIS_SENTINELS=redis_sentinel_1:26379,redis_sentinel_2:26379,redis_sentinel_3:26379
       - SMTP_URL=smtp://smtp:1025
     ports:
       - 3000:3000
@@ -38,6 +39,7 @@ services:
       - BUNDLE_PATH=/bundle/vendor
       - DATABASE_URL=postgres://postgres:password@db/
       - REDIS_URL=redis://redis:6379
+      - REDIS_SENTINELS=redis_sentinel_1:26379,redis_sentinel_2:26379,redis_sentinel_3:26379
       - SMTP_URL=smtp://smtp:1025
     depends_on:
       - db
@@ -56,10 +58,30 @@ services:
       - postgres:/var/lib/postgresql/data
 
   redis:
-    image: library/redis
-    command: redis-server
+    image: redis:latest
     volumes:
-      - redis:/data
+      - "./tmp/redis_master:/data"
+
+  redis_slave:
+    image: redis:latest
+    command: redis-server --slaveof redis 6379
+    volumes:
+      - "./tmp/data-slave:/data"
+
+  redis_sentinel_1:
+    build:
+      context: ./docker/redis-sentinel/
+    command: redis-server /redis/sentinel.conf --sentinel
+
+  redis_sentinel_2:
+    build:
+      context: ./docker/redis-sentinel/
+    command: redis-server /redis/sentinel.conf --sentinel
+
+  redis_sentinel_3:
+    build:
+      context: ./docker/redis-sentinel/
+    command: redis-server /redis/sentinel.conf --sentinel
 
   smtp:
     image: mailhog/mailhog
@@ -69,4 +91,3 @@ services:
 volumes:
   bundle: {}
   postgres: {}
-  redis: {}
diff --git a/docker/redis-sentinel/Dockerfile b/docker/redis-sentinel/Dockerfile
new file mode 100644
index 000000000..a4921b6d5
--- /dev/null
+++ b/docker/redis-sentinel/Dockerfile
@@ -0,0 +1,11 @@
+FROM redis:latest
+
+RUN mkdir -p /redis
+
+WORKDIR /redis
+
+COPY sentinel.conf .
+
+RUN chown redis:redis /redis/sentinel.conf
+
+EXPOSE 26379
diff --git a/docker/redis-sentinel/sentinel-entrypoint.sh b/docker/redis-sentinel/sentinel-entrypoint.sh
new file mode 100755
index 000000000..1e6bed480
--- /dev/null
+++ b/docker/redis-sentinel/sentinel-entrypoint.sh
@@ -0,0 +1,7 @@
+#!/bin/sh
+
+sed -i "s/\$SENTINEL_QUORUM/$SENTINEL_QUORUM/g" /redis/sentinel.conf
+sed -i "s/\$SENTINEL_DOWN_AFTER/$SENTINEL_DOWN_AFTER/g" /redis/sentinel.conf
+sed -i "s/\$SENTINEL_FAILOVER/$SENTINEL_FAILOVER/g" /redis/sentinel.conf
+
+redis-server /redis/sentinel.conf --sentinel
diff --git a/docker/redis-sentinel/sentinel.conf b/docker/redis-sentinel/sentinel.conf
new file mode 100644
index 000000000..2112891f0
--- /dev/null
+++ b/docker/redis-sentinel/sentinel.conf
@@ -0,0 +1,4 @@
+port 26379
+
+sentinel monitor redis redis 6379 2
+sentinel resolve-hostnames yes
Clone this wiki locally