-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #335 from appuio/improve-redis-health-checks
[redis] Improve readiness check
- Loading branch information
Showing
14 changed files
with
250 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,5 @@ | |
.project | ||
.idea/ | ||
*.tmproj | ||
|
||
hack/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Redis Failover Experiments | ||
|
||
Scripts and random notes for redis sentinel failover experiments. | ||
|
||
## Files | ||
|
||
- `values-sentinel.yaml` Helm values for a simple redis cluster with sentinels enabled. | ||
- `monitor.sh` Reports status changes for all nodes in a cluster. | ||
- `fill_cluster.sh` Fills a redis cluster with random keys. Takes the id of the current master node as the first argument. | ||
- `random_keys.lua` Used for `fill_cluster.sh`. | ||
- `deny-redis-traffic-to-node-*-networkpolicy.yaml` Deny network traffic to a single node. | ||
|
||
## Resources | ||
|
||
>> Is it OK to wait until 'master_link_status' becomes 'up', and 'master_sync_in_progress' becomes '0' and 'master_last_io_seconds' becomes >= 0? | ||
> If you have no reason to believe something has gone haywire, this ought to tell you that the initial sync process has completed, yes. | ||
- https://groups.google.com/g/redis-db/c/JPvnyfUWx_Q?pli=1 | ||
|
||
- https://lzone.de/cheat-sheet/Redis%20Sentinel |
24 changes: 24 additions & 0 deletions
24
appuio/redis/hack/redis-failover-scripts/deny-redis-traffic-to-node-0-networkpolicy.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
apiVersion: networking.k8s.io/v1 | ||
kind: NetworkPolicy | ||
metadata: | ||
name: deny-redis-traffic-node-0 | ||
spec: | ||
podSelector: | ||
matchLabels: | ||
release: redis-test-cluster | ||
statefulset.kubernetes.io/pod-name: redis-test-cluster-node-0 | ||
# Exception for test-client | ||
ingress: | ||
- from: | ||
- podSelector: | ||
matchLabels: | ||
role: client | ||
egress: | ||
- to: | ||
- podSelector: | ||
matchLabels: | ||
role: client | ||
|
||
policyTypes: | ||
- Ingress | ||
- Egress |
24 changes: 24 additions & 0 deletions
24
appuio/redis/hack/redis-failover-scripts/deny-redis-traffic-to-node-1-networkpolicy.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
apiVersion: networking.k8s.io/v1 | ||
kind: NetworkPolicy | ||
metadata: | ||
name: deny-redis-traffic-node-1 | ||
spec: | ||
podSelector: | ||
matchLabels: | ||
release: redis-test-cluster | ||
statefulset.kubernetes.io/pod-name: redis-test-cluster-node-1 | ||
# Exception for test-client | ||
ingress: | ||
- from: | ||
- podSelector: | ||
matchLabels: | ||
role: client | ||
egress: | ||
- to: | ||
- podSelector: | ||
matchLabels: | ||
role: client | ||
|
||
policyTypes: | ||
- Ingress | ||
- Egress |
24 changes: 24 additions & 0 deletions
24
appuio/redis/hack/redis-failover-scripts/deny-redis-traffic-to-node-2-networkpolicy.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
apiVersion: networking.k8s.io/v1 | ||
kind: NetworkPolicy | ||
metadata: | ||
name: deny-redis-traffic-node-2 | ||
spec: | ||
podSelector: | ||
matchLabels: | ||
release: redis-test-cluster | ||
statefulset.kubernetes.io/pod-name: redis-test-cluster-node-2 | ||
# Exception for test-client | ||
ingress: | ||
- from: | ||
- podSelector: | ||
matchLabels: | ||
role: client | ||
egress: | ||
- to: | ||
- podSelector: | ||
matchLabels: | ||
role: client | ||
|
||
policyTypes: | ||
- Ingress | ||
- Egress |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/bin/bash | ||
|
||
set -eu | ||
|
||
node=redis-test-cluster-node-$1 | ||
|
||
kubectl cp scratchspace/random_keys.lua ${node}:/tmp/random_keys.lua -credis; | ||
|
||
for i in {0..1000} | ||
do | ||
kubectl exec ${node} -it -c redis -- 2>/dev/null redis-cli -h localhost -p 6379 -a $REDIS_PASSWORD --eval /tmp/random_keys.lua | ||
kubectl exec ${node} -it -c redis -- 2>/dev/null redis-cli -h localhost -p 6379 -a $REDIS_PASSWORD dbsize | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/bin/bash | ||
|
||
echo Monitoring nodes | ||
|
||
declare -A last_state=() | ||
|
||
while : | ||
do | ||
for i in {0..2} | ||
do | ||
node=redis-test-cluster-node-$i | ||
response=$( | ||
kubectl exec ${node} -c redis -- redis-cli -h localhost -p 6379 --no-auth-warning -a $REDIS_PASSWORD --eval /health/node_ready.lua 2>&1 | ||
) | ||
if [ "$response" != "${last_state[$node]}" ] | ||
then | ||
echo "### $(date +%R:%S): Node $node state changed" | ||
echo "'${last_state[$node]}' -> '$response'" | ||
last_state[$node]=$response | ||
fi | ||
done | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
local random_string = function(length) | ||
local res = "" | ||
for i = 1, length do | ||
res = res .. string.char(math.random(97, 122)) | ||
end | ||
return res | ||
end | ||
|
||
-- Seeds random | ||
-- https://redis.io/commands/eval#selective-replication-of-commands | ||
redis.replicate_commands() | ||
|
||
for _ = 1, 100000, 1 do | ||
local str = random_string(10) | ||
redis.call("SET", "RAND_"..str.."key", str); | ||
end | ||
|
||
return redis.status_reply("ok") |
26 changes: 26 additions & 0 deletions
26
appuio/redis/hack/redis-failover-scripts/values-sentinel.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
password: Fbma0DPVG7 | ||
cluster: | ||
slaveCount: 3 | ||
podDisruptionBudget: | ||
enabled: true | ||
minAvailable: "" | ||
maxUnavailable: 1 | ||
slave: | ||
podAnnotations: | ||
restart: Wed Aug 18 15:29:07 CEST 2021 | ||
persistence: | ||
size: 16Gi | ||
readinessProbe: | ||
initialDelaySeconds: 30 | ||
sentinel: | ||
enabled: true | ||
staticID: true | ||
downAfterMilliseconds: 3000 | ||
failoverTimeout: 5000 | ||
resources: | ||
requests: | ||
cpu: 100m | ||
memory: 32Mi | ||
limits: | ||
cpu: 200m | ||
memory: 64Mi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
local raw_state = redis.call("info", "replication") | ||
|
||
local split = function(text, delim) | ||
return text:gmatch("[^"..delim.."]+") | ||
end | ||
|
||
local collect = function(iter) | ||
local elements = {} | ||
for s in iter do table.insert(elements, s); end | ||
return elements | ||
end | ||
|
||
local has_prefix = function(text, prefix) | ||
return text:find(prefix, 1, true) == 1 | ||
end | ||
|
||
local replication_state = {} | ||
for s in split(raw_state, "\r\n") do | ||
(function(s) | ||
if has_prefix(s,"#") then | ||
return | ||
end | ||
|
||
local kv = collect(split(s, ":")) | ||
replication_state[kv[1]] = kv[2] | ||
end)(s) | ||
end | ||
|
||
local isSlave = replication_state["role"] == "slave" | ||
local isMasterLinkDown = replication_state["master_link_status"] == "down" | ||
local isSyncing = replication_state["master_sync_in_progress"] == "1" | ||
|
||
if isSlave and isMasterLinkDown then | ||
if isSyncing then | ||
return redis.error_reply("node is syncing") | ||
else | ||
return redis.error_reply("link to master down") | ||
end | ||
end | ||
|
||
return redis.status_reply("ready") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters