According to this approach, each instance of a microservice runs on its own individual lightweight container. The container is nothing but a virtualization mechanism at the operating system level. This means that you can package your service as a container image, such as a Docker image, and you can deploy that image as a container, as illustrated in the following diagram:
As you can see in the preceding diagram, each container is virtualized over the operating system of the VM.
Docker is one of the most popular container-based technologies. Docker provides a way of packaging and deploying services. Each service is packaged as a Docker image, which is then deployed as a Docker container. You can use Docker containers with the following Docker clustering frameworks to manage your containers:
Kubernetes
Marathon/Mesos
Amazon EC2 Container Service
Docker images have their own port namespace and root filesystem and you can also set a resource utilization limit for each container.
Let's have a look at the benefits and drawbacks of this method.
Benefits
The benefits of the container approach are similar to those of the VM approach. It also has the following additional advantages:
Unlike VMs, containers are a lightweight technology
Building a container image is much faster than building a VM image; this is because the container doesn't have any lengthy OS boot mechanisms and it starts only the application process, rather than an entire OS
Each service instance is isolated, just like the VM approach
Drawbacks
This pattern has the following drawbacks:
Currently, the container infrastructure is not as mature as the infrastructure for VMs
The container infrastructure is not secure as the infrastructure for VMs
Containers don't provide as rich an infrastructure as VMs
It has less efficient resource utilization compared to the multiple services per host pattern because there are more hosts
We've now looked at multiple different approaches to deploying microservices. You can choose either VMs or containers for deploying microservices, according to your requirements.
Another method of deploying microservices that is becoming increasingly popular is serverless deployment. In the next section, we'll learn how to deploy microservices in a serverless environment.