Skip to content

Commit 7157142

Browse files
committed
feat: enhance Docker Compose configuration for Redis with cluster and sentinel support
1 parent b7e6d3d commit 7157142

File tree

6 files changed

+136
-26
lines changed

6 files changed

+136
-26
lines changed

.github/workflows/testing.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ jobs:
2828
uses: hoverkraft-tech/compose-action@v2.0.2
2929
with:
3030
compose-file: './compose.yaml'
31+
compose-flags: '--profile cluster'
3132
up-flags: '-d'
3233
down-flags: '-v'
33-
- run: docker compose exec cluster-m1 redis-cli -a mycluster --cluster create 127.0.0.1:7380 127.0.0.1:7381 127.0.0.1:7382 --cluster-yes
3434
- run: pnpm install --frozen-lockfile
3535
- run: pnpm -F "@nestjs-labs/nestjs-redis" run lint
3636
- run: pnpm -F "@nestjs-labs/nestjs-redis" run test
37-
- run: pnpm -F "@nestjs-labs/nestjs-redis" run test:e2e
37+
# - run: pnpm -F "@nestjs-labs/nestjs-redis" run test:e2e

compose.yaml

Lines changed: 103 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,107 @@
1-
# sudo docker compose exec cluster-m1 redis-cli -a mycluster --cluster create 127.0.0.1:7380 127.0.0.1:7381 127.0.0.1:7382 --cluster-yes
1+
---
2+
# image tag 8.0-RC2-pre is the one matching the 8.0 GA release
3+
x-client-libs-stack-image: &client-libs-stack-image
4+
image: 'redislabs/client-libs-test:${CLIENT_LIBS_TEST_STACK_IMAGE_TAG:-8.0.2}'
5+
6+
x-client-libs-image: &client-libs-image
7+
image: 'redislabs/client-libs-test:${CLIENT_LIBS_TEST_IMAGE_TAG:-8.0.2}'
28

39
services:
410
redis:
5-
image: redis/redis-stack-server:latest
6-
network_mode: host
11+
<<: *client-libs-image
12+
container_name: redis-standalone
13+
environment:
14+
- TLS_ENABLED=yes
15+
- REDIS_CLUSTER=no
16+
- PORT=6379
17+
- TLS_PORT=6666
18+
command: ${REDIS_EXTRA_ARGS:---enable-debug-command yes --enable-module-command yes --tls-auth-clients optional --save ""}
19+
ports:
20+
- 6379:6379
21+
- 6666:6666 # TLS port
22+
volumes:
23+
- './dockers/standalone:/redis/work'
24+
profiles:
25+
- standalone
26+
- sentinel
27+
- replica
28+
- all-stack
29+
- all
30+
31+
replica:
32+
<<: *client-libs-image
33+
container_name: redis-replica
34+
depends_on:
35+
- redis
36+
environment:
37+
- TLS_ENABLED=no
38+
- REDIS_CLUSTER=no
39+
- PORT=6380
40+
command: ${REDIS_EXTRA_ARGS:---enable-debug-command yes --replicaof redis 6379 --protected-mode no --save ""}
41+
ports:
42+
- 6380:6380
43+
volumes:
44+
- './dockers/replica:/redis/work'
45+
profiles:
46+
- replica
47+
- all-stack
48+
- all
49+
50+
cluster:
51+
<<: *client-libs-image
52+
container_name: redis-cluster
53+
environment:
54+
- REDIS_CLUSTER=yes
55+
- NODES=6
56+
- REPLICAS=1
57+
- TLS_ENABLED=yes
58+
- PORT=16379
59+
- TLS_PORT=27379
60+
command: ${REDIS_EXTRA_ARGS:---enable-debug-command yes --enable-module-command yes --tls-auth-clients optional --save ""}
61+
ports:
62+
- '16379-16384:16379-16384'
63+
- '27379-27384:27379-27384'
64+
volumes:
65+
- './dockers/cluster:/redis/work'
66+
profiles:
67+
- cluster
68+
- all-stack
69+
- all
70+
71+
sentinel:
72+
<<: *client-libs-image
73+
container_name: redis-sentinel
74+
depends_on:
75+
- redis
76+
environment:
77+
- REDIS_CLUSTER=no
78+
- NODES=3
79+
- PORT=26379
80+
command: ${REDIS_EXTRA_ARGS:---sentinel}
81+
ports:
82+
- 26379:26379
83+
- 26380:26380
84+
- 26381:26381
85+
volumes:
86+
- './dockers/sentinel.conf:/redis/config-default/redis.conf'
87+
- './dockers/sentinel:/redis/work'
88+
profiles:
89+
- sentinel
90+
- all-stack
91+
- all
92+
93+
redis-stack:
94+
<<: *client-libs-stack-image
95+
container_name: redis-stack
796
environment:
8-
- 'REDIS_ARGS=--port 6380 --requirepass myredis --appendonly yes'
9-
cluster-m1:
10-
image: redis:latest
11-
network_mode: host
12-
command: 'redis-server --port 7380 --requirepass mycluster --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes'
13-
cluster-m2:
14-
image: redis:latest
15-
network_mode: host
16-
command: 'redis-server --port 7381 --requirepass mycluster --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes'
17-
cluster-m3:
18-
image: redis:latest
19-
network_mode: host
20-
command: 'redis-server --port 7382 --requirepass mycluster --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes'
97+
- REDIS_CLUSTER=no
98+
- PORT=6379
99+
command: ${REDIS_EXTRA_ARGS:---enable-debug-command yes --enable-module-command yes --save ""}
100+
ports:
101+
- 6479:6379
102+
volumes:
103+
- './dockers/redis-stack:/redis/work'
104+
profiles:
105+
- standalone
106+
- all-stack
107+
- all

