Virtualization (MSA)
Virtualization (MSA)
Virtualization (MSA)
• They offer isolation between different VMs, ensuring that processes and applications within
one VM do not interfere with others.
• Use Case: Ideal when different services require completely separate OS environments.
Advantages:
• Can run multiple OS types (e.g., Linux, Windows) on the same physical machine.
Disadvantages:
• Containers are lightweight virtual machines but do not include a full operating system.
• They share the host OS kernel while still maintaining isolation between containers using
namespaces and cgroups.
• Use Case: Ideal for microservices because they require fewer resources and start faster than
VMs.
Advantages:
Disadvantages:
• Slightly less isolated than VMs since they share the host OS.
• Not suitable for running applications that need different kernels or OS types.
Containerization Platforms:
1. Docker
2. Rancher
3. Podman
4. Containerd
• It allows developers to package applications and their dependencies into a single, portable
unit (container) that can run consistently across different environments, whether on a
developer's laptop, in a testing environment, or in production.
• Docker simplifies the software delivery process by enabling applications to run in isolation
from each other, making it easier to manage different services and versions.
1. Docker Image
• It includes:
o Application code
Example: A Docker image for a web service might contain the app code, a web server, and all
required libraries.
2. Docker Container
Example: Multiple containers can run the same image with different configurations for development,
testing, and production.
3. Docker Registry
• Private registries can also be created within organizations for secure image distribution.
Use Case: Store your microservice images on Docker Hub or in a private registry and pull them when
deploying.
4. Docker Compose
How it works: It uses a docker-compose.yml file to define services and their relationships.
6. Advantages of Docker
2. Portability:
o Problem: Moving services across systems or cloud providers can cause compatibility
issues.
o Solution: Containers are portable and run on any machine supporting Docker.
o Solution: Docker images support versioning. If an update fails, you can roll back to a
previous image version.
o Problem: Building, testing, and deploying without automation can be slow and
prone to errors.
1. Managing multiple containers across multiple hosts: Requires tools like Kubernetes.
4. Managing persistent data: Docker doesn't handle persistent data across container restarts
well.
5. Resource management: It doesn’t manage CPU and memory across a cluster of containers.
6. Service Discovery: Requires integration with tools for service registration and discovery (like
Eureka).
Creating a Docker Image for a Spring Boot Application
Prerequisites
• You need a WAR file generated by IntelliJ after building your Spring Boot service.
1. Create a Dockerfile
• Create a file named Dockerfile and place it in the base project folder (e.g., inside the demo
folder).
FROM container-registry.oracle.com/java/openjdk:latest
ARG JAR_FILE=build/libs/demo-0.0.1-SNAPSHOT.war
• FROM: Specifies the base image. Here, it uses the latest OpenJDK image from Oracle.
• ARG JAR_FILE: Declares an argument that holds the path to the WAR file.
• COPY: Copies the specified WAR file into the container and renames it to myDemo.jar.
• ENTRYPOINT: Specifies the command to run when the container starts, which is the Java
command to execute the JAR file.
To create a Docker image from the Dockerfile, run the following command in the terminal from the
demo directory:
3. Running a Container
To run a container based on the created image, use the following command:
Command Breakdown
• docker run: Creates and runs a new container from the specified image.
• --name myContainer: Assigns a name to the container (you can choose any name).
After running the command, you can access your microservice at:
http://localhost:9090
docker ps -a
5. Stopping a Container
To run commands inside a running Docker container, use the docker exec command. This is useful for
debugging or monitoring:
• <command>: Replace with the command you want to execute inside the container.
Kubernetes
Why Do We Need Kubernetes?
Docker is great for running individual containers, but when you need to:
6. Make services discoverable automatically (so they can talk to each other)
That’s where Kubernetes helps as an orchestration platform to manage all these challenges.
1. Pod
• For microservices, the best practice is one container per pod to keep things simple and
isolated.
2. ReplicaSet
• Example: If you specify 3 replicas of a pod, Kubernetes will keep 3 instances running.
If one crashes, it will automatically start a new one to replace it.
3. Service
• Example: You may have 5 instances of an Order Processing service running as pods.
A Service ensures that traffic is distributed to the right pods and allows other services to
discover it.
4. Deployment
• Used to manage stateless applications (apps that don’t store data across restarts).
• Pods are dynamic (they are created with random names, and if they stop, new ones are
created).
5. StatefulSet
• Used for managing stateful applications (apps that need to keep data across restarts).
• Pods have stable identities (like consistent hostnames), so even if a pod is restarted, it can
reconnect to the right storage.
PersistentVolumeClaim (PVC)
• It abstracts storage, meaning the application doesn’t need to know the details of the
underlying storage.
It just asks for storage, and Kubernetes makes sure the request is fulfilled.
How Kubernetes Works (Using the Image Above)
1. Kubernetes Control Plane: Manages the overall cluster and decides which pod should run on
which node.
3. HTTP Communication: The microservices (inside containers) communicate with each other
over HTTP.
Using GitHub for Container Image Repository
1. Create a Personal Access Token (PAT) on GitHub
1. Login to GitHub
2. Settings:
3. Developer Settings:
o Check:
write:packages
read:packages
• Replace:
The Kubernetes Secret will store your GitHub credentials, so the cluster can pull images from
your private GitHub registry.
--docker-server=ghcr.io \
--docker-username=<UserName> \
--docker-password=<PAT> \
• Replace:
1. Deployment YAML:
o Defines how many replicas (pods) to run and what image to use.
o The deployment ensures pods are running and automatically restarted if needed.
2. Service YAML:
o Creates pods and pulls the image from GitHub using the secret.
Once the deployment is up, access your application using the External IP and Port.
• You can change the number of replicas (pods) by editing the deployment.yaml file.