模拟时间故障
非官方测试版翻译
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
TimeChaos 介绍
Chaos Mesh 提供了 TimeChaos 实验类型。您可以使用该实验类型模拟时间偏移场景。本文档介绍如何创建 TimeChaos 实验及其关联的配置文件。
备注
TimeChaos 仅影响容器 PID 命名空间中的 PID 1 进程和 PID 1 的子进程。例如,通过 kubectl exec 启动的进程不会受到影响。
您可以通过 Chaos Dashboard 或 YAML 配置文件创建实验。
使用 Chaos Dashboard 创建实验
-
打开 Chaos Dashboard,点击页面上的 新建实验 创建新实验:

Create Experiment -
在 选择目标 区域,选择 时钟偏移 并填写时钟 ID 和时间偏移量:

TimeChaos 实验 -
填写实验信息,并指定实验范围和计划的实验持续时间:

实验信息 -
提交实验信息。
使用 YAML 文件创建实验
-
将实验配置写入 YAML 配置文件。以下示例使用
time-shift.yaml文件:apiVersion: chaos-mesh.org/v1alpha1
kind: TimeChaos
metadata:
name: time-shift-example
namespace: chaos-mesh
spec:
mode: one
selector:
labelSelectors:
'app': 'app1'
timeOffset: '-10m100ns'此实验配置将使指定 Pod 中进程的时间向前偏移 10 分钟 100 纳秒。
-
配置文件准备完成后,使用
kubectl创建实验:kubectl apply -f time-shift.yaml
YAML 配置文件中的字段如下表所述:
| Parameter | Type | Note | Default value | Required | Example |
|---|---|---|---|---|---|
| timeOffset | string | Specifies the length of time offset. | None | Yes | -5m |
| clockIds | []string | Specifies the ID of clock that will be offset. See the | ["CLOCK_REALTIME"] | No | ["CLOCK_REALTIME", "CLOCK_MONOTONIC"] |
| mode | string | Specifies the mode of the experiment. The mode options include one (selecting a random Pod), all (selecting all eligible Pods), fixed (selecting a specified number of eligible Pods), fixed-percent (selecting a specified percentage of Pods from the eligible Pods), and random-max-percent (selecting the maximum percentage of Pods from the eligible Pods). | None | Yes | one |
| value | string | Provides parameters for the mode configuration, depending on mode.For example, when mode is set to fixed-percent, value specifies the percentage of Pods. | None | No | 1 |
| containerNames | []string | Specifies the name of the container into which the fault is injected. | None | No | ["nginx"] |
| selector | struct | Specifies the target Pod. For details, refer to Define the experiment scope. | None | Yes |