The question is, how to deploy the microservices after building them for your system? The answer containerization, or simple: how to pack everything from the operating system, applications, network configurations… all into one container.
In the world of microservices, container will help you deploy microservices much easier and more independent. Furthermore, it will also enable microservices to self-maneuver better via self-containing.
This article shall cover the following topics:
- Introduction about container concepts and related topics in the microservices context.
- How to approach the management, maintenance, and deployment of Docker images in the production environment.
What is Container?
Quite a hot topic, isn’t it? There are numerous definitions on container, but the easiest way to understand it is that it is something built on top of an operating system. In simpler words, it is the digitalization of operating systems.
Now, some programmers will question, what is the difference between VM and container?
The answer is illustrated as follows:
Benefits of Containers
What is Docker?
Docker is a platform that allows you to build, ship, and run containers based on Linux’s core. It is default support for Linux platforms.
Basic components of a Docker
1. The Docker daemon
The docker daemon is server parts that run on a Virtual Machine (VM), and is hot as servers that build, run, and distribute Docker containers. Docker clients shall interact with this daemon via a Rest API.
2.The Docker client
Docker client is a CLI that allows interaction with the Docker daemon via a socket or Rest API. Docker client can be run on the same host with the daemon or run separately on another host and connected to the daemon using CLI.
3. The Docker image
The most important concept to a Docker is the Docker image. This is a copy of OS libraries as well as related applications. In the microservices context built on Spring Boot platform, the Docker image can be contained in a Linux’s distribution like Alpine, JRE8 and in a Spring Boot microservice jar.
4. The Docker container
This can be understood simply as instances of a Docker image. The Docker container will utilize the kernel of Linux OS, and have its own filesystem and network configurations.
A file that includes scripts with instructions on how a building goes to a Docker image. Or in simpler words, it is a text file named Dockerfile.
Deploying microserivces on Docker
Let’s assume that we have a booking application, developed using a Spring Boot-based microservices structure, one like the following diagram:
Now, start to deploy containers for Dockers following the below steps:
1. Install Docker
To deploy microservices by Docker, you will first have to install Docker from (www.docker.com) and follow the installation instructions.
2. Prepare Dockerfile
In this step, we shall assume that you are designing services using Spring Boot and communicating via RabbitMQ. In such case, you will need to set application.properties as the reporting station of rabbitmg-connected related configurations, using an IP address instead of localhost. This is because localhost cannot be identified inside Docker containers. In reality, this will be performed using DNS or via a load balancer.
Create a Dockerfile and insert it to the original folder with the microservice source code for development. The file, in basic, is as follows:
// Replace the IP address of your machine, not using localhost
ADD target/reservation-1.0.jar reservation.jar
3. Build Docker
After reporting, we shall move on to building them (as in downloading the JDK8 images and performing commands in the Dockerfile), by simply running the docker build command in the folder with the Dockerfile.
$ docker build –t reservation:1.0
Here we will have 2 microservices: reservation and customer. Repeat steps 2 and 3 for the customer microservice.
4. Run a Docker container
Finally, we will run a Docker container using the command docker run. This shall load the container and run the file jar as reported in the Dockerfile.
$ docker run -p 8090:8090 -t reservation:1.0
To review whether all services are started, use the command
$ docker ps
5. Run RabbitMQ
As you have known, RabbitMQ is common in microservice structure. Therefore, to setup RabbitMQ as a Docker container, the easiest way is to take a pre-existing image on the Docker Hub. Now, you can easily communicate among the microservices with rabbitmq.
6. Docker Registry
Docker Hub is the storage for Docker Images. However, for security reasons, they can be stored in a private hub (local hub) instead. We can easily do this for Docker by registering your own Docker Registry.
Create a private registry with gate 3000
$ docker run -d -p 3000:3000 –restart=always –name registry registry:latest
$ docker tag reservation:1.0 localhost:3000/reservation:1.0
Finally, move the desired image to the registry.
$ docker push localhost:3000/reservation:1.0
It is safe to say that you have now attained a general outlook on how to build, ship, and manage Docker images on public and private registries in microservice deployment. Let’s hope that in the future, we can continue onto the topic of Docker and Microservices in working with EC2 (AWS), as well as other Microservices topics.
Dao Trong Nguyen – FPT Software
Source: Potato Tech Mag