Prometheus 栈包括:
Prometheus
Alertmanager
kube-state-metrics
node-exporter
Grafana
我们还可以在其中包括警报和仪表板。
Capacity planningCluster healthDeploymentsk8s cluster rsrc usek8s node rsrc usek8s resources clusterk8s resources namespacek8s resources podkube DNSkubeletNodesPodsStatefulsetKubernetes all-nodesKubernetes cluster-allKubernetes pods-clusterKubernetes resources-requests
Component Down (API Server、Kubelet、Node exporter、Alertmanager 以及 Prometheus 等等)
Pod alerts (Crashloopbackoff、Pending,尚未就绪)
Workload controller alerts (Replicas Mismatch、DaemonSet NotScheduled、DaemonSet MisScheduled、Job Failed 和 Long-running Jobs)
Resources alerts (Cpu overcommit、Memory overcommit、Quota exceeded)
Persistent Volume alerts
Kube API error 和 Client alerts
Prometheus configuration error alerts
第一步:从 GitHub 克隆 Prometheus-grafana 仓库:
git clone URL to GIT REPO
第二步:创建一个 manifest 文件:
cd Prometheus-grafanaawk ‘FNR==1 {print “ — -”}{print}’ manifests/* > “prometheus_grafana_manifest.yaml”
第三步:安装 Prometheus-Grafana 栈:
kubectl apply -f prometheus_grafana_manifest.yaml
第四步:为 Grafana 创建 ingress:
如果集群中有一个 ingress 控制器,请更新 grafana-ingress.yaml 文件中的域和 ingress 类,并创建 ingress 资源。
kubectl apply -f grafana-ingress.yaml
如果没有 ingress 控制器,仍然可以使用负载平衡服务或节点端口服务,或使用 Kube-proxy 访问 grafana 。
Grafana 的默认凭据为:
Username:Cloud
Password:Cloud
Grafana 登陆页面:
你可以根据自己的兴趣设置自己的用户名和密码。
在更新凭据机密文件中的值之前,必须以 base64 格式对用户名和密码进行编码。
echo “myuser” | base64bXl1c2VyCg==echo “HgTf0n9L@wrd” | base64 HgTf0n9L@wrdGHJKLYuiGFDYH=
现在,我们将使用 manifests 目录下的 2-grafana-cerdentials-secret.yaml 中用 base64 编码的用户名和密码来“更新 admin-user 和 admin-password 的值”。
apiVersion: v1kind: Secretmetadata:name: grafananamespace: prometheuslabels:app.kubernetes.io/name: prometheusapp.kubernetes.io/component: grafanatype: Opaquedata:admin-user: jdvchksojs)==admin-password: GHJKLYuiGFDYH=
运行命令:
kubectl apply -f 2-grafana-credentials-secret.yaml
如果 Grafana 已经安装并正在运行,则必须删除现有的 Pod。我们将看到一个新的 Pod,具有最新配置和更新配置。
你可以通过解码值从 secret 中获得凭据:
echo "Username: $(kubectl get secret grafana --namespace prometheus \--output=jsonpath='{.data.admin-user}' | base64 --decode)"echo "Password: $(kubectl get secret grafana --namespace prometheus \--output=jsonpath='{.data.admin-password}' | base64 --decode)"
我们还可以看到,在 Prometheus 中,无需身份验证即可登录到 Web 界面。
Prometheus Web 界面:
在安装栈时,必须提供警报接收器的详细信息。
否则,你将永远不会收到有关集群状态变更和资源利用率的通知。
我们可以根据需要更改配置。
Alert Manager 配置了一个以 YAML 格式编写的配置文件,该文件定义了规则、通知路由和接收器。
下面是 Email、Slack 和 Webhook 接收器的配置示例:
Email :
global:resolve_timeout: 5mreceivers:- name: email_configemail_configs:- to: "< to_address >"from: "< from_address >"smarthost: "< smtp_host:port >"auth_username: "< smtp_username >"auth_password: "< smtp_password >"route:group_by:- jobreceiver: email_configgroup_interval: 5mgroup_wait: 30srepeat_interval: 30m
Slack :
global:resolve_timeout: 5mslack_api_url: "< slack_webhook_url >"receivers:- name: "slack-notifications"slack_configs:- channel: "#alerts"route:group_by:- jobreceiver: "slack-notifications"group_interval: 5mgroup_wait: 30srepeat_interval: 30m
Web-hook :
global:resolve_timeout: 5mreceivers:- name: webhookwebhook_configs:- url: "< webhook_url >"route:group_by:- jobrepeat_interval: 30mgroup_interval: 5mgroup_wait: 30sreceiver: webhook
如上所述,在 mainifests 目录下的 1-alermanager-configmap.yaml 文件中更新配置,并应用配置。
kubectl apply -f 1-alertmanager-configmap.yaml
更新 coonfigmap 后,重启正在运行的 alertmanager pod。将使用更新后的配置创建一个新的 pod。
参考阅读:
https://medium.com/faun/how-to-monitor-kubernetes-cluster-with-prometheus-and-grafana-8ec7e060896f
InfoQ 读者交流群上线啦!各位小伙伴可以扫描下方二维码,添加 InfoQ 小助手,回复关键字“进群”申请入群。大家可以和 InfoQ 读者一起畅所欲言,和编辑们零距离接触,超值的技术礼包等你领取,还有超值活动等你参加,快来加入我们吧!
点个在看少个 bug