Deploying Sell-it Locally
14 Dec 2023 - 10 min read
We're continuing from here, where we successfully set up the project again and registered a user.
In this chapter, my focus is on configuring a Kubernetes setup to deploy the app locally using Minikube. This involves a bit of over-engineering, as I mentioned in my first post about migrating Sell-it. I'm doing this for fun and to learn.
In a real-world scenario, the app could be deployed on Vercel for example, since we don't have many things. However, since I'm planning to decouple some of the API endpoints into microservices (again, just over-engineering), I prefer to manage all the infrastructure locally to avoid paying for it on a cloud provider.
Setup MongoDb
In a real production environment, I would prefer to host the production database directly on a cloud provider. However, for the sake of flexibility (using mongo:3.6
), the current setup is sufficient.
These are the configuration files with the Objects declared for Kubernetes:
Service
Deployment
Pvc
Setup server
Now it's time for the server, in this case we need to create a containerized version of our app to use in Kubernetes
ā ļø Git had to be installed for Bower installation
And finally here are the Kubernetes config files:
Service
Deployment
The deployment is just a template that will be replaced by a generated one using envsubst, by this way I can set automatically the new IMAGE_TAG
every time a build a new one.
Configmap
The configmap is just a template as well that will be replaced by a generated one containing the variables, I'm not adding the env variables as Secret.
Additionally, there is a livenessProbe
with a new endpoint I created. Ideally, this could be managed by some Kubernetes library instead of having the endpoint here, but for now, that's not a problem.
Some notes about the config files
For the image hosting I'm not using Dockerhub, but a local registry, you can setup yours follwing these steps, the only thing you must take into account is to have Minikube started using the minikube start --insecure-registry "your-insecure-registry ip and port"
, additional configuration might be needed, for example in my case I added in the /etc/hosts
a custom domain personal.local
and I had to added in my Docker engine config.
Deploy
Since I'm not deploying anywhere in the cloud, I can't use a proper CI/CD system to automate the necessary steps. Instead, I'm currently doing it manually. I will explore other solutions in the future, but sometimes the priority is to make it work before optimizing processes.
Makefile
To make things easier, I thought on creating a Makefile to automate the process by doing the following steps:
- Build the image, tag it and push it to the local registry
- Make use on
envsubst
to replace my.env.production
variables to the placeholders of my Kubernetes files
Additionally there are some dev
commands.
And that's all, with this configuration we can run make deploy
that will apply the new configuration to our Minikube cluster.
From this point there are other things we can do apart from optimizing the Makefile (there are some things like the building image process), the K8s files or the Dockerfile.
In addition I set a custom domain sell-it.localhost
(the localhost is just to signal Chrome we are local and then use Secure options) to my /etc/hosts
.
Finally, in order to access the site, I use minikube tunnel
although you can use also minikube service <your-service>
Recap
In this chapter, my goal was to deploy the app and MongoDB in Minikube. The experience has been interesting and fun, and it has sparked my interest to delve deeper into Kubernetes and related technologies.
Here are the code changes
Next steps
Now, I'm planning on populating the application with some data to make it more realistic and fix some issues. After that, I'm considering whether to start creating a frontend, decoupling some parts of the API, or simulating a desire for a new feature in the existing code. I'll give it some thought.
Let's continue with the adventure! š