|
1 | | -# Python sample project for EKS |
2 | | - |
3 | | -[](https://sonarcloud.io/summary/new_code?id=DevSecOpsSamples_eks-python-api) [](https://sonarcloud.io/summary/new_code?id=DevSecOpsSamples_eks-python-api) |
4 | | - |
5 | | -The sample project to deploy Python REST API application, Service, HorizontalPodAutoscaler, Ingress on EKS. |
6 | | - |
7 | | -- [app.py](app/app.py) |
8 | | -- [Dockerfile](app/Dockerfile) |
9 | | -- [python-ping-api-template.yaml](app/python-ping-api-template.yaml) |
10 | | - |
11 | | ---- |
12 | | - |
13 | | -## Prerequisites |
14 | | - |
15 | | -### Installation |
16 | | - |
17 | | -- [Installing or updating the latest version of the AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) |
18 | | - |
19 | | -### Set AWS configurations |
20 | | - |
21 | | -Set AWS configurations with `aws configure` for default region and keys: |
22 | | - |
23 | | -```bash |
24 | | -aws configure |
25 | | -``` |
26 | | - |
27 | | -```bash |
28 | | -AWS Access Key ID [None]: <enter-your-access-key> |
29 | | -AWS Secret Access Key [None]: <enter-your-secret-key> |
30 | | -Default region name [None]: us-east-1 |
31 | | -Default output format [None]: |
32 | | -``` |
33 | | - |
34 | | -```bash |
35 | | -aws configure get default.region |
36 | | -us-east-1 |
37 | | -``` |
38 | | - |
39 | | ---- |
40 | | - |
41 | | -## Create an EKS cluster and deploy AWS Load Balancer Controller |
42 | | - |
43 | | -Refer to the https://github.com/DevSecOpsSamples/eks-eksctl page. |
44 | | - |
45 | | -## Set environment variables |
46 | | - |
47 | | -```bash |
48 | | -REGION=$(aws configure get default.region) |
49 | | -ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text) |
50 | | -CLUSTER_NAME=$(kubectl config current-context | cut -d '@' -f2 | cut -d '.' -f1) |
51 | | -echo "REGION: ${REGION}, ACCOUNT_ID: ${ACCOUNT_ID}, CLUSTER_NAME: ${CLUSTER_NAME}" |
52 | | -``` |
53 | | - |
54 | | -## Deploy python-ping-api |
55 | | - |
56 | | -Build and push to ECR: |
57 | | - |
58 | | -```bash |
59 | | -cd ../app |
60 | | -docker build -t python-ping-api . --platform linux/amd64 |
61 | | - |
62 | | -docker tag python-ping-api:latest ${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/python-ping-api:latest |
63 | | - |
64 | | -aws ecr get-login-password --region ${REGION} | docker login --username AWS --password-stdin ${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com |
65 | | - |
66 | | -docker push ${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/python-ping-api:latest |
67 | | -``` |
68 | | - |
69 | | -Create and deploy K8s Deployment, Service, HorizontalPodAutoscaler, Ingress using the [python-ping-api-template.yaml](app/python-ping-api-template.yaml) template file. |
70 | | - |
71 | | -```bash |
72 | | - |
73 | | -sed -e "s|<account-id>|${ACCOUNT_ID}|g" python-ping-api-template.yaml | sed -e "s|<region>|${REGION}|g" > python-ping-api.yaml |
74 | | -cat python-ping-api.yaml |
75 | | - |
76 | | -kubectl apply -f python-ping-api.yaml |
77 | | -``` |
78 | | - |
79 | | -It may take around 5 minutes to create a load balancer, including health checking. |
80 | | - |
81 | | -Confirm that Pod and ALB logs. |
82 | | - |
83 | | -```bash |
84 | | -kubectl logs -l app=python-ping-api |
85 | | - |
86 | | -kubectl describe pods |
87 | | - |
88 | | -kubectl logs -f $(kubectl get po -n kube-system | egrep -o 'aws-load-balancer-controller-[A-Za-z0-9-]+') -n kube-system |
89 | | -``` |
90 | | - |
91 | | ---- |
92 | | - |
93 | | -## Cleanup |
94 | | - |
95 | | -```bash |
96 | | -kubectl delete -f app/python-ping-api.yaml |
97 | | - |
98 | | -``` |
99 | | - |
100 | | -## References |
101 | | - |
102 | | -- [Application load balancing on Amazon EKS](https://docs.aws.amazon.com/eks/latest/userguide/alb-ingress.html) |
| 1 | +# _eks-python-api |
| 2 | +Python REST API sample for EKS |
0 commit comments