Charmed MongoDB K8s Tutorials > Deploy a replica set > 6. Integrate with other applications
Integrate MongoDB with other applications
Juju integrations, previously known as “relations”, are the easiest way to create a user for MongoDB in Charmed MongoDB K8s. Relations automatically create a username, password, and database for the desired user/application. The best practice is to connect to MongoDB via a specific user rather than the admin user, like we did earlier in this tutorial.
In this part of the tutorial, you will learn how to integrate MongoDB with another charm, access an integrated database, and manage users via integrations.
Summary
- Deploy the
data-integrator
charm - Integrate with MongoDB
- Access the integrated database
- Remove a user
- Recreate a user
Deploy the data-integrator
charm
Before relating to a charmed application, we must first deploy our charmed application. In this tutorial we will relate to the Data Integrator Charm.
This is a bare-bones charm that allows for central management of database users, providing support for different kinds of data platforms products (e.g. MongoDB, MySQL, PostgreSQL, Kafka, etc) with a consistent, opinionated and robust user experience.
When deploying the Data Integrator charm, we will set a name for a database called test-database
using juju’s config flag:
juju deploy data-integrator --config database-name=test-database
The expected output:
Located charm "data-integrator" in charm-hub...
Deploying "data-integrator" from charm-hub charm "data-integrator"...
Integrate with MongoDB
Now that the Database Integrator charm has been set up, we can relate it to MongoDB. This will automatically create a username, password, and database for the Database Integrator Charm.
Integrate the two applications with:
juju integrate data-integrator mongodb-k8s
Wait for juju status --watch 1s
to show:
Model Controller Cloud/Region Version SLA Timestamp
tutorial overlord microk8s/localhost 3.1.6 unsupported 17:24:23Z
App Version Status Scale Charm Channel Rev Address Exposed Message
data-integrator active 1 data-integrator edge 11 10.152.183.190 no
mongodb-k8s active 2 mongodb-k8s 6/beta 37 10.152.183.20 no
Unit Workload Agent Address Ports Message
data-integrator/0* active idle 10.1.42.142
mongodb-k8s/0* active idle 10.1.42.137
mongodb-k8s/1 active idle 10.1.42.140
To retrieve information such as the username, password, and database. Type:
juju run data-integrator/leader get-credentials
This should output something like:
Running operation 19 with 1 task
- task 20 on unit-data-integrator-0
Waiting for task 20...
mongodb:
database: test-database
endpoints: mongodb-k8s-0.mongodb-k8s-endpoints,mongodb-k8s-1.mongodb-k8s-endpoints
password: iW19U0ClFbSDurxMju98Zsgy4BovY4uf
replset: mongodb-k8s
uris: mongodb://relation-2:iW19U0ClFbSDurxMju98Zsgy4BovY4uf@mongodb-k8s-0.mongodb-k8s-endpoints,mongodb-k8s-1.mongodb-k8s-endpoints/test-database?replicaSet=mongodb-k8s&authSource=admin
username: relation-2
ok: "True"
Save the value listed under uris:
(Note: your hostnames, usernames, and passwords will likely be different.)
Access the integrated database
Notice that in the previous step when you typed juju run-action data-integrator/leader get-credentials --wait
the command not only outputted the username, password, and database, but also outputted the URI. This means you do not have to generate the URI yourself.
To connect to this URI, first ssh into mongodb-k8s/0
:
juju ssh --container=mongod mongodb-k8s/0
Then access mongo
with the URI that you copied above:
mongosh "<uri copied from get-credentials>"
Make sure you wrap the URI in quotation marks (
""
) with no trailing whitespace.
You will now be in the mongo shell as the user created for this relation. When you integrate two applications, Charmed MongoDB K8s automatically sets up a user and database for you.
Enter db.getName()
into the MongoDB shell. This will output
test-database
This is the name of the database we specified when we first deployed the data-integrator
charm.
To create a collection in test-database
and then show the collection, enter:
db.createCollection("test-collection")
show collections
Now insert a document into this database:
db.test_collection.insertOne(
{
First_Name: "Jammy",
Last_Name: "Jellyfish",
})
You can verify this document was inserted by running:
db.test_collection.find()
Return to original shell
Leave the MongoDB shell by typing exit
.
You will be back in the host of Charmed MongoDB K8s (mongodb-k8s/0
). Exit this host by typing exit
again.
You should now be at the original shell where you can interact with Juju and MicroK8s
Remove the user
Removing the integration automatically removes the user that was created with it.
To remove the integration (and therefore the user), run the following command:
juju remove-relation mongodb-k8s data-integrator
Now try again to connect to the same URI you just used in the section Access the integrated database:
juju ssh --container=mongod mongodb-k8s/0
mongosh "<uri copied from get-credentials>"
Make sure you wrap the URI in quotation marks (
""
) with no trailing whitespace.
This will output an error message:
Current Mongosh Log ID: <your connection id>
Connecting to: mongodb://<credentials>@mongodb-k8s-0.mongodb-k8s-endpoints,mongodb-k8s-1.mongodb-k8s-endpoints/test-database?replicaSet=mongodb-k8s&authSource=admin&appName=mongosh+2.0.1
MongoServerError: Authentication failed.
This is expected, since the user no longer exists.
Return to original shell
Leave the MongoDB shell by typing exit
.
You will be back in the host of Charmed MongoDB K8s (mongodb-k8s/0
). Exit this host by typing exit
again.
You should now be at the original shell where you can interact with Juju and MicroK8s
Recreate the user
If you wanted to recreate the user we just removed, all you would need to do is integrate the two applications again:
juju integrate mongodb-k8s data-integrator
Re-relating generates a new password for this user, and therefore a new URI. You can see the new URI with:
juju run data-integrator/leader get-credentials
Save the result listed with uris:
.
You can connect to the database with this new URI:
juju ssh --container=mongod mongodb-k8s/0
mongosh "<uri copied from get-credentials>"
Make sure you wrap the URI in quotation marks (
""
) with no trailing whitespace.
From there, if you enter db.test_collection.find()
you will see all of your original documents are still present in the database.
Return to original shell
Leave the MongoDB shell by typing exit
.
You will be back in the host of Charmed MongoDB K8s (mongodb-k8s/0
). Exit this host by typing exit
again.
You should now be at the original shell where you can interact with Juju and MicroK8s
Next step: 7. Enable security