Join us

Deploying applications on Kubernetes

1_aHPQA928MSZEcugNSjg6ug.jpeg

In this article, we will explore k8s and Docker to manage our application.

We will divide this in the following manner:

  • What are microservices?
  • Build docker images and push to docker hub
  • Create deployment and services
  • Finally, run kubectl commands.

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:

  1. Build a docker image for frontend and backend

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

  • We want a deployment named backend, with labels app: server and tier: backend (labels are used by Service to route traffic to pods of deployment).
  • We want 1 replicas i.e 1 pod running a container with image provided and container is exposed at port 80. So in case of failure of Pod, new pods are created to match desired state.

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

  • We want a service with name back-svc, with selectors 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.)
  • Type of this service is ClusterIP and any request get redirected to the appropriate pods.

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👏🏻😁.


Only registered users can post comments. Please, login or signup.

Start blogging about your favorite technologies, reach more readers and earn rewards!

Join other developers and claim your FAUN account now!

User Popularity
46

Influence

2k

Total Hits

1

Posts