混沌工具之ChaosMesh源码编译安装

Posted zuozewei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了混沌工具之ChaosMesh源码编译安装相关的知识,希望对你有一定的参考价值。

ChaosMesh 如果你想自己改点啥,加点功能,那就得从源码开始编译安装。
本篇就带你来看下如何实现编译安装。

环境要求:

  • 操作系统:macOS 11.1
  • golang 1.15.7

安装依赖:

brew install golang nodejs yarn kind

安装 Docker 和 Docker Registry:

brew install --cask docker
docker run -d -p 5000:5000 --name registry registry:2.7.1

添加环境变量:

echo 'export DOCKER_REGISTRY=localhost:5000' >> ~/.bash_profile
echo 'export PATH=$(go env GOPATH)/bin:${PATH}' >> ~/.bash_profile
source ~/.bash_profile

拉取源码:

git clone https://github.com/chaos-mesh/chaos-mesh.git && cd chaos-mesh
git clone -b v2.0.0-beta.2 https://github.com/chaos-mesh/chaos-mesh.git v2.0.0-b

Kubernetes API 开发框架下载 :

make ensure-all
ensuring kubectl
ROOT=/Users/jinlong/projects/chaos-mesh && source ./hack/lib.sh && hack::ensure_kubectl
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 44.1M  100 44.1M    0     0  1608k      0  0:00:28  0:00:28 --:--:-- 2046k
ensuring helm
ROOT=/Users/jinlong/projects/chaos-mesh && source ./hack/lib.sh && hack::ensure_helm
ensuring kind
ROOT=/Users/jinlong/projects/chaos-mesh && source ./hack/lib.sh && hack::ensure_kind
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   625  100   625    0     0    729      0 --:--:-- --:--:-- --:--:--   728
100 6630k  100 6630k    0     0  1487k      0  0:00:04  0:00:04 --:--:-- 2279k
ensuring kubebuilder
ROOT=/Users/jinlong/projects/chaos-mesh && source ./hack/lib.sh && hack::ensure_kubebuilder
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    69  100    69    0     0     36      0  0:00:01  0:00:01 --:--:--    36
100   124  100   124    0     0     49      0  0:00:02  0:00:02 --:--:--    49
100   645  100   645    0     0    196      0  0:00:03  0:00:03 --:--:--  1135
100 62.8M  100 62.8M    0     0   607k      0  0:01:45  0:01:45 --:--:--  531k
ensuring kustomize
ROOT=/Users/jinlong/projects/chaos-mesh && source ./hack/lib.sh && hack::ensure_kustomize
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   644  100   644    0     0    297      0  0:00:02  0:00:02 --:--:--   297
100 19.4M  100 19.4M    0     0   506k      0  0:00:39  0:00:39 --:--:--  560k
ensuring kubetest2
ROOT=/Users/jinlong/projects/chaos-mesh && source ./hack/lib.sh && hack::ensure_kubetest2
info: downloading kubetest2 v0.1.0
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   633  100   633    0     0    591      0  0:00:01  0:00:01 --:--:--   591
100 2682k  100 2682k    0     0   452k      0  0:00:05  0:00:05 --:--:--  644k
info: downloading kubetest2-kind v0.1.0
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   638  100   638    0     0    699      0 --:--:-- --:--:-- --:--:--   698
100 2751k  100 2751k    0     0   540k      0  0:00:05  0:00:05 --:--:--  832k

测试工具链:

make
GO15VENDOREXPERIMENT="1" CGO_ENABLED=0 GOOS="" GOARCH="" go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.4.1
go: downloading sigs.k8s.io/controller-tools v0.4.1
go: found sigs.k8s.io/controller-tools/cmd/controller-gen in sigs.k8s.io/controller-tools v0.4.1
.........................
go: downloading github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe
b1f488f95040d683c2643a52dfb06d261b49317570f287d3c0fa73546b2ea154
DOCKER_BUILDKIT=1 docker build -t localhost:5000/pingcap/chaos-dashboard:latest --build-arg HTTP_PROXY= --build-arg HTTPS_PROXY= --build-arg UI= --build-arg SWAGGER= --build-arg LDFLAGS="-s -w -X 'github.com/chaos-mesh/chaos-mesh/pkg/version.buildDate=2021-07-02T03:39:50Z' -X 'github.com/chaos-mesh/chaos-mesh/pkg/version.gitCommit=8c584e01bfa384a71bc5cbc9487746a7aaee4103' -X 'github.com/chaos-mesh/chaos-mesh/pkg/version.gitVersion=master-g8c584e01bfa384'" --build-arg CRATES_MIRROR="" images/chaos-dashboard
[+] Building 29.9s (8/8) FINISHED
 => [internal] load build definition from Dockerfile                                           0.0s
 => => transferring dockerfile: 348B                                                           0.0s
 => [internal] load .dockerignore                                                              0.0s
 => => transferring context: 2B                                                                0.0s
 => [internal] load metadata for docker.io/library/debian:buster-slim                         15.4s
 => CACHED [1/3] FROM docker.io/library/debian:buster-slim@sha256:c6e92d5b7730fdfc2753c4cce68  0.0s
 => [internal] load build context                                                              0.9s
 => => transferring context: 38.98MB                                                           0.8s
 => [2/3] RUN apt-get update && apt-get install tzdata -y && rm -rf /var/lib/apt/lists/*      13.7s
 => [3/3] COPY bin/chaos-dashboard /usr/local/bin/chaos-dashboard                              0.3s
 => exporting to image                                                                         0.3s
 => => exporting layers                                                                        0.3s
 => => writing image sha256:b5a025ae1d84a19524dc13f92cc734af07ff1887e41d93b9fc8c416c8301664f   0.0s
 => => naming to localhost:5000/pingcap/chaos-dashboard:latest                                 0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
touch images/chaos-dashboard/.dockerbuilt
  • Kind 新建 Kubernetes 集群

修改 hack/kind-cluster-build.sh 添加一个端口映射方便 chaos-dashboard 访问

  extraPortMappings:
  - containerPort: 30001
    hostPort: 808${i}
    listenAddress: "0.0.0.0"
    protocol: tcp


hack/kind-cluster-build.sh

clusterName: kind
nodeNum: 3
k8sVersion: v1.20.2
volumeNum: 5
registryName: registry
registryPort: 5000
ensuring kind
ensuring kubectl
############# start create cluster:[kind] #############
clean data dir: /Users/jinlong/kind/kind/data
start to create k8s cluster
Creating cluster "kind" ...
 ✓ Ensuring node image (kindest/node:v1.20.2) 🖼 ^[[B^[[B
 ✓ Preparing nodes 📦 📦 📦 📦
 ✓ Writing configuration 📜
 ✓ Starting control-plane 🕹️
 ✓ Installing CNI 🔌
 ✓ Installing StorageClass 💾
 ✓ Joining worker nodes 🚜
Set kubectl context to "kind-kind"
You can now use your cluster with:

kubectl cluster-info --context kind-kind

Not sure what to do next? 😅  Check out https://kind.sigs.k8s.io/docs/user/quick-start/
connect the local docker registry to the cluster network
storageclass.storage.k8s.io/local-storage created
configmap/local-provisioner-config created
daemonset.apps/local-volume-provisioner created
serviceaccount/local-storage-admin created
clusterrolebinding.rbac.authorization.k8s.io/local-storage-provisioner-pv-binding created
clusterrole.rbac.authorization.k8s.io/local-storage-provisioner-node-clusterrole created
clusterrolebinding.rbac.authorization.k8s.io/local-storage-provisioner-node-binding created
namespace/chaos-testing created
############# success create cluster:[kind] #############
To start using your cluster, run:
    export KUBECONFIG=/Users/jinlong/kind/kind/config

检查构建:

# docker ps

构建新镜像

# 只使用 make 不构建 chaos-dashboard
make
# 同时构建 chaos-dashboard
UI=1 make
# make docker-push  # 推送到镜像仓库,目前本机操作不用。如果用阿里云镜像服务,可以修改Makefile。

cd ./api/v1alpha1 ;\\
		/Users/jinlong/go/bin/controller-gen "crd:trivialVersions=true,preserveUnknownFields=false,crdVersions=v1" rbac:roleName=manager-role paths="./..." output:crd:artifacts:config=../../config/crd/bases ;\\
		/Users/jinlong/go/bin/controller-gen "crd:trivialVersions=true,preserveUnknownFields=false,crdVersions=v1" rbac:roleName=manager-role paths="./..." output:crd:artifacts:config=../../helm/chaos-mesh/crds ;
ensuring kustomize
ROOT=/Users/jinlong/projects/chaos-mesh/v2.0.0-beta.2 && source ./hack/lib.sh && hack::ensure_kustomize
/Users/jinlong/projects/chaos-mesh/v2.0.0-beta.2/output/bin/kustomize build config/default > manifests/crd.yaml
rm -rf output/config-v1beta1
cp -r ./config ./output/config-v1beta1
..........................
Done in 354.05s.
hack/embed_ui_assets.sh
+ Preflight check
+ Embed UI assets
go: downloading github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd
go: downloading github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749
writing assets_vfsdata.go
  - Assets handler written to pkg/uiserver/embedded_assets_handler.go
GO15VENDOREXPERIMENT="1" CGO_ENABLED=1 GOOS="" GOARCH="" go build -ldflags "-s -w -X 'github.com/chaos-mesh/chaos-mesh/pkg/version.buildDate=2021-07-03T02:25:13Z' -X 'github.com/chaos-mesh/chaos-mesh/pkg/version.gitCommit=20733ac694d0578fe817a878f42246b295bfd56f' -X 'github.com/chaos-mesh/chaos-mesh/pkg/version.gitVersion=chart-2.0.0-beta.2'" -tags " ui_server" -o images/chaos-dashboard/bin/chaos-dashboard ./cmd/chaos-dashboard/main.go
49c9c07e001012fa3626bd4bc2e53b2573fc5ee22465cfb2719f0d02f62b9734
DOCKER_BUILDKIT=1 docker build -t "localhost:5000"/pingcap/chaos-dashboard:latest --build-arg HTTP_PROXY= --build-arg HTTPS_PROXY= --build-arg UI=1 --build-arg SWAGGER= --build-arg LDFLAGS="-s -w -X 'github.com/chaos-mesh/chaos-mesh/pkg/version.buildDate=2021-07-03T02:25:13Z' -X 'github.com/chaos-mesh/chaos-mesh/pkg/version.gitCommit=20733ac694d0578fe817a878f42246b295bfd56f' -X 'github.com/chaos-mesh/chaos-mesh/pkg/version.gitVersion=chart-2.0.0-beta.2'" --build-arg CRATES_MIRROR="" images/chaos-dashboard
[+] Building 31.9s (8/8) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                    0.0s
 => => transferring dockerfile: 37B                                                                                                                     0.0s
 => [internal] load .dockerignore                                                                                                                       0.0s
 => => transferring context: 2B                                                                                                                         0.0s
 => [internal] load metadata for docker.io/library/debian:buster-slim                                                                                  15.5s
 => [internal] load build context                                                                                                                       5.2s
 => => transferring context: 42.35MB                                                                                                                    5.2s
 => CACHED [1/3] FROM docker.io/library/debian:buster-slim@sha256:c6e92d5b7730fdfc2753c4cce68c90d6c86a6a3391955549f9fe8ad6ce619ce0                      0.0s
 => [2/3] RUN apt-get update && apt-get install tzdata -y && rm -rf /var/lib/apt/lists/*                                                               15.3s
 => [3/3] COPY bin/chaos-dashboard /usr/local/bin/chaos-dashboard                                                                                       0.3s
 => exporting to image                                                                                                                                  0.3s
 => => exporting layers                                                                                                                                 0.2s
 => => writing image sha256:74561dabbe07fb06dd3ee66450fbd1504672a27336c52e5995bd9ec4eb2b89d0                                                            0.0s
 => => naming to localhost:5000/pingcap/chaos-dashboard:latest                                                                                          0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
touch images/chaos-dashboard/.dockerbuilt

如果不构建UI,则访问界面会提示:

加载镜像到 kind

kind load docker-image localhost:5000/pingcap/chaos-mesh:latest
kind load docker-image localhost:5000/pingcap/chaos-daemon:latest
kind load docker-image localhost:5000/pingcap/chaos-dashboard:latest

修改 helm/chaos-mesh/values.yaml
替换 chaos-mesh,chaos-daemon,chaos-dashboard 镜像


修改 chaos-dashboard 端口:

helm 部署

helm install chaos-mesh helm/chaos-mesh --namespace=chaos-testing --set chaosDaemon.runtime=containerd --set chaosDaemon.socketPath=/run/containerd/containerd.sock

检查:

kubectl get pod,svc -n chaos-testing


更新:

helm upgrade chaos-mesh helm/chaos-mesh --namespace=chaos-testing

以上是关于混沌工具之ChaosMesh源码编译安装的主要内容,如果未能解决你的问题,请参考以下文章

混沌工程之ChaosMesh使用之模拟CPU使用率

混沌工程之ChaosMesh使用之模拟CPU使用率

混沌工程之ChaosMesh使用之模拟POD网络丢包

混沌工程之ChaosMesh使用之模拟POD网络丢包

混沌工程之ChaosMesh使用之模拟网络Duplicate包

混沌工程之ChaosMesh使用之模拟网络Duplicate包