我试着将Prometheus Operator引入OpenShift v4.6,并用它来监控用户定义的项目!
背景 –
最初の目的は、Argo RolloutsでProgressive Deliveryを実施する際にPrometheusのメトリクスを使用するユースケースを試したかったことでした。そのため、OpenShift上でPrometheusのメトリクスを使う方法を調査しました。すると、v4.6でユーザー定義プロジェクトのモニタリング機能がGAされており、これを使ってArgo Rolloutsと連携できるかもしれないと考えました。しかし、結論としてArgo Rolloutsでは認証なしのHTTPエンドポイントしかサポートされていないことがわかりました(GitHub上で確認中)。一方、ユーザー定義プロジェクトのモニタリング機能は認証が必要なHTTPSエンドポイントで構成されるため、そのままでは利用できませんでした。そのため、HTTPの認証無しでアクセスできるようにしたい場合は、個別にプロキシー用のPodを用意する必要がありますとのことでした。しかし、今回はArgo RolloutsとPrometheusの連携を試したいだけであるため、個別のプロキシーを用意するよりも、従来から使用されているPrometheus Operatorをユーザー定義プロジェクトのメトリクス用に別途インストールする方が簡単だと考えました。そのため、この記事ではその構成手順をまとめています。
请参考此文章,以了解如何配置在v4.6版本中发布的用户自定义项目的监控功能。
执行步骤
禁用用户自定义项目的监控。
如果启用了用户定义项目的监控功能,就会与Prometheus Operator的功能产生冲突,导致两者的功能无法正常工作。因此,请参考以下手册,将用户定义项目的监控功能禁用。

PS D:\git> oc -n openshift-user-workload-monitoring get pod
No resources found in openshift-user-workload-monitoring namespace.
PS D:\git>
从OperatorHub引入Prometheus Operator。
prometheus-operatorプロジェクトを作成
PS D:\git> oc new-project prometheus-operator
Now using project "prometheus-operator" on server "https://c103-e.us-south.containers.cloud.ibm.com:31989".
You can add applications to this project with the 'new-app' command. For example, try:
oc new-app rails-postgresql-example
to build a new example application in Ruby. Or use kubectl to deploy a simple Kubernetes application:
kubectl create deployment hello-node --image=k8s.gcr.io/serve_hostname
PS D:\git>
-
- Administratorパースペクティブを選択し、左メニューよりOperators->OperatorHubを選択
prometheus-operatorプロジェクトを選択
フィルター入力欄にprometheus operatorを入力し、表示されたPrometheus Operatorをクリック

- Show Community Operatorのダイアログが表示された場合はそのままContinueをクリック

- Operatorの説明の画面で内容を確認し、Installをクリック

- Operatorの設定画面で特に設定は変更せずにInstallをクリック


创建所需资源
根据以下手册进行所需资源的创建。
应用Prometheus Operator的示例清单文件。
- Prometheus OperatorのGitHubリポジトリをクローン
PS D:\git> git clone https://github.com/prometheus-operator/prometheus-operator.git
Cloning into 'prometheus-operator'...
remote: Enumerating objects: 62454, done.
remote: Counting objects: 100% (1248/1248), done.
remote: Compressing objects: 100% (581/581), done.
remote: Total 62454 (delta 816), reused 977 (delta 647), pack-reused 61206 eceiving objects: 100% (62454/62454), 74.74 MReceiving objects: 100% (62454/62454), 75.78 MiB | 5.68 MiB/s, done.
Resolving deltas: 100% (37198/37198), done.
PS D:\git>
- Prometheus Operator用のサンプルマニフェストファイルを適用
PS D:\git> oc apply -f .\prometheus-operator\bundle.yaml
customresourcedefinition.apiextensions.k8s.io/alertmanagerconfigs.monitoring.coreos.com created
Warning: oc apply should be used on resource created by either oc create --save-config or oc apply
customresourcedefinition.apiextensions.k8s.io/alertmanagers.monitoring.coreos.com configured
Warning: oc apply should be used on resource created by either oc create --save-config or oc apply
customresourcedefinition.apiextensions.k8s.io/podmonitors.monitoring.coreos.com configured
Warning: oc apply should be used on resource created by either oc create --save-config or oc apply
customresourcedefinition.apiextensions.k8s.io/probes.monitoring.coreos.com configured
Warning: oc apply should be used on resource created by either oc create --save-config or oc apply
customresourcedefinition.apiextensions.k8s.io/prometheuses.monitoring.coreos.com configured
Warning: oc apply should be used on resource created by either oc create --save-config or oc apply
customresourcedefinition.apiextensions.k8s.io/prometheusrules.monitoring.coreos.com configured
Warning: oc apply should be used on resource created by either oc create --save-config or oc apply
customresourcedefinition.apiextensions.k8s.io/servicemonitors.monitoring.coreos.com configured
Warning: oc apply should be used on resource created by either oc create --save-config or oc apply
customresourcedefinition.apiextensions.k8s.io/thanosrulers.monitoring.coreos.com configured
clusterrolebinding.rbac.authorization.k8s.io/prometheus-operator created
clusterrole.rbac.authorization.k8s.io/prometheus-operator created
deployment.apps/prometheus-operator created
serviceaccount/prometheus-operator created
service/prometheus-operator created
PS D:\git>
创建用于样例应用程序的清单文件。
- Deployment、Service、ServiceMonitorのマニフェストを作成
apiVersion: apps/v1
kind: Deployment
metadata:
name: example-app
spec:
replicas: 3
selector:
matchLabels:
app: example-app
template:
metadata:
labels:
app: example-app
spec:
containers:
- name: example-app
image: fabxc/instrumented_app
ports:
- name: web
containerPort: 8080
kind: Service
apiVersion: v1
metadata:
name: example-app
labels:
app: example-app
spec:
selector:
app: example-app
ports:
- name: web
port: 8080
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: example-app
labels:
team: frontend
spec:
selector:
matchLabels:
app: example-app
endpoints:
- port: web
- マニフェストファイルを適用
PS D:\git> oc apply -f .\example-app-deployment.yaml
deployment.apps/example-app created
PS D:\git> oc apply -f .\example-app-service.yaml
service/example-app created
PS D:\git> oc apply -f .\example-app-service-monitor.yaml
servicemonitor.monitoring.coreos.com/example-app created
PS D:\git>
启用Prometheus Pod的RBAC规则。
- ServiceAccount、ClusterRole、ClusterRoleBindingのマニフェストファイルを作成
apiVersion: v1
kind: ServiceAccount
metadata:
name: prometheus
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
name: prometheus
rules:
- apiGroups: [""]
resources:
- nodes
- nodes/metrics
- services
- endpoints
- pods
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources:
- configmaps
verbs: ["get"]
- apiGroups:
- networking.k8s.io
resources:
- ingresses
verbs: ["get", "list", "watch"]
- nonResourceURLs: ["/metrics"]
verbs: ["get"]
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: prometheus
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: prometheus
subjects:
- kind: ServiceAccount
name: prometheus
namespace: prometheus-operator
- マニフェストファイルを適用
PS D:\git> oc apply -f .\prometheus-serviceaccount.yaml
serviceaccount/prometheus created
PS D:\git> oc apply -f .\prometheus-clusterrole.yaml
W0713 15:13:03.597032 12648 warnings.go:67] rbac.authorization.k8s.io/v1beta1 ClusterRole is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 ClusterRole
W0713 15:13:03.898235 12648 warnings.go:67] rbac.authorization.k8s.io/v1beta1 ClusterRole is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 ClusterRole
clusterrole.rbac.authorization.k8s.io/prometheus created
PS D:\git> oc apply -f .\prometheus-clusterrolebinding.yaml
W0713 15:13:13.667507 18008 warnings.go:67] rbac.authorization.k8s.io/v1beta1 ClusterRoleBinding is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 ClusterRoleBinding
W0713 15:13:13.937245 18008 warnings.go:67] rbac.authorization.k8s.io/v1beta1 ClusterRoleBinding is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 ClusterRoleBinding
clusterrolebinding.rbac.authorization.k8s.io/prometheus created
PS D:\git>
包括ServiceRole
- PrometheusのインスタンスにServiceRoleを含めるためのマニフェストファイルを作成
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
name: prometheus
spec:
serviceAccountName: prometheus
serviceMonitorSelector:
matchLabels:
team: frontend
resources:
requests:
memory: 400Mi
enableAdminAPI: false
- マニフェストファイルを適用
PS D:\git> oc apply -f .\prometheus-include-servicemonitor.yaml
prometheus.monitoring.coreos.com/prometheus created
PS D:\git>
发布《普罗米修斯》
- Prometheus公開用のServiceのマニフェストファイルを作成
apiVersion: v1
kind: Service
metadata:
name: prometheus
spec:
type: NodePort
ports:
- name: web
nodePort: 30900
port: 9090
protocol: TCP
targetPort: web
selector:
prometheus: prometheus
- マニフェストファイルを適用
PS D:\git> oc apply -f .\prometheus-service.yaml
service/prometheus created
PS D:\git>
确认运行
- Prometheus Serviceの9090ポートをlocalhostに転送
PS D:\git> oc port-forward svc/prometheus 9090:9090
Forwarding from 127.0.0.1:9090 -> 9090
Forwarding from [::1]:9090 -> 9090
- localhost:9090に接続し、Expressionにhttp_requests_totalを入力しExecuteをクリック

取得了样本应用程序的指标。
总结
OpenShift v4.6已经发布了用户定义项目的监控功能,但我们发现即使像以前一样单独引入Prometheus Operator,也可以实现对用户定义项目的监控。然而,据我们了解,用户定义项目的监控功能和Prometheus Operator无法共存,除非我们在像本次这样使用Prometheus的指标,并且使用没有HTTP认证的端点的特殊情况下,否则最好使用用户定义项目的监控功能。