-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredis-cluster-debug.yml
222 lines (210 loc) · 5.72 KB
/
redis-cluster-debug.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#
# Redis Cluster service
#
apiVersion: v1
kind: Service
metadata:
annotations:
service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
name: redis-cluster
namespace: dipper
labels:
app: redis-cluster
spec:
ports:
- port: 6379
targetPort: 6379
name: client
- port: 16379
targetPort: 16379
name: gossip
clusterIP: None
#type: ClusterIP
selector:
app: redis-cluster
---
#
# Redis configuration file for clustered mode
#
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-cluster-config
namespace: dipper
data:
redis.conf: |+
cluster-enabled yes
cluster-require-full-coverage no
cluster-node-timeout 15000
cluster-config-file nodes.conf
cluster-migration-barrier 1
activerehashing yes
appendonly yes
appendfsync everysec
hash-max-ziplist-entries 128
hash-max-ziplist-value 1024
slave-read-only yes
stop-writes-on-bgsave-error no
loglevel notice
protected-mode no
databases 16
maxmemory-policy volatile-lru
maxmemory-samples 5
dbfilename dump.rdb
dir /var/lib/redis
bootstrap-pod.sh: |+
#!/bin/bash
set -e
PORT=6379
CLUSTER_IPS=""
mkdir -p /var/lib/redis
redis-server /conf/redis.conf &
sleep 1
wait
#chmod u+x /bin/peer-finder
#chmod u+x /usr/local/bin/redis-trib.rb
#chmod u+x /conf/meet-cluster.sh
#peer-finder -on-start '/conf/meet-cluster.sh' -service redis-cluster -ns $POD_NAMESPACE
sleep 1
wait
meet-cluster.sh: |+
#!/bin/bash
set -e
PORT=6379
CLUSTER_IPS=""
CLUSTER_BUILDER=$(getent hosts redis-cluster-5.redis-cluster.dipper.svc.cluster.local|awk {'print $1'})
PET_ORDINAL=$(cat /etc/podinfo/pod_name | rev | cut -d- -f1)
sleep 10
if [[ $PET_ORDINAL == 5 ]]; then
echo "creating cluster..."
sleep 10
while read -ra LINE; do
CLUSTER_IPS="${CLUSTER_IPS} $(getent hosts ${LINE} | cut -d ' ' -f1):${PORT}"
done
until redis-cli -h 127.0.0.1 ping; do sleep 1; done
echo yes | /usr/local/bin/redis-trib.rb create --replicas 1 ${CLUSTER_IPS}
elif [[ $PET_ORDINAL -gt 5 ]]; then
echo "add new node..."
sleep 10
if [[ $(expr $PET_ORDINAL % 2) == 0 ]]; then
echo "add as a master node..."
/usr/local/bin/redis-trib.rb add-node $(getent hosts $(hostname)|awk {'print $1'}):$PORT $CLUSTER_BUILDER:$PORT
else
echo "add slave for the new master..."
/usr/local/bin/redis-trib.rb add-node --slave $(getent hosts $(hostname)|awk {'print $1'}):$PORT $CLUSTER_BUILDER:$PORT
echo $CLUSTER_BUILDER
/usr/local/bin/redis-trib.rb rebalance --use-empty-masters $CLUSTER_BUILDER:$PORT
fi
else
echo "cluster too small"
fi
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: redis-cluster
namespace: dipper
spec:
serviceName: redis-cluster
replicas: 8
template:
metadata:
labels:
app: redis-cluster
annotations:
# NOTE: Init container must be idempotent
# Add a baked-in Redis config file that enables cluster mode
#pod.alpha.kubernetes.io/init-containers: '[
#]'
spec:
terminationGracePeriodSeconds: 10
#hostNetwork: true
containers:
- name: redis-cluster
image: dipperroy/redis:3.2.8-rb
ports:
- containerPort: 6379
name: client
- containerPort: 16379
name: gossip
command:
- sh
args:
- /conf/bootstrap-pod.sh
# Ensure that Redis is online before initializing the next node.
# TODO: Test that the cluster node is init'd properly.
readinessProbe:
exec:
command:
- sh
- -c
- "redis-cli -h $(hostname) ping"
initialDelaySeconds: 15
timeoutSeconds: 5
# Mark a node as down if Redis server stops running
livenessProbe:
exec:
command:
- sh
- -c
- "redis-cli -h $(hostname) ping"
initialDelaySeconds: 20
periodSeconds: 3
env:
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
volumeMounts:
#- name: data
# mountPath: /var/lib/redis
# readOnly: false
- name: conf
mountPath: /conf
readOnly: false
- name: podinfo
mountPath: /etc/podinfo
readOnly: false
volumes:
# Mount the PVC to storage redis data and info of cluster
# - name: "data"
# persistentVolumeClaim:
# claimName: "data"
# Insert our pre-baked Redis configuration file into /conf/redis.conf
- name: conf
configMap:
name: redis-cluster-config
items:
- key: redis.conf
path: redis.conf
- key: bootstrap-pod.sh
path: bootstrap-pod.sh
- key: meet-cluster.sh
path: meet-cluster.sh
# The init container will use this info to find cluster peers
- name: podinfo
downwardAPI:
items:
- path: "labels"
fieldRef:
fieldPath: metadata.labels
- path: "annotations"
fieldRef:
fieldPath: metadata.annotations
- path: "pod_name"
fieldRef:
fieldPath: metadata.name
- path: "pod_namespace"
fieldRef:
fieldPath: metadata.namespace
# volumeClaimTemplates:
# - metadata:
# name: data
# namespace: dipper
# spec:
# accessModes:
# - ReadWriteOnce
# resources:
# requests:
# storage: 1Gi
# storageClassName: ceph-rbd