Deployment, Portability and Scalability of Microservices


1) Deployment Challenges:

Deploying 100s of Microservices with Less Effort & Cost

Deploying microservices involves managing many independent services that need to be consistently available across various environments. Docker helps streamline this process by packaging each microservice into a lightweight, self-contained container. This reduces the overhead and complexity associated with deploying each microservice separately, leading to cost efficiency and easier maintenance.

2) Portability Challenges:

Moving 100s of Microservices Across Environments with Less Effort, Configurations & Cost

Docker makes microservices highly portable across different environments, such as development, staging, and production. Since Docker containers package the application and all its dependencies into a single unit, the application can run consistently across any environment where Docker is supported, with minimal modifications needed.

3) Scalability Challenges:

Scaling Applications Based on Demand on the Fly with Minimum Effort & Cost

Docker supports dynamic scaling by allowing new containers to be instantiated based on demand without affecting the performance or stability of existing containers. This scalability is crucial for handling varying workloads without additional infrastructure investments.


Solution: Docker

Docker is an open-source platform that automates software deployment inside lightweight, portable containers. A container is a standard unit of software that packages up code and all its dependencies so that an application runs quickly and reliably from one computing environment to another.

Containers:

Namespaces and Control Groups (cgroups) in Docker:

Namespaces: Control Groups (cgroups):

Virtual Machine vs Docker

Virtual Machines (VMs): Docker:

Generating Docker Image from Existing Microservices

  1. Build the microservice: Compile and build the application as a JAR file (for Java Spring Boot) or executable (for other languages).
  2. Create a Dockerfile: A Dockerfile is a script containing instructions on how to build a Docker image.
  3. # Sample Dockerfile
    # Use the official Java image as base
    FROM openjdk:11
    
    # Set the working directory inside the container
    WORKDIR /app
    
    # Copy the compiled JAR file into the container
    COPY target/your-app.jar /app/
    
    # Expose the port that the application will run on
    EXPOSE 8080
    
    # Command to run the application
    CMD ["java", "-jar", "your-app.jar"]
    
  4. Build the Docker image: Navigate to the directory containing the Dockerfile and run the following command:
    docker build -t your-app .

Running Spring boot app as a Containers using Dockerfile

  1. Build the application: Compile the Spring Boot application to a standalone executable JAR. Run the command
    mvn clean install

  2. Create Dockerfile: Place the Dockerfile in the same directory as your JAR file.

  3. Build the Docker image: Run
    docker build -t image-name:tag .
    from the directory containing the Dockerfile.

  4. Run the Docker container:
    docker run -p 8080:8080 your-app

PORT Mapping in Docker

By default, containers are connected to an isolated network within the Docker host. To access a container from your local network, you need to configure port mapping explicitly. This involves specifying the desired mapping between an external port on your local machine and a port within the container. For example, when running the application using the `docker run` command, you can use the `-p 8080:8080` option to map port 8080 on the host machine to port 8080 within the container. The first value (`8080`) represents the external port, while the second value (`8080`) indicates the port inside the container.


Build docker image without Dockerfile

Buildpacks

Buildpacks are tools that automate the process of building container images directly from application source code without requiring a Dockerfile. They analyze the codebase, detect the programming language and dependencies, and create an optimized image with the necessary runtime and dependencies.

Buildpacks are part of the Cloud Native Buildpacks initiative, supported by platforms like Heroku and Cloud Foundry. They streamline containerization and are particularly useful for developers unfamiliar with writing Dockerfiles.

Add Buildpacks Dependency in Spring Boot's pom.xml

mvn spring-boot:build-image

Google Jib

Google Jib is an open-source Java containerization tool designed to create optimized Docker and OCI images for Java applications directly from their build systems, such as Maven or Gradle, without needing a Docker daemon.

Jib simplifies the process by layering the application, dependencies, and runtime efficiently, leading to faster builds and smaller image sizes.

Add Google Jib Dependency in Spring Boot's pom.xml

mvn compile jib:dockerBuild