[stable/redis-ha] redis-ha doesn't failover properly
Is this a request for help?:
This is notifying the project of a problem with the redis-ha chart where the default behavior makes it unable to failover. I've forked the chart and am working on fixing these issues on my own but don't have the ability to open source that code (yet).
Is this a BUG REPORT or FEATURE REQUEST? (choose one):
This is a Bug Report.
Version of Helm and Kubernetes:
> helm version
Client: &version.Version{SemVer:"v2.6.0", GitCommit:"5bc7c619f85d74702e810a8325e0a24f729aa11a", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.6.0", GitCommit:"5bc7c619f85d74702e810a8325e0a24f729aa11a", GitTreeState:"clean"}
> kubectl version
Client Version: version.Info{Major:"1", Minor:"7", GitVersion:"v1.7.6", GitCommit:"4bc5e7f9a6c25dc4c03d4d656f2cefd21540e28c", GitTreeState:"clean", BuildDate:"2017-09-14T06:55:55Z", GoVersion:"go1.8.3", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"6+", GitVersion:"v1.6.11-gke.0", GitCommit:"1870178bced7159908f06015f5bb0aa4ded6db76", GitTreeState:"clean", BuildDate:"2017-09-29T05:45:11Z", GoVersion:"go1.7.6", Compiler:"gc", Platform:"linux/amd64"}
Which chart:
redis-ha
What happened:
We tried to use this chart "out of the box" and found that it does not failover correctly and can lose all data. It also can take a long time to failover in default configuration when it does failover.
What you expected to happen:
Various things are expected to happen:
- If the master pod goes away (there are several ways to replicate this), then a slave should be promoted to master.
- If Kubernetes creates a new master pod, then it should sync to a promoted slave (and possibly become master).
How to reproduce it (as minimally and precisely as possible):
> helm install redis-ha --name foo
...
# k=kubectl
> k get pods
NAME READY STATUS RESTARTS AGE
foo-redis-ha-master-0 2/2 Running 0 34s
foo-redis-ha-sentinel-1737623389-1w5sl 1/1 Running 0 34s
foo-redis-ha-sentinel-1737623389-7b3jx 1/1 Running 0 34s
foo-redis-ha-sentinel-1737623389-zrfxf 1/1 Running 0 34s
foo-redis-ha-slave-0 1/1 Running 0 34s
...
> k delete pod foo-redis-ha-sentinel-1737623389-1w5sl
# now repeat the pod deletion on the newly launched sentinel pod 4 more times
...
# At this point, we have simulated losing 5 sentinel pods over time. The three
# remaining sentinels (two in independent pods, one in the master) know about
# a total of 8 sentinels, even though only 4 are running.
> k exec -ti foo-redis-ha-sentinel-1737623389-7b3jx -- bash -c 'cat sentinel.conf'
sentinel myid d7b3f54f334fd0721cca169cf7f792669c3558ab
sentinel monitor mymaster 10.4.50.15 6379 2
sentinel down-after-milliseconds mymaster 60000
sentinel config-epoch mymaster 0
# Generated by CONFIG REWRITE
port 26379
dir "/"
protected-mode no
sentinel leader-epoch mymaster 0
sentinel known-slave mymaster 10.4.51.24 6379
sentinel known-sentinel mymaster 10.4.51.27 26379 cc981e138859ba752a269a014bbfef2201382de5
sentinel known-sentinel mymaster 10.4.49.17 26379 54744636b1a4bb13bcc0e518cd4b9268d06c52fe
sentinel known-sentinel mymaster 10.4.51.26 26379 1ee83fac0d64c032e022492754e2bbe8e01d3a1e
sentinel known-sentinel mymaster 10.4.50.15 26379 26a9bcfaef60d5bc79bc52cc12f2d140c16b0761
sentinel known-sentinel mymaster 10.4.51.23 26379 96700e230e4cd86e28f25be33bfd340f215c0c00
sentinel known-sentinel mymaster 10.4.44.15 26379 24fbc2c84c73345c7917b5218d6ebcdf60aaceb2
sentinel known-sentinel mymaster 10.4.44.14 26379 bb7ac45221b560ef8ee1c1e56fa410a99c01bf83
sentinel known-sentinel mymaster 10.4.51.25 26379 dfa78ef5ab0cc5f9e68ca0454ec0a501ced1ae2c
# In another terminal, simulate the master becoming unavailable
> k exec -ti foo-redis-ha-master-0 bash
Defaulting container name to master.
Use 'kubectl describe pod/foo-redis-ha-master-0' to see all of the containers in this pod.
bash-4.3# redis-cli -p 6379 DEBUG sleep 1000
... # this hangs for 1000 seconds
# or alternately just delete the master pod
At this point, if one of the newer sentinels tried to become leader then failover might happen. If one of the older ones does, it won't be able to get a majority because so many sentinels are gone and no slave will be promoted. If the master then comes back (on the same IP address, as with container death), then it will become master again. I think (but haven't played with this part), if you're not using persistent volumes then you would lose all your data.
If the master pod instead just goes away entirely, the statefulset will helpfully create a new one, but the sentinels will never consider it master. The existing slave was never promoted to become master either (depending on which sentinel tried to be leader and how long it took) so the service is permanently unusable without intervention. The only intervention that makes everything available again is to tell all the sentinels to reset so they can fail over, but by the time this happens probably the slave hasn't synced from master in a long time and may not be a candidate for failover.
Side note: it also takes 6 minutes in the current (default) configuration between attempts to elect a leader for failover. It's normal for at least one leader election to fail during loss of a master. If the only slave then fails in that six minutes (or 12 -- I saw it take 25 minutes to failover) then you would certainly lose your data, though in our case the sentinels after that 25 minutes decided the slave wasn't promotable.
Anything else we need to know:
There are a few issues going on here some of which I've fixed in our private code (will share if I can!) and some I'm planning to fix using some examples I've found:
- The default timeouts are way too long. Our biggest availability concern (and I suspect many users) is just being able to not care about individual pods. For example, if you want to do a Kubernetes cluster version upgrade in GKE, it needs to be able to drain each node in sequence. So a shorter timeout to detect lost pods is more important than a longer timeout to allow recovery without failover after network split. I've modified the underlying docker container startup script to allow injecting the full configuration (master, slave and sentinel mode) via /etc/config as a mounted volume with default values taken out of the chart values file.
- Per redis sentinel docs, all existing sentinels should be told to "reset" when removing a sentinel (search that page for "Adding or Removing Sentinels") or slave instance. I've made this work by having an additional pod startup that watches for
DELETEDevents via the watch api (specifically/api/v1/watch/namespaces/{}/podswith a selector) and then sends a reset command to each sentinel in the endpoint ('/api/v1/namespaces/{}/endpoints/{service}) with a sleep in between (this is again per the docs). Another alternative is to have a sidecar in each sentinel pod to do this, but I also had to add an api proxy to get the credentials to work right and in any case, would prefer not to add roles to the sentinels if I can add it just to a single isolated pod that doesn't take even indirect external traffic. - Having master and slaves in different stateful sets makes it hard to work with. Failing back to the master doesn't happen because if a new instance of master is started (on death of old one), it doesn't start up in slave mode and so never syncs data and can never be promoted (if it comes back on the same IP, then the sentinel will tell it to slave). Other examples I've looked at (see below) just put them all in the same stateful set which allows (on first install of the chart) the first one to start to become master and the rest slave. I'm planning to do this.
- Labeling which pod is currently in each role (master or slave) by interrogating the sentinels may allow having services for write vs read-only endpoints. I might do this, but it's not necessary for our use cases (we always call thru a sentinel-aware client).
Some examples I used to figure things out:
- https://github.com/tarosky/k8s-redis-ha - uses fairly non-ideal interrogation of internal DNS names (using
dig) to figure out which pods are active, but assigns master/slave roles in a startup script for hte redis service itself. - https://github.com/CommercialTribe/kube-redis - uses a sidecar container to each redis-server that make
kubectlcalls at startup to figure out role and then monitors. - https://github.com/muchlearning/kubernetes-haproxy/blob/master/watch.py - example of how to monitor for pod changes (this case is somewhat different, but it was a good reference)
Note on server versions: when we first discovered this issue, it was actually on a cluster on version v1.7.8-gke.0 so I don't believe this is an issue with the kubernetes version.
Updating to note that I should be able to submit some PRs with fixes for parts of this, so hopefully I can do that soon. :)
@ludwick Thank you for this detailed write-up. We ran into all of the issues you described when testing out this chart. Looking forward to your PRs! I'm also interested in helping fix the above issues, so let me know if there's anything I can help work on.
Hi, we've been confronted to the same problems as @ludwick , especially the dead sentinels remaining in the list. Is there any news about a fix ?
@Paic I ended up moving off of the chart and I'm currently testing the redis operator from Spotahome with success.
https://github.com/spotahome/redis-operator/ https://product.spotahome.com/redis-operator-for-kubernetes-released-9c6775e7da9b
Life gets ahead of one!
We've been using our fork and improvements for a while, but I haven't packaged them up yet (I can contribute but it's not exactly a priority for me to do that work during "business" hours). Anyway, I'll try to put up a simple fork up even if it's not ready to get merged here.
@ludwick just a soft reminder, that it would be great if you can manage to get the fork up :)
Hey all - any news on this?
Okay this is completely not in the shape to "just merge". For one, it looks like the docker image used to run redis itself has had some changes. But in the interests of making sure others can re-use it, here's my changes.
First, the underlying docker image used to run the redis pods (primary & backups & sentinels) as updated to use a single statefulset. This change is here.
We wrote a sidecar monitoring container to keep track of when sentinels died because we discovered you could end up with stale sentinels that didn't exist but that contributed to quorum decisions. This repo is right here.
Finally, here are the chart changes. Note this branch is off a pretty old commit of this repo. I didn't want to try to resolve any conflicts and get it up-to-date right now (would just put this off even more).
About this version has been running for us in production since last November with few issues. There are still some theoretical but rare issues. For example, if you lose the current primary (master) pod and then very quickly (<30 seconds) lose too many backups, then it doesn't really recover properly and you have to just kick pods over (most common cause: loss of nodes the redis pods are on). This and other less than ideal choices could be fixed with more work, but it's rare in production for us (since we have enough nodes and redis pods are unlikely to land on the same node) though we have seen it in our usually constrained dev/test clusters occasionally. Practically speaking, we're likely to move to a cloud redis offering since we don't want to be experts on maintaining this kind of thing. :)
There's so many issues with the stable/redis-ha chart it really should be moved into incubator. I just ran into the same issues with no failover happening.
Just to name a few other big issues I've encountered as well: Race condition with masters #5441 Announced service no longer working #3197 PVCs possibilities #3403
I'm working on a major refactor of the chart loosely based off the PR #6706 to try to make it more production ready but still has some way to go.
@ssalaues thanks! I just hit an issue this morning as well. Master died and then I had 3 pods labeled redis-role: slave. I relabeled one of the pods to master and of course k8s relabeled it back to slave.
I'll give the redis-operator a shot, but look forward to your update
@camflan I just opened the PR #7323 to refactor the chart after refining for a couple weeks now. I'm hoping it's close to production ready now but any input from others that have experienced the same issues would be awesome!
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Any further update will cause the issue/pull request to no longer be considered stale. Thank you for your contributions.
This issue is being automatically closed due to inactivity.