cloud native monitoring app
1.0.0
이것은 Python으로 구축 된 모니터링 앱이며 Docker와 함께 연속화되어 EK에 배치됩니다.
aws configure 구성psutil 및 Flask , Plotly, Boto3 라이브러리를 사용합니다. PIP pip3 install -r requirements.txt 사용하여 설치하십시오pip3 install psutil 및 FLASK pip install flaskpip install boto3 용 Python을 설치하십시오pip install kubernetes vscode에서 kubernetes의 확장응용 프로그램을 실행하려면 프로젝트의 루트 디렉토리로 이동하여 다음 명령을 실행하십시오.
$ python3 app.py
이것은 localhost:5000 에서 플라스크 서버를 시작합니다. 애플리케이션에 액세스하려면 브라우저에서 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 의 Docker 컨테이너에서 플라스크 서버를 시작합니다. 애플리케이션에 액세스하려면 브라우저에서 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>
AWS 콘솔에서 EKS 클러스터 cloud-native-cluster 생성 및 노드 그룹 추가
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
)25 행의 이미지 이름을 이미지 URL로 편집하십시오.
배포 및 서비스에 대한 K8S 명령을 실행하려면 Python 스크립트를 추가하는 대신
deployment.yml and service.yml을 사용합니다kubectl apply -f deployment.yml및kubectl apply -f 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 -w포드가 가동되고 실행되면 포트 포워드를 실행하여 서비스를 노출시킵니다.
kubectl port-forward service/<service_name> 5000:5000학습을 위해이 저장소를 사용할 계획이라면 별을 치십시오. 감사해요!