Join us
@muzairs ã» Apr 30,2025 ã» 3 min read ã» Originally posted on medium.com
In this article, we will explore k8s and Docker to manage our application.
We will divide this in the following manner:
We will first explore the micro-services architectural style.
What are microservices?
Micro-services architecture (often shortened to microservices) refers to an architectural style for developing applications. Micro-services allow a large application to be separated into smaller independent parts.
What this means is, your application consists of different functions such as payment gateway, front-end, backend, etc. (These are very broad classifications and consist of many entities which can be broken down into simpler units.). Managing all these if they are tightly coupled is difficult. Hence micro-service architecture tends to break your functionalities into simpler, manageable units which can be managed and scaled independently. If you want to explore more about this see the article.
How to achieve this?
It can be achieved through containerization and docker helps achieve this. Since this article focuses on managing apps with docker and k8s, if you want to learn more about docker and Kubernetes see this.
We have our app, React as Frontend, and node as Backend.
Link for repository:
To build an image we need a docker file, first, we will do it for the backend.
Since it is a node app we will need a node image (which is already available on Docker Hub and we will build on top of that). Second line WORKDIR /app
sets working directory when app will run as /app.
The third line, copies package files to install all dependencies we require in our app. COPY . .
this line copies all contents from the directory where Dockerfile is located to /app
. And finally, to run our app we run command npm index.js.
EXPOSE 8080 is more like documentation detail to tell port on which app will run on.
To build a Docker image we will run: docker build -t muzairs/classroom:latest .
We can push this image to the Docker registry, by logging into the docker hub and creating a repository of the name classroom
.
docker push muzairs/classroom:latest
.
If you name your repository on docker hub with different name then prior to push you have to tag it correctly. docker tag <username>/repository:tag
This will start building the image as specified in Dockerfile layer by layer, each line in the file adds a layer.
Now, we will create a deployment manifest so that we can manage it with Kubernetes. If you didnât understand something in this manifest take a look at k8s docs or comment below.
Above manifests, is a declarative approach to tell k8s API server, that
app: server
and tier: backend
(labels are used by Service to route traffic to pods of deployment).Now, to expose our deployment, we need to create a service.
The Service manifest is as follows:
Above Service, is a declarative approach to tell k8s API server, that
app: server
and tier: backend
(Note: selectors should match selectors of Pods otherwise Pods created will not be considered by Service and your application will not be reachable.)Now, the Backend part is completed.
Now we move to frontend.
For this also we need docker file, deployment and service.
Dockerfile:
We require node as the base image as previous, set our working directory, copy package JSON files, install dependencies and copy all files.
After doing the above steps we tell to build our react app npm run build
.
We will be using nginx to serve our files. So we also need an nginx image and copy our .conf
file and build files at the correct place.
Deployment:
Service:
Only change here is type of Service is NodePort, because we want our users to reach our frontend and use our app, with type as NodePort, a port is exposed on each node which is accessible externally. (You can also set type as LoadBalancer).
We have created all the required files we needed, now we have to deploy them on the k8s cluster.
Note: It is better to create service before Pods/deployments which depend on that particular service. (Order of creation matters)
Now, if you run kubectl get svc front-svc
you should get a IP address from which you can access your app.
(If you are running on minikube things are bit different, you need to run minikube service front-svc --url
, copy the URL obtained to access your app.
And, you have successfully deployed on k8s.ð¥³ðð»
If you like, donât forget to clapðð»ð.
Join other developers and claim your FAUN account now!
Influence
Total Hits
Posts
Only registered users can post comments. Please, login or signup.