• Stars
    star
    114
  • Rank 308,031 (Top 7 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created about 10 years ago
  • Updated almost 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Redis Sentinel high availabillity daemon

redishappy

Build Status Coverage Status

One method of providing a highly available Redis service is to deploy using Redis Sentinel.

Redis Sentinel monitors your Redis cluster and on detecting failure, promotes a slave to become the new master. RedisHappy provides a daemon to monitor for this promotion and to tell the outside world that this has happened.

Currently we support HAProxy with redishappy-haproxy and Consul with redishappy-consul.

Features

  • Automatic discovery and healthchecking of Redis Sentinels.
  • Extensible to support various service discovery mechanisims
  • Developed in Golang, clean deployment with no additional dependencies.
  • Read-only RestAPI.
  • Syslog integration.
  • RPM and Deb packages available.
  • Support for Linux, Solaris and Windows
  • Puppet module available.

Deployment

Redishappy ships in two forms redishappy-haproxy and redishappy-consul.

redishappy-haproxy

redishappy-haproxy updates HAProxy's configuration file on Redis master promotion and then reloads the HAProxy configuration file. The reload maintains current connections.

redishappy_haproxy

The redishappy daemon is installed on the same machine as HAProxy and runs with correct user rights to interact with HAProxy. Multiple instance of HAProxy/redishappy-haproxy can be deployed and operate seperatly.

redishappy-consul

redishappy-consul updates entries in a Consul instance on Redis master promotion.

redishappy_consul

FAQ

Q. Why - I thought in the modern age Redis clients should be Sentinel aware? They should connect to the correct Redis instance on failover.

A. Some do, some don't. Some it seems to be an eternal 'work in progress'. Rather than fixing all of the clients we needed to work correctly with Sentinel, RedisHappy was built upon the fact that all of the clients I have tested are great at connecting to a single address.

Operations teams also need to support legacy applications and libraries - adding redishappy, Sentinels and HAProxy can help provide a HA enviroment for Redis backed applications.

Q. Why - This article suggests that HAProxy can healthcheck Redis instances quite fine by itself.

A. Yes. It can do. But not reliably... I'll explain.

Suppose we have this setup. R1 and R2 are redis instances, S1,S2,S3 are Sentinel instances, H1 and H2 are HAProxy instances.

    R1,R2
    S1, S2, S3
    H1, H2
  • Life is good - R1 and R2 are in a master slave configuration, H1 and H2 correctly identify R1 as the master
    R1      R2
    M  ---- S
    ^
    |
    ---------
    |       |
    H1      H2
  • Disaster! - R1 dies or is partitioned but don't fear R2 is now the "master". Day saved!
    *       R2
            M
            ^
            |
    ---------
    |       |
    H1      H2
  • Disaster! - R1 comes back online and announces itself as a "master". Both R1 and R2 are now accepting writes, as HAProxy's healthcheck identifies both as online.
    R1        R2
    M       M
    ^        ^
    |       |
    ---------
    |       |
    H1      H2
  • R1 is made the "slave" of R2. Everything is ok now, except for the writes that R1 accepted which are lost forever.
    R1      R2
    S ----- M
            ^
            |
    ---------
    |       |
    H1      H2

When a Redis instance is started and stopped it initially announces itself as a "master". It will some time later be made a "slave" but in the meantime accept writes which will be lost when it is correctly made a slave.

RedisHappy attempts to avoid this failure mode by only presenting the correct server to HAProxy or any other service once it is confirmed as a "master". We assume clients will either block or fail until the master is online again.

Building

Using Vagrant

The provided vagrant file creates a virtual machine with all of the dependancies to build redishappy, smoke test it, and build the deb and rpm packages.

vagrant up

The packages are automatically built to - $GOPATH/src/github.com/mdevilliers/redishappy/

The vagrant box also installs HAProxy, Docker and https://github.com/mdevilliers/docker-rediscluster for manual testing.

Download and build.

Install golang 1.4 +

go get github.com/mdevilliers/redishappy

cd $GOPATH/src/github.com/mdevilliers/redishappy

go get github.com/tools/godep
go get github.com/axw/gocov/gocov
go get github.com/mattn/goveralls
go get golang.org/x/tools/cmd/cover
go get golang.org/x/tools/cmd/goimports

godep restore

build/ci.sh

Defaults

Installing using the deb and rpm packages will set the following defaults -

  • Installs to /usr/bin/redis-haproxy
  • Configuration to /etc/redishappy-haproxy
  • Logs to file in /var/log/redishappy-haproxy
  • Warnings, Errors go to syslog

Configuration

Example configurations can be found in the main folders

Definitions for the elements

{
  // OPTIONAL - TCP Keep-Alive time (in seconds)
  "SentinelTCPKeepAlive" : 0
  // REQUIRED - needs to contain at least one logical cluster
  "Clusters" :[
  {
    "Name" : "testing", // logical name of Redis cluster
    "ExternalPort" : 6379 // port to expose for the cluster via HAProxy
  }],
  // REQUIRED - needs to contain the details of at least one cluster
  // redishappy will discover additional sentinels as they come online
  "Sentinels" : [
      {"Host" : "172.17.42.1", "Port" : 26377}
  ],
  // OPTIONAL for running redishappy-haproxy
  "HAProxy" :
    {
      // REQUIRED - absolute path to the template file
      "TemplatePath": "/var/redishappy/haproxy_template.cfg",
      // REQUIRED - absolute path to HAProxy's config file
      "OutputPath": "/etc/haproxy/haproxy.cfg",
      // REQUIRED - command to run to reload the config file on changes
      "ReloadCommand": "haproxy -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid -sf $(cat /var/run/haproxy.pid)"
    },
    // OPTIONAL for running redishappy-consul
   "Consul" : {
       // REQUIRED - path to Consul instance
    "Address" : "127.0.0.1:8500",
    // REQUIRED - for each cluster in the main config there should be a defined service
      "Services" : [
        {
            // REQUIRED - should match a name of a Cluster in the main config
            "Cluster" : "testing",
            // REQUIRED - logical name for the node
            "Node" : "redis-1",
            // REQUIRED - logical name for the data centre
            "Datacenter": "dc1",
            // REQUIRED - tags for the service
            "tags" : [ "redis", "master", "anothertag"]
          }
      ]
  }
}

Or you can configure with the following environmental variables -

Environment Variable Example Notes
REDISHAPPY_CLUSTERS clustername:6379 multiple values can be ; seperated
REDISHAPPY_SENTINELS ip_name:26377 multiple values can be ; seperated
REDISHAPPY_HAPROXY_TEMPLATE_PATH string, see config file for example
REDISHAPPY_HAPROXY_OUTPUT_PATH string, see config file for example
REDISHAPPY_HAPROXY_RELOAD_CMD string, see config file for example

API

RedisHappy provides a readonly API on port 8000. You can change the port by specifying a PORT environment variable. It is also possible to use a BIND environment variable if you wish to bind to other interfaces etc.

  • GET /api/ping - will reply "pong" if running
  • GET /api/configuration - displays the start up configuration
  • GET /api/sentinels - displays the sentinels being currently monitored and their current states
  • GET /api/topology - displays the current view of the Redis clusters, their master and their host/ip addresses

redishappy-haproxy provides the following additional read only apis

  • GET /api/template - displays the current template file
  • GET /api/haproxy - displays the rendered HAProxy file

Hacking

Running the following script will gofmt, govet, run the tests, build all of the executables.

build/ci.sh

Benchmarks

Find some benchmarking information here.

Testing with Docker

https://github.com/mdevilliers/docker-rediscluster

Will start up a master/slave, 3 sentinel redis cluster for testing.

Thanks

Big thanks to

Copyright and license

Code and documentation copyright 2017 Mark deVilliers. Code released under the Apache 2.0 license.

More Repositories

1

SignalR.RabbitMq

MessageBus implementation using RabbitMq as the backing store.
JavaScript
90
star
2

docker-rediscluster

Docker script to set up a disposable redis cluster for testing sentinel failover.
Shell
64
star
3

Toxiproxy.Net

Toxiproxy.Net is a .Net client for Shopify's Toxiproxy. Toxiproxy is a proxy to test your system by simulating network failure.
C#
52
star
4

vagrant-redis

Vagrant script to start a redis server
Shell
50
star
5

erlang-mesos

An erlang binding for mesos - http://mesos.apache.org/
Erlang
33
star
6

golang-k8s-template

Golang template to kickstart a project that deploys to Kubernetes
Go
7
star
7

kubernetes-bestiary

Go
7
star
8

rabbitmq-stamp

RabbitMQ Exchange to provide unique incrementing identifiers for messages sent to it.
Makefile
7
star
9

kubernetes-rabbitmq-cluster

Go
7
star
10

ring_buffer

An erlang ring buffer backed by ets or dts tables
Erlang
4
star
11

fastly-cli

Opinionated CLI to aid me in my day-to-day tasks with Fastly
Go
4
star
12

cache-service

Cache server backed by Redis with a GRPC API
Go
3
star
13

org-scrounger

Go
3
star
14

merkxx

Mesos framework example using the erlang-mesos binding
Erlang
3
star
15

vagrant-erlang-development-environment

Vagrant script for creating a development environment for erlang development.
Ruby
3
star
16

lambda-deployer

Automate the deployment of AWS Lambda functions easily from either a developers machine or CI system.
Go
2
star
17

dtree

Go
2
star
18

ckeditor-nuget

Holds the scripts, nuget definition files for ckeditor
Batchfile
2
star
19

vagrant-mesos-development-environment

Vagrant script to set up a mesos development environment
Shell
2
star
20

erlang-bestiary

Erlang snippets, tests and experiments
Erlang
1
star
21

open-telemetery-golang-bestiary

Go
1
star
22

Orleans.StorageProvider.Redis

Orleans Storage Provider backed with Redis
C#
1
star
23

ksqldb-go

Go
1
star
24

ports-ahoy

Go
1
star
25

zenroom-demo

Lua
1
star
26

reasonml-bestiary

OCaml
1
star
27

aws-tools

Python
1
star
28

vagrant-teamcity-environment

Vagrant script for creating an installation of team city
Ruby
1
star
29

vagrant-rabbitmq-development-environment

Vagrant script for creating a development environment for RabbitMQ development.
Shell
1
star