38 Practical Redis Sentinel Mode Part 2

38 Practical Redis Sentinel Mode Part 2 #

In the previous article, we introduced the setup and operation principle of Redis Sentinel. In this article, we will focus on the command operations and code implementation of Sentinel.

Sentinel Command Operations #

To use Sentinel, we need to connect to the Sentinel server. Just like connecting to the Redis server, we can use redis-cli to connect to Sentinel. The command is as follows:

[@iZ2ze0nc5n41zomzyqtksmZ:~]$ redis-cli -h 127.0.0.1 -p 26379 -a pwd654321
127.0.0.1:26379> ping
PONG

Where:

  • After -h, enter the IP address of the Sentinel.
  • After -p, enter the port number of the Sentinel. The default port number is 26379.
  • After -a, enter the password.

The port number of Sentinel can be configured in the sentinel.conf file by setting the port option.

Note: Sentinel can monitor multiple master nodes, not just one server. To monitor multiple master nodes, you just need to configure multiple sentinel monitor master-name ip port quorum statements in the configuration file. We use master-name to distinguish different master nodes.

Querying the Information of All Monitored Master Servers #

127.0.0.1:26379> sentinel masters
1)  1) "name"
    2) "mymaster"
    3) "ip"
    4) "127.0.0.1"
    5) "port"
    6) "6377"
    7) "runid"
    8) "eb3552c6fc8974f91466c4ada90fe23ef30fd89c"
    9) "flags"
   10) "master"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "400"
   19) "last-ping-reply"
   20) "400"
   21) "down-after-milliseconds"
   22) "30000"
   23) "info-refresh"
   24) "5731"
   25) "role-reported"
   26) "master"
   27) "role-reported-time"
   28) "75963321"
   29) "config-epoch"
   30) "7"
   31) "num-slaves"
   32) "2"
   33) "num-other-sentinels"
   34) "1"
   35) "quorum"
   36) "2"
   37) "failover-timeout"
   38) "180000"
   39) "parallel-syncs"
   40) "1"

Syntax:

sentinel masters

Since our configured Sentinel only monitors one master server, there is information only for one server.

Querying the Information of a Specific Master Node #

127.0.0.1:26379> sentinel master mymaster
 1) "name"
 2) "mymaster"
 3) "ip"
 4) "127.0.0.1"
 5) "port"
 6) "6377"
 7) "runid"
 8) "eb3552c6fc8974f91466c4ada90fe23ef30fd89c"
 9) "flags"
10) "master"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "250"
19) "last-ping-reply"
20) "250"
21) "down-after-milliseconds"
22) "30000"
23) "info-refresh"
24) "8191"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "76096303"
29) "config-epoch"
30) "7"
31) "num-slaves"
32) "2"
33) "num-other-sentinels"
34) "1"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "180000"
39) "parallel-syncs"
40) "1"

Syntax:

sentinel master master-name

View the IP and port of a master node #

127.0.0.1:26379> sentinel get-master-addr-by-name mymaster
1) "127.0.0.1"
2) "6377"

Syntax:

sentinel get-master-addr-by-name master-name

Retrieve information about slave nodes #

127.0.0.1:26379> sentinel slaves mymaster # Method 1
1)  1) "name"
    2) "127.0.0.1:6379"
    3) "ip"
    4) "127.0.0.1"
    5) "port"
    6) "6379"
    7) "runid"
    8) "14734d6065d745d89f115ca4735e7eeeeaa1a59b"
    9) "flags"
   10) "slave"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "389"
   19) "last-ping-reply"
   20) "389"
   21) "down-after-milliseconds"
   22) "30000"
   23) "info-refresh"
   24) "390"
   25) "role-reported"
   26) "slave"
   27) "role-reported-time"
   28) "982798"
   29) "master-link-down-time"
   30) "1582192784000"
   31) "master-link-status"
   32) "err"
   33) "master-host"
   34) "127.0.0.1"
   35) "master-port"
   36) "6377"
   37) "slave-priority"
   38) "100"
   39) "slave-repl-offset"
   40) "1"
2)  1) "name"
    2) "127.0.0.1:6378"
    3) "ip"
    4) "127.0.0.1"
    5) "port"
    6) "6378"
    7) "runid"
    8) "f9d69479ace6c9eb4a6dffa58ebc1ddf3de456e0"
    9) "flags"
   10) "slave"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "390"
   19) "last-ping-reply"
   20) "390"
   21) "down-after-milliseconds"
   22) "30000"
   23) "info-refresh"
   24) "4004"
   25) "role-reported"
   26) "slave"
   27) "role-reported-time"
   28) "76212633"
   29) "master-link-down-time"
   30) "0"
   31) "master-link-status"
   32) "ok"
   33) "master-host"
   34) "127.0.0.1"
   35) "master-port"
   36) "6377"
   37) "slave-priority"
   38) "100"
   39) "slave-repl-offset"
   40) "10811245"
127.0.0.1:26379> sentinel replicas mymaster # Method 2
1)  1) "name"
    2) "127.0.0.1:6379"
    3) "ip"
    4) "127.0.0.1"
    5) "port"
    6) "6379"
    7) "runid"
    8) "14734d6065d745d89f115ca4735e7eeeeaa1a59b"
    9) "flags"
   10) "slave"
   11) "link-pending-commands"
  12) "0"
  13) "link-refcount"
  14) "1"
  15) "last-ping-sent"
  16) "0"
  17) "last-ok-ping-reply"
  18) "100"
  19) "last-ping-reply"
  20) "100"
  21) "down-after-milliseconds"
  22) "30000"
  23) "info-refresh"
  24) "100"
  25) "role-reported"
  26) "slave"
  27) "role-reported-time"
  28) "1071687"
  29) "master-link-down-time"
  30) "1582192873000"
  31) "master-link-status"
  32) "err"
  33) "master-host"
  34) "127.0.0.1"
  35) "master-port"
  36) "6377"
  37) "slave-priority"
  38) "100"
  39) "slave-repl-offset"
  40) "1"
    1. “name”
    2. “127.0.0.1:6378”
    3. “ip”
    4. “127.0.0.1”
    5. “port”
    6. “6378”
    7. “runid”
    8. “f9d69479ace6c9eb4a6dffa58ebc1ddf3de456e0”
    9. “flags”
    10. “slave”
    11. “link-pending-commands”
    12. “0”
    13. “link-refcount”
    14. “1”
    15. “last-ping-sent”
    16. “0”
    17. “last-ok-ping-reply”
    18. “100”
    19. “last-ping-reply”
    20. “100”
    21. “down-after-milliseconds”
    22. “30000”
    23. “info-refresh”
    24. “2496”
    25. “role-reported”
    26. “slave”
    27. “role-reported-time”
    28. “76301522”
    29. “master-link-down-time”
    30. “0”
    31. “master-link-status”
    32. “ok”
    33. “master-host”
    34. “127.0.0.1”
    35. “master-port”
    36. “6377”
    37. “slave-priority”
    38. “100”
    39. “slave-repl-offset”
    40. “10823208”

Syntax:

sentinel replicas mymaster

or

sentinel slaves master-name

Query Other Sentinel Information in the Sentinel Cluster #

127.0.0.1:26379> sentinel sentinels mymaster
1) 1) "name"
   2) "6455f2f74614a71ce0a63398b2e48d6cd1cf0d06"
   3) "ip"
   4) "127.0.0.1"
   5) "port"
   6) "26377"
   7) "runid"
   8) "6455f2f74614a71ce0a63398b2e48d6cd1cf0d06"
   9) "flags"
   10) "sentinel"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "571"
   19) "last-ping-reply"
   20) "571"
   21) "down-after-milliseconds"
   22) "30000"
   23) "last-hello-message"
   24) "1043"
   25) "voted-leader"
   26) "?"
   27) "voted-leader-epoch"
   28) "0"

Syntax:

sentinel sentinels master-name

Checking the number of available Sentinels #

127.0.0.1:26379> sentinel ckquorum mymaster
OK 2 usable Sentinels. Quorum and failover authorization can be reached

There are 2 usable Sentinels that can reach quorum and failover authorization.

Related syntax:

sentinel ckquorum master-name

Forcing failover #

127.0.0.1:26379> sentinel failover mymaster
OK

Related syntax:

sentinel failover master-name

Modifying configuration information online #

Before Redis 2.8.4, if you wanted to modify the Sentinel configuration file, such as adding or removing a monitored master node, you had to stop the Sentinel service, find and modify the configuration file, and then restart the Sentinel. This caused a lot of inconvenience, especially in production environments where we cannot manually stop the service for non-fatal issues. Luckily, starting from Redis 2.8.4, we can modify the configuration file online without stopping the service. The following commands can be used for modification.

Adding a monitored master node #

Use the command sentinel monitor mymaster IP Port Quorum to add a monitored master node. The command is as follows:

127.0.0.1:26379> sentinel monitor mymaster 127.0.0.1 6379 2
OK

The “OK” response indicates that the monitored master node was added successfully.

Removing monitoring of a master node #

Use the command sentinel remove master-name to remove monitoring of a master node. The command is as follows:

127.0.0.1:26379> sentinel remove mymaster
OK

The “OK” response indicates that the operation was successful.

Modifying the quorum parameter #

Use the command sentinel set master-name quorum n to modify the quorum parameter. The command is as follows:

127.0.0.1:26379> sentinel set mymaster quorum 1
OK

The quorum parameter represents the number of Sentinels that confirm the master node is down. If the quorum is set to 1, it means that once one Sentinel confirms the subjective down status of the master node, the master node will be considered objectively down.

Tip: All the modifications to the configuration file will automatically be reflected in the physical configuration file sentinel.conf.

Practical examples #

In this article, we use Java code to connect to Sentinel, obtain the relevant Redis client, and perform Redis operations. This way, Sentinel will handle disaster recovery for us, and we don’t need to worry about operating on a specific Redis server, as a server failure would render the program unusable. The specific implementation code is as follows:

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;
import utils.Config;

import java.util.HashSet;
import java.util.Set;

public class SentinelExample {
    // master name
    private static String _MASTER_NAME = "mymaster";

    public static void main(String[] args) {
        // Sentinel configuration information
        Set<String> set = new HashSet<>();
        // Connection information ip:port
        set.add("127.0.0.1:26379");
        // Create Sentinel connection pool
        JedisSentinelPool jedisSentinel = new JedisSentinelPool(_MASTER_NAME,
                set, Config.REDIS_AUTH);
        // Get Redis client
        Jedis jedis = jedisSentinel.getResource();
        // Set element
        String setRes = jedis.set("key", "Hello, redis.");
        System.out.println(setRes);
        // Get element
        System.out.println(jedis.get("key"));
    }
}

The output of the above program is as follows:

OK
Hello, redis.

Summary #

In this article, we discussed the commands related to Sentinel, mainly used to query information about master-slave nodes and other Sentinels. We also covered three methods introduced in version 2.8.4 for online modification of Sentinel parameters, making it more convenient to use Sentinel. Finally, we provided an example of using code to obtain the master node through Sentinel and perform Redis server operations. This concludes the complete introduction and application of Sentinel.