如何访问哨兵模式实例

本指南演示如何使用行业标准客户端库与 Redis 哨兵模式实例建立连接。示例涵盖 go-redis、Jedis、Lettuce 和 Redisson 的配置详情。有关其他客户端选项,请参阅使用 Redis 客户端 API 库进行连接。

身份验证要求

Redis 哨兵模式实例支持以下身份验证选项:

  • 密码身份验证:配置密码后,所有客户端连接都必须提供有效凭据
  • 无密码访问:如果在创建实例时禁用了设置密码选项,客户端无需身份验证即可连接

一个哨兵模式实例包含两个相互独立的密码,分别在创建实例时的两个不同选项卡中设置:

密码设置位置身份验证对象客户端配置
Redis 密码设置 Redis 密码选项卡 (spec.passwordSecret)为命令提供服务的 Redis 数据节点客户端的常规密码选项
Sentinel 密码设置 Sentinel 密码选项卡 (spec.sentinel.passwordSecret)客户端查询以发现主节点和执行故障转移的 Sentinel 节点专用的 Sentinel 密码选项,具体请参阅下方各客户端的说明

自 v5.1.0 起,设置 Sentinel 密码选项卡默认启用设置密码,因此从 Web Console 创建的实例通常会设置 Sentinel 密码。仅发送 Redis 密码的客户端将无法访问 Sentinel 节点,也无法发现主节点。在 v5.1.0 之前创建的实例不会发生变化,并会保留原有的 Sentinel 身份验证配置。

安全最佳实践

对于生产环境,强烈建议实施密码身份验证以保护数据。有关配置和维护安全凭据的详细说明,请参阅用户管理。

连接端点参考

集群内部访问

对于部署在同一 Kubernetes 集群中的应用程序,可在集群内访问部分的访问方式选项卡中获取集群内部访问端点。

参数描述
连接地址Redis Sentinel 的 Kubernetes 服务名称和端口组合。

集群外部访问

对于从 Kubernetes 环境外部连接的应用程序,在创建实例时配置后即可使用外部访问端点。可在集群外访问部分的访问方式选项卡中找到这些端点。

参数描述
Sentinel 节点访问地址Redis Sentinel 中 sentinel Pod 的外部 IP 地址和端口,用于从 Kubernetes 网络外部建立连接。

交互式调试

在实例详情页面中,点击右上角的终端控制台,然后使用 redis-cli 命令连接到每个 Redis 节点。

redis-cli -h <internal-routing-ip> -p 6379

下面是一个调试示例。
演示 set/get 的调试会话示例:

192.168.0.10:6379> set a 1
OK
192.168.0.10:6379> get a
"1"
192.168.0.10:6379>

客户端集成示例

以下示例演示了使用各种客户端库连接 Redis Sentinel 实例的最佳实践。

注意:在 Sentinel 模式中注册的主从集群名称固定为 mymaster。

go-redis
Jedis
Lettuce
Redisson
package main

import (
    "context"
    "fmt"
    "time"

    // It is recommended to periodically upgrade to the latest version of the client for the latest bug fixes.
    "github.com/redis/go-redis/v9"
)

func main() {
    client := redis.NewFailoverClient(&redis.FailoverOptions{
        SentinelAddrs: []string{"<address>"},
        MasterName:    "mymaster",
        // Password of the Redis data nodes.
        Password: "<password>",
        // Password of the Sentinel nodes, set on the "Set Sentinel Password" tab.
        SentinelPassword: "<sentinel-password>",
        OnConnect: func(ctx context.Context, conn *redis.Conn) error {
            ctx, cancel := context.WithTimeout(ctx, 500*time.Millisecond)
            defer cancel()
            return conn.Ping(ctx).Err()
        },
        // Client name for identification and tracking
        ClientName: "go-demo",
        // Using Context for timeout control
        ContextTimeoutEnabled: true,
        // Maximum number of retries
        MaxRetries: 3,
        // Minimum backoff time for retries
        MinRetryBackoff: 20 * time.Millisecond,
        // Maximum backoff time for retries
        MaxRetryBackoff: 200 * time.Millisecond,
        // Connection timeout
        DialTimeout: 3 * time.Second,
        // Read timeout
        ReadTimeout: 5 * time.Second,
        // Write timeout
        WriteTimeout: 10 * time.Second,
        // Connection pool size for each node
        PoolSize: 100,
        // Maximum wait time for available connections in the pool
        PoolTimeout: time.Second,
        // Minimum idle connections for each node
        MinIdleConns: 5,
        // Maximum idle connections for each node
        MaxIdleConns: 10,
        // Maximum active connections for each node
        MaxActiveConns: 100,
        // Maximum idle time for connections
        ConnMaxIdleTime: time.Minute * 5,
    })
    defer client.Close()

    if val, err := client.Get(context.TODO(), "test").Result(); err != nil {
        panic(err)
    } else {
        fmt.Println(val)
    }
}

有关更详细的配置,请参阅社区文档。