dockers/sentinel.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
sentinel resolve-hostnames yes
2+
sentinel monitor redis-py-test redis 6379 2
3+
sentinel down-after-milliseconds redis-py-test 5000
4+
sentinel failover-timeout redis-py-test 60000
5+
sentinel parallel-syncs redis-py-test 1

examples/nest-redis-example/src/app.module.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,33 @@ import { AppService } from './app.service';
1515
// url: 'redis://localhost:6379',
1616
// }),
1717

18+
// use factory
19+
// RedisModule.forRootAsync({
20+
// imports: [ConfigModule],
21+
// useFactory: (configService: ConfigService) => {
22+
// const url: string = configService.get('REDIS_URL')!;
23+
// return {
24+
// url,
25+
// isGlobal: true,
26+
// };
27+
// },
28+
// inject: [ConfigService],
29+
// }),
30+
1831
// use factory
1932
RedisModule.forRootAsync({
2033
imports: [ConfigModule],
2134
useFactory: (configService: ConfigService) => {
22-
const url: string = configService.get('REDIS_URL')!;
23-
console.log(url);
35+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
36+
const _url: string = configService.get('REDIS_URL')!;
2437
return {
25-
url,
26-
isGlobal: true,
38+
cluster: {
39+
rootNodes: [
40+
{
41+
url: 'redis://127.0.0.1:16379',
42+
},
43+
],
44+
},
2745
};
2846
},
2947
inject: [ConfigService],

examples/nest-redis-example/src/app.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export class AppService {
1515
const redis = this.redisService.getClient();
1616
const info = await redis.info();
1717
return {
18+
isCluster: this.redisService.isClusterMode(),
1819
message: 'Redis connection successful',
1920
serverInfo: info,
2021
};

packages/node-redis/lib/redis.service.spec.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ describe('RedisService', () => {
1313
cluster: {
1414
rootNodes: [
1515
{
16-
url: 'redis://127.0.0.1:7380',
17-
password: 'mycluster'
18-
}
19-
]
16+
url: 'redis://127.0.0.1:16379',
17+
},
18+
],
2019
}
2120
})
2221
]

0 commit comments

Comments
 (0)