|
| 1 | +# Using Prometheus with your MongoDB Resource |
| 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 |
| 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. |
| 7 | + |
| 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. |
| 11 | + |
| 12 | +## Quick Start |
| 13 | + |
| 14 | +We have tested this setup with version 0.54 of the [Prometheus |
| 15 | +Operator](https://github.com/prometheus-operator/prometheus-operator). |
| 16 | + |
| 17 | +### Installing Prometheus Operator |
| 18 | + |
| 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): |
| 22 | + |
| 23 | +This can be done with: |
| 24 | + |
| 25 | +``` shell |
| 26 | +helm repo add prometheus-community https://prometheus-community.github.io/helm-charts |
| 27 | +helm repo update |
| 28 | +helm install prometheus prometheus-community/kube-prometheus-stack --namespace prometheus-system --create-namespace |
| 29 | +``` |
| 30 | + |
| 31 | +### Installing MongoDB |
| 32 | + |
| 33 | +*Change after release to a proper Helm install.* |
| 34 | + |
| 35 | +* Create a Namespace to hold our MongoDB Operator and Resources |
| 36 | + |
| 37 | +``` shell |
| 38 | +kubectl create namespace mongodb |
| 39 | +``` |
| 40 | + |
| 41 | +* Follow the [Installation Instructions](https://github.com/mongodb/mongodb-kubernetes-operator/blob/master/docs/install-upgrade.md#operator-in-same-namespace-as-resources) |
| 42 | + |
| 43 | +## Creating a MongoDB Resource |
| 44 | + |
| 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. |
| 48 | + |
| 49 | +You can apply it directly with: |
| 50 | + |
| 51 | +``` shell |
| 52 | +kubectl apply -f mongodb-prometheus-sample.yaml |
| 53 | +``` |
| 54 | + |
| 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. |
| 58 | + |
| 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` |
| 61 | +namespace. |
| 62 | + |
| 63 | + |
| 64 | +## Bonus: Enable TLS on the Prometheus Endpoint |
| 65 | + |
| 66 | +### Installing Cert-Manager |
| 67 | + |
| 68 | +We will install [Cert-Manager](https://cert-manager.io/) from using Helm. |
| 69 | + |
| 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 | +``` |
| 80 | + |
| 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`. |
| 83 | + |
| 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. |
| 87 | + |
| 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 | +``` |
| 93 | + |
| 94 | +And now we are ready to create a new `Issuer` and `Certificate`, by running the |
| 95 | +following command: |
| 96 | + |
| 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 | +``` |
| 102 | + |
| 103 | +### Enabling TLS on the MongoDB CRD |
| 104 | + |
| 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> |
| 107 | + |
| 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. |
| 112 | + |
| 113 | +``` shell |
| 114 | +$ kubectl patch mdbc mongodb --type='json' \ |
| 115 | + -p='[{"op": "add", "path": "/spec/prometheus/tlsSecretKeyRef", "value":{"name": "prometheus-target-cert"}}]' \ |
| 116 | + --namespace mongodb |
| 117 | + |
| 118 | +mongodbcommunity.mongodbcommunity.mongodb.com/mongodb patched |
| 119 | +``` |
| 120 | + |
| 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. |
| 124 | + |
| 125 | +### Update ServiceMonitor |
| 126 | + |
| 127 | +To update our `ServiceMonitor` we will again patch the resource: |
| 128 | + |
| 129 | +``` shell |
| 130 | +$ kubectl patch servicemonitors mongodb-sm --type='json' \ |
| 131 | + -p=' |
| 132 | +[ |
| 133 | + {"op": "replace", "path": "/spec/endpoints/0/scheme", "value": "https"}, |
| 134 | + {"op": "add", "path": "/spec/endpoints/0/tlsConfig", "value": {"insecureSkipVerify": true}} |
| 135 | +] |
| 136 | +' \ |
| 137 | + --namespace mongodb |
| 138 | + |
| 139 | +servicemonitor.monitoring.coreos.com/mongodb-sm patched |
| 140 | +``` |
| 141 | + |
| 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. |
| 146 | + |
| 147 | +Prometheus should now be able to scrape the MongoDB's target using HTTPS this |
| 148 | +time. |
0 commit comments