Skip to content

ALPHAX28/kubernetes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Project Structure

├── 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

Directory Descriptions

  • 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.

Components Overview

Client

  • Purpose: The client component serves as the entry point for users. It is a Python-based service that communicates with the backend services (server1 and server2). 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.

Server1

  • Purpose: Server1 is 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 for server1. 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 how server1 should 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 for server1. It details the steps required to build the image, such as copying files, installing Python dependencies, and defining the service’s entry point.

Server2

  • Purpose: Server2 is the second backend service, typically used for different processing tasks than Server1. Like Server1, it is stateless and can be easily scaled within the Kubernetes cluster.

  • Key Files:

    • server2.py: The Python script for Server2, implementing its specific logic. It handles requests from the client, processes them, and sends back responses.
    • server2-dep.yaml: The Kubernetes deployment file for Server2. It defines the number of replicas, the Docker image to use, and other deployment configurations.
    • server2-service.yaml: The Kubernetes service descriptor for Server2, which determines how this service is exposed within the cluster or to external clients.
    • Dockerfile.server2: The Dockerfile for Server2, detailing how to build its Docker image, including installing dependencies and setting the entry point.

Kubernetes (k8s)

  • 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.

Deployment Guide

Prerequisites

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.

Steps to Deploy

1. Build Docker Images

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 .

2. Push Images to a Docker Registry

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-image

3. Deploy Services to Kubernetes

Deploy 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.yaml

4. Set Up Load Balancer

To ensure that traffic is evenly distributed across your services, apply the load balancer manifest:

kubectl apply -f k8s/load-balancer-service.yaml

This command will set up a load balancer that directs incoming traffic to the appropriate services.

5. Access the Application

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 services

Look for the external IP associated with the load balancer service. Use this IP in your web browser to interact with the client application.

6. Check Pod Status

kubectl get pods

Ensure all the server pods are running by checking the pod status:

7. Check Logs

kubectl logs <pod-name>

Check the logs by providing the pod name

The output of the logfile will be as follows :-

Hello from server1-7b698c776d-z2x7t
Hello from server2-887b99986-467b7
Hello from server1-7b698c776d-z2x7t
Hello from server2-887b99986-467b7

Future Enhancements

To 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.

Conclusion

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages