Skip to content

Commit 93c8667

Browse files
authored
(DOCSP-21346): Revised the Prometheus tutorial. (mongodb#932)
* (DOCSP-21346): Revised the Prometheus tutorial. * (DOCSP-21346): Incorporated JW's feedback. * (DOCSP-21346): Fixed link. * (DOCSP-21346): Added note about full URL. * (DOCSP-21346): Fixed broken link.
1 parent 365ef6c commit 93c8667

File tree

1 file changed

+123
-82
lines changed

1 file changed

+123
-82
lines changed

docs/prometheus/README.md

+123-82
Original file line numberDiff line numberDiff line change
@@ -1,148 +1,189 @@
1-
# Using Prometheus with your MongoDB Resource
1+
# Use Prometheus with your MongoDB Resource
22

3-
We have added a sample yaml file that you could use to deploy a MongoDB resource
4-
in your Kubernetes cluster, with a
3+
You can use the [mongodb-prometheus-sample.yaml](mongodb-prometheus-sample.yaml) file to
4+
deploy a MongoDB resource in your Kubernetes cluster, with a
55
[`ServiceMonitor`](https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/user-guides/getting-started.md#related-resources)
6-
to indicate Prometheus how to consume metrics data from it.
6+
to indicate to Prometheus how to consume metrics data from
7+
it.
78

8-
This is a simple MongoDB resource with one user, and with the `spec.Prometheus`
9-
attribute with basic HTTP Auth and no TLS, that will allow you to test
10-
Prometheus metrics coming from MongoDB.
9+
The sample specifies a simple MongoDB resource with one user,
10+
and the `spec.Prometheus` attribute with basic HTTP
11+
authentication and no TLS. The sample lets you test
12+
the metrics that MongoDB sends to Prometheus.
1113

1214
## Quick Start
1315

14-
We have tested this setup with version 0.54 of the [Prometheus
16+
We tested this setup with version 0.54 of the [Prometheus
1517
Operator](https://github.com/prometheus-operator/prometheus-operator).
1618

17-
### Installing Prometheus Operator
19+
### Prerequisites
1820

19-
The Prometheus Operator can be installed using Helm. Find the installation
20-
instructions
21-
[here](https://github.com/prometheus-community/helm-charts/tree/main/charts/kube-prometheus-stack#kube-prometheus-stack):
21+
* Kubernetes 1.16+
22+
* Helm 3+
2223

23-
This can be done with:
24+
### Install the Prometheus Operator
25+
26+
You can install the Prometheus Operator using Helm. To learn
27+
more, see the [installation instructions](https://github.com/prometheus-community/helm-charts/tree/main/charts/kube-prometheus-stack#kube-prometheus-stack).
28+
29+
To install the Prometheus Operator using Helm, run the
30+
following commands:
2431

2532
``` shell
2633
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
2734
helm repo update
28-
helm install prometheus prometheus-community/kube-prometheus-stack --namespace prometheus-system --create-namespace
35+
helm install prometheus prometheus-community/ \
36+
kube-prometheus-stack --namespace <prometheus-system> \
37+
--create-namespace
2938
```
3039

31-
### Installing MongoDB
32-
33-
*Change after release to a proper Helm install.*
40+
### Install the MongoDB Community Kubernetes Operator
3441

35-
* Create a Namespace to hold our MongoDB Operator and Resources
42+
Run the following command to install the Community Kubernetes
43+
Operator and create a namespace to contain the Community
44+
Kubernetes Operator and resources:
3645

3746
``` shell
38-
kubectl create namespace mongodb
47+
helm install community-operator mongodb/community-operator --namespace <mongodb> --create-namespace
3948
```
4049

41-
* Follow the [Installation Instructions](https://github.com/mongodb/mongodb-kubernetes-operator/blob/master/docs/install-upgrade.md#operator-in-same-namespace-as-resources)
50+
To learn more, see the [Installation Instructions](/docs/install-upgrade.md#operator-in-same-namespace-as-resources).
4251

43-
## Creating a MongoDB Resource
52+
## Create a MongoDB Resource
4453

45-
We have created a sample yaml definition that you can use to create a MongoDB
46-
resource and a `ServiceMonitor` that will indicate Prometheus to start scraping
47-
its metrics information.
54+
You can use the [mongodb-prometheus-sample.yaml](mongodb-prometheus-sample.yaml) file to
55+
deploy a MongoDB resource in your Kubernetes cluster, with a
56+
[`ServiceMonitor`](https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/user-guides/getting-started.md#related-resources)
57+
to indicate to Prometheus how to consume metrics data from
58+
it.
4859

49-
You can apply it directly with:
60+
You can apply the sample directly with the following command:
5061

5162
``` shell
52-
kubectl apply -f mongodb-prometheus-sample.yaml
63+
kubectl apply -f <mongodb-prometheus-sample.yaml>
5364
```
5465

55-
This will create 2 `Secrets` containing authentication for a new MongoDB user
56-
and Basic HTTP Auth for the Prometheus endpoint. All of this in the `mongodb`
57-
Namespace.
66+
**Note:** If you haven't cloned the
67+
[mongodb-kubernetes-operator](https://github.com/mongodb/mongodb-kubernetes-operator)
68+
repository, you must provide the full URL that points to the
69+
[mongodb-prometheus-sample.yaml](mongodb-prometheus-sample.yaml) file in the command:
70+
[https://raw.githubusercontent.com/mongodb/mongodb-kubernetes-operator/master/docs/prometheus/mongodb-prometheus-sample.yaml](mongodb-prometheus-sample.yaml)
71+
72+
This command creates two `Secrets` that contain authentication
73+
for a new MongoDB user and basic HTTP authentication for the
74+
Prometheus endpoint. The command creates both `Secrets` in the
75+
`mongodb` namespace.
5876

59-
It will also create a `ServiceMonitor` that will configure Prometheus to consume
60-
this resurce's metrics. This will be created in the `prometheus-system`
77+
This command also creates a `ServiceMonitor` that configures
78+
Prometheus to consume this resource's metrics. This command
79+
creates the `ServiceMonitor` in the `prometheus-system`
6180
namespace.
6281

82+
## Optional: Enable TLS on the Prometheus Endpoint
6383

64-
## Bonus: Enable TLS on the Prometheus Endpoint
84+
### Install Cert-Manager
6585

66-
### Installing Cert-Manager
86+
1. Run the following commands to install
87+
[Cert-Manager](https://cert-manager.io/) using Helm:
6788

68-
We will install [Cert-Manager](https://cert-manager.io/) from using Helm.
89+
``` shell
90+
helm repo add jetstack https://charts.jetstack.io
91+
helm repo update
92+
helm install \
93+
cert-manager jetstack/cert-manager \
94+
--namespace cert-manager \
95+
--create-namespace \
96+
--version v1.7.1 \
97+
--set installCRDs=true
98+
```
6999

70-
``` shell
71-
helm repo add jetstack https://charts.jetstack.io
72-
helm repo update
73-
helm install \
74-
cert-manager jetstack/cert-manager \
75-
--namespace cert-manager \
76-
--create-namespace \
77-
--version v1.7.1 \
78-
--set installCRDs=true
79-
```
100+
2. Now with Cert-Manager installed, create a Cert-Manager
101+
`Issuer` and then a `Certificate`. You can use the two files
102+
that we provide to create a new `Issuer`:
80103

81-
Now with Cert-Manager installed we we'll create a Cert-Manager `Issuer` and then
82-
a `Certificate`. We provide 2 files that can be used to create a new `Issuer`.
104+
a. Run the following command to create a `Secret` that
105+
contains the TLS certificate `tls.crt` and `tls.key`
106+
entries. You can use the certificate and key files that
107+
we provide in the [`testdata/tls`](/testdata/tls) directory to create a Cert-Manager `Certificate`.
83108

84-
First we need to create a `Secret` holding a TLS certificate `tls.crt` and
85-
`tls.key` entries. We provide the certificate and key files that can be used to
86-
create a Cert-Manager `Certificate`, they are in the `testdata/tls` directory.
109+
``` shell
110+
kubectl create secret tls issuer-secret --cert=../../testdata/tls/ca.crt --key=../../testdata/tls/ca.key \
111+
--namespace mongodb
112+
```
87113

88-
``` shell
89-
$ kubectl create secret tls issuer-secret --cert=../../testdata/tls/ca.crt --key=../../testdata/tls/ca.key \
90-
--namespace mongodb
91-
secret/issuer-secret created
92-
```
114+
The following response appears:
93115

94-
And now we are ready to create a new `Issuer` and `Certificate`, by running the
95-
following command:
116+
``` shell
117+
secret/issuer-secret created
118+
```
96119

97-
``` shell
98-
$ kubectl apply -f issuer-and-cert.yaml --namespace mongodb
99-
issuer.cert-manager.io/ca-issuer created
100-
certificate.cert-manager.io/prometheus-target-cert created
101-
```
120+
b. Run the following command to create a new `Issuer` and
121+
`Certificate`:
122+
123+
``` shell
124+
kubectl apply -f issuer-and-cert.yaml --namespace mongodb
125+
```
126+
The following response appears:
102127

103-
### Enabling TLS on the MongoDB CRD
128+
``` shell
129+
issuer.cert-manager.io/ca-issuer created
130+
certificate.cert-manager.io/prometheus-target-cert created
131+
```
104132

105-
<center>_Make sure this configuration is not used in Production environments! A Security
106-
expert should be advising you on how to configure TLS_</center>
133+
### Enable TLS on the MongoDB CRD
107134

108-
We need to add a new entry to `spec.prometheus` section of the MongoDB
109-
`CustomResource`; we can do it executing the following
110-
[patch](https://kubernetes.io/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/)
111-
operation.
135+
**Important!** Do **NOT** use this configuration in Production
136+
environments! A security expert should advise you about how to
137+
configure TLS.
138+
139+
To enable TLS, you must add a new entry to the
140+
`spec.prometheus` section of the MongoDB `CustomResource`. Run
141+
the following [patch](https://kubernetes.io/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/)
142+
operation to add the needed entry.
112143

113144
``` shell
114-
$ kubectl patch mdbc mongodb --type='json' \
115-
-p='[{"op": "add", "path": "/spec/prometheus/tlsSecretKeyRef", "value":{"name": "prometheus-target-cert"}}]' \
116-
--namespace mongodb
145+
kubectl patch mdbc mongodb --type='json' \
146+
-p='[{"op": "add", "path": "/spec/prometheus/tlsSecretKeyRef", "value":{"name": "prometheus-target-cert"}}]' \
147+
--namespace mongodb
148+
```
117149

150+
The following response appears:
151+
152+
``` shell
118153
mongodbcommunity.mongodbcommunity.mongodb.com/mongodb patched
119154
```
120155

121-
After a few minutes, the MongoDB resource should be back in Running phase. Now
122-
we need to configure our Prometheus `ServiceMonitor` to point at the HTTPS
123-
endpoint.
156+
After a few minutes, the MongoDB resource should return to the
157+
Running phase. Now you must configure the Prometheus
158+
`ServiceMonitor` to point to the HTTPS endpoint.
124159

125160
### Update ServiceMonitor
126161

127-
To update our `ServiceMonitor` we will again patch the resource:
162+
To update the `ServiceMonitor`, run the following command to
163+
patch the resource again:
128164

129165
``` shell
130-
$ kubectl patch servicemonitors mongodb-sm --type='json' \
166+
kubectl patch servicemonitors mongodb-sm --type='json' \
131167
-p='
132168
[
133169
{"op": "replace", "path": "/spec/endpoints/0/scheme", "value": "https"},
134170
{"op": "add", "path": "/spec/endpoints/0/tlsConfig", "value": {"insecureSkipVerify": true}}
135171
]
136172
' \
137173
--namespace mongodb
174+
```
138175

176+
The following reponse appears:
177+
178+
``` shell
139179
servicemonitor.monitoring.coreos.com/mongodb-sm patched
140180
```
141181

142-
With these changes, the new `ServiceMonitor` will be pointing at the HTTPS
143-
endpoint (defined in `/spec/endpoints/0/scheme`). We are also setting
144-
`spec/endpoints/0/tlsConfig/insecureSkipVerify` to `true`, which will make
145-
Prometheus to not verify TLS certificates on MongoDB's end.
182+
With these changes, the new `ServiceMonitor` points to the HTTPS
183+
endpoint (defined in `/spec/endpoints/0/scheme`). You also
184+
set `spec/endpoints/0/tlsConfig/insecureSkipVerify` to `true`,
185+
so that Prometheus doesn't verify the TLS certificates on
186+
MongoDB's end.
146187

147-
Prometheus should now be able to scrape the MongoDB's target using HTTPS this
148-
time.
188+
Prometheus should now be able to scrape the MongoDB target
189+
using HTTPS.

0 commit comments

Comments
 (0)