├── client
│ ├── client-dep.yaml
│ ├── client.py
│ └── Dockerfile.client
├── k8s
│ └── load-balancer-service.yaml
├── server1
│ ├── Dockerfile.server1
│ ├── server1-dep.yaml
│ ├── server1-service.yaml
│ └── server1.py
├── server2
│ ├── Dockerfile.server2
│ ├── server2-dep.yaml
│ ├── server2-service.yaml
│ └── server2.py- client: Contains everything related to the frontend service. This includes the Python code that runs the service, the Kubernetes deployment descriptor, and the Dockerfile used to build the Docker image for the service.
- k8s: Contains Kubernetes-specific configuration files, like the load balancer manifest, which handles traffic distribution among the services.
- server1: Contains everything related to the first backend service. This includes the Python code for the backend logic, Kubernetes deployment and service descriptors, and the Dockerfile for containerization.
- server2: Contains everything related to the second backend service. Similar to
server1, it has the Python code, Kubernetes descriptors, and a Dockerfile.
-
Purpose: The client component serves as the entry point for users. It is a Python-based service that communicates with the backend services (
server1andserver2). It sends requests to these services and displays the results. -
Key Files:
client.py: The main script for the frontend logic, written in Python. It contains code for sending HTTP requests to the backend services and processing the responses.client-dep.yaml: This Kubernetes deployment file defines how the client service should be deployed in the cluster. It includes details like the number of replicas, the Docker image to use, and resource allocation.Dockerfile.client: A Dockerfile that specifies how to build the Docker image for the client service. It includes instructions for copying the necessary files, installing dependencies, and setting the entry point.
-
Purpose:
Server1is the first backend service, designed to handle specific business logic. It is typically stateless, allowing Kubernetes to easily scale it up or down based on demand. -
Key Files:
server1.py: The Python script that implements the business logic for this service. It processes requests from the client and returns the appropriate responses.server1-dep.yaml: The Kubernetes deployment file forserver1. It specifies how many replicas of this service should run, which Docker image to use, and how to configure the service in the cluster.server1-service.yaml: A Kubernetes service descriptor that defines howserver1should be exposed to other services or external clients. This could be a ClusterIP, NodePort, or LoadBalancer service.Dockerfile.server1: The Dockerfile used to create the Docker image forserver1. It details the steps required to build the image, such as copying files, installing Python dependencies, and defining the service’s entry point.
-
Purpose:
Server2is the second backend service, typically used for different processing tasks thanServer1. LikeServer1, it is stateless and can be easily scaled within the Kubernetes cluster. -
Key Files:
server2.py: The Python script forServer2, implementing its specific logic. It handles requests from the client, processes them, and sends back responses.server2-dep.yaml: The Kubernetes deployment file forServer2. It defines the number of replicas, the Docker image to use, and other deployment configurations.server2-service.yaml: The Kubernetes service descriptor forServer2, which determines how this service is exposed within the cluster or to external clients.Dockerfile.server2: The Dockerfile forServer2, detailing how to build its Docker image, including installing dependencies and setting the entry point.
-
Purpose: Kubernetes is used to orchestrate the deployment, scaling, and management of the services. It ensures that the application is resilient and can handle varying loads by automatically scaling the services and managing their lifecycle.
-
Key File:
load-balancer-service.yaml: This Kubernetes manifest file defines a load balancer service that distributes incoming traffic across multiple instances of the client service (or any other services). This helps in achieving high availability and better resource utilization.
Before deploying the application, ensure you have the following tools and configurations:
- Docker: Installed and running on your local machine. Docker is required to build and run the containers.
- Kubernetes Cluster: Set up and running. You can use Minikube or Microk8s for local development or any cloud provider (e.g., GKE, EKS, AKS) for production-grade clusters.
- kubectl: The Kubernetes command-line tool should be configured to interact with your cluster. This tool is used to deploy and manage the services within Kubernetes.
Each service (client, server1, and server2) needs to be containerized. Navigate to the respective directories and build the Docker images using the following commands:
# Build client image
cd client
docker build -t client-image -f Dockerfile.client .
# Build server1 image
cd ../server1
docker build -t server1-image -f Dockerfile.server1 .
# Build server2 image
cd ../server2
docker build -t server2-image -f Dockerfile.server2 .If you are deploying to a remote Kubernetes cluster, you need to push the Docker images to a registry like Docker Hub or any other private registry.
# Tag and push client image
docker tag client-image <your-docker-repo>/client-image
docker push <your-docker-repo>/client-image
# Tag and push server1 image
docker tag server1-image <your-docker-repo>/server1-image
docker push <your-docker-repo>/server1-image
# Tag and push server2 image
docker tag server2-image <your-docker-repo>/server2-image
docker push <your-docker-repo>/server2-imageDeploy each service (client, server1, server2) using the Kubernetes YAML files. These files define the deployment configurations such as the number of replicas, the Docker images to use, and more.
# Deploy client service
kubectl apply -f client/client-dep.yaml
# Deploy server1 service
kubectl apply -f server1/server1-dep.yaml
kubectl apply -f server1/server1-service.yaml
# Deploy server2 service
kubectl apply -f server2/server2-dep.yaml
kubectl apply -f server2/server2-service.yamlTo ensure that traffic is evenly distributed across your services, apply the load balancer manifest:
kubectl apply -f k8s/load-balancer-service.yamlThis command will set up a load balancer that directs incoming traffic to the appropriate services.
After deploying all the services and setting up the load balancer, you can access the application using the external IP provided by the load balancer.
kubectl get servicesLook for the external IP associated with the load balancer service. Use this IP in your web browser to interact with the client application.
kubectl get podsEnsure all the server pods are running by checking the pod status:
kubectl logs <pod-name>Check the logs by providing the pod name
Hello from server1-7b698c776d-z2x7t
Hello from server2-887b99986-467b7
Hello from server1-7b698c776d-z2x7t
Hello from server2-887b99986-467b7To further improve the application and its deployment process, consider the following enhancements:
-
CI/CD Integration: Automate the build, test, and deployment processes using continuous integration/continuous deployment (CI/CD) pipelines. Tools like Jenkins, GitLab CI, or GitHub Actions can be integrated to automatically build and deploy the application whenever changes are made.
-
Logging and Monitoring: Integrate logging and monitoring solutions such as Prometheus, Grafana, or ELK stack (Elasticsearch, Logstash, Kibana) to gain insights into the application's performance, identify issues, and monitor resource usage.
-
Scaling: Implement Kubernetes Horizontal Pod Autoscaler (HPA) to automatically scale the number of replicas based on CPU or memory usage. This ensures the application can handle increased load without manual intervention.
-
Security: Secure your deployment by implementing role-based access control (RBAC), network policies, and secret management. This helps protect sensitive data and restricts access to critical resources.
This project exemplifies the principles of modern application deployment using microservices architecture, Docker for containerization, and Kubernetes for orchestration. The detailed setup guide, along with the future enhancements, provides a robust foundation for deploying scalable and resilient applications in a production environment.