You can configure the MongoDB Community Kubernetes Operator to use TLS certificates to encrypt traffic between:
- MongoDB hosts in a replica set, and
- Client applications and MongoDB deployments.
The Operator automates TLS configuration through its integration with cert-manager, a certificate management tool for Kubernetes.
Before you secure MongoDBCommunity resource connections using TLS, you must Create a database user to authenticate to your MongoDBCommunity resource.
To secure connections to MongoDBCommunity resources with TLS using cert-manager:
-
Add the
cert-managerrepository to yourhelmrepository list and ensure it's up to date:helm repo add jetstack https://charts.jetstack.io helm repo update -
Install
cert-manager:helm install cert-manager jetstack/cert-manager --namespace cert-manager --create-namespace --set crds.enabled=true -
Create a TLS-secured MongoDBCommunity resource:
This assumes you already have the operator installed in namespace
<namespace>helm upgrade --install community-operator mongodb/community-operator \ --namespace <namespace> --set resource.tls.useCertManager=true \ --set createResource=true --set resource.tls.enabled=true \ --set namespace=<namespace>
This creates a resource secured with TLS and generates the necessary
certificates with cert-manager according to the values specified in
the values.yaml file in the Community Kubernetes Operator
chart repository.
cert-manager automatically reissues certificates according to the
value of resource.tls.certManager.renewCertBefore. To alter the
reissuance interval, either:
-
Set
resource.tls.certManager.renewCertBeforeinvalues.yamlto the desired interval in hours before runninghelm upgrade -
Set
spec.renewBeforein the Certificate resource file generated bycert-managerto the desired interval in hours after runninghelm upgrade
-
Test your connection over TLS by
- Connecting to a
mongodcontainer inside a pod usingkubectl:
kubectl exec -it <mongodb-replica-set-pod> -c mongod -- bashWhere
mongodb-replica-set-podis the name of a pod from your MongoDBCommunity resource- Then, use
mongoshto connect over TLS: For how to get the connection string look at Deploy A Replica Set
mongosh "<connection-string>" --tls --tlsCAFile /var/lib/tls/ca/ca.crt --tlsCertificateKeyFile /var/lib/tls/server/*.pemWhere
mongodb-replica-setis the name of your MongoDBCommunity resource,namespaceis the namespace of your deployment andconnection-stringis a connection string for your<mongodb-replica-set>-svcservice. - Connecting to a