|
1 |
| -# Using Prometheus with your MongoDB Resource |
| 1 | +# Use Prometheus with your MongoDB Resource |
2 | 2 |
|
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 |
5 | 5 | [`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. |
7 | 8 |
|
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. |
11 | 13 |
|
12 | 14 | ## Quick Start
|
13 | 15 |
|
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 |
15 | 17 | Operator](https://github.com/prometheus-operator/prometheus-operator).
|
16 | 18 |
|
17 |
| -### Installing Prometheus Operator |
| 19 | +### Prerequisites |
18 | 20 |
|
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+ |
22 | 23 |
|
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: |
24 | 31 |
|
25 | 32 | ``` shell
|
26 | 33 | helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
|
27 | 34 | 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 |
29 | 38 | ```
|
30 | 39 |
|
31 |
| -### Installing MongoDB |
32 |
| - |
33 |
| -*Change after release to a proper Helm install.* |
| 40 | +### Install the MongoDB Community Kubernetes Operator |
34 | 41 |
|
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: |
36 | 45 |
|
37 | 46 | ``` shell
|
38 |
| -kubectl create namespace mongodb |
| 47 | +helm install community-operator mongodb/community-operator --namespace <mongodb> --create-namespace |
39 | 48 | ```
|
40 | 49 |
|
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). |
42 | 51 |
|
43 |
| -## Creating a MongoDB Resource |
| 52 | +## Create a MongoDB Resource |
44 | 53 |
|
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. |
48 | 59 |
|
49 |
| -You can apply it directly with: |
| 60 | +You can apply the sample directly with the following command: |
50 | 61 |
|
51 | 62 | ``` shell
|
52 |
| -kubectl apply -f mongodb-prometheus-sample.yaml |
| 63 | +kubectl apply -f <mongodb-prometheus-sample.yaml> |
53 | 64 | ```
|
54 | 65 |
|
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. |
58 | 76 |
|
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` |
61 | 80 | namespace.
|
62 | 81 |
|
| 82 | +## Optional: Enable TLS on the Prometheus Endpoint |
63 | 83 |
|
64 |
| -## Bonus: Enable TLS on the Prometheus Endpoint |
| 84 | +### Install Cert-Manager |
65 | 85 |
|
66 |
| -### Installing Cert-Manager |
| 86 | +1. Run the following commands to install |
| 87 | + [Cert-Manager](https://cert-manager.io/) using Helm: |
67 | 88 |
|
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 | + ``` |
69 | 99 |
|
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`: |
80 | 103 |
|
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`. |
83 | 108 |
|
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 | + ``` |
87 | 113 |
|
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: |
93 | 115 |
|
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 | + ``` |
96 | 119 |
|
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: |
102 | 127 |
|
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 | + ``` |
104 | 132 |
|
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 |
107 | 134 |
|
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. |
112 | 143 |
|
113 | 144 | ``` 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 | +``` |
117 | 149 |
|
| 150 | +The following response appears: |
| 151 | + |
| 152 | +``` shell |
118 | 153 | mongodbcommunity.mongodbcommunity.mongodb.com/mongodb patched
|
119 | 154 | ```
|
120 | 155 |
|
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. |
124 | 159 |
|
125 | 160 | ### Update ServiceMonitor
|
126 | 161 |
|
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: |
128 | 164 |
|
129 | 165 | ``` shell
|
130 |
| -$ kubectl patch servicemonitors mongodb-sm --type='json' \ |
| 166 | +kubectl patch servicemonitors mongodb-sm --type='json' \ |
131 | 167 | -p='
|
132 | 168 | [
|
133 | 169 | {"op": "replace", "path": "/spec/endpoints/0/scheme", "value": "https"},
|
134 | 170 | {"op": "add", "path": "/spec/endpoints/0/tlsConfig", "value": {"insecureSkipVerify": true}}
|
135 | 171 | ]
|
136 | 172 | ' \
|
137 | 173 | --namespace mongodb
|
| 174 | +``` |
138 | 175 |
|
| 176 | +The following reponse appears: |
| 177 | + |
| 178 | +``` shell |
139 | 179 | servicemonitor.monitoring.coreos.com/mongodb-sm patched
|
140 | 180 | ```
|
141 | 181 |
|
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. |
146 | 187 |
|
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