-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis-setup.sh
executable file
·275 lines (243 loc) · 7.92 KB
/
redis-setup.sh
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/bin/bash
# Function to generate SSL certificates
generate_ssl_certs() {
mkdir -p ssl
# Generate CA key and certificate
openssl genrsa -out ssl/ca.key 4096
openssl req -x509 -new -nodes -key ssl/ca.key -sha256 -days 1024 -out ssl/ca.crt -subj "/CN=Redis-Cluster-CA"
# Generate server key and certificate signed by the CA
openssl genrsa -out ssl/redis.key 2048
openssl req -new -key ssl/redis.key -out ssl/redis.csr -subj "/CN=redis-cluster"
openssl x509 -req -in ssl/redis.csr -CA ssl/ca.crt -CAkey ssl/ca.key -CAcreateserial -out ssl/redis.crt -days 365 -sha256
# Create Kubernetes secret with all certificates
kubectl create secret generic redis-certificates --from-file=ssl
}
generate_password() {
openssl rand -base64 32 | tr -d "=+/" | cut -c1-32
}
# Function to create Redis password secret
create_redis_password_secret() {
local redis_password=$1
kubectl create secret generic redis-password --from-literal=password=$redis_password
}
# Function to generate compute node names based on count
generate_compute_nodes() {
local num_nodes=$1
for i in $(seq 1 $num_nodes); do
echo "compute-node-$i"
done
}
# Function to randomly select unique compute nodes
select_unique_compute_nodes() {
local num_redis_nodes=$1
local total_compute_nodes=$2
if [ $total_compute_nodes -lt $num_redis_nodes ]; then
echo "Error: Not enough compute nodes available. Found $total_compute_nodes, need $num_redis_nodes." >&2
exit 1
fi
local compute_nodes=($(generate_compute_nodes $total_compute_nodes))
shuf -e "${compute_nodes[@]}" | head -n $num_redis_nodes
}
generate_redis_cluster_yaml() {
local num_redis_nodes=$1
local redis_password=$2
shift 2
local selected_nodes=("$@")
cat << EOF
---
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-cluster-config
data:
redis.conf: |
port 0
tls-port 6379
tls-cert-file /ssl/redis.crt
tls-key-file /ssl/redis.key
tls-ca-cert-file /ssl/ca.crt
tls-auth-clients no
tls-replication yes
tls-cluster yes
requirepass ${redis_password}
masterauth ${redis_password}
protected-mode yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
appendfsync everysec
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis-cluster
spec:
serviceName: redis-cluster
replicas: $num_redis_nodes
selector:
matchLabels:
app: redis-cluster
template:
metadata:
labels:
app: redis-cluster
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- redis-cluster
topologyKey: "kubernetes.io/hostname"
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
EOF
for node in "${selected_nodes[@]}"; do
echo " - $node"
done
cat << EOF
containers:
- name: redis
image: redis:6.2
ports:
- containerPort: 6379
name: tls
command: ["redis-server", "/conf/redis.conf"]
resources:
requests:
cpu: 500m
memory: 750Mi
limits:
cpu: 1
memory: 1Gi
volumeMounts:
- name: conf
mountPath: /conf
readOnly: false
- name: data
mountPath: /data
- name: ssl
mountPath: /ssl
readOnly: true
volumes:
- name: conf
configMap:
name: redis-cluster-config
items:
- key: redis.conf
path: redis.conf
- name: ssl
secret:
secretName: redis-certificates
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 2Gi
---
apiVersion: v1
kind: Service
metadata:
name: redis-cluster
spec:
selector:
app: redis-cluster
clusterIP: None
ports:
- port: 6379
targetPort: 6379
name: tls
EOF
}
# Function to wait for all Redis Cluster pods to be running
wait_for_redis_pods() {
echo "Waiting for all Redis Cluster pods to be running..."
while true; do
running_pods=$(kubectl get pods -l app=redis-cluster -o jsonpath='{range .items[*]}{.status.phase}{"\n"}{end}' | grep -c "Running")
total_pods=$(kubectl get pods -l app=redis-cluster -o jsonpath='{range .items[*]}{.status.phase}{"\n"}{end}' | wc -l)
if [ "$running_pods" -eq "$total_pods" ]; then
echo "All $total_pods Redis Cluster pods are now running."
break
else
echo "$running_pods out of $total_pods pods are running. Waiting..."
sleep 10
fi
done
}
# Function to check cluster status with retries
check_cluster_status() {
local max_retries=5
local retry_interval=10
local retry_count=0
while [ $retry_count -lt $max_retries ]; do
echo "Checking cluster status (attempt $((retry_count+1))/$max_retries)..."
status=$(kubectl exec -it redis-cluster-0 -- redis-cli --tls --cert /ssl/redis.crt --key /ssl/redis.key --cacert /ssl/ca.crt -a $redis_password cluster info | grep cluster_state | cut -d: -f2 | tr -d '[:space:]')
if [ "$status" = "ok" ]; then
echo "Cluster is now in OK state."
return 0
else
echo "Cluster state is still: $status. Waiting $retry_interval seconds before next check."
sleep $retry_interval
retry_count=$((retry_count+1))
fi
done
echo "Cluster failed to reach OK state after $max_retries attempts."
return 1
}
# Check if number of compute nodes argument is provided
if [ $# -ne 1 ]; then
echo "Usage: $0 <number_of_compute_nodes>"
exit 1
fi
total_compute_nodes=$1
# Validate input is a positive integer
if ! [[ "$total_compute_nodes" =~ ^[1-9][0-9]*$ ]]; then
echo "Error: Please provide a positive integer for the number of compute nodes."
exit 1
fi
# Main script execution
generate_ssl_certs
# Generate password
redis_password=$(generate_password)
# Create Redis password secret
create_redis_password_secret "$redis_password"
# Calculate the number of Redis nodes (minimum 4, maximum 10)
num_redis_nodes=$(( total_compute_nodes > 10 ? 10 : total_compute_nodes ))
echo "Number of Redis nodes to be created: $num_redis_nodes"
# Randomly select from all available compute nodes
readarray -t selected_nodes < <(select_unique_compute_nodes $num_redis_nodes $total_compute_nodes)
echo "Selected compute nodes: ${selected_nodes[*]}"
# Generate and apply the Redis Cluster YAML
generate_redis_cluster_yaml $num_redis_nodes "$redis_password" "${selected_nodes[@]}" > kubernetes-manifests/generated/redis-cluster.yaml
kubectl apply -f kubernetes-manifests/generated/redis-cluster.yaml
# Wait for all pods to be in the running state
wait_for_redis_pods
# Get the list of Redis node IPs
node_ips=$(kubectl get pods -l app=redis-cluster -o jsonpath='{range.items[*]}{.status.podIP}{" "}{end}')
# Create the Redis Cluster
echo "Creating Redis Cluster with $num_redis_nodes nodes..."
kubectl exec -it redis-cluster-0 -- redis-cli --cluster create \
$(echo $node_ips | sed -e 's/\([0-9.]*\)/\1:6379/g') \
--cluster-replicas $(( (num_redis_nodes - 3) / 3 )) \
--tls --cert /ssl/redis.crt --key /ssl/redis.key --cacert /ssl/ca.crt -a $redis_password
echo "Waiting for cluster to stabilize..."
sleep 10 # Give the cluster some time to stabilize
# Check cluster status with retries
if check_cluster_status; then
echo "Redis Cluster is now fully operational."
else
echo "Warning: Redis Cluster may not be fully operational. Please check manually."
fi
rm -r ssl/