一、前言
很久之前曾基於linux內核自帶的TC和netem模擬一些公網中遇到的極端情況(延遲、丟包、重複、損壞和亂序等),驗證了我們傳輸程序的健壯性!
具體細節可見這篇老博客: https://blog.csdn.net/u013128262/article/details/84784663
最近在復現kafka生產端一個timeout異常場景時,發現之前方案時因爲內核和OS版本問題有些差異而無法直接使用老的命令,便把目光迅速調整到阿里開源的這款混沌工程新貴ChaosBlade(https://github.com/chaosblade-io/chaosblade
)。
二、ChaosBlade簡介
ChaosBlade 是阿里巴巴開源的一款遵循混沌工程原理和混沌實驗模型的實驗注入工具,幫助企業提升分佈式系統的容錯能力,並且在企業上雲或往雲原生系統遷移過程中業務連續性保障。
ChaosBlade 不僅使用簡單,而且支持豐富的實驗場景,場景包括:
- 基礎資源:比如 CPU、內存、網絡、磁盤、進程等實驗場景;
- Java 應用:比如數據庫、緩存、消息、JVM 本身、微服務等,還可以指定任意類方法注入各種複雜的實驗場景;
- C++ 應用:比如指定任意方法或某行代碼注入延遲、變量和返回值篡改等實驗場景;
- Docker 容器:比如殺容器、容器內 CPU、內存、網絡、磁盤、進程等實驗場景;
- 雲原生平臺:比如 Kubernetes 平臺節點上 CPU、內存、網絡、磁盤、進程實驗場景,Pod 網絡和 Pod 本身實驗場景如殺 Pod,容器的實驗場景如上述的 Docker 容器實驗場景;
ChaosBlade還提供混沌實驗管理工具,包含創建實驗、銷燬實驗、查詢實驗、實驗環境準備、實驗環境撤銷等命令,是混沌實驗的執行工具,執行方式包含 CLI 和 HTTP 兩種。提供完善的命令、實驗場景、場景參數說明,操作簡潔清晰。
所以ChaosBlade是可以非常方便地支持網絡故障模擬場景!
三、使用ChaosBlade實現網絡故障模擬
Talk is cheap,just do it!
1、下載程序包
直接從github上下載最新的程序包: https://github.com/chaosblade-io/chaosblade/releases
2、ChaosBlade支持的網絡故障實驗
[$] <> ./blade create network -h
Network experiment
Usage:
blade create network [flags]
blade create network [command]
Examples:
network delay --interface eth0 --time 3000
# You can execute "blade query network interface" command to query the interfaces
Available Commands:
corrupt Corrupt experiment
delay Delay experiment
dns Dns experiment
drop Drop experiment
duplicate Duplicate experiment
loss Loss network package
occupy Occupy the specify port
reorder Reorder experiment
Flags:
-h, --help help for network
Global Flags:
-d, --debug Set client to DEBUG mode
--uid string Set Uid for the experiment, adapt to docker
從helper看出ChaosBlade支持一下網絡故障模擬:
- corrupt Corrupt experiment 網絡包損壞實驗場景
- delay Delay experiment 網絡延遲實驗場景
- dns Dns experiment 篡改 dns 域名解析實驗場景
- drop Drop experiment 網絡屏蔽實驗場景
- duplicate Duplicate experiment 網絡屏蔽只支持端口,不支持整個網卡,具有侷限性,建議使用網絡丟包 100% 來替代此命令
- loss Loss network package 網絡丟包實驗場景
- occupy Occupy the specify port 網絡本地端口占用
- reorder Reorder experiment 網絡包重排序實驗場景
更多操作詳見官方入門手冊: https://github.com/chaosblade-io/chaosblade/wiki/%E6%96%B0%E6%89%8B%E6%8C%87%E5%8D%97
3、使用網絡丟包實驗場景
復現kafka生產端timeout場景的話,直接使用最常用的網絡丟包實驗即可。
Loss network package
支持的參數:
--destination-ip string destination ip. Support for using mask to specify the ip range such as 92.168.1.0/24 or comma separated multiple ips, for example 10.0.0.1,11.0.0.1.
--exclude-ip string Exclude ips. Support for using mask to specify the ip range such as 92.168.1.0/24 or comma separated multiple ips, for example 10.0.0.1,11.0.0.1
--exclude-port string Exclude local ports. Support for configuring multiple ports, separated by commas or connector representing ranges, for example: 22,8000. This flag is invalid when --local-port or --remote-port is specified
--force Forcibly overwrites the original rules
--ignore-peer-port ignore excluding all ports communicating with this port, generally used when the ss command does not exist
--interface string Network interface, for example, eth0 (required)
--local-port string Ports for local service. Support for configuring multiple ports, separated by commas or connector representing ranges, for example: 80,8000-8080
--percent string loss percent, [0, 100] (required)
--remote-port string Ports for remote service. Support for configuring multiple ports, separated by commas or connector representing ranges, for example: 80,8000-8080
--timeout string set timeout for experiment
我們實際的一個demo:
# eth0發往遠端9092端口的數據包將持續120s丟包5%
blade create network loss --percent 5 --interface eth0 --remote 9092 --timeout 120
執行完會有一個狀態返回值:
{"code":200,"success":true,"result":"5cbde40d2594f891"}
通過status
參數可以查詢到實際的狀態
blade status 5cbde40d2594f891
四、實現分析
ChaosBlade功能強大,並且提供了便捷的使用接口和管理工具。
作爲一個技術人不但要用好工具,更要了解工具的運行原理!
ChaosBlade基礎資源相關的實現都在chaosblade-exec-os這個模塊。
我們先看看程序入口:
https://github.com/chaosblade-io/chaosblade-exec-os/blob/master/exec/network_loss.go
此處主要還是一些業務邏輯,沒有核心實現。
https://github.com/chaosblade-io/chaosblade-exec-os/blob/master/exec/bin/tcnetwork/tcnetwork.go
不難看出ChaosBlade在網絡故障模擬底層技術仍然是TC和netem這2大殺器。