用户管理

实施正确的身份验证和访问控制对于保护 Redis 环境至关重要。本节介绍 Redis 实例的密码管理和基于角色的访问控制配置。

版本兼容性说明

Redis 6.0 及更高版本实现了全面的 Access Control List (ACL) 系统,支持多个具有细粒度权限的用户。Redis 6.0+ 为了与旧版客户端保持向后兼容,会保留默认用户。

查看用户

以下方法可用于查看与 Redis 实例关联的所有用户。

CLI
Web Console

Redis 实例(6.0+ 版本)使用 RedisUser Custom Resource (CR) 来管理用户账户。要列出与特定实例关联的所有用户:

$ kubectl -n default get redisuser -l middleware.instance/name=<instance_name>

例如,要列出名为 s6 的实例的所有用户:

$ kubectl -n test get redisuser -l middleware.instance/name=s6
NAME                  INSTANCE   USERNAME   PHASE     AGE
rfr-acl-s6-admin      s6         admin      Success   22s
rfr-acl-s6-app-a      s6         app-a      Success   45s
rfr-acl-s6-app-b      s6         app-b      Success   6s
rfr-acl-s6-default    s6         default    Success   3m17s
rfr-acl-s6-operator   s6         operator   Success   3m17s

输出字段提供以下信息:

字段描述
NAMERedisUser 自定义资源标识符
INSTANCE关联的 Redis 实例名称
USERNAME在 Redis 中注册的用户名
PHASE同步状态:
  • Success:用户配置已成功同步到所有 Redis 节点
  • Fail:用户同步在一个或多个 Redis 节点上失败
  • Pending:关联的 Redis 实例未就绪或正在同步中
系统 operator 账户

每个 Redis 实例都包含一个内置的 operator 用户,该用户会在实例创建期间自动配置。此系统账户具有全面权限(包括用户管理能力),并使用复杂的 64 字符密码进行保护。

此账户仅保留用于系统操作,绝不应用于应用访问。对该账户配置的任何修改都可能导致实例严重不稳定,并可能引发无法恢复的故障状态。

更新密码

以下操作步骤可用于更新用户密码,以增强安全性。建议定期轮换密码,作为安全最佳实践。

操作步骤

CLI
Web Console
  1. 从 RedisUser 列表中确定目标用户。

    在本示例中,我们将更新实例 s6default 用户密码。

  2. 获取 RedisUser 资源,以确定关联的密码 Secret:

    $ kubectl -n default get RedisUser rfr-acl-s6-default -o yaml
    apiVersion: redis.middleware.alauda.io/v1
    kind: RedisUser
    metadata:
      annotations:
        middleware.alauda.io/acl-supported-version: "6.0"
      creationTimestamp: "2025-02-28T02:07:26Z"
      finalizers:
      - redisusers.redis.middleware.alauda.io/finalizer
      generation: 1
      labels:
        managed-by: redis-operator
        middleware.instance/name: s6
      name: rfr-acl-s6-default
      namespace: default
      ownerReferences:
      - apiVersion: databases.spotahome.com/v1
        blockOwnerDeletion: true
        controller: true
        kind: RedisFailover
        name: s6
        uid: 56596fa0-4a42-408b-ba26-d61b3a42b60c
      - apiVersion: middleware.alauda.io/v1
        blockOwnerDeletion: true
        kind: Redis
        name: s6
        uid: 80d2e7b0-02d2-490f-bec4-f145f2c738fc
      resourceVersion: "1014064"
      uid: aae888f3-86ba-48bd-9376-710070bf07e0
    spec:
      accountType: default
      aclRules: +@all -acl -flushall -flushdb -keys ~*
      arch: sentinel
      passwordSecrets:
      - redis-s6-2hqxb
      redisName: s6
      username: default
    status:
      Phase: Success
      lastUpdateSuccess: "2025-02-28T02:16:28Z"

    spec.passwordSecrets[0] 字段可以看出,密码存储在 Secret redis-s6-2hqxb 中。

  3. 查看当前密码 Secret:

    $ kubectl -n default get secret redis-s6-2hqxb -o yaml
    apiVersion: v1
    data:
      password: YWRtaW5AMTIz
    kind: Secret
    metadata:
      annotations:
        cpaas.io/creator: admin
        cpaas.io/operator: admin
        cpaas.io/updated-at: "2025-02-28T08:49:38Z"
      creationTimestamp: "2025-02-28T02:07:24Z"
      generateName: redis-s6-
      labels:
        managed-by: redis-operator
        middleware.instance/name: s6
      name: redis-s6-2hqxb
      namespace: default
      ownerReferences:
      - apiVersion: redis.middleware.alauda.io/v1
        blockOwnerDeletion: true
        controller: true
        kind: RedisUser
        name: rfr-acl-s6-default
        uid: aae888f3-86ba-48bd-9376-710070bf07e0
      resourceVersion: "1183397"
      uid: 45a5faa7-5b47-4a6a-a4e4-c614feccb806
    type: Opaque

    注意:password 键是 Redis Operator 使用的保留字段名称,其值以 base64 编码字符串形式存储。

  4. 使用新密码更新 Secret:

    $ kubectl -n default patch secret redis-s6-2hqxb --type=merge --patch='{"stringData": {"password": "admin@12345"}}'
  5. 验证密码更新:

    $ kubectl -n default get secret redis-s6-2hqxb -o jsonpath='{.data.password}' | base64 -d
    admin@12345

    更新密码后,RedisUser 资源会暂时进入 Pending 状态,待更改传播到所有 Redis 节点后,状态将恢复为 Success

更新权限

Redis 6.0+ 通过其 ACL 系统支持细粒度访问控制。平台为常见使用场景提供了预定义的权限配置文件:

权限配置文件ACL 规则描述
NotDangerous+@all -@dangerous ~*授予对所有键上所有命令的访问权限,但排除被归类为潜在危险操作的命令
ReadWrite-@all +@write +@read -@dangerous ~*允许对所有键执行读写操作,同时阻止危险命令
ReadOnly-@all +@read -keys ~*限制为所有键上的只读操作
Administrator+@all -acl ~*提供对所有 Redis 功能的全面访问,但不包括 ACL 管理命令

对于高级场景,还支持自定义 ACL 规则。有关详细语法和功能,请参阅 Redis ACL 文档

注意:所有权限配置文件,包括自定义配置文件,都会显式撤销 ACL 管理权限。用户修改必须通过平台的用户管理界面执行。

操作步骤

CLI
Web Console
$ kubectl -n default patch RedisUser s56 --type=merge --patch='{"spec": {"aclRules": "+@all ~*"}}'

创建成功后,用户会随机进入 Pending 状态。请等待其变更为 Success 状态,这表示更新已成功完成。

创建用户

操作步骤

CLI
Web Console

为 Redis 集群模式实例创建用户。

$ cat << EOF | kubectl -n default create -f -
apiVersion: redis.middleware.alauda.io/v1
kind: RedisUser
metadata:
  name: rfr-acl-s6-app-a
spec:
  accountType: custom
  aclRules: +@all -acl ~*
  passwordSecrets:
  - new-redis-password
  redisName: s6
  username: app-a
EOF

Once the resource status changes to `Success`, you can connect with the new password for debugging.