Created
January 24, 2021 20:26
-
-
Save nickadam/1d1a7812fbe20dcd71c36020fa81b58e to your computer and use it in GitHub Desktop.
Launches a cluster with 3 nodes and 3 sentinel instances to demonstrate how automated redis failover and recovery behaves.
This file contains hidden or 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
| #!/bin/bash | |
| PASSWORD="badpassword" | |
| create_config(){ | |
| cat <<EOF > "${1}" | |
| port 555"${2}" | |
| sentinel monitor mycluster 127.0.0.1 7770 2 | |
| sentinel down-after-milliseconds mycluster 5000 | |
| sentinel failover-timeout mycluster 60000 | |
| sentinel parallel-syncs mycluster 1 | |
| sentinel auth-pass mycluster "${PASSWORD}" | |
| EOF | |
| } | |
| # start up three instances of both redis and sentinel using 0,1,2 for the configs and stuff | |
| for n in {0..2} | |
| do | |
| # create a directory for each server's config file | |
| instance_dir="${PWD}"/redis-local-"${n}" | |
| mkdir "${instance_dir}" 2>/dev/null | |
| # create a redis-conf for each instance so dumb.rdb is saved to a unique location | |
| echo dir "${instance_dir}" > "${instance_dir}"/redis.conf | |
| # each redis instance needs a unique port on localhost | |
| echo port 777"${n}" >> "${instance_dir}"/redis.conf | |
| # use append only mode | |
| echo appendonly yes >> "${instance_dir}"/redis.conf | |
| # set a password for authenticating the replicas to the primary | |
| echo masterauth "${PASSWORD}" >> "${instance_dir}"/redis.conf | |
| # set password to protect all remote operations | |
| echo 'user default on +@all ~* >'"${PASSWORD}" >> "${instance_dir}"/redis.conf | |
| if [ ! -f "${instance_dir}"/sentinel.conf ] | |
| then | |
| # when sentinel.conf doesn't exist start redis as primary or replica's connected to primary | |
| if [ "${n}" == "0" ] | |
| then | |
| # start primary | |
| redis-server "${instance_dir}"/redis.conf &>"${instance_dir}"/redis.log & | |
| else | |
| # start replica | |
| redis-server "${instance_dir}"/redis.conf --replicaof 127.0.0.1 7770 &>"${instance_dir}"/redis.log & | |
| fi | |
| # create a sentinel config file for each instance on first run only | |
| create_config "${instance_dir}"/sentinel.conf "${n}" | |
| # sentinel.conf exists so just start redis | |
| else | |
| redis-server "${instance_dir}"/redis.conf &>"${instance_dir}"/redis.log & | |
| fi | |
| # start sentinel | |
| redis-sentinel "${instance_dir}"/sentinel.conf &>"${instance_dir}"/sentinel.log & | |
| done | |
| wait |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment