Prometheus 与 OSM 集成

Prometheus 与 OSM 集成

To familiarize yourself on how OSM works with Prometheus, try installing a new mesh with sample applications to see which metrics are collected. 为了熟悉 OSM 如何与 Promethues 工作,试着安装网格和示例应用来看收集了哪些指标。

  1. 安装 OSM 并使用你自己的 Prometheus 实例:

    1. $ osm install --set osm.deployPrometheus=true,osm.enablePermissiveTrafficPolicy=true
    2. OSM installed successfully in namespace [osm-system] with mesh name [osm]
  2. 为示例工作负载创建命名空间:

    1. $ kubectl create namespace metrics-demo
    2. namespace/metrics-demo created
  3. 让 OSM 监控新创建的命名空间:

    1. $ osm namespace add metrics-demo
    2. Namespace [metrics-demo] successfully added to mesh [osm]
  4. 配置 Prometheus 从新的命名空间获取指标:

    1. $ osm metrics enable --namespace metrics-demo
    2. Metrics successfully enabled in namespace [metrics-demo]
  5. 安装示例应用:

    1. $ kubectl apply -f https://raw.githubusercontent.com/openservicemesh/osm-docs/release-v1.2/manifests/samples/curl/curl.yaml -n metrics-demo
    2. serviceaccount/curl created
    3. deployment.apps/curl created
    4. $ kubectl apply -f https://raw.githubusercontent.com/openservicemesh/osm-docs/release-v1.2/manifests/samples/httpbin/httpbin.yaml -n metrics-demo
    5. serviceaccount/httpbin created
    6. service/httpbin created
    7. deployment.apps/httpbin created

    确保所有的 pod 和容器已经启动并运行:

    1. $ kubectl get pods -n metrics-demo
    2. NAME READY STATUS RESTARTS AGE
    3. curl-54ccc6954c-q8s89 2/2 Running 0 95s
    4. httpbin-8484bfdd46-vq98x 2/2 Running 0 72s
  6. 生成流量:

    下面的命令让 curl Pod 来不停地以每秒 1 个请求的速度访问 httpbin Pod。

    1. $ kubectl exec -n metrics-demo -ti "$(kubectl get pod -n metrics-demo -l app=curl -o jsonpath='{.items[0].metadata.name}')" -c curl -- sh -c 'while :; do curl -i httpbin.metrics-demo:14001/status/200; sleep 1; done'
    2. HTTP/1.1 200 OK
    3. server: envoy
    4. date: Tue, 23 Mar 2021 17:27:44 GMT
    5. content-type: text/html; charset=utf-8
    6. access-control-allow-origin: *
    7. access-control-allow-credentials: true
    8. content-length: 0
    9. x-envoy-upstream-service-time: 1
    10. HTTP/1.1 200 OK
    11. server: envoy
    12. date: Tue, 23 Mar 2021 17:27:45 GMT
    13. content-type: text/html; charset=utf-8
    14. access-control-allow-origin: *
    15. access-control-allow-credentials: true
    16. content-length: 0
    17. x-envoy-upstream-service-time: 2
    18. ...
  7. 在 Prometheus 中查看指标:

    转发 Prometheus 端口:

    1. $ kubectl port-forward -n osm-system $(kubectl get pods -n osm-system -l app=osm-prometheus -o jsonpath='{.items[0].metadata.name}') 7070
    2. Forwarding from 127.0.0.1:7070 -> 7070
    3. Forwarding from [::1]:7070 -> 7070

    在浏览器中访问 http://localhost:7070 查看 Prometheus 用户界面。下面的查询会显示 curl pod 每秒发送多少请求到 httpbin pod,应该是 1:

    1. irate(envoy_cluster_upstream_rq_xx{source_service="curl", envoy_cluster_name="metrics-demo/httpbin"}[30s])

    在 Prometheus 用户界面中可随意访问其他的指标。

  8. 清理

    一旦演示资源使用完,通过删除应用命名空间来清理:

    1. $ kubectl delete ns metrics-demo
    2. namespace "metrics-demo" deleted

    然后,卸载 OSM:

    1. $ osm uninstall mesh
    2. Uninstall OSM [mesh name: osm] ? [y/n]: y
    3. OSM [mesh name: osm] uninstalled

    在卸载 OSM 后删除集群范围的资源,执行下面的命令。参阅 卸载指南 获取更多信息。

    1. $ osm uninstall mesh --delete-cluster-wide-resources