{"id":15968889,"url":"https://github.com/yasheela-alla/cloud-native-devops","last_synced_at":"2025-04-04T14:26:06.272Z","repository":{"id":255837472,"uuid":"853714512","full_name":"yasheela-alla/Cloud-Native-devOps","owner":"yasheela-alla","description":"Cloud Native app on K8S to monitor system resources using python","archived":false,"fork":false,"pushed_at":"2024-09-07T10:37:08.000Z","size":9,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-10T00:19:24.621Z","etag":null,"topics":["aws","dockerfiles","ecr","eks-cluster"],"latest_commit_sha":null,"homepage":"","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yasheela-alla.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-09-07T10:27:13.000Z","updated_at":"2024-09-07T10:39:18.000Z","dependencies_parsed_at":"2024-09-07T11:57:29.465Z","dependency_job_id":null,"html_url":"https://github.com/yasheela-alla/Cloud-Native-devOps","commit_stats":null,"previous_names":["yasheela-alla/cloud-native-devops"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yasheela-alla%2FCloud-Native-devOps","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yasheela-alla%2FCloud-Native-devOps/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yasheela-alla%2FCloud-Native-devOps/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yasheela-alla%2FCloud-Native-devOps/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yasheela-alla","download_url":"https://codeload.github.com/yasheela-alla/Cloud-Native-devOps/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247192477,"owners_count":20899107,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","dockerfiles","ecr","eks-cluster"],"created_at":"2024-10-07T19:04:23.574Z","updated_at":"2025-04-04T14:26:06.243Z","avatar_url":"https://github.com/yasheela-alla.png","language":"HTML","funding_links":[],"categories":[],"sub_categories":[],"readme":"# **Cloud Native Resource Monitoring Python App on K8s!**\n\n## **Prerequisites** !\n\n- [x]  AWS Account.\n- [x]  Programmatic access and AWS configured with CLI.\n- [x]  Python3 Installed.\n- [x]  Docker and Kubectl installed.\n- [x]  Code editor (Vscode)\n\n\n## **Part 1: Deploying the Flask application locally**\n\n### **Step 1: Clone the code**\n\nClone the code from the repository:\n\n```\ngit clone \u003crepository_url\u003e\n```\n\n### **Step 2: Install dependencies**\n\nThe application uses the **`psutil`** and **`Flask`, Plotly, boto3** libraries. Install them using pip:\n\n```\npip3 install -r requirements.txt\n```\n\n### **Step 3: Run the application**\n\nTo run the application, navigate to the root directory of the project and execute the following command:\n\n```\npython3 app.py\n```\n\nThis will start the Flask server on **`localhost:5000`**. Navigate to [http://localhost:5000/](http://localhost:5000/) on your browser to access the application.\n\n## **Part 2: Dockerizing the Flask application**\n\n### **Step 1: Create a Dockerfile**\n\nCreate a **`Dockerfile`** in the root directory of the project with the following contents:\n\n```\n# Use the official Python image as the base image\nFROM python:3.9-slim-buster\n\n# Set the working directory in the container\nWORKDIR /app\n\n# Copy the requirements file to the working directory\nCOPY requirements.txt .\n\nRUN pip3 install --no-cache-dir -r requirements.txt\n\n# Copy the application code to the working directory\nCOPY . .\n\n# Set the environment variables for the Flask app\nENV FLASK_RUN_HOST=0.0.0.0\n\n# Expose the port on which the Flask app will run\nEXPOSE 5000\n\n# Start the Flask app when the container is run\nCMD [\"flask\", \"run\"]\n```\n\n### **Step 2: Build the Docker image**\n\nTo build the Docker image, execute the following command:\n\n```\ndocker build -t \u003cimage_name\u003e .\n```\n\n### **Step 3: Run the Docker container**\n\nTo run the Docker container, execute the following command:\n\n```\ndocker run -p 5000:5000 \u003cimage_name\u003e\n```\n\nThis will start the Flask server in a Docker container on **`localhost:5000`**. Navigate to [http://localhost:5000/](http://localhost:5000/) on your browser to access the application.\n\n## **Part 3: Pushing the Docker image to ECR**\n\n### **Step 1: Create an ECR repository**\n\nCreate an ECR repository using Python:\n\n```\nimport boto3\n\n# Create an ECR client\necr_client = boto3.client('ecr')\n\n# Create a new ECR repository\nrepository_name = 'my-ecr-repo'\nresponse = ecr_client.create_repository(repositoryName=repository_name)\n\n# Print the repository URI\nrepository_uri = response['repository']['repositoryUri']\nprint(repository_uri)\n```\n\n### **Step 2: Push the Docker image to ECR**\n\nPush the Docker image to ECR using the push commands on the console:\n\n```\ndocker push \u003cecr_repo_uri\u003e:\u003ctag\u003e\n```\n\n## **Part 4: Creating an EKS cluster and deploying the app using Python**\n\n### **Step 1: Create an EKS cluster**\n\nCreate an EKS cluster and add node group\n\n### **Step 2: Create a node group**\n\nCreate a node group in the EKS cluster.\n\n### **Step 3: Create deployment and service**\n\n```jsx\nfrom kubernetes import client, config\n\n# Load Kubernetes configuration\nconfig.load_kube_config()\n\n# Create a Kubernetes API client\napi_client = client.ApiClient()\n\n# Define the deployment\ndeployment = client.V1Deployment(\n    metadata=client.V1ObjectMeta(name=\"my-flask-app\"),\n    spec=client.V1DeploymentSpec(\n        replicas=1,\n        selector=client.V1LabelSelector(\n            match_labels={\"app\": \"my-flask-app\"}\n        ),\n        template=client.V1PodTemplateSpec(\n            metadata=client.V1ObjectMeta(\n                labels={\"app\": \"my-flask-app\"}\n            ),\n            spec=client.V1PodSpec(\n                containers=[\n                    client.V1Container(\n                        name=\"my-flask-container\",\n                        image=\"568373317874.dkr.ecr.us-east-1.amazonaws.com/my-cloud-native-repo:latest\",\n                        ports=[client.V1ContainerPort(container_port=5000)]\n                    )\n                ]\n            )\n        )\n    )\n)\n\n# Create the deployment\napi_instance = client.AppsV1Api(api_client)\napi_instance.create_namespaced_deployment(\n    namespace=\"default\",\n    body=deployment\n)\n\n# Define the service\nservice = client.V1Service(\n    metadata=client.V1ObjectMeta(name=\"my-flask-service\"),\n    spec=client.V1ServiceSpec(\n        selector={\"app\": \"my-flask-app\"},\n        ports=[client.V1ServicePort(port=5000)]\n    )\n)\n\n# Create the service\napi_instance = client.CoreV1Api(api_client)\napi_instance.create_namespaced_service(\n    namespace=\"default\",\n    body=service\n)\n```\n\nmake sure to edit the name of the image on line 25 with your image Uri.\n\n- Once you run this file by running “python3 eks.py” deployment and service will be created.\n- Check by running following commands:\n\n```jsx\nkubectl get deployment -n default (check deployments)\nkubectl get service -n default (check service)\nkubectl get pods -n default (to check the pods)\n```\n\nOnce your pod is up and running, run the port-forward to expose the service\n\n```bash\nkubectl port-forward service/\u003cservice_name\u003e 5000:5000\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyasheela-alla%2Fcloud-native-devops","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyasheela-alla%2Fcloud-native-devops","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyasheela-alla%2Fcloud-native-devops/lists"}