|
1 | | -# python-gradle |
2 | | -Python, pytest, Gradle, Sonarqbue |
| 1 | +# Python sample project for GKE |
| 2 | + |
| 3 | +[](https://sonarcloud.io/summary/new_code?id=DevSecOpsSamples_gke-python-api) [](https://sonarcloud.io/summary/new_code?id=DevSecOpsSamples_gke-python-api) |
| 4 | + |
| 5 | +The sample project to deploy Python REST API application, Service, HorizontalPodAutoscaler, Ingress, and BackendConfig on GKE. |
| 6 | + |
| 7 | +- [app.py](app/app.py) |
| 8 | +- [python-ping-api-template.yaml](app/python-ping-api-template.yaml) |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +### Installation |
| 15 | + |
| 16 | +- [Install the gcloud CLI](https://cloud.google.com/sdk/docs/install) |
| 17 | +- [Install kubectl and configure cluster access](https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-access-for-kubectl) |
| 18 | + |
| 19 | +### Set environment variables |
| 20 | + |
| 21 | +```bash |
| 22 | +PROJECT_ID="sample-project" # replace with your project |
| 23 | +COMPUTE_ZONE="us-central1" |
| 24 | +``` |
| 25 | + |
| 26 | +### Set GCP project |
| 27 | + |
| 28 | +```bash |
| 29 | +gcloud config set project ${PROJECT_ID} |
| 30 | +gcloud config set compute/zone ${COMPUTE_ZONE} |
| 31 | +``` |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +## Create a GKE cluster |
| 36 | + |
| 37 | +Create an Autopilot GKE cluster. It may take around 9 minutes. |
| 38 | + |
| 39 | +```bash |
| 40 | +gcloud container clusters create-auto sample-cluster --region=${COMPUTE_ZONE} |
| 41 | +gcloud container clusters get-credentials sample-cluster |
| 42 | +``` |
| 43 | + |
| 44 | +--- |
| 45 | + |
| 46 | +## Deploy python-ping-api |
| 47 | + |
| 48 | +Build and push to GCR: |
| 49 | + |
| 50 | +```bash |
| 51 | +cd ../app |
| 52 | +docker build -t python-ping-api . --platform linux/amd64 |
| 53 | +docker tag python-ping-api:latest gcr.io/${PROJECT_ID}/python-ping-api:latest |
| 54 | + |
| 55 | +gcloud auth configure-docker |
| 56 | +docker push gcr.io/${PROJECT_ID}/python-ping-api:latest |
| 57 | +``` |
| 58 | + |
| 59 | +Create and deploy K8s Deployment, Service, HorizontalPodAutoscaler, Ingress, and GKE BackendConfig using the [python-ping-api-template.yaml](app/python-ping-api-template.yaml) template file. |
| 60 | + |
| 61 | +```bash |
| 62 | +sed -e "s|<project-id>|${PROJECT_ID}|g" python-ping-api-template.yaml > python-ping-api.yaml |
| 63 | +cat python-ping-api.yaml |
| 64 | + |
| 65 | +kubectl apply -f python-ping-api.yaml |
| 66 | +``` |
| 67 | + |
| 68 | +It may take around 5 minutes to create a load balancer, including health checking. |
| 69 | + |
| 70 | +Confirm that pod configuration and logs after deployment: |
| 71 | + |
| 72 | +```bash |
| 73 | +kubectl logs -l app=python-ping-api |
| 74 | + |
| 75 | +kubectl describe pods |
| 76 | + |
| 77 | +kubectl get ingress python-ping-api-ingress |
| 78 | +``` |
| 79 | + |
| 80 | +Confirm that response of `/ping` API. |
| 81 | + |
| 82 | +```bash |
| 83 | +LB_IP_ADDRESS=$(gcloud compute forwarding-rules list | grep python-ping-api | awk '{ print $2 }') |
| 84 | +echo ${LB_IP_ADDRESS} |
| 85 | +``` |
| 86 | + |
| 87 | +```bash |
| 88 | +curl http://${LB_IP_ADDRESS}/ping |
| 89 | +``` |
| 90 | + |
| 91 | +```json |
| 92 | +{ |
| 93 | + "host": "<your-ingress-endpoint-ip>", |
| 94 | + "message": "ping-api", |
| 95 | + "method": "GET", |
| 96 | + "url": "http://<your-ingress-endpoint-ip>/ping" |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +--- |
| 101 | + |
| 102 | +## Screenshots |
| 103 | + |
| 104 | +- Loadbalncer |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +- Loadbalncer Details |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +## Cleanup |
| 115 | + |
| 116 | +```bash |
| 117 | +kubectl delete -f app/python-ping-api.yaml |
| 118 | + |
| 119 | +gcloud container clusters delete sample-cluster |
| 120 | +``` |
| 121 | + |
| 122 | +## References |
| 123 | + |
| 124 | +- [Cloud SDK > Documentation > Reference > gcloud container clusters](https://cloud.google.com/sdk/gcloud/reference/container/clusters) |
| 125 | + |
| 126 | +- [Google Kubernetes Engine (GKE) > Documentation > Guides > GKE Ingress for HTTP(S) Load Balancing](https://cloud.google.com/kubernetes-engine/docs/concepts/ingress) |
0 commit comments