一、helm命令安裝。
檢查helm命令是否存在:helm version
安裝helm命令:
參考資料:https://blog.csdn.net/zhoumengshun/article/details/108161015
參考官網:
https://helm.sh/docs/intro/install/
https://helm.sh/zh/docs/intro/install/
二、安裝chaosblade。
(1)檢查chaosblade是否存在
helm ls --all chaosblade-operator
(2)安裝chaosblade:
helm v2安裝:helm install --namespace chaosblade --name chaosblade-operator chaosblade-operator-VERSION-v2.tgz
helm v3安裝:helm install chaosblade-operator chaosblade-operator-VERSION-v3.tgz --namespace chaosblade
(3)安裝在 kube-system 命令空間下后,ChaosBlade Operator 啟動后會在每個節點部署 chaosblade-tool Pod 和一個chaosblade-operator Pod。可通過以下命令查看安裝結果
(4)卸載chaosblade步驟。
helm delete chaosblade-operator
查看狀態
helm ls --all chaosblade-operator
徹底刪除:
helm del --purge chaosblade-operator
三、安裝metrics-service服務。
metrics-server是用來擴展k8s的第三方apiserver,其主要作用是收集pod或node上的cpu,內存,磁盤等指標數據,並提供一個api接口供kubectl top命令訪問;默認情況kubectl top 命令是沒法正常使用,其原因是默認apiserver上沒有對應的接口提供收集pod或node的cpu,內存,磁盤等核心指標數據;kubectl top命令主要用來顯示pod/node資源的cpu,內存,磁盤的占用比例;該命令能夠正常使用必須依賴Metrics API。
(1)下載metrics-service的yaml配置文件:
wget https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.4.0/components.yaml
(2)修改配置:
增加配置:- --kubelet-insecure-tls
修改配置:- --kubelet-preferred-address-types=InternalIP
修改鏡像:image: phperall/metrics-server:v0.4.1
注銷健康檢查:
(3)給節點打標簽。
查看components.yaml文件中配置的節點標簽信息:
打標簽:kubectl label nodes node-name kubernetes.io/os=linux (node-name為節點名稱,例如master1、slave1)
(4)配置RBAC授權(配置策略參考官網:https://kubernetes.io/zh/docs/reference/access-authn-authz/rbac/;https://kubernetes.io/zh/docs/reference/access-authn-authz/rbac/#kubectl-auth-reconcile)
[root@k8s-master cfg]# kubectl top pod -A
Error from server (Forbidden): pods.metrics.k8s.io is forbidden: User "system:kube-proxy" cannot list resource "pods" in API group "metrics.k8s.io" at the cluster scope
[root@k8s-master cfg]# kubectl top pod
Error from server (Forbidden): pods.metrics.k8s.io is forbidden: User "system:kube-proxy" cannot list resource "pods" in API group "metrics.k8s.io" in the namespace "default"
配置授權,如下(命名空間根據top命令實際需要訪問的命名空間進行配置):
cat metrics_RBAC_rule.yaml
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
namespace: default
name: metrics-reader
rules:
- apiGroups: ["metrics.k8s.io"]
resources: ["pods"]
verbs: ["get", "watch", "list"]
- apiGroups: ["metrics.k8s.io"]
resources: ["nodes"]
verbs: ["get", "watch", "list"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: read-pods
namespace: default
subjects:
- kind: User
name: system:kube-proxy #用戶名稱
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: metrics-reader
apiGroup: rbac.authorization.k8s.io
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: metrics-reader
rules:
- apiGroups: ["metrics.k8s.io"]
resources: ["pods"]
verbs: ["get", "watch", "list"]
- apiGroups: ["metrics.k8s.io"]
resources: ["nodes"]
verbs: ["get", "watch", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: metrics
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: metrics-reader
subjects:
- kind: User
name: system:kube-proxy #用戶名稱
apiGroup: rbac.authorization.k8s.io
配置生效:
測試應用 RBAC 對象的清單文件,顯示將要進行的更改:kubectl auth reconcile -f metrics_RBAC_rule.yaml --dry-run
應用 RBAC 對象的清單文件,保留角色中的額外權限和綁定中的其他主體:kubectl auth reconcile -f metrics_RBAC_rule.yaml
四、故障實驗
(1)注入故障(更多實驗文件參考:https://github.com/chaosblade-io/chaosblade-operator/tree/v1.3.0/examples)
kubectl apply -f chaosblade_cpu_load.yaml
(2)銷毀故障
根據實驗名稱停止:
kubectl get blade
kubectl delete blade names
通過yaml配置文件停止
kubectl delete -f chaosblade_cpu_load.yaml
通過blade命令停止
此方式僅限使用 blade 創建的實驗,使用以下命令停止:
blade destroy <UID>
五、卸載chaosblade
執行 helm del --purge chaosblade-operator 卸載即可,將會停止全部實驗,刪除所有創建的資源