跳至主内容
版本:2.6.7

模拟 Redis 故障

非官方测试版翻译

本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →

本文介绍如何使用 Chaosd 模拟 Redis 故障。该功能基于 go-redis 包的 Golang 接口和 redis-server 命令行工具,支持通过命令行模式或服务模式创建实验。

使用命令行模式创建实验

创建实验前,可执行以下命令查看 Chaosd 支持的 Redis 故障类型:

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 官方文档

备注

当前 Chaosd 不支持恢复已执行 cache-expiration 的键值,如需恢复请提前备份。

缓存过期命令

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

缓存过期配置说明

Configuration itemAbbreviationTypeDescriptionValue
addrastringThe address and port of Redis server to be injected into the fault, for example 127.0.0.1:6379Default value: ""
expirationNonestringThe specified key will be expired after expiration arrivesDefault value: "0". Make sure that the string is in the format supported by time.Duration
keykstringThe key to be expiredDefault value: "", which means the expiration is set for all keys
optionNonestringAdditional options for expiration. Only versions of Redis after 7.0.0 support this flagDefault value: "". Only NX, XX, GT, and LT are supported
passwordpstringThe password to log in to the serverDefault value: ""

模拟缓存过期示例

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

缓存限流配置说明

Configuration itemAbbreviationTypeDescriptionValue
addrastringThe address and port of Redis server to be injected into the fault, such as 127.0.0.1:6379Default value: ""
passwordpstringThe password to log in to the serverDefault value: ""
percentNonestringSpecifies maxmemory as a percentage of the original valueDefault value: ""
sizesstringSpecifies the size of maxmemoryDefault 0, which means no limitation of memory

模拟缓存限流示例

chaosd attack redis cache-limit -a 127.0.0.1:6379 -s 256M

使用命令行模式模拟缓存穿透

该命令通过 Redis Pipeline 向 Redis 服务器快速发送指定数量的 GET 请求。由于请求的键在 Redis 中不存在,这些请求将导致缓存穿透现象。

缓存穿透命令

chaosd attack redis cache-penetration -h

结果如下:

penetrate cache

Usage:
chaosd attack redis cache-penetration [flags]

Flags:
-a, --addr string The address of redis server
-h, --help help for cache-penetration
-p, --password string The password of server
--request-num int The number of requests

Global Flags:
--log-level string the log level of chaosd. The value can be 'debug', 'info', 'warn' and 'error'
--uid string the experiment ID

缓存穿透配置说明

Configuration itemAbbreviationTypeDescriptionValue
addrastringThe address and port of Redis server to be injected into the fault, such as 127.0.0.1:6379Default value: ""
passwordpstringThe password to log in to the serverDefault value: ""
request-numNoneintSpecifies the number of requests to be sent to the Redis serverDefault value: 0

模拟缓存穿透示例

chaosd attack redis cache-penetration -a 127.0.0.1:6379 --request-num 100000

使用命令行模式模拟哨兵重启

哨兵重启命令

chaosd attack redis sentinel-restart -h

结果如下:

restart sentinel

Usage:
chaosd attack redis sentinel-restart [flags]

Flags:
-a, --addr string The address of redis server
-c, --conf string The config of Redis server
--flush-config Force Sentinel to rewrite its configuration on disk (default true)
-h, --help help for sentinel-restart
-p, --password string The password of server
--redis-path string The path of the redis-server command

Global Flags:
--log-level string the log level of chaosd. The value can be 'debug', 'info', 'warn' and 'error'
--uid string the experiment ID

哨兵重启配置说明

Configuration itemAbbreviationTypeDescriptionValue
addrastringThe address and port of Sentinel to be injected into the fault, such as 127.0.0.1:26379Default value: ""
confcstringSpecifies the path of Sentinel config file, this file will be used to revover the SentinelDefault value: ""
flush-configNoneboolForces Sentinel to rewrite its configuration on disk, including the current Sentinel stateDefault value: true
passwordpstringThe password to log in to the serverDefault value: ""
redis-pathNonestringSpecifies the path of redis-server command-line toolDefault value: ""

模拟哨兵停止示例

chaosd attack redis sentinel-restart -a 127.0.0.1:26379 --conf /home/redis-test/sentinel-26379.conf

使用命令行模式模拟哨兵停止

哨兵停止命令

chaosd attack redis sentinel-stop -h

结果如下:

stop sentinel

Usage:
chaosd attack redis sentinel-stop [flags]

Flags:
-a, --addr string The address of redis server
-c, --conf string The config path of Redis server
--flush-config Force Sentinel to rewrite its configuration on disk (default true)
-h, --help help for sentinel-stop
-p, --password string The password of server
--redis-path string The path of the redis-server command

Global Flags:
--log-level string the log level of chaosd. The value can be 'debug', 'info', 'warn' and 'error'
--uid string the experiment ID

哨兵停止配置说明

Configuration itemAbbreviationTypeDescriptionValue
addrastringThe address and port of Sentinel to be injected into the fault, such as 127.0.0.1:26379Default value: ""
confcstringSpecifies the path of Sentinel configuration file, which is used to recover the SentinelDefault value: ""
flush-configNoneboolForces Sentinel to rewrite its configuration on disk, including the current Sentinel stateDefault value: true
passwordpstringThe password to log in to the serverDefault value: ""
redis-pathNonestringSpecifies the path of redis-server command-line toolDefault value: ""

模拟哨兵停止示例

chaosd attack redis sentinel-stop -a 127.0.0.1:26379 --conf /home/redis-test/sentinel-26379.conf

使用服务模式创建 Redis 故障实验

要通过服务模式创建实验,请按以下步骤操作:

  1. 在服务模式下运行 Chaosd:

    chaosd server --port 31767
  2. 向 Chaosd 服务的 /api/attack/redis 路径发送 POST HTTP 请求:

    curl -X POST 127.0.0.1:31767/api/attack/redis -H "Content-Type:application/json" -d '{fault-configuration}'

    上述命令中,需根据故障类型配置 fault-configuration 参数,具体请参考后续各故障类型的参数说明和示例。

备注

运行实验时,请务必记录实验的 UID。当您需要结束对应 UID 的实验时,应向 Chaosd 服务的 /api/attack/{uid} 路径发送 DELETE HTTP 请求。

使用服务模式模拟缓存过期

模拟缓存过期参数

ParameterDescriptionTypeValue
actionAction of the experimentstringset to "corrupt"
addrThe address and port of Redis server to be injected into the fault, such as 127.0.0.1:6379stringDefault value: ""
expirationThe specified key will be expired after expiration arrivesstringDefault value: "0". Make sure that the string is in the format supported by time.Duration
keyThe key to be expiredstringDefault value: "", which means the expiration is set for all keys
optionAdditional options for expiration. Only versions of Redis after 7.0.0 support this flagstringDefault value: "". Only NX, XX, GT, and LT are supported
passwordThe password to log in to the serverDefault value: ""

服务模式模拟缓存过期示例

curl -X POST 127.0.0.1:31767/api/attack/redis -H "Content-Type:application/json" -d '{"action":"expiration", "expiration":"1m","addr":"127.0.0.1:6379"}'

通过服务模式模拟缓存限制

模拟缓存限制的参数

ParameterDescriptionTypeValue
actionAction of the experimentstringset to "cacheLimit"
addrThe address and port of Redis server to be injected into the fault, such as 127.0.0.1:6379stringDefault value: ""
passwordThe password to log in to the serverstringDefault value: ""
percentSpecifies maxmemory as a percentage of the original valuestringDefault value: ""
sizeSpecifies the size of maxmemorystringDefault 0, which means no limitation of memory

通过服务模式模拟缓存限制的示例

curl -X POST 127.0.0.1:31767/api/attack/redis -H "Content-Type:application/json" -d '{"action":"cacheLimit", ""addr":"127.0.0.1:6379", "percent":"50%"}'

通过服务模式模拟缓存穿透

模拟缓存穿透的参数

ParameterDescriptionTypeValue
actionAction of the experimentstringset to "penetration"
addrThe address and port of Redis server to be injected into the fault, such as 127.0.0.1:6379stringDefault value: ""
passwordThe password to log in to the serverstringDefault value: ""
request-numSpecifies the number of requests to be sent to the Redis serverintDefault value: 0

通过服务模式模拟缓存穿透的示例

curl -X POST 127.0.0.1:31767/api/attack/redis -H "Content-Type:application/json" -d '{"action":"penetration", ""addr":"127.0.0.1:6379", "request-num":"10000"}'

通过服务模式模拟 Sentinel 重启

模拟 Sentinel 重启的参数

ParameterDescriptionTypeValue
actionAction of the experimentstringset to "restart"
addrThe address and port of Sentinel to be injected into the fault, such as 127.0.0.1:26379stringDefault value: ""
confSpecifies the path of Sentinel configuration file, which is used to recover the SentinelstringDefault value: ""
flush-configForces Sentinel to rewrite its configuration on disk, including the current Sentinel stateboolDefault value: true
passwordThe password to log in to the serverstringDefault value: ""
redis-pathSpecifies the path of redis-server command-line toolstringDefault value: ""

通过服务模式模拟 Sentinel 重启的示例

curl -X POST 127.0.0.1:31767/api/attack/redis -H "Content-Type:application/json" -d '{"action":"restart", ""addr":"127.0.0.1:26379", "conf":"/home/redis-test/sentinel-26379.conf"}'

通过服务模式模拟 Sentinel 停止

模拟 Sentinel 停止的参数

ParameterDescriptionTypeValue
actionAction of the experimentstringset to "stop"
addrThe address and port of Sentinel to be injected into the fault, such as 127.0.0.1:26379stringDefault value: ""
confSpecifies the path of Sentinel configuration file, which is used to recover the SentinelstringDefault value: ""
flush-configForces Sentinel to rewrite its configuration on disk, including the current Sentinel stateboolDefault value: true
passwordThe password to log in to the serverstringDefault value: ""
redis-pathSpecifies the path of redis-server command-line toolstringDefault value: ""

通过服务模式模拟 Sentinel 停止的示例

curl -X POST 127.0.0.1:31767/api/attack/redis -H "Content-Type:application/json" -d '{"action":"stop", ""addr":"127.0.0.1:26379", "conf":"/home/redis-test/sentinel-26379.conf"}'