All Products
Search
Document Center

:Use the Sentinel-compatible mode to connect to a Tair instance

最終更新日:Aug 06, 2024

Tair uses the high-availability (HA) component developed by Alibaba Cloud instead of Redis Sentinel. To improve the compatibility of Tair with open source Redis and minimize code modifications, Tair provides the Sentinel-compatible mode. After you enable this mode, clients can connect to a Tair instance in the same manner as they connect to Redis Sentinel.

Overview of Redis Sentinel

Redis Sentinel provides open source Redis with features such as master and replica monitoring, fault alerting, and automatic failover. Redis Sentinel is suitable for business scenarios in which self-managed Redis databases are used and high reliability is required. To facilitate the migration of Redis databases to the cloud in such scenarios, Alibaba Cloud provides the Sentinel-compatible mode. After you enable the Sentinel-compatible mode, you can run the Sentinel commands described in the following table.

Command

Description

SENTINEL sentinels

Queries Sentinel instances of a master and the status of the Sentinel instances. Command syntax:

SENTINEL sentinels <Name of a master>

SENTINEL get-master-addr-by-name

Queries the IP address and port number of a master. Command syntax:

SENTINEL get-master-addr-by-name <Name of a master>

For more information about the Sentinel commands that are supported by different engine versions, see Limits on commands supported by Tair.

Prerequisites

The internal IP address of your Elastic Compute Service (ECS) instance or the public IP address of your on-premises host is added to an IP address whitelist of the Tair instance. For more information, see Step 2: Configure whitelists.

Procedure

  1. Log on to the Tair console and go to the Instances page. In the top navigation bar, select the region in which the instance that you want to manage resides. Then, find the instance and click the instance ID.

  2. In the left-side navigation pane of the Instance Information page, click Parameter Settings.

  3. Enable the Sentinel-compatible mode for the instance by modifying the corresponding parameter based on the instance architecture. For more information, see Modify the values of parameters for an instance.

    • If the instance is a read/write splitting instance or a cluster instance in proxy mode, set the sentinel_compat_enable parameter to 1.

    • If the instance is a standard instance, set the #no_loose_sentinel-enabled parameter to yes.

    Note
    • You can view the instance architecture on the instance details page.

    • Cluster instances in direct connection mode use open source Redis Cluster for load balancing, eliminating the need for the Sentinel component. Additionally, Sentinel parameters cannot be configured in this mode.

    After you enable the Sentinel-compatible mode for the instance, you can connect to the instance and run the SENTINEL sentinels test command. If the command execution is successful, the Redis Sentinel-compatible mode is enabled for the instance. The Sentinel-compatible mode does not provide additional endpoints. You can use the original endpoint, such as r-********.redis.rds.aliyuncs.com:6379, to connect to the instance.

Connect to a Tair instance in Sentinel-compatible mode

After the Sentinel-compatible mode is enabled, you can use one of the following methods to connect to the Tair instance: If password-free access is enabled for the Tair instance, you can connect to the instance in Sentinel-compatible mode. Otherwise, you must configure authentication information when you connect to the instance.

Connect to a Tair instance in Sentinel-compatible mode without using a password

Note

For information about how to enable password-free access, see Enable password-free access.

The following examples show how to connect to a Tair instance in Sentinel-compatible mode without using a password.

Spring Data Redis

In this example, Spring Data Redis 2.4.2 is used.

    @Bean
    public JedisConnectionFactory connectionFactory() {
        RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
                .master("testmaster")
                .sentinel("r-bp10noxlhcoim2****.redis.rds.aliyuncs.com", 6379);
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        ...
        JedisConnectionFactory connectionFactory = new JedisConnectionFactory(sentinelConfig, poolConfig);
        return connectionFactory;
    }

Parameters:

  • master: the name of the master. You can use the default value, such as testmaster.

  • sentinel: the virtual private cloud (VPC) endpoint and port number of the Tair instance. Separate the endpoint and port number with a comma (,). Example: "r-bp10noxlhcoim2****.redis.rds.aliyuncs.com", 6379.

redis-py

In this example, Python 3.9 and redis-py 4.3.6 are used.

from redis.sentinel import Sentinel
SENTINEL_HOST = "r-bp10noxlhcoim2****.redis.rds.aliyuncs.com"
SENTINEL_PORT = 6379
SENTINEL_MASTER_NAME = "testmaster"

sentinel = Sentinel([(SENTINEL_HOST, SENTINEL_PORT)])
r = sentinel.master_for(SENTINEL_MASTER_NAME, db=0)
r.set('foo', 'bar')
print(r.get('foo'))

Parameters:

  • SENTINEL_HOST and SENTINEL_PORT: the VPC endpoint and port number of the Tair instance.

  • SENTINEL_MASTER_NAME: the name of the master. You can use the default value, such as testmaster.

Connect to a Tair instance in Sentinel-compatible mode by using a password

The following examples show how to connect to a Tair instance in Sentinel-compatible mode by using a password.

Java

In this example, a Java client of the earliest version is used. The client must meet the following requirements:

  • Jedis 3.6.0 or later is used.

  • Lettuce 5.3.0.RELEASE or later is used.

  • Spring Data Redis 2.5.1 or later is used. The spring.redis.sentinel.password parameter is specified for the Spring Data Redis client.

Note

We recommend that you upgrade the client to the latest stable version. For information about the latest versions of clients, see What's New in Maven.

        String masterName = "any-name";
        Set<String> sentinels = new HashSet<>();
        sentinels.add("r-bp10noxlhcoim2****.redis.rds.aliyuncs.com:6379");
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        String dbPassword = "testaccount:Rp829dlwa";
        String sentinelPassword = "testaccount:Rp829dlwa";

        JedisSentinelPool jedisSentinelPool =
                new JedisSentinelPool(masterName, sentinels, poolConfig,
                        2000, 2000, dbPassword,
                        0, null, 2000, 2000,
                        sentinelPassword, null);

Parameters:

  • masterName: the name of the master. You can use the default value, such as testmaster.

  • sentinels.add: the VPC endpoint and port number of the Tair instance. Example: r-bp10noxlhcoim2****.redis.rds.aliyuncs.com:6379.

  • dbPassword and sentinelPassword: the account password of the Tair instance. The password format varies based on the account that you select. If you forget your password, reset it. For more information, see Change or reset the password.

    Note
    • If you use the default account whose username is the same as the instance ID, directly enter the password.

    • If you use a custom account, enter the password in the <user>:<password> format. A password in this format can also be used for default account logon. For example, if the username of the custom account is testaccount and the password is Rp829dlwa, enter testaccount:Rp829dlwa as the password.

redis-py

In this example, Python 3.9 and redis-py 4.3.6 are used.

from redis.sentinel import Sentinel
SENTINEL_HOST = "r-bp10noxlhcoim2****.rds.aliyuncs.com"
SENTINEL_PORT = 6379
SENTINEL_MASTER_NAME = "redis_master"  # Note: This name cannot be changed.
SENTINEL_REDIS_PWD = "testaccount:Rp829dlwa"

conf = {
    'password': SENTINEL_REDIS_PWD,
}

sentinel = Sentinel([(SENTINEL_HOST, SENTINEL_PORT)], sentinel_kwargs=conf)
r = sentinel.master_for(SENTINEL_MASTER_NAME, db=0, **conf)
r.set('foo', 'bar')
print(r.get('foo'))

Parameters:

  • SENTINEL_HOST and SENTINEL_PORT: the VPC endpoint and port number of the Tair instance.

  • SENTINEL_MASTER_NAME : the name of the master in the Redis Sentinel configuration. The name is set to redis_master and cannot be changed.

  • SENTINEL_REDIS_PWD: the account password of the Tair instance.

FAQ

  • What do I do if the NOAUTH Authentication required error message appears when I switch from the native Redis Sentinel mode to the Sentinel-compatible mode provided by Tair?

    You can set the #no_loose_sentinel-password-free-access parameter to yes to connect to the instance in Sentinel-compatible mode over a VPC without enabling password-free access.

    If the engine version of the instance is earlier than Redis 6.0, upgrade your client, modify the code to add a password for Sentinel authentication, and then try again. For more information, see the "Connect to a Tair instance in Sentinel-compatible mode by using a password" section of this topic.