重慶網(wǎng)站快速排名優(yōu)化百度市場應(yīng)用官方app
《OpenShift / RHEL / DevSecOps 匯總目錄》
說明:本文已經(jīng)在 OpenShift 4.13 的環(huán)境中驗(yàn)證
文章目錄
- OpenShift 的監(jiān)控功能構(gòu)成
- 部署被監(jiān)控應(yīng)用
- 用 OpenShift 內(nèi)置功能監(jiān)控應(yīng)用
- 用 Grafana 監(jiān)控應(yīng)用
- 安裝 Grafana 運(yùn)行環(huán)境
- 配置 Grafana 數(shù)據(jù)源
- 定制監(jiān)控 Dashboard
- 演示視頻
- 參考
OpenShift 的監(jiān)控功能構(gòu)成
構(gòu)成 OpenShift 監(jiān)控功能的附件分為兩部分:“平臺監(jiān)控組件” 和 “用戶項(xiàng)目監(jiān)控組件”。
- 在平臺監(jiān)控組件中包括:Prometheus、Thanos Querier 和 Alertmanager 三部分重要組成,這些組件是由 Cluster Monitoring Operator 總體部署和管理生命周期的。通過平臺監(jiān)控組件可以對 OpenShift 集群的 DNS、日志系統(tǒng)、etcd、Kubelet、API Server、Scheduler 等重要環(huán)境進(jìn)行監(jiān)控。
- 用戶項(xiàng)目監(jiān)控組件是對用戶自有項(xiàng)目中應(yīng)用資源進(jìn)行監(jiān)控。它由單獨(dú)的 Prometheus、Thanos Ruler 構(gòu)成,并共用平臺的 Alertmanager 和 Thanos Querier 組件。
除了可以使用 OpenShift 控制臺內(nèi)置的監(jiān)控功能和界面外,還可通過 Thanos Querier 的訪問地址外接其他監(jiān)控軟件,例如使用 Grafana 定制的儀表盤顯示 OpenShift 或用戶應(yīng)用的運(yùn)行情況。
部署被監(jiān)控應(yīng)用
- 創(chuàng)建項(xiàng)目
$ oc new-project app-monitoring
- 部署測試應(yīng)用
$ oc new-app quay.io/brancz/prometheus-example-app:v0.2.0 -l app=prometheus-example-app
- 創(chuàng)建 Service 和 Route。
$ cat << EOF | oc apply -f -
apiVersion: v1
kind: Service
metadata:labels:app: prometheus-example-appname: prometheus-example-app
spec:ports:- port: 8080protocol: TCPname: 8080-tcpselector:app: prometheus-example-apptype: ClusterIP
EOF$ oc expose svc prometheus-example-app
- 分別訪問應(yīng)用缺省地址和 /err 地址,返回的 HTTP 代碼分別為 200 和 404。
$ curl -sw "%{http_code}\n" -o /dev/null $(oc get route prometheus-example-app -ojsonpath={.spec.host})
200
$ curl -sw "%{http_code}\n" -o /dev/null $(oc get route prometheus-example-app -ojsonpath={.spec.host})/err
404
- 訪問應(yīng)用的 /metrics 地址,查看應(yīng)用返回的 HTTP 代碼為 200 和 404 請求數(shù)量統(tǒng)計(jì)。
$ curl $(oc get route prometheus-example-app -ojsonpath={.spec.host})/metrics
# HELP http_requests_total Count of all HTTP requests
# TYPE http_requests_total counter
http_requests_total{code="200",method="get"} 1
http_requests_total{code="404",method="get"} 1
# HELP version Version information about this binary
# TYPE version gauge
version{version="v0.2.0"} 1
說明:
也可在控制臺上部署 quay.io/brancz/prometheus-example-app:v0.2.0 容器鏡像,但需要增加 app=prometheus-example-app 標(biāo)簽,并且去掉 “安全路由” 選項(xiàng)。
用 OpenShift 內(nèi)置功能監(jiān)控應(yīng)用
- 啟用 OpenShift 對用戶應(yīng)用監(jiān)控功能。
$ cat << EOF | oc apply -f -
apiVersion: v1
kind: ConfigMap
metadata:name: cluster-monitoring-confignamespace: openshift-monitoring
data:config.yaml: |enableUserWorkload: true
EOF
- 確認(rèn)主要監(jiān)控服務(wù)云子運(yùn)行正常。
$ oc get pod -n openshift-user-workload-monitoring
NAME READY STATUS RESTARTS AGE
prometheus-operator-77d547b4dc-fcflk 2/2 Running 0 34h
prometheus-user-workload-0 6/6 Running 0 34h
thanos-ruler-user-workload-0 4/4 Running 0 34h
- 創(chuàng)建對 prometheus-example-app 應(yīng)用監(jiān)控的 ServiceMonitor 對象。
$ cat << EOF | oc apply -f -
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:name: prometheus-example-monitornamespace: app-monitoring
spec:endpoints:- interval: 30sport: 8080-tcppath: /metricsselector:matchLabels:app: prometheus-example-app
EOF
- 創(chuàng)建完后在 OpenShift 的 “目標(biāo)” 菜單中在 “過濾器” 中選擇 “用戶”,稍等后可以看到目標(biāo)的監(jiān)視端點(diǎn)。
該端點(diǎn)地址是運(yùn)行應(yīng)用的 Pod 使用的 IP 地址。
$ oc get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
prometheus-example-app-b744f9c85-bmk7p 1/1 Running 0 6m15s 10.217.0.123 crc-2zx29-master-0 <none> <none>
- 創(chuàng)建 PrometheusRule。下面的 expr 表達(dá)式會統(tǒng)計(jì)過去 5 分鐘 HTTP 請求返回結(jié)果是 404 的每秒速率,如果 > 0.3 則報警。
$ cat << EOF | oc apply -f -
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:name: app-alertnamespace: app-monitoring
spec:groups:- name: app-alertrules:- alert: HttpRequestErrorRateIncreaseexpr: rate(http_requests_total{code="404",job="prometheus-example-app"}[5m]) > 0.3labels:severity: warningannotations:summary: Prometheus example app's error rate increase.message: Prometheus example app's error rate increase.
EOF
創(chuàng)建完后可以在“報警” 菜單中的 “報警規(guī)則” 頁面中通過將 “過濾器” 中選擇 “用戶”,可以看到該報警規(guī)則。
- 執(zhí)行以下命令,持續(xù)訪問應(yīng)用的 /err 地址。
$ for i in `seq 1 10000`
docurl -sw "%{http_code}\n" -o /dev/null $(oc get route prometheus-example-app -ojsonpath={.spec.host})/errsleep 1
done
- 在 OpenShift 的 “指標(biāo)” 頁面中先將輸入查詢條件設(shè)為 “rate(http_requests_total{code=“404”,job=“prometheus-example-app”}[5m])”,然后點(diǎn)擊 “運(yùn)行查詢”。再將時間設(shè)為 5m,并將頁面刷新時間設(shè)置為 15秒。在等待一會兒后可以看到值已經(jīng)超過 0.3。
- 在 OpenShift 的 “報警” 頁面中的 “過濾器” 中選中 “用戶” ,確認(rèn)已經(jīng)被觸發(fā)。
- 點(diǎn)擊上圖的 HttpRequestErrorRateIncrease,然后可以看到和步驟 7 類似的指標(biāo)監(jiān)控圖。
- 另外,在 app-monitoring 項(xiàng)目中也可以看到 HttpRequestErrorRateIncrease 報警。
用 Grafana 監(jiān)控應(yīng)用
安裝 Grafana 運(yùn)行環(huán)境
- 創(chuàng)建 my-grafana 項(xiàng)目,然后在其中安裝 Grafana Operator v4 版本。
- 執(zhí)行以下命令,創(chuàng)建一個名為 my-grafana 的 Grafana 實(shí)例。注意:以下 YAML 中的 dataStorage 使用了 OpenShift 缺省的存儲類。
$ cat << EOF | oc apply -f -
apiVersion: integreatly.org/v1alpha1
kind: Grafana
metadata:name: my-grafananamespace: my-grafana
spec:config:security:admin_user: adminadmin_password: my-passworddataStorage:accessModes:- ReadWriteOncesize: 1Giingress:enabled: truetls:enabled: true
EOF
- 創(chuàng)建名為 grafana-view 的 clusterrolebinding,為所有命名空間的 grafana-serviceaccount 提供 cluster-monitoring-view 角色。
$ oc create clusterrolebinding grafana-view --clusterrole=cluster-monitoring-view --serviceaccount=my-grafana:grafana-serviceaccount
配置 Grafana 數(shù)據(jù)源
- 執(zhí)行命令創(chuàng)建一個 GrafanaDataSource,其中使用了基于 grafana-serviceaccoun 的 token 來訪問 Thanos Querier。
$ TOKEN=$(oc create token grafana-serviceaccount -n my-grafana)
$ cat << EOF | oc apply -f -
apiVersion: integreatly.org/v1alpha1
kind: GrafanaDataSource
metadata:name: prometheusnamespace: my-grafana
spec:datasources:- basicAuthUser: internalaccess: proxyeditable: truesecureJsonData:httpHeaderValue1: >-Bearer ${TOKEN}name: Prometheusurl: 'https://thanos-querier.openshift-monitoring.svc.cluster.local:9091'jsonData:httpHeaderName1: AuthorizationtimeInterval: 5stlsSkipVerify: truebasicAuth: falseisDefault: trueversion: 1type: prometheusname: test_name
EOF
- 訪問 grafana-route 對應(yīng)的 Grafana 頁面,然后使用創(chuàng)建 my-grafana 實(shí)例時指定的用戶和密碼登錄。
- 進(jìn)入 Configuration 的 Data Sources 菜單,可以看到名為 Prometheus 的數(shù)據(jù)源,點(diǎn)擊進(jìn)入。
- 點(diǎn)擊頁面下方的 “ Save & Test”,確認(rèn)顯示 Data srouce is working。
定制監(jiān)控 Dashboard
- 進(jìn)入 Create 的 Dashboard 菜單。
- 在 New dashboard 頁面點(diǎn)擊 Add an empty panel。
- 在下圖中為 Metrics 提供以下內(nèi)容,然后將 Panel title 設(shè)為 Example App Err Rate,最后點(diǎn)擊 Apply 按鈕。
rate(http_requests_total{code="404",job="prometheus-example-app"}[5m])
4. 在 Dashboard 頁面點(diǎn)擊右上方的 Dashboard settings 圖標(biāo)。
5. 設(shè)置 Name,然后保存。
6. 最后通過定制的 Dashboard 監(jiān)控的應(yīng)用指標(biāo)如下圖。
演示視頻
演示視頻
參考
https://github.com/k-srkw/openshift-monitoring-handson/blob/main/monitoring-handson.md
https://cloud.redhat.com/blog/your-guide-to-openshift-observability-part-1
https://access.redhat.com/solutions/5335491
https://access.redhat.com/documentation/en-us/openshift_container_platform/4.5/html/monitoring/monitoring-your-own-services
https://catalog.workshops.aws/aws-openshift-workshop/en-US/8-observability/2-metrics/5-app-dashboard
https://github.com/brancz/prometheus-example-app
https://developers.redhat.com/articles/2023/08/08/how-monitor-workloads-using-openshift-monitoring-stack#how_to_monitor_a_sample_application
https://shonpaz.medium.com/monitor-your-application-metrics-using-the-openshift-monitoring-stack-862cb4111906
https://github.com/OpenShiftDemos/openshift-ops-workshops/blob/ocp4-dev/workshop/content/monitoring-basics.adoc
https://github.com/pittar/openshift-user-workload-monitoring
https://github.com/alvarolop/quarkus-observability-app/blob/main/README.adoc
https://prometheus.io/docs/prometheus/latest/querying/basics/
https://github.com/alvarolop/quarkus-observability-app