นี่คือแอพตรวจสอบที่สร้างขึ้นด้วย Python และมันจะถูกเชื่อมต่อกับ Docker และนำไปใช้กับ EKS
aws configurepsutil และ Flask , Plotly, Libraries Boto3 ติด pip3 install -r requirements.txtpip3 install psutil และ flask pip install flaskpip install boto3pip install kubernetes การขยายตัวของ Kubernetes ใน VScodeในการเรียกใช้แอปพลิเคชันนำทางไปยังไดเรกทอรีรูทของโครงการและดำเนินการคำสั่งต่อไปนี้:
$ python3 app.py
สิ่งนี้จะเริ่มต้นเซิร์ฟเวอร์ Flask บน 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> สิ่งนี้จะเริ่มต้นเซิร์ฟเวอร์ Flask ในคอนเทนเนอร์ Docker บน localhost:5000 ไปที่ http: // localhost: 5000/บนเบราว์เซอร์ของคุณเพื่อเข้าถึงแอปพลิเคชัน
ecr.py :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>
สร้างคลัสเตอร์ cloud-native-cluster และเพิ่มกลุ่มโหนดในคอนโซล AWS
สร้างโหนดกลุ่ม nodes ในคลัสเตอร์ EKS
สร้างการปรับใช้และบริการในโฟลเดอร์ 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หากคุณวางแผนที่จะใช้ repo นี้เพื่อการเรียนรู้โปรดไปที่ดาว ขอบคุณ!