Okay, So the first environment variable we're going to handle is providing a MongoDB URI and we're
going to take advantage of MongoDB Atlas as a managed service to run MongoDB for us and then we can
connect to it in both our local and Gcloud Kubernetes cluster.
The reason for this is because it's totally free to start out and we don't need to set up any complicated
infrastructure, and Atlas makes it super easy to just get started with what we need out of the box.
And they also provide auto scaling so that our MongoDB infrastructure will grow and scale as more load
hits our system, which is a great feature to have.
So make sure you head to Mongodb.com slash cloud, slash atlas slash register.
Go ahead and sign up for Atlas.
It's completely free to get up and running and we will get a MongoDB set up.
So after you've created your account, you should see that you get a message to verify your email.
Go ahead and verify your email.
After email is verified.
We'll go ahead and click continue to move on with the process.
You can go ahead and complete out this quick form to tell them what kind of app we are building.
You can just fill this out with whatever you'd like and we can set our preferred language to JavaScript
here.
Go ahead and click finish now we can go ahead and select the kind of database we want to deploy.
You can see the three types here, a dedicated cluster, a serverless cluster that will auto scale based
on our needs and a free cluster that uses a shared environment.
We are going to choose the free environment so we can continue to build.
You can always upgrade later on if you think you need more compute power.
You can then choose any provider.
I'll go ahead and use Google Cloud since we're already using it for the region.
I'll go ahead and select the American region US Central to be close to where our Kubernetes cluster
will be running.
Feel free to change this however you like, to any region you want, and we can just continue calling
this cluster sleeper here.
Go ahead and click create.
Next, you can see we're brought to a security Quickstart where we can determine our authentication
method.
We'll use a username and password and I'll use the default username.
It's supplying of sleep or nest app and I will auto generate a secure password and click the Create
user button.
Make sure you take note of this username and password before you create the user and then you can see
we are brought to where we would like to connect from.
We are going to go ahead and click my local environment and then for the IP access list, we're going
to go ahead and edit this.
0.0.0.0 and click on description.
We will enter all IPS because by default we're going to allow traffic on all IP addresses just to make
our building this database easier.
We are still locking down access with a username and password, so go ahead and click update entry and
then we can click finish and close.
I'll click on Go to Databases and you should see that our database is now up and running.
Now out of the box, we're going to have a three node replica set here, which means we're going to
have fault tolerance in this database set up all out of the box.
And this was all super easy to set up.
Okay, so now that we have our MongoDB connection string, we need a way to provide it to our underlying
pods in a secure way.
We don't want to paste our connection string directly in to a file that we can commit because that means
we're going to be committing plain text secrets, meaning our MongoDB username and password, which
is a bad practice.
So Kubernetes offers a way around this using a concept known as a secret.
So we're going to go ahead and create a secret which is just going to be inside of this cluster and
it will be base 64 encoded.
So let's go ahead and create the secret using our new connection string.
We'll run kubectl, create secret generic, which will be the type of secret and we'll call it MongoDB.
And then we're going to use dash dash from literal and set it equal to the key name we'll call connection
String here and set that equal to our full connection string.
So go ahead and paste our connection string in here you can see the username and password has been entered
in and if you don't remember the password for your user, you can go back into Atlas Cloud, into database
access, click on our user here and edit the password to generate a new one.
So I'll go ahead and create this secret.
And now if we run Kubectl get secrets, we can see we have this MongoDB secret and if we try to get
it and run output yaml you can see that our connection string here has been base 64 encoded.
Okay.
So now that we have the mongodb secret defined with our connection string, let's go ahead and actually
inject it as an environment variable into our deployments.
So we'll start off with the reservations deployment here under the container section, I'm going to
add a new ENV property here, and underneath that we'll add a name to provide the environment variable
we want.
Of course we know the environment variable we need is MongoDB URI.
This is the environment variable name from our app module here.
Next, we'll go ahead and specify value from here and then we'll provide a secret key ref.
And now we can provide the name of the secret we want to inject.
We know this is MongoDB, the one we just created, and then we provide the key inside of the secret.
In this case, it's going to be connection string.
Now we can run helm upgrade Sleeper to upgrade our helm deployment run get pods.
And if we now look at the logs for reservation, we can see it's no longer complaining about the MongoDB
URI and we can even describe the pod here to see that we are providing the MongoDB URI and you can see
it's hidden here to the secret we've defined.
Let's go ahead and copy this environment variable and provide it in.
To the rest of our services that actually need it.
So next, let's add the same environment variable to the auth deployment, which is the other service
that needs MongoDB.
I'll go ahead and paste this in.
And now we can run helm upgrade one more time and if we run Kubectl get pods, we can see the auth pod
is actually completely up and running now.
And now if we run kubectl logs on the auth pod again we see the same thing.
MongoDB is no longer being required.
So we've set up MongoDB Atlas and provided it to the deployments that need it.
Let's go ahead and finish setting up the rest of our environment variables.