Okay, so now that we've pushed our images up to Gcloud, we're going to go ahead and start by running
our own local Kubernetes cluster and have our images running locally.
Now I'm using Docker desktop as my Kubernetes provider here.
So if you use Docker desktop, you should be able to go into the settings and see a Kubernetes field
here where you can enable a Kubernetes cluster on Docker desktop to make it very easy to get started
with Kubernetes back in the command line.
You can see if I run Kubectl get namespaces, I have the default namespaces for Kubernetes cluster and
if I run Kubectl get pods, I have nothing running in my default namespace right now.
So let's go ahead and change that by starting to create our first deployment.
We're going to be using Helm as our dependency manager for Kubernetes so that we can easily deploy our
application anywhere locally or on Gcloud very easily using helm.
If you don't have helm already installed, simply go to helm sh slash docs intro install here where
you can get instructions for how to install helm easily on any system, including from a script or through
package managers like Homebrew or Chocolaty.
It's very easy to install helm so go ahead and make sure you have this running.
So to start, let's go ahead and create a new folder in our project called K80s, which is where all
of our Kubernetes deployments and manifests will live.
Now in our terminal.
I'll CD into that folder and then I'll run helm Create Sleeper here to create a new helm chart called
Sleeper.
And what this is going to do is it's going to create some starter files for Sleeper here and give us
some starting templates as well as an all important chart YAML file which defines our application and
all the dependencies for this chart.
So we're going to go ahead and clean things up here.
I'll go ahead and remove everything in the templates folder that was generated.
We don't need any of this.
We can keep the helm ignore here, which will make sure we don't commit any of these unneeded files.
And then in the values file, I'll go ahead and delete everything as well.
So now we just have a completely empty chart called Sleeper here.
Okay.
So the next thing we want to do is actually create deployments for each of our services.
Deployments are what's going to define the manifest for each of our microservices and make sure we always
have a pod running for each one of these apps.
So let's go ahead and start creating one.
We're going to go ahead and use the Kubectl command here and run create deployment and we'll start off
with reservations here and then I'll provide the image name here.
And of course, we know we need to get the image name from our Gcloud repository.
So we'll go ahead and copy the full path to the image here and paste in the image and of course make
sure we add slash production to the end here.
Next, we're going to set this flag called dry run equal to client here to make sure that we don't actually
execute this, but we want to actually output this to YAML so that we can source control and commit
it as part of our helm chart and make sure it's always going to be run when we do helm install.
So let's go ahead and add dry run equal client and then we'll add output equal to YAML here and pipe
this to a file called deployment dot yaml.
So now if we go back to our folder, we have this generated yaml here which is the manifest for the
reservations deployment and you can see we have everything we need out of the box to create this deployment.
Go ahead and clean this up a bit.
I'll get rid of the creation timestamp up here, then I'll get rid of the strategy section.
We can get rid of the creation timestamp resources status and then I'll change the name of the container
here to be reservations.
Next, let's go ahead and in the sleeper directory we need to move this deployment manifest into our
templates folder, which is where everything is going to be actually deployed from.
So let's create a new directory in the templates folder here called Reservations, and then we'll move
the Deployment.yaml into that reservations directory.
So let's go ahead and give this a try.
Now if we CD into Sleeper and then we can run helm, install Sleeper and then provide the path to the
helm chart.
In this case, it's in the current directory, we can see our deployment was successful and now if we
do kubectl get pods, we can see our reservations.
Pod is running, which is awesome.
However, we have an image pull backoff issue here, which means we're not able to correctly pull the
image.
So if we do describe pod and enter it, you can see we're getting denied access on our image.
So let's go ahead and take care of this next.
So remember that out of the box that our artifact registry is completely private, which means that
people can't pull our images without correct access.
And that means even on our local Kubernetes cluster, we need a way to configure access to these images,
just like we did for local Docker.
We were pushing and pulling.
So let's go ahead and do just that.
We'll go into the G Cloud console and go to APIs and services, then go ahead and click on credentials,
and then we're going to click Create Credentials and click on service account.
I'll go ahead and call this artifact image pull, then click create and continue.
Then for roles here, click on roles and scroll down until we find the artifact registry.
And then we're going to click on Artifact Registry Reader so that we can read items from our artifact
registry.
Then click on Continue.
Then we'll go ahead and finish by clicking Done.
So now that we've created the service account, go ahead and click on it and then we're going to click
on Keys here and add a new key and click Create new Key.
This will be JSON.
So go ahead and click Create and then we'll click Allow to download this.
So make sure you download this JSON and save it somewhere on your machine where you can find it.
Okay, so now we need to tell Kubernetes to use our JSON key file when pulling from GCR by creating
a new secret.
So we'll call kubectl, create secret of type Docker registry and we'll call this GCR JSON key and provide
a flag here called Docker Server.
And this is going to be equal to the domain of where our repository is living, which we can also get
by clicking on a repository, setup instructions and copying this here the US East four in my case.package.dev.
Copy this and paste this in.
Then we'll provide a Docker username and we'll set this equal to underscore JSON key.
And then importantly the Docker password.
This is going to be equal to open up quotes and inside of here we'll do dollar sign and then open up
parentheses and in the parentheses we're going to run a command cat and then enter the path to the JSON
key file.
In my case, I've put it in this exact directory here, so I will paste the path here.
Finally, we'll provide a Docker email here and I'll set this equal to our email that we set up Sleeper
Nest app at gmail.com.
And then you can see our secret JCR JSON key was created.
Finally, we need to add the secret to our default service account so that it's actually going to be
used to do this.
We can run Kubectl patch service account default and add dash P, open up single quotes here and inside
of here we're going to open up a new object and we're going to use open up quotes and put in image pull
secrets here.
And set this equal to an array.
And inside of that array we'll have another object with a name here.
And the name is going to be equal to the key.
We just created GCR, JSON Key.
So go ahead and patch this.
Now we can go ahead and run kubectl rollout, restart deployment.
Reservations so that a new reservations pod will be created can now run kubectl get pods to see our
reservations.
Pod is running and if we take a look we can see it's currently crashed loopback offing.
If we run kubectl logs and paste the pod name in, we can see that the startup failed because we're
not supplying the pod with the correct environment variables, which is what we expect to see because
our validation is working correctly.
Let's go ahead and continue fleshing out our deployment.
Okay, so we're going to go ahead and create the deployments for the rest of our services before we
define our environment variables.
So let's go ahead and in our folder, in the templates directory, let's go ahead and create a new folder
for each of our services.
We of course already have the deployment here for the reservation service, so we'll go ahead and add
an additional templates folder here for the auth service.
Now in the terminal, we can CD into the templates folder and let's start off with the auth deployment.
So CD into the auth folder and we'll run the command we already ran earlier for the reservations deployment.
We'll run Kubectl, create deployment and we'll provide the image name now so we can go back to Google
Cloud into artifact registry.
So we'll click on Auth and click the copy button here to get the path to the auth repository and then
we'll paste in the image name here.
Make sure we also add the name of the deployment here in this case auth, and then we'll go ahead and
specify dry run equal to client here and specify the output should be in YAML format and then we will
pipe this to a deployment dot yaml file.
So we should now see in the auth folder we have the deployment now filled out, we'll get rid of the
time stamps here.
So go ahead and delete the two creation time stamps, the resources status and strategy object and go
ahead and add the slash production to the end of our image here to make sure we're using the production
image.
We'll go ahead and back in the terminal.
We'll repeat the same process next for the payments deployment.
So go ahead and regenerate this create deployment command and we will go ahead and just swap out the
repository URL here to make sure we're targeting the payments image and go ahead and change the name
of the deployment to payments as well.
Then we'll go ahead and open up this new deployment and repeat the same process, delete the strategy,
time stamps, resources and status, and go ahead and add a slash production to the end of the image
name here.
So finally, we need to do the same thing for the notifications service.
So let's go ahead and create a new deployment here, change payments out and add notifications and change
the name of the deployment.
Now to notifications as well.
Let's go ahead and open up the notifications deployment, delete the time stamps, strategy, resources
and status, and add the slash production to notifications image.
So now we have all of our deployments.
We're going to need to deploy our app.
Let's go ahead and upgrade our helm installation so that we can get these new deployments.
To do this, we can make sure we're at the root of the chart here and we'll go ahead and run helm upgrade
Sleeper and provide the path to the chart YAML In this case, in the current directory you can see that
our deployment here was upgraded and we can run kubectl get pods to see all of our different deployments
running and you can see they're all in an errored state and that's because we have no environment variables
as we've already seen with the Reservations pod.
We can run kubectl logs on the auth pod here and see the same issue.
We're not passing the environment variable validation.
So let's go ahead and continue to set up these environment variables properly.