模拟 Redis 故障
本文主要介绍如何使用 Chaosd 模拟 Redis 故障。该功能通过使用 go-redis
包中的 Golang 接口和 redis-server
命令行工具模拟 Redis 故障场景,支持通过命令行模式或服务模式创建实验。
使用命令行模式创建实验
在创建 Redis 故障实验前,可运行以下命令查看 Chaosd 支持的进程故障类型:
chaosd attack redis -h
输出结果如下所示:
Redis attack related commands
Usage:
chaosd attack redis [command]
Available Commands:
cache-expiration expire keys in Redis
cache-limit set maxmemory of Redis
cache-penetration penetrate cache
sentinel-restart restart sentinel
sentinel-stop stop sentinel
Flags:
-h, --help help for redis
Global Flags:
--log-level string the log level of chaosd. The value can be 'debug', 'info', 'warn' and 'error'
--uid string the experiment ID
Use "chaosd attack redis [command] --help" for more information about a command.
目前 Chaosd 支持模拟缓存过期、缓存限流、缓存穿透、哨兵重启和哨兵不可用等故障场景。
使用命令行模式模拟缓存过期
该命令与 Redis EXPIRE
操作意义一致,详情参考 Redis 官方文档。
注意
目前不支持恢复执行了 cache-expiration
操作的 key,如需恢复请提前备份。
缓存过期命令
chaosd attack redis cache-expiration -h
输出结果如下所示:
expire keys in Redis
Usage:
chaosd attack redis cache-expiration [flags]
Flags:
-a, --addr string The address of redis server
--expiration string The expiration of the key. A expiration string should be able to be converted to a time duration, such as "5s" or "30m" (default "0")
-h, --help help for cache-expiration
-k, --key string The key to be set a expiration, default expire all keys
--option string The additional options of expiration, only NX, XX, GT, LT supported
-p, --password string The password of server
Global Flags:
--log-level string the log level of chaosd. The value can be 'debug', 'info', 'warn' and 'error'
--uid string the experiment ID
模拟缓存过期相关配置说明
配置项 | 配置缩写 | 说明 | 值 |
---|---|---|---|
addr | a | 需要注入故障的 Redis 服务器的地址以及端口号,如 127.0.0.1:6379 | string 类型,默认为 "" |
expiration | 无 | 指定的键值对将会在到达 expiration 之后过期 | string 类型,请确保输入的字符串为 time.Duration 支持的格式,默认为 0 |
key | k | 要设置过期时间的键 | string 类型,默认为 "" 。当该值为默认时,将对所有键设置过期时间 |
option | 无 | 对 expiration 的额外操作,用于设置键的过期条件。只有 Redis 7.0.0 之后的版本支持该参数 | string 类型,默认为 "" 。只支持 NX,XX,GT,LT |
password | p | 登录 Redis 服务器的密码 | string 类型,默认为 "" |
模拟缓存过期示例
chaosd attack redis cache-expiration -a 127.0.0.1:6379 --option GT --expiration 1m
使用命令行模式模拟缓存限流
模拟缓存限流命令
chaosd attack redis cache-limit -h
输出结果如下所示:
set maxmemory of Redis
Usage:
chaosd attack redis cache-limit [flags]
Flags:
-a, --addr string The address of redis server
-h, --help help for cache-limit
-p, --password string The password of server
--percent string The percentage of maxmemory
-s, --size string The size of cache (default "0")
Global Flags:
--log-level string the log level of chaosd. The value can be 'debug', 'info', 'warn' and 'error'
--uid string the experiment ID