模拟网络故障

    本文主要介绍如何使用 Chaosd 模拟网络故障场景。该功能通过使用 iptables、ipsets、tc 等工具修改网络路由、流量控制来模拟网络故障。

    请确保 Linux 内核拥有 请确保 Linux 内核拥有 NET_SCH_NETEM 模块。对于 CentOS,可以通过 kernel-modules-extra 包安装该模块,大部分其他发行版已默认安装相应模块。

    本节介绍如何在命令行模式创建网络故障实验。

    在创建网络故障实验前,可以运行以下命令查看 Chaosd 支持的网络故障类型:

    输出结果如下所示:

    1. Usage:
    2. chaosd attack network [command]
    3. Available Commands:
    4. corrupt corrupt network packet
    5. delay delay network
    6. duplicate duplicate network packet
    7. loss loss network packet
    8. Flags:
    9. -h, --help help for network
    10. Global Flags:
    11. --log-level string the log level of chaosd, the value can be 'debug', 'info', 'warn' and 'error'
    12. Use "chaosd attack network [command] --help" for more information about a command.

    目前 Chaosd 支持模拟网络包错误(corrupt)、延迟(delay)、重复(duplicate)、丢失(loss)四种实验场景。

    可以运行以下命令,查看模拟网络包错误场景支持的配置:

    1. chaosd attack network corrupt --help

    输出结果如下所示:

    1. corrupt network packet
    2. Usage:
    3. chaosd attack network corrupt [flags]
    4. Flags:
    5. -c, --correlation string correlation is percentage (10 is 10%) (default "0")
    6. -d, --device string the network interface to impact
    7. -e, --egress-port string only impact egress traffic to these destination ports, use a ',' to separate or to indicate the range, such as 80, 8001:8010. It can only be used in conjunction with -p tcp or -p udp
    8. -H, --hostname string only impact traffic to these hostnames
    9. -i, --ip string only impact egress traffic to these IP addresses
    10. --percent string percentage of packets to corrupt (10 is 10%) (default "1")
    11. -p, --protocol string only impact traffic using this IP protocol, supported: tcp, udp, icmp, all
    12. -s, --source-port string only impact egress traffic from these source ports, use a ',' to separate or to indicate the range, such as 80, 8001:8010. It can only be used in conjunction with -p tcp or -p udp
    13. Global Flags:
    14. --log-level string the log level of chaosd, the value can be 'debug', 'info', 'warn' and 'error'

    相关配置说明如下所示:

    1. chaosd attack network delay --help

    输出结果如下所示:

    1. delay network
    2. Usage:
    3. chaosd attack network delay [flags]
    4. Flags:
    5. -c, --correlation string correlation is percentage (10 is 10%) (default "0")
    6. -d, --device string the network interface to impact
    7. -e, --egress-port string only impact egress traffic to these destination ports, use a ',' to separate or to indicate the range, such as 80, 8001:8010. It can only be used in conjunction with -p tcp or -p udp
    8. -h, --help help for delay
    9. -H, --hostname string only impact traffic to these hostnames
    10. -i, --ip string only impact egress traffic to these IP addresses
    11. -j, --jitter string jitter time, time units: ns, us (or µs), ms, s, m, h.
    12. -l, --latency string delay egress time, time units: ns, us (or µs), ms, s, m, h.
    13. -p, --protocol string only impact traffic using this IP protocol, supported: tcp, udp, icmp, all
    14. -s, --source-port string only impact egress traffic from these source ports, use a ',' to separate or to indicate the range, such as 80, 8001:8010. It can only be used in conjunction with -p tcp or -p udp
    15. Global Flags:
    16. --log-level string the log level of chaosd, the value can be 'debug', 'info', 'warn' and 'error'

    相关配置说明如下所示:

    可以运行以下命令,查看模拟网络包重复场景支持的配置:

    输出结果如下所示:

    1. duplicate network packet
    2. Usage:
    3. chaosd attack network duplicate [flags]
    4. Flags:
    5. -c, --correlation string correlation is percentage (10 is 10%) (default "0")
    6. -d, --device string the network interface to impact
    7. -h, --help help for duplicate
    8. -H, --hostname string only impact traffic to these hostnames
    9. --percent string percentage of packets to duplicate (10 is 10%) (default "1")
    10. -p, --protocol string only impact traffic using this IP protocol, supported: tcp, udp, icmp, all
    11. -s, --source-port string only impact egress traffic from these source ports, use a ',' to separate or to indicate the range, such as 80, 8001:8010. It can only be used in conjunction with -p tcp or -p udp
    12. Global Flags:
    13. --log-level string the log level of chaosd, the value can be 'debug', 'info', 'warn' and 'error'

    相关配置说明如下所示:

    可以运行以下命令,查看模拟网络包丢失场景支持的配置:

    1. chaosd attack network loss --help

    输出结果如下所示:

    1. loss network packet
    2. Usage:
    3. chaosd attack network loss [flags]
    4. Flags:
    5. -c, --correlation string correlation is percentage (10 is 10%) (default "0")
    6. -d, --device string the network interface to impact
    7. -e, --egress-port string only impact egress traffic to these destination ports, use a ',' to separate or to indicate the range, such as 80, 8001:8010. It can only be used in conjunction with -p tcp or -p udp
    8. -h, --help help for loss
    9. -H, --hostname string only impact traffic to these hostnames
    10. -i, --ip string only impact egress traffic to these IP addresses
    11. --percent string percentage of packets to drop (10 is 10%) (default "1")
    12. -p, --protocol string only impact traffic using this IP protocol, supported: tcp, udp, icmp, all
    13. -s, --source-port string only impact egress traffic from these source ports, use a ',' to separate or to indicate the range, such as 80, 8001:8010. It can only be used in conjunction with -p tcp or -p udp
    14. Global Flags:
    15. --log-level string the log level of chaosd, the value can be 'debug', 'info', 'warn' and 'error'

    相关配置说明如下所示:

    模拟网络包错误:

    1. chaosd attack network corrupt -d eth0 -i 172.16.4.4 --percent 50
    1. Attack network successfully, uid: 4eab1e62-8d60-45cb-ac85-3c17b8ac4825

    模拟网络包延迟:

    输出结果如下所示:

    1. Attack network successfully, uid: 4b23a0b5-e193-4b27-90a7-3e04235f32ab

    模拟网络包重复:

    1. chaosd attack network duplicate -d eth0 -i 172.16.4.4 --percent 50

    输出结果如下所示:

    1. Attack network successfully, uid: 7bcb74ee-9101-4ae4-82f0-e44c8a7f113c

    模拟网络包丢失:

    1. chaosd attack network loss -d eth0 -i 172.16.4.4 --percent 50

    输出结果如下所示:

    1. Attack network successfully, uid: 1e818adf-3942-4de4-949b-c8499f120265

    在运行实验时,请注意保存实验的 uid 信息。在不需要网络故障场景时,使用 recover 命令来结束 uid 对应的实验:

    输出结果如下所示:

    (正在持续更新中)