How sentinel build Redis master-slave

Server Environment

1
2
3
4
5
$ cat /etc/issue
Ubuntu 16.04.2 LTS \n \l

$ redis-server --version
Redis server v=3.0.6 sha=00000000:0 malloc=jemalloc-3.6.0 bits=64

Target Redis cluster

1
2
3
4
redis-master //Port: 6300
redis-slave1 //Port: 6301
redis-slave2 //Port: 6302
redis-sentinel //Port: 16300

Install redis-server

1
$ sudo apt-get install -y redis-server

Install redis-sentinel

1
$ sudo apt-get install -y redis-sentinel

Make the directory and config file

1
2
3
4
5
6
$ cd /app/
$ mkdir -p redis-cluster/node1 redis-cluster/node2 redis-cluster/node3 redis-sentinel/node1
$ touch redis-cluster/node1/redis.conf
$ touch redis-cluster/node2/redis.conf
$ touch redis-cluster/node3/redis.conf
$ touch redis-sentinel/node1/sentinel.conf

Directory looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
$ tree app
app
|-- redis-cluster
| |-- node1
| | `-- redis.conf
| |-- node2
| | `-- redis.conf
| `-- node3
| `-- redis.conf
`-- redis-sentinel
`-- node1
`-- sentinel.conf

And the redis cluster config file content:

1
2
3
4
5
6
7
8
9
$ cat redis-cluster/node1/redis.conf
bind 127.0.0.1
port 6300
daemonize yes
slave-read-only yes
dir /app/redis-cluster/node1
pidfile /app/redis-cluster/node1/node.pid
logfile /app/redis-cluster/node1/node.log
dbfilename node.rdb

1
2
3
4
5
6
7
8
9
10
$ cat redis-cluster/node2/redis.conf
bind 127.0.0.1
port 6301
slaveof 127.0.0.1 6300
daemonize yes
slave-read-only yes
dir /app/redis-cluster/node2
pidfile /app/redis-cluster/node2/node.pid
logfile /app/redis-cluster/node2/node.log
dbfilename node.rdb
1
2
3
4
5
6
7
8
9
10
$ cat redis-cluster/node3/redis.conf
bind 127.0.0.1
port 6302
slaveof 127.0.0.1 6300
daemonize yes
slave-read-only yes
dir /app/redis-cluster/node3
pidfile /app/redis-cluster/node3/node.pid
logfile /app/redis-cluster/node3/node.log
dbfilename node.rdb

Redis sentinel config file content:

1
2
3
4
5
6
7
8
9
10
11
$ cat redis-sentinel/node1/sentinel.conf
bind 127.0.0.1
port 16300
daemonize yes
dir /app/redis-sentinel/node1
pidfile /app/redis-sentinel/node1/s.pid
logfile /app/redis-sentinel/node1/s.log

sentinel monitor redis-cluster 127.0.0.1 6300 1
sentinel down-after-milliseconds redis-cluster 5000
sentinel failover-timeout redis-cluster 10000

  • Look at line 9:
    “redis-cluster” is redis cluster name which master node ‘s ip is 127.0.0.1 and port is 6300. The last param “1” means that one sentinel can elect the master redis node.

  • Line 10 means that if no response in 5 seconds, the election will start

Start Redis server and Sentinel

1
2
3
4
$ redis-server redis-cluster/node1/redis.conf 
$ redis-server redis-cluster/node2/redis.conf
$ redis-server redis-cluster/node3/redis.conf
$ redis-sentinel redis-sentinel/node1/sentinel.conf
1
2
3
4
5
$ ps aux | grep redis
root 109 0.0 0.1 37220 3528 ? Ssl 06:43 0:00 redis-server 127.0.0.1:6300
root 113 0.0 0.1 37220 3500 ? Ssl 06:43 0:00 redis-server 127.0.0.1:6301
root 118 0.0 0.1 37220 3428 ? Ssl 06:43 0:00 redis-server 127.0.0.1:6302
root 125 0.2 0.1 37220 3420 ? Ssl 06:44 0:00 redis-sentinel 127.0.0.1:16300 [sentinel]

Check the Redis cluster info

1
2
3
4
5
6
7
$ redis-cli -p 6300 info | grep -A 5 Replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6301,state=online,offset=32382,lag=1
slave1:ip=127.0.0.1,port=6302,state=online,offset=32382,lag=1
master_repl_offset:32382

Cluster has been build success.

Is Node automatically selected as the master node

Halt the master node1

1
$ redis-cli -p 6300 debug segfault

Check the process

1
2
3
4
$ ps aux | grep redis
root 113 0.0 0.1 37220 3500 ? Ssl 06:43 0:00 redis-server 127.0.0.1:6301
root 118 0.0 0.1 37220 3428 ? Ssl 06:43 0:00 redis-server 127.0.0.1:6302
root 125 0.2 0.1 37220 3420 ? Ssl 06:44 0:01 redis-sentinel 127.0.0.1:16300 [sentinel]

Then Check the redis cluster status

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ redis-cli -p 6301 info | grep -A 5 Replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6302
master_link_status:up
master_last_io_seconds_ago:1

$ redis-cli -p 6302 info | grep -A 5 Replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6301,state=online,offset=2840,lag=1
master_repl_offset:2840
repl_backlog_active:1

  • Slave’s election will mainly evaluate slave in the following reason:
    1) Slave priority
    2) Process ID

Get the master node by sentinel

1
2
3
$ redis-cli -p 16300 sentinel get-master-addr-by-name redis-cluster
1) "127.0.0.1"
2) "6302"

Ok, we can see the node with port 6302 has been elected as the master node.

Thank you for reading.
This post is copyrighted by Liyuliang’s Blog.
If reproduced, please indicate the source: Liyuliang’s Blog
This blog uses Creative Commons Attribution-NonCommercial-Share-Sharing 4.0 International License Agreement to license.


Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×