How to bind your application to a database service without the Service Binding Operator.
There are a few ways of binding your application to a database service with the help of odo. The recommended way is with the help of Service Binding Operator(SBO), but it is also possible to bind without it, and this blog will show you how.
Architectureโ
We have a simple CRUD application built in Go that can create/list/update/delete a place. This application requires connecting to a MongoDB database in order to function correctly, which will be deployed as a microservice on the cluster.
Prerequisites:โ
This blog assumes:
- odo v3.0.0-beta1
- you have access to a Kubernetes or OpenShift cluster.
- you have Helm installed on your system. See https://helm.sh/docs/intro/install/ for installation instructions.
(Optional) Setting up the namespaceโ
- We will create a new namespace to deploy our application in, with the help of odo.
odo create namespace restapi-mongodb
Setting up the MongoDB microserviceโ
We are going to use the Bitnami's helm charts for creating our MongoDB database.
- Add the Bitnami's Helm charts repository and make your Helm client up to date with it:
helm repo add bitnami https://charts.bitnami.com/bitnami && helm repo update
- Declare the necessary environment variables:
MY_MONGODB_ROOT_USERNAME=root
MY_MONGODB_ROOT_PASSWORD=my-super-secret-root-password
MY_MONGODB_USERNAME=my-app-username
MY_MONGODB_PASSWORD=my-app-super-secret-password
MY_MONGODB_DATABASE=my-app
Make sure MY_MONGODB_ROOT_USERNAME
, and MY_MONGODB_ROOT_PASSWORD
are declared/exported in any new terminal session from where you might run an odo command for this application.
- Create the MongoDB service.
helm install mongodb bitnami/mongodb \
--set auth.rootPassword=$MY_MONGODB_ROOT_PASSWORD \
--set auth.username=$MY_MONGODB_USERNAME \
--set auth.password=$MY_MONGODB_PASSWORD \
--set auth.database=$MY_MONGODB_DATABASE
Expected output:
Notice the resources(sevice, deployment, and secrets) that are deployed.
Wait for the pods to come up, this might take a few minutes:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
mongodb-85fff797f6-fnwvl 1/1 Running 0 63s
Setting up the applicationโ
- Clone the repository, and cd into it.
git clone https://github.com/valaparthvi/restapi-mongodb-odo.git && cd restapi-mongodb-odo
Download the devfile.yamlโ
- Run
odo init
to fetch the necessary devfile.
odo init --devfile go --name places
Expected output:
If you run odo dev
to deploy the application at this point, you will notice that the 'run' command has failed with some logs, this is expected, because like we mentioned before, our Go application is dependent on the MongoDB service and will not function unless it is connected to it.
Adding the connection information to devfile.yamlโ
There are a few changes that we will need to make to our devfile:
6.1 Change the schemaVersion
of devfile to 2.2.0.
schemaVersion: 2.2.0
Please note that this change is only necessary because we are using devfile variable substitution.
6.2 Add a variables
field in the devfile.
variables:
PASSWORD: password
USERNAME: user
HOST: host
6.3 Edit the 'runtime' container component in devfile to add information such as username, password, and host required to connect to the MongoDB service.
components:
- container:
...
...
env:
- name: username
value: "{{USERNAME}}"
- name: password
value: "{{PASSWORD}}"
- name: host
value: "{{HOST}}"
name: runtime
The values for username, password, and host will be passed to devfile.yaml with the --var
flag when we run the odo dev
command.
Your final devfile.yaml should look something like this:
Deploy the applicationโ
- Run
odo dev
to deploy the application on the cluster.
odo dev \
--var PASSWORD=$MY_MONGODB_ROOT_PASSWORD \
--var USERNAME=$MY_MONGODB_ROOT_USERNAME \
--var HOST="mongodb"
The value for host is name of the service that belongs to our database application, in this case it is a service resource called "mongodb", you might have noticed it when we deployed the helm chart.
Expected output:
Accessing the applicationโ
- Run the following curl command to test the application:
curl 127.0.0.1:40001/api/places
This will return a null response since the database is currently empty, but it also means that we have successfully connected to our database application.
- Add some data to the database:
curl -sSL -XPOST -d '{"title": "Agra", "description": "Land of Tajmahal"}' 127.0.0.1:40001/api/places
- Fetch the list of places again:
$ curl 127.0.0.1:40001/api/places
{"id":"62c2a0659fa147e382a4db31","title":"Agra","description":"Land of Tajmahal"}
List of available API endpointsโ
- GET
/api/places
- List all places - POST
/api/places
- Add a new place - PUT
/api/places
- Update a place - GET
/api/places/<id>
- Fetch place with id<id>
- DELETE
/api/places/<id>
- Delete place with id<id>
Conclusionโ
To conclude this blog, it is possible to connect your application with another microservice without the Service Binding Operator if you have the correct connection information. Using the Service Binding Operator with a Bindable Operator makes it easy for you to not care about finding the connection information and ease the binding.