Learn intelligent software delivery at your own pace. Step-by-step tutorials, videos, and reference docs to help you deliver customer happiness.
When it comes to modern software development, microservices are one of the hottest trends. These small, standalone software services allow developers to build and deploy smaller code chunks that can be updated more frequently and respond faster to user needs. In addition, with microservices, teams can update individual services on their own schedule instead of waiting for a new version of the entire application or OS upgrade. Each service is also much smaller, making it easier to understand, test, document, and maintain. Moreover, microservices help you address any challenges that may arise during development.
This blog post will explain what microservices are, why you should use them, their benefits, and how to deploy them using Harness.
A microservice is a small, autonomous software service inside a larger application. Traditional monolithic applications are comprised of modules built as one piece in a single codebase. In a microservices-based architecture, services are designed as independent and modular pieces and can be deployed separately. Each microservice typically handles one specific business function and is built, deployed, and managed independently
Image credit: microservices.io
While there are many different definitions of what a microservice is, there are a few characteristics they all possess:
As we mentioned earlier, these small services can be updated more frequently. This is especially advantageous when the requirements often change, new features need to be added, or if a particular service becomes bogged down with lots of requests and needs an upgrade. With microservices, teams can update individual services on their own schedule instead of waiting for a new version of the entire application or OS upgrade.
Another reason to use microservices is that they help you address new challenges in software development, such as scalability, continuous integration (CI), deployment, and maintenance. Let’s look at each of these challenges in more detail.
Scalability is the ability of a system, application, or process to handle increased amounts of work. When an application’s user base grows, you need to be able to add more servers to scale up the system so it can handle the increased load. You’ll need to add more servers when the application’s user base grows because the app can only handle so much work before it slows down or stops working completely.
With microservices, you can scale each service independently. You don’t have to add more servers to the entire application; you can just add more servers for the specific service that’s getting hit the hardest. This approach is known as sharding and is often used in database systems like Oracle, Postgres, and MySQL to scale out systems.
CI is the practice of building and testing new code regularly. When you’re consistently testing code, you can catch bugs and other issues before they release into production. The CI process often includes versioning, dependencies, code quality metrics, and other tools that help your team build software better and faster. icroservices are best used to design your application so that you can easily integrate and test each service independently. This is often referred to as “service-driven development.”
Deployment is the process of moving your application from your development environment to your production environment. You should design your application with microservices in mind so that each service is easy to deploy. With microservices, you can easily move each service between environments or update one service without affecting other services.
Let’s build a node.js microservice to know the distance between two zipcodes.
Create an app directory to store your application code and use the command npm init -y to initialize the project. Then, let’s add the main application code in the server.js file.
Let’s define routes and controller logic. First, create a new folder by the name api inside your root folder to hold our routes and controller logic.
The complete project folder structure is below:
Inside the api folder, create two files - controller.js and routes.js.
Add following code into the routes.js file:
Now, it is time to write some code to handle the external API. We will use zipcodeapi.com to calculate the distance between two locations with pincodes. Go to http://www.zipcodeapi.com and get the api key.
Now, create a new folder inside the root folder and name it service. Inside the service folder, create a new file named distance.js and add the following code:
Now, go to the main folder of the application and start the application by using the command npm start.
Go to your http://localhost:3002/about and you should see the name of the folder and version.
Next, check our next route i.e distance. Go to http://localhost:3000/distance/pincode1/pincode2.
Add pincode1 and pincode2 and you should see the distance between these two zip code areas.
Example is below,
Try again to find the distance between two zip codes - 35004 and 86556.
So, we have successfully built a simple microservice to know the distance between two zip codes.
Harness is a modern software delivery platform that helps organizations easily deploy their applications. We will now see how we can deploy this application on Kubernetes using Harness.
The first thing we need to do is to dockerize our application using a Dockerfile. Let’s write a simple Dockerfile for our application.
Using the below command, build the Docker image:
Now that we have pushed our application as an image to Dcoker Hub, we can deploy the application on Kubernetes using Harness CD module.
But before, we need to create Kubernetes manifest files deployment.yaml and service.yaml to deploy our application.
Let’s create deployment.yaml file.
Push the application code to a new GitHub repository.
Signup for the Harness platform and opt for the CD (continuous delivery) module.
Set up the required connectors, such as Docker Hub, GitHub etc and the delegate.
The Harness Delegate is a tool you need to install and run on the target cluster (Kubernetes cluster in our case) to connect your artifacts, infrastructure, collaboration, verification and other providers with the Harness Manager. When you set up Harness for the first time, you install a Harness Delegate.
Configure a simple pipeline for your application. This is where you will define service, infrastructure, execution type.
A service represents what you are deploying, like a Kubernetes manifest and a Docker image of your microservice. Infrastructure tells Harness where you're going to deploy your application. Execution is how this stage deploys its service to its infrastructure.
Make sure you have configured everything properly, save and run the pipeline to deploy your application onto Kubernetes.
You should see a successful deployment message.
Congratulations! We successfully built a node.js microservice and deployed it on Kubernetes with the help of Harness CD.
Ultimately, the pros and cons of using microservices will depend on your particular situation. However, in general, microservices are excellent for large and complex applications that need to scale. Therefore, they are often used in conjunction with containers and service-oriented architecture. On the other hand, microservices are not always valid for small applications. However, microservices are suitable for both large and small applications, and their usefulness depends on how they are implemented.
Microservices are the new trend in modern software development. They allow developers to build and deploy smaller, more independent code chunks that can be updated more frequently and respond better to user needs. While there are many different types of microservice architectures, we recommend that you choose a microservices architecture that makes sense for your business and technology stack.
Check out how Harness and microservices can take your application to the next level!
Enjoyed reading this blog post or have questions or feedback?
Share your thoughts by creating a new topic in the Harness community forum.