Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Commit

Permalink
Redis clusters for dshop backends (#4483)
Browse files Browse the repository at this point in the history
* adds redis cluster for rinkeby backend

* adds mainnet cluster
  • Loading branch information
mikeshultz authored Jul 7, 2020
1 parent e6134e8 commit a862c4e
Show file tree
Hide file tree
Showing 9 changed files with 352 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ We truncate at 63 chars because some Kubernetes name fields are limited to this
{{- printf "%s-%s" .Release.Name "dshop-backend-mainnet" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{- define "dshopRedisMainnet.fullname" -}}
{{- printf "%s-%s" .Release.Name "dshop-redis-mainnet" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{- define "dshopBackendRinkeby.fullname" -}}
{{- printf "%s-%s" .Release.Name "dshop-backend-rinkeby" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{- define "dshopRedisRinkeby.fullname" -}}
{{- printf "%s-%s" .Release.Name "dshop-redis-rinkeby" | trunc 63 | trimSuffix "-" -}}
{{- end -}}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ spec:
name: {{ template "dshopBackendRinkeby.fullname" . }}
key: DATABASE_URL
- name: REDIS_URL
value: redis://localhost:6379/0
value: "redis://{{ template "dshopRedisRinkeby.fullname" . }}-0.{{ template "dshopRedisRinkeby.fullname" . }}.experimental.svc.cluster.local:6379/0"
- name: DSHOP_CACHE
value: /data/dshop
volumeMounts:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ template "dshopRedisMainnet.fullname" . }}
labels:
app: {{ template "dshopRedisMainnet.fullname" . }}
app.kubernetes.io/name: dshop
helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/version: {{ .Chart.AppVersion }}
app.kubernetes.io/component: backend
app.kubernetes.io/part-of: dshop-backend-mainnet
data:
redis.conf: |+
dir /data/redis
cluster-enabled yes
cluster-require-full-coverage no
cluster-node-timeout 15000
cluster-config-file nodes.conf
cluster-migration-barrier 1
# Restoring with RDB with appendonly https://stackoverflow.com/a/23233940/402412
appendonly yes
# Other cluster members need to be able to connect
protected-mode no
bootstrap-pod.sh: |+
#!/bin/sh
set -e
# Create the data dir
mkdir -p /data/redis
# Find which member of the Stateful Set this pod is running
ORDINAL=$(hostname | rev | cut -d- -f1)
# Only restore if the dump file exists and restore wasn't explicity disabled
if [[ -z "$DISABLE_RESTORE" && -f /data/redis/restore.rdb ]]; then
echo "Restore redis dump found. Restoring..."
# move original data over if it exists
[[ -f /data/redis/dump.rdb ]] && mv /data/redis/dump.rdb /data/redis/dump.rdb.backup
[[ -f /data/redis/appendonly.aof ]] && mv /data/redis/appendonly.aof /data/redis/appendonly.aof.backup
# Move restore dump to where it will be loaded
mv /data/redis/restore.rdb /data/redis/dump.rdb
fi
# Launch a server instance so we can configure it
redis-server /config/redis.conf &
# TODO: Wait until redis-server process is ready
sleep 1
if [ $ORDINAL = "0" ]; then
echo "This is the MASTER node"
# The first member of the cluster should control all slots initially
redis-cli cluster addslots $(seq 0 16383)
else
echo "This is the SLAVE node. Master: $STS_NAME-0.$STS_NAME.$NAMESPACE.svc.cluster.local"
# Other members of the cluster join as slaves
MASTER_IP=$(echo "print inet_ntoa(scalar(gethostbyname('$STS_NAME-0.$STS_NAME.$NAMESPACE.svc.cluster.local')))" | perl -MSocket)
redis-cli cluster meet $MASTER_IP 6379
sleep 1
# Become the slave of a random master node
MASTER_ID=$(redis-cli --csv cluster slots | cut -d, -f 5 | sed -e 's/^"//' -e 's/"$//')
redis-cli cluster replicate $MASTER_ID
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: v1
kind: Service
metadata:
name: {{ template "dshopRedisMainnet.fullname" . }}
labels:
app: {{ template "dshopRedisMainnet.fullname" . }}
app.kubernetes.io/name: dshop
helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/version: {{ .Chart.AppVersion }}
app.kubernetes.io/component: backend
app.kubernetes.io/part-of: dshop-backend-mainnet
spec:
clusterIP: None
selector:
app: {{ template "dshopRedisMainnet.fullname" . }}
ports:
- name: redis
port: 6379
- name: gossip
port: 16379
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: {{ template "dshopRedisMainnet.fullname" . }}
labels:
app: {{ template "dshopRedisMainnet.fullname" . }}
app.kubernetes.io/name: dshop
helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/version: {{ .Chart.AppVersion }}
app.kubernetes.io/component: backend
app.kubernetes.io/part-of: dshop-backend-mainnet
spec:
replicas: {{ default 2 .Values.dshopRedisMainnetReplicas }}
selector:
matchLabels:
app: {{ template "dshopRedisMainnet.fullname" . }}
serviceName: {{ template "dshopRedisMainnet.fullname" . }}
template:
metadata:
labels:
app: {{ template "dshopRedisMainnet.fullname" . }}
spec:
initContainers:
- name: configure-redis
image: "{{ .Values.dshopRedisImage }}:{{ .Values.dshopRedisImageTag }}"
command: ["/bin/bash", "/config/bootstrap-pod.sh"]
env:
- name: NAMESPACE
value: experimental
- name: STS_NAME
value: {{ template "dshopRedisMainnet.fullname" . }}
volumeMounts:
- mountPath: /data
name: {{ template "dshopRedisMainnet.fullname" . }}-redis
- mountPath: /config
name: config
readOnly: false
containers:
- name: redis
image: "{{ .Values.dshopRedisImage }}:{{ .Values.dshopRedisImageTag }}"
command: ["redis-server", "/config/redis.conf"]
volumeMounts:
- mountPath: /data
name: {{ template "dshopRedisMainnet.fullname" . }}-redis
- mountPath: /config
name: config
readOnly: false
port:
- name: redis
containerPort: 6379
- name: gossip
containerPort: 16379
volumes:
- name: config
configMap:
name: {{ template "dshopRedisMainnet.fullname" . }}
items:
- key: redis.conf
path: redis.conf
- key: bootstrap-pod.sh
path: bootstrap-pod.sh
volumeClaimTemplates:
- metadata:
name: {{ template "dshopRedisMainnet.fullname" . }}-redis
labels:
app: {{ template "dshopRedisMainnet.fullname" . }}
spec:
accessModes:
- ReadWriteOnce # Read-write for a single node only
storageClassName: "standard"
resources:
requests:
storage: {{ default "8Gi" .Values.dshopRedisStorageSize }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ template "dshopRedisRinkeby.fullname" . }}
labels:
app: {{ template "dshopRedisRinkeby.fullname" . }}
app.kubernetes.io/name: dshop
helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/version: {{ .Chart.AppVersion }}
app.kubernetes.io/component: backend
app.kubernetes.io/part-of: dshop-backend-rinkeby
data:
redis.conf: |+
dir /data/redis
cluster-enabled yes
cluster-require-full-coverage no
cluster-node-timeout 15000
cluster-config-file nodes.conf
cluster-migration-barrier 1
# Restoring with RDB with appendonly https://stackoverflow.com/a/23233940/402412
appendonly yes
# Other cluster members need to be able to connect
protected-mode no
bootstrap-pod.sh: |+
#!/bin/sh
set -e
# Create the data dir
mkdir -p /data/redis
# Find which member of the Stateful Set this pod is running
ORDINAL=$(hostname | rev | cut -d- -f1)
# Only restore if the dump file exists and restore wasn't explicity disabled
if [[ -z "$DISABLE_RESTORE" && -f /data/redis/restore.rdb ]]; then
echo "Restore redis dump found. Restoring..."
# move original data over if it exists
[[ -f /data/redis/dump.rdb ]] && mv /data/redis/dump.rdb /data/redis/dump.rdb.backup
[[ -f /data/redis/appendonly.aof ]] && mv /data/redis/appendonly.aof /data/redis/appendonly.aof.backup
# Move restore dump to where it will be loaded
mv /data/redis/restore.rdb /data/redis/dump.rdb
fi
# Launch a server instance so we can configure it
redis-server /config/redis.conf &
# TODO: Wait until redis-server process is ready
sleep 1
if [ $ORDINAL = "0" ]; then
echo "This is the MASTER node"
# The first member of the cluster should control all slots initially
redis-cli cluster addslots $(seq 0 16383)
else
echo "This is the SLAVE node. Master: $STS_NAME-0.$STS_NAME.$NAMESPACE.svc.cluster.local"
# Other members of the cluster join as slaves
MASTER_IP=$(echo "print inet_ntoa(scalar(gethostbyname('$STS_NAME-0.$STS_NAME.$NAMESPACE.svc.cluster.local')))" | perl -MSocket)
redis-cli cluster meet $MASTER_IP 6379
sleep 1
# Become the slave of a random master node
MASTER_ID=$(redis-cli --csv cluster slots | cut -d, -f 5 | sed -e 's/^"//' -e 's/"$//')
redis-cli cluster replicate $MASTER_ID
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: v1
kind: Service
metadata:
name: {{ template "dshopRedisRinkeby.fullname" . }}
labels:
app: {{ template "dshopRedisRinkeby.fullname" . }}
app.kubernetes.io/name: dshop
helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/version: {{ .Chart.AppVersion }}
app.kubernetes.io/component: backend
app.kubernetes.io/part-of: dshop-backend-rinkeby
spec:
clusterIP: None
selector:
app: {{ template "dshopRedisRinkeby.fullname" . }}
ports:
- name: redis
port: 6379
- name: gossip
port: 16379
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: {{ template "dshopRedisRinkeby.fullname" . }}
labels:
app: {{ template "dshopRedisRinkeby.fullname" . }}
app.kubernetes.io/name: dshop
helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/version: {{ .Chart.AppVersion }}
app.kubernetes.io/component: backend
app.kubernetes.io/part-of: dshop-backend-rinkeby
spec:
replicas: {{ default 2 .Values.dshopRedisRinkebyReplicas }}
selector:
matchLabels:
app: {{ template "dshopRedisRinkeby.fullname" . }}
serviceName: {{ template "dshopRedisRinkeby.fullname" . }}
template:
metadata:
labels:
app: {{ template "dshopRedisRinkeby.fullname" . }}
spec:
initContainers:
- name: configure-redis
image: "{{ .Values.dshopRedisImage }}:{{ .Values.dshopRedisImageTag }}"
command: ["/bin/bash", "/config/bootstrap-pod.sh"]
env:
- name: NAMESPACE
value: experimental
- name: STS_NAME
value: {{ template "dshopRedisRinkeby.fullname" . }}
volumeMounts:
- mountPath: /data
name: {{ template "dshopRedisRinkeby.fullname" . }}-redis
- mountPath: /config
name: config
readOnly: false
containers:
- name: redis
image: "{{ .Values.dshopRedisImage }}:{{ .Values.dshopRedisImageTag }}"
command: ["redis-server", "/config/redis.conf"]
volumeMounts:
- mountPath: /data
name: {{ template "dshopRedisRinkeby.fullname" . }}-redis
- mountPath: /config
name: config
readOnly: false
port:
- name: redis
containerPort: 6379
- name: gossip
containerPort: 16379
volumes:
- name: config
configMap:
name: {{ template "dshopRedisRinkeby.fullname" . }}
items:
- key: redis.conf
path: redis.conf
- key: bootstrap-pod.sh
path: bootstrap-pod.sh
volumeClaimTemplates:
- metadata:
name: {{ template "dshopRedisRinkeby.fullname" . }}-redis
labels:
app: {{ template "dshopRedisRinkeby.fullname" . }}
spec:
accessModes:
- ReadWriteOnce # Read-write for a single node only
storageClassName: "standard"
resources:
requests:
storage: {{ default "8Gi" .Values.dshopRedisStorageSize }}
4 changes: 3 additions & 1 deletion devops/kubernetes/charts/origin-experimental/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ containerRegistry: gcr.io/origin-214503

clusterIssuer: letsencrypt-prod


dshopBackendMainnetReplicas: 1
dshopBackendMainnetImage: dshop-backend
dshopBackendMainnetImageTag: mainnet

dshopBackendRinkebyReplicas: 1
dshopBackendRinkebyImage: dshop-backend
dshopBackendRinkebyImageTag: latest

dshopRedisImage: redis
dshopRedisImageTag: 6.0

0 comments on commit a862c4e

Please sign in to comment.