cloud native monitoring app
1.0.0
這是一個由Python構建的監視應用程序,它將與Docker置換並部署到EKS
aws configurepsutil和Flask ,繪圖,boto3庫。使用pip pip3 install -r requirements.txt安裝它們pip3 install psutil和燒瓶pip install flaskpip install boto3pip install kubernetes kubernetes在vscode中的擴展要運行應用程序,請導航到項目的根目錄並執行以下命令:
$ python3 app.py
這將在localhost:5000上啟動Blask Server。導航到http:// localhost:5000/在瀏覽器上訪問應用程序。
Dockerfile : # Use the official Python image as the base image
FROM python:3.9-slim-buster
# Set the working directory in the container
WORKDIR /app
# Copy the requirements file to the working directory
COPY requirements.txt .
RUN pip3 install --no-cache-dir -r requirements.txt
# Copy the application code to the working directory
COPY . .
# Set the environment variables for the Flask app
ENV FLASK_RUN_HOST=0.0.0.0
# Expose the port on which the Flask app will run
EXPOSE 5000
# Start the Flask app when the container is run
CMD ["flask", "run"] $ docker build -t <image_name> . $ docker run -p 5000:5000 <image_name>這將在localhost:5000 。導航到http:// localhost:5000/在瀏覽器上訪問應用程序。
ecr.py中使用Python創建ECR存儲庫:view push commands中找到該過程 import boto3
# Create an ECR client
ecr_client = boto3 . client ( 'ecr' )
# Create a new ECR repository
repository_name = 'my-ecr-repo'
response = ecr_client . create_repository ( repositoryName = repository_name )
# Print the repository URI
repository_uri = response [ 'repository' ][ 'repositoryUri' ]
print ( repository_uri )然後運行此python3 ecr.py
$ docker push <ecr_repo_uri>:<tag>
創建EKS群集cloud-native-cluster並在AWS控制台中添加節點組
在EKS群集中創建節點組nodes 。
在文件夾eks.py中創建部署和服務
from kubernetes import client , config
# Load Kubernetes configuration
config . load_kube_config ()
# Create a Kubernetes API client
api_client = client . ApiClient ()
# Define the deployment
deployment = client . V1Deployment (
metadata = client . V1ObjectMeta ( name = "my-flask-app" ),
spec = client . V1DeploymentSpec (
replicas = 1 ,
selector = client . V1LabelSelector (
match_labels = { "app" : "my-flask-app" }
),
template = client . V1PodTemplateSpec (
metadata = client . V1ObjectMeta (
labels = { "app" : "my-flask-app" }
),
spec = client . V1PodSpec (
containers = [
client . V1Container (
name = "my-flask-container" ,
image = "568373317874.dkr.ecr.us-east-1.amazonaws.com/my-cloud-native-repo:latest" ,
ports = [ client . V1ContainerPort ( container_port = 5000 )]
)
]
)
)
)
)
# This is an automation to run deployment and svc using python
# Create the deployment
api_instance = client . AppsV1Api ( api_client )
api_instance . create_namespaced_deployment (
namespace = "default" ,
body = deployment
)
# Define the service
service = client . V1Service (
metadata = client . V1ObjectMeta ( name = "my-flask-service" ),
spec = client . V1ServiceSpec (
selector = { "app" : "my-flask-app" },
ports = [ client . V1ServicePort ( port = 5000 )]
)
)
# Create the service
api_instance = client . CoreV1Api ( api_client )
api_instance . create_namespaced_service (
namespace = "default" ,
body = service
)確保使用圖像URL在第25行上編輯圖像的名稱。
要運行用於部署和服務的k8s命令,而不是添加python腳本
kubectl apply -f deployment.yml您kubectl apply -f service.yml創建deployment.yml and service.yml
aws eks update-kubeconfig --name cloud-native-cluster kubectl get deployment -n default (check deployments)
kubectl get service -n default (check service)
kubectl get pods <name of pod> -n default (to check the pods)
# edit images created if u made errors
kubectl edit deployment my-flask-app -n default
# this will pull down the editted image
kubectl get pod -n default -wPOD啟動並運行後,運行Port-Fornward以公開服務
kubectl port-forward service/<service_name> 5000:5000如果您打算使用此倉庫進行學習,請擊中明星。謝謝!