diff --git a/charts/app/templates/configmap.yaml b/charts/app/templates/configmap.yaml index 5b33ca14..f82f3e94 100644 --- a/charts/app/templates/configmap.yaml +++ b/charts/app/templates/configmap.yaml @@ -14,6 +14,17 @@ data: {{- include "app.configValue" ( dict "Key" "APP_COOKIE_POLICY" "Value" .cookiePolicy ) }} {{- end }} + # redis settings + {{- if .Values.redis.enabled }} + {{- if .Values.redis.sentinel.enabled }} + APP_REDIS_SENTINEL: "true" + {{- end }} + {{- else }} + {{- with .Values.app.redis }} + {{- include "app.configValue" ( dict "Key" "APP_REDIS_SENTINEL" "Value" .sentinel ) }} + {{- end }} + {{- end }} + # session settings {{- with .Values.app.session }} {{- include "app.configValue" ( dict "Key" "APP_SESSION_LIFETIME" "Value" .lifetime ) }} diff --git a/charts/app/templates/secrets.yaml b/charts/app/templates/secrets.yaml index 0c002a29..5254b320 100644 --- a/charts/app/templates/secrets.yaml +++ b/charts/app/templates/secrets.yaml @@ -9,8 +9,13 @@ type: Opaque data: # redis settings {{- if .Values.redis.enabled }} - # provisioned redis settings + {{- if .Values.redis.sentinel.enabled }} + # provisioned redis settings for standalone + APP_REDIS_URI: {{ printf "redis://%s:26379" .Release.Name | b64enc }} + {{- else }} + # provisioned redis settings for standalone APP_REDIS_URI: {{ printf "redis://%s-redis-master:6379" .Release.Name | b64enc }} + {{- end }} {{- else }} # custom redis settings {{- with .Values.app.redis }} diff --git a/charts/app/values.yaml b/charts/app/values.yaml index a7fad7cc..201d12e4 100644 --- a/charts/app/values.yaml +++ b/charts/app/values.yaml @@ -57,6 +57,7 @@ app: # redis settings redis: uri: '' + sentinel: '' # database settings database: @@ -146,12 +147,19 @@ redis: # should the redis chart be deployed enabled: true - # architecture type - architecture: standalone + # use sentinel + sentinel: + enabled: true + + # disable persistence on replica as we rely on sentinels + replica: + persistence: + enabled: false auth: # no password/authentication enabled: false + sentinel: false # MongoDB settings # when using this sub chart diff --git a/docs/settings.md b/docs/settings.md index 3aa61061..8278f384 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -25,9 +25,10 @@ It's recommended to disable compression if you may delegate it to a reverse prox ## Redis configuration -| Name | Type | Default | Comment | -| ------------- | ------ | ---------------------- | ----------------------- | -| APP_REDIS_URI | String | redis://127.0.0.1:6379 | Redis connection string | +| Name | Type | Default | Comment | +| ------------------ | ------- | ---------------------- | ----------------------- | +| APP_REDIS_URI | String | redis://127.0.0.1:6379 | Redis connection string | +| APP_REDIS_SENTINEL | Boolean | false | Use sentinels | ## Session configuration diff --git a/src/server/core/config.ts b/src/server/core/config.ts index a949cabf..e2d415f5 100644 --- a/src/server/core/config.ts +++ b/src/server/core/config.ts @@ -58,6 +58,7 @@ const config = { redis: { uri: getString(getPrefix('REDIS_URI'), 'redis://127.0.0.1:6379'), + sentinel: getBoolean(getPrefix('REDIS_SENTINEL'), false), }, session: { diff --git a/src/server/core/pubSub.ts b/src/server/core/pubSub.ts index d5a3bf6c..1307caf4 100644 --- a/src/server/core/pubSub.ts +++ b/src/server/core/pubSub.ts @@ -1,7 +1,6 @@ import { EJSON, Document } from 'bson'; import { RedisPubSub } from 'graphql-redis-subscriptions'; -import IORedis from 'ioredis'; -import config from './config'; +import { createRedisInstance } from './redis'; export const getPubSub = (): RedisPubSub => { if (global.pubSub) { @@ -9,8 +8,8 @@ export const getPubSub = (): RedisPubSub => { } global.pubSub = new RedisPubSub({ - publisher: new IORedis(config.redis.uri, { enableOfflineQueue: false }), - subscriber: new IORedis(config.redis.uri, { enableOfflineQueue: false }), + publisher: createRedisInstance(), + subscriber: createRedisInstance(), serializer: (source: Document): string => JSON.stringify(EJSON.serialize(source)), deserializer: (source: string): Document => EJSON.deserialize(JSON.parse(source)) as Document, }); diff --git a/src/server/core/redis.ts b/src/server/core/redis.ts index 485f04a9..cf20010d 100644 --- a/src/server/core/redis.ts +++ b/src/server/core/redis.ts @@ -1,12 +1,33 @@ -import IORedis, { Redis } from 'ioredis'; +import IORedis, { Redis, RedisOptions } from 'ioredis'; import config from './config'; +export const createRedisInstance = () => { + const details = new URL(config.redis.uri); + + const options: RedisOptions = { + enableOfflineQueue: false, + }; + + if (config.redis.sentinel) { + options.sentinels = [{ host: details.hostname, port: parseInt(details.port, 10) }]; + options.sentinelUsername = details.username || undefined; + options.sentinelPassword = details.password || undefined; + } else { + options.host = details.hostname; + options.port = parseInt(details.port, 10); + options.username = details.username || undefined; + options.password = details.password || undefined; + } + + return new IORedis(options); +}; + const getRedisInstance = (): Redis => { if (global.redis) { return global.redis; } - global.redis = new IORedis(config.redis.uri, { enableOfflineQueue: false }); + global.redis = createRedisInstance(); return global.redis; };