Tair (Redis OSS-compatible) は独自の高可用性 (HA) コンポーネントを使用しており、Sentinel には依存していません。ただし、インスタンスの互換性を向上させ、ユーザーによるコード変更を最小限に抑えるため、Tair (Redis OSS-compatible) は Sentinel 互換モードを提供しています。この機能を有効化すると、ネイティブな Redis Sentinel 環境に接続するのと同じ方法で、Tair または Community Edition インスタンスに接続できます。
Redis Sentinel
Redis Sentinel は、オープンソース版 Redis 向けにマスター/レプリカインスタンスの監視、障害アラート、自動フェイルオーバーなどのサービスを提供します。Sentinel は、オンプレミスの自主管理 Redis データベースを使用し、高い信頼性が求められるビジネスシナリオで広く利用されています。このようなシナリオにおける Redis データベースのクラウドへの移行を容易にするため、Alibaba Cloud は Sentinel 互換モードを開発しました。Sentinel 互換モードを有効化すると、以下の Sentinel 関連コマンドを使用できます。これらのコマンドにおけるマスターネームは、必ず redis_master である必要があります。
コマンド | 説明 |
SENTINEL sentinels | 指定された Master の Sentinel インスタンスとそのステータスを照会します。構文は次のとおりです。 SENTINEL sentinels redis_master
|
SENTINEL get-master-addr-by-name | 指定された Master の IP アドレスとポート番号を照会します。構文は次のとおりです。 SENTINEL get-master-addr-by-name redis_master
|
各バージョンでサポートされる Sentinel コマンドの詳細については、「Community Edition のコマンドサポート」をご参照ください。
接続例
Sentinel 互換モードを有効化すると、次の 2 つの方法でインスタンスに接続できます。インスタンスで Virtual Private Cloud (VPC) 経由のパスワードなしのアクセスが有効になっている場合は、パスワード不要で接続できます。それ以外の場合は、認証が必要です。
パスワード不要の Sentinel 接続
以下は、Sentinel 互換モードでの接続設定例です。
Spring Data Redis
この例では Spring Data Redis 2.4.2 を使用しています。
@Bean
public JedisConnectionFactory connectionFactory() {
RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
.master("redis_master")
.sentinel("r-bp10noxlhcoim2****.redis.rds.aliyuncs.com", 6379);
JedisPoolConfig poolConfig = new JedisPoolConfig();
...
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(sentinelConfig, poolConfig);
return connectionFactory;
}
パラメーター:
master:Sentinel 名。必ず redis_master に設定する必要があります。
sentinel:インスタンスの VPC エンドポイントとポート番号。sentinel メソッドに個別の引数として渡します。例:"r-bp10noxlhcoim2****.redis.rds.aliyuncs.com", 6379。
redis-py
この例では Python 3.9 および redis-py 4.3.6 を使用しています。
from redis.sentinel import Sentinel
SENTINEL_HOST = "r-bp10noxlhcoim2****.redis.rds.aliyuncs.com"
SENTINEL_PORT = 6379
SENTINEL_MASTER_NAME = "redis_master"
sentinel = Sentinel([(SENTINEL_HOST, SENTINEL_PORT)])
r = sentinel.master_for(SENTINEL_MASTER_NAME, db=0)
r.set('foo', 'bar')
print(r.get('foo'))
パラメーター:
パスワード認証付き Sentinel 接続
以下は、Sentinel 互換モードでのパスワード認証付き接続の設定例です。
Java
パスワード認証付き Sentinel 接続を使用するには、クライアントが以下の最低バージョン要件を満たしている必要があります。
説明 最新の安定版にクライアントをスペックアップすることを推奨します。最新バージョンを確認するには、MVN Repository で検索してください。
Jedis
String masterName = "redis_master";
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);
Lettuce
String masterName = "redis_master";
String sentinelNodes = "r-bp10noxlhcoim2****.redis.rds.aliyuncs.com:6379";
String sentinelPassword = "testaccount:Rp829dlwa";
RedisURI redisURI = RedisURI.create("redis-sentinel://" + sentinelPassword + "@" + sentinelNodes + "#" + masterName);
redisURI.getSentinels().forEach(it -> it.setPassword(sentinelPassword));
RedisClient client = RedisClient.create(redisURI);
RedisCommands<String, String> sync = client.connect().sync();
System.out.println(sync.set("key", "value"));
System.out.println(sync.get("key"));
client.close();
パラメーター:
masterName:Sentinel 名。必ず redis_master に設定する必要があります。
sentinels.add/sentinelNodes:インスタンスの VPC エンドポイントとポート番号。r-bp10noxlhcoim2****.redis.rds.aliyuncs.com:6379 の形式で指定します。
dbPassword および sentinelPassword:インスタンスアカウントのパスワード。パスワードのフォーマットはアカウントタイプによって異なります。パスワードを忘れた場合は、「パスワードの変更またはリセット」をご参照ください。
redis-py
この例では Python 3.9 および redis-py 4.3.6 を使用しています。
from redis.sentinel import Sentinel
SENTINEL_HOST = "r-bp10noxlhcoim2****.rds.aliyuncs.com"
SENTINEL_PORT = 6379
SENTINEL_MASTER_NAME = "redis_master" # 注:この名前は変更できません。
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'))
パラメーター:
SENTINEL_HOST および SENTINEL_PORT:インスタンスの VPC エンドポイントとポート番号。
SENTINEL_MASTER_NAME:Sentinel 名。必ず redis_master に設定する必要があります。
SENTINEL_REDIS_PWD: インスタンスアカウントのパスワード。
よくある質問
Q:自主管理 Redis Sentinel 環境から Sentinel 互換モードに切り替えた後、NOAUTH Authentication required エラーが発生するのはなぜですか?
A:#no_loose_sentinel-password-free-access パラメーターを yes に設定して有効化してください。これにより、グローバルなパスワードなしのアクセス機能を有効化しなくても、VPC エンドポイント経由でパスワード不要の Sentinel 接続が可能になります。
インスタンスの Redis バージョンが 6.0 より前の場合は、クライアントをスペックアップし、Sentinel 認証用パスワードを含むようにコードを修正してから、再度接続を試行してください。詳細については、本トピックの「パスワード認証付き Sentinel 接続」セクションをご参照ください。
Q:Unknown sentinel subcommand 'MASTERS' エラーが発生するのはなぜですか?
A:このエラーは、インスタンスのマイナーバージョンがこのコマンドをサポートしていないことを示しています。マイナーバージョンを更新してから、再度お試しください。