模拟 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 item | Abbreviation | Type | Description | Value |
|---|---|---|---|---|
addr | a | string | The address and port of Redis server to be injected into the fault, for example 127.0.0.1:6379 | Default value: "" |
expiration | None | string | The specified key will be expired after expiration arrives | Default value: "0". Make sure that the string is in the format supported by time.Duration |
key | k | string | The key to be expired | Default value: "", which means the expiration is set for all keys |
option | None | string | Additional options for expiration. Only versions of Redis after 7.0.0 support this flag | Default value: "". Only NX, XX, GT, and LT are supported |
password | p | string | The password to log in to the server | Default 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 item | Abbreviation | Type | Description | Value |
|---|---|---|---|---|
addr | a | string | The address and port of Redis server to be injected into the fault, such as 127.0.0.1:6379 | Default value: "" |
password | p | string | The password to log in to the server | Default value: "" |
percent | None | string | Specifies maxmemory as a percentage of the original value | Default value: "" |
size | s | string | Specifies the size of maxmemory | Default 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 item | Abbreviation | Type | Description | Value |
|---|---|---|---|---|
addr | a | string | The address and port of Redis server to be injected into the fault, such as 127.0.0.1:6379 | Default value: "" |
password | p | string | The password to log in to the server | Default value: "" |
request-num | None | int | Specifies the number of requests to be sent to the Redis server | Default 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 item | Abbreviation | Type | Description | Value |
|---|---|---|---|---|
addr | a | string | The address and port of Sentinel to be injected into the fault, such as 127.0.0.1:26379 | Default value: "" |
conf | c | string | Specifies the path of Sentinel config file, this file will be used to revover the Sentinel | Default value: "" |
flush-config | None | bool | Forces Sentinel to rewrite its configuration on disk, including the current Sentinel state | Default value: true |
password | p | string | The password to log in to the server | Default value: "" |
redis-path | None | string | Specifies the path of redis-server command-line tool | Default 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 item | Abbreviation | Type | Description | Value |
|---|---|---|---|---|
addr | a | string | The address and port of Sentinel to be injected into the fault, such as 127.0.0.1:26379 | Default value: "" |
conf | c | string | Specifies the path of Sentinel configuration file, which is used to recover the Sentinel | Default value: "" |
flush-config | None | bool | Forces Sentinel to rewrite its configuration on disk, including the current Sentinel state | Default value: true |
password | p | string | The password to log in to the server | Default value: "" |
redis-path | None | string | Specifies the path of redis-server command-line tool | Default value: "" |
模拟哨兵停止示例
chaosd attack redis sentinel-stop -a 127.0.0.1:26379 --conf /home/redis-test/sentinel-26379.conf
使用服务模式创建 Redis 故障实验
要通过服务模式创建实验,请按以下步骤操作:
-
在服务模式下运行 Chaosd:
chaosd server --port 31767 -
向 Chaosd 服务的
/api/attack/redis路径发送POSTHTTP 请求: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 请求。
使用服务模式模拟缓存过期
模拟缓存过期参数
| Parameter | Description | Type | Value |
|---|---|---|---|
action | Action of the experiment | string | set to "corrupt" |
addr | The address and port of Redis server to be injected into the fault, such as 127.0.0.1:6379 | string | Default value: "" |
expiration | The specified key will be expired after expiration arrives | string | Default value: "0". Make sure that the string is in the format supported by time.Duration |
key | The key to be expired | string | Default value: "", which means the expiration is set for all keys |
option | Additional options for expiration. Only versions of Redis after 7.0.0 support this flag | string | Default value: "". Only NX, XX, GT, and LT are supported |
password | The password to log in to the server | Default 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"}'
通过服务模式模拟缓存限制
模拟缓存限制的参数
| Parameter | Description | Type | Value |
|---|---|---|---|
action | Action of the experiment | string | set to "cacheLimit" |
addr | The address and port of Redis server to be injected into the fault, such as 127.0.0.1:6379 | string | Default value: "" |
password | The password to log in to the server | string | Default value: "" |
percent | Specifies maxmemory as a percentage of the original value | string | Default value: "" |
size | Specifies the size of maxmemory | string | Default 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%"}'
通过服务模式模拟缓存穿透
模拟缓存穿透的参数
| Parameter | Description | Type | Value |
|---|---|---|---|
action | Action of the experiment | string | set to "penetration" |
addr | The address and port of Redis server to be injected into the fault, such as 127.0.0.1:6379 | string | Default value: "" |
password | The password to log in to the server | string | Default value: "" |
request-num | Specifies the number of requests to be sent to the Redis server | int | Default 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 重启的参数
| Parameter | Description | Type | Value |
|---|---|---|---|
action | Action of the experiment | string | set to "restart" |
addr | The address and port of Sentinel to be injected into the fault, such as 127.0.0.1:26379 | string | Default value: "" |
conf | Specifies the path of Sentinel configuration file, which is used to recover the Sentinel | string | Default value: "" |
flush-config | Forces Sentinel to rewrite its configuration on disk, including the current Sentinel state | bool | Default value: true |
password | The password to log in to the server | string | Default value: "" |
redis-path | Specifies the path of redis-server command-line tool | string | Default 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 停止的参数
| Parameter | Description | Type | Value |
|---|---|---|---|
action | Action of the experiment | string | set to "stop" |
addr | The address and port of Sentinel to be injected into the fault, such as 127.0.0.1:26379 | string | Default value: "" |
conf | Specifies the path of Sentinel configuration file, which is used to recover the Sentinel | string | Default value: "" |
flush-config | Forces Sentinel to rewrite its configuration on disk, including the current Sentinel state | bool | Default value: true |
password | The password to log in to the server | string | Default value: "" |
redis-path | Specifies the path of redis-server command-line tool | string | Default 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"}'