采用两个工具
chaosblade
chaosblade-operator
项目介绍
ChaosBlade 是阿里巴巴开源的一款遵循混沌工程原理和混沌实验模型的实验注入工具,帮助企业提升分布式系统的容错能力,并且在企业上云或往云原生系统迁移过程中业务连续性保障。
Chaosblade 是内部 MonkeyKing 对外开源的项目,其建立在阿里巴巴近十年故障测试和演练实践基础上,结合了集团各业务的最佳创意和实践。
ChaosBlade 不仅使用简单,而且支持丰富的实验场景,场景包括:
- 基础资源:比如 CPU、内存、网络、磁盘、进程等实验场景;
- Java 应用:比如数据库、缓存、消息、JVM 本身、微服务等,还可以指定任意类方法注入各种复杂的实验场景;
- C++ 应用:比如指定任意方法或某行代码注入延迟、变量和返回值篡改等实验场景;
- Docker 容器:比如杀容器、容器内 CPU、内存、网络、磁盘、进程等实验场景;
- 云原生平台:比如 Kubernetes 平台节点上 CPU、内存、网络、磁盘、进程实验场景,Pod 网络和 Pod 本身实验场景如杀 Pod,容器的实验场景如上述的 Docker 容器实验场景;
自动注入脚本
create_k8s_yaml.sh
#!/bin/bash
if [ ! -n "$1" ] ;then
echo "$1 is namespaces,please input !!"
exit
fi
str_date=`date +%Y%m%d%H`
namespaces=${1}
function new_yaml(){
#### sed file
cp pod-cpu-load-by-name.yml tmp_pod-cpu-load-${pod_name}-${namespaces}-${str_date}.yml
sed -i 's/pod-example-01/'"${pod_name}"'/g' tmp_pod-cpu-load-${pod_name}-${namespaces}-${str_date}.yml
sed -i 's/namespaces-example/'"${namespaces}"'/g' tmp_pod-cpu-load-${pod_name}-${namespaces}-${str_date}.yml
sed -i 's/cpu-load/'"cpu-load-${pod_name}"'/g' tmp_pod-cpu-load-${pod_name}-${namespaces}-${str_date}.yml
#cat tmp_pod-cpu-load-${pod_name}-${namespaces}-${str_date}.yml
echo "please kubectl apply -f tmp_pod-cpu-load-${pod_name}-${namespaces}-${str_date}.yml !"
}
tmp_file="./tmp_server_list.txt"
touch ${tmp_file}
kubectl get pod -n ${namespaces} -o wide|grep -E "hello"|awk '{print $1 "," $7}'>${tmp_file}
if [[ ! -s ${tmp_file} ]]; then
exit "not have file !!"
fi
function remote_host(){
pod_host=${1}
pod_name=${2}
docker_id=`ssh ${pod_host} -n docker ps | grep ${pod_name} | grep -v pause |awk '{print $1}'`
docker_inspect_source=`ssh $pod_host -n docker inspect ${docker_id}|grep MergedDir|awk -F[\"] '{print $4}'`
ssh ${pod_host} -n "cd ${docker_inspect_source} && mkdir -p opt/chaosblade/bin && chmod 777 -R opt/chaosblade "&
echo ${2}
}
while read line
do
#echo "line is ====>"$line
pod_name=`echo $line|awk -F"," '{print $1}'`
pod_host=`echo $line|awk -F"," '{print $2}'`
remote_host ${pod_host} ${pod_name}
new_yaml ${pod_name} ${namespaces}
done < ${tmp_file}
pod-cpu-load-by-name.yml
apiVersion: chaosblade.io/v1alpha1
kind: ChaosBlade
metadata:
name: cpu-load
spec:
experiments:
- scope: pod
target: cpu
action: fullload
desc: "increase node cpu load by names"
matchers:
- name: namespace
value:
- "namespaces-example"
- name: names
value:
- "pod-example-01"
- name: cpu-percent
value:
- "80"
- scope: pod
target: mem
action: "load"
desc: "increase node mem load by names"
matchers:
- name: namespace
value:
- "namespaces-example"
- name: names
value:
- "pod-example-01"
- name: mem-percent
value:
- "80"
- name: mode
value:
- "ram"