用 Weave Scope 監控集羣
創建 Kubernetes 集羣並部署容器化應用只是第一步。一旦集羣運行起來,我們需要確保一起正常,所有必要組件就位並各司其職,有足夠的資源滿足應用的需求。Kubernetes 是一個複雜系統,運維團隊需要有一套工具幫助他們獲知集羣的實時狀態,併爲故障排查提供及時和準確的數據支持。
Kubernetes 常用的監控方案
Weave Scope
Weave Scope 是 Docker 和 Kubernetes 可視化監控工具。Scope 提供了至上而下的集羣基礎設施和應用的完整視圖,用戶可以輕鬆對分佈式的容器化應用進行實時監控和問題診斷。
安裝 Scope
安裝 Scope 的方法很簡單,執行如下命令:
kubectl apply -f "https://cloud.weave.works/k8s/scope.yaml?k8s-version=$(kubectl version | base64 | tr -d '\n')&k8s-service-type=NodePort"
部署成功後,有如下相關組件:
[root@k8s-master ~]# kubectl apply -f "https://cloud.weave.works/k8s/scope.yaml?k8s-version=$(kubectl version | base64 | tr -d '\n')&k8s-service-type=NodePort"
namespace/weave unchanged
serviceaccount/weave-scope created
clusterrole.rbac.authorization.k8s.io/weave-scope configured
clusterrolebinding.rbac.authorization.k8s.io/weave-scope configured
deployment.apps/weave-scope-app created
service/weave-scope-app created
deployment.apps/weave-scope-cluster-agent created
daemonset.apps/weave-scope-agent created
[root@k8s-master ~]# kubectl get pod -n weave
NAME READY STATUS RESTARTS AGE
weave-scope-agent-bct26 1/1 Running 0 30s
weave-scope-agent-vzdbr 0/1 ContainerCreating 0 30s
weave-scope-agent-xvwsp 0/1 ContainerCreating 0 30s
weave-scope-app-cf544d8c4-v9x5z 1/1 Running 0 30s
weave-scope-cluster-agent-6466d94ccf-r4vql 0/1 ContainerCreating 0 30s
[root@k8s-master ~]# kubectl get pod -n weave
NAME READY STATUS RESTARTS AGE
weave-scope-agent-bct26 1/1 Running 0 57s
weave-scope-agent-vzdbr 1/1 Running 0 57s
weave-scope-agent-xvwsp 1/1 Running 0 57s
weave-scope-app-cf544d8c4-v9x5z 1/1 Running 0 57s
weave-scope-cluster-agent-6466d94ccf-r4vql 1/1 Running 0 57s
[root@k8s-master ~]# kubectl get svc -n weave
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
weave-scope-app NodePort 10.108.59.226 <none> 80:31013/TCP 68s
[root@k8s-master ~]# kubectl get deployments. -n weave
NAME READY UP-TO-DATE AVAILABLE AGE
weave-scope-app 1/1 1 1 81s
weave-scope-cluster-agent 1/1 1 1 80s
DaemonSet weave-scope-agent,集羣每個節點上都會運行的 scope agent 程序,負責收集數據。
Deployment weave-scope-app,scope 應用,從 agent 獲取數據,通過 Web UI 展示並與用戶交互。
Service weave-scope-app,默認是 ClusterIP 類型,已經在上面的命令中添加了參數k8s-service-type=NodePort修改爲 NodePort。
使用 Scope
瀏覽器訪問 http://192.168.56.106:31013/,Scope 默認顯示當前所有的 Controller(Deployment、DaemonSet 等)。
拓撲結構
Scope 會自動構建應用和集羣的邏輯拓撲。比如點擊頂部 PODS,會顯示所有 Pod 以及 Pod 之間的依賴關係。
點擊 HOSTS,會顯示各個節點之間的關係。
實時資源監控
可以在 Scope 中查看資源的 CPU 和內存使用情況。
支持的資源有 Host、Pod 和 Container。
在線操作
Scope 還提供了便捷的在線操作功能,比如選中某個 Host,點擊 >_ 按鈕可以直接在瀏覽器中打開節點的命令行終端:
點擊 Deployment 的 + 可以執行 Scale Up 操作:
可以查看 Pod 的日誌:
可以 attach、restart、stop 容器,以及直接在 Scope 中排查問題:
強大的搜索功能
Scope 支持關鍵字搜索和定位資源。
還可以進行條件搜索,比如查找和定位 MEMORY > 100M 的 Pod。
Weave Scope 界面極其友好,操作簡潔流暢。