|
| 1 | +# High-Level Design of GKE Deployment Workflow |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The GKE Deployment Workflow deploys the Online Boutique microservices to a Google Kubernetes Engine (GKE) cluster using Skaffold. Skaffold orchestrates building Docker images from source code, pushing them to a container registry (e.g., Artifact Registry), rendering Kubernetes manifests with updated image tags via Kustomize, and deploying to the cluster via kubectl. |
| 6 | + |
| 7 | +This workflow supports local execution for developers or remote execution via Google Cloud Build for CI/CD pipelines. It assumes prerequisites like an existing GKE cluster, authenticated gcloud, and a registry repository. The entry point is `skaffold run --default-repo=<registry>`, with image tags generated from git commits for versioning. |
| 8 | + |
| 9 | +Relevant codebase elements: |
| 10 | +- `skaffold.yaml`: Defines build artifacts, manifests, and profiles (e.g., `gcb` for cloud builds). |
| 11 | +- `kubernetes-manifests/`: Base Kubernetes YAMLs for services, aggregated by `kustomization.yaml`. |
| 12 | +- `cloudbuild.yaml`: Configuration for triggering the workflow in Cloud Build. |
| 13 | +- `src/*/Dockerfile`: Build contexts for each microservice. |
| 14 | + |
| 15 | +Post-deployment, the frontend is accessible via the external IP of the `frontend-external` LoadBalancer service. |
| 16 | + |
| 17 | +## Components |
| 18 | + |
| 19 | +- **Skaffold**: Core orchestrator handling build-push-deploy cycle. Configured for multi-platform builds (linux/amd64, linux/arm64), local Docker/Buildkit, or Google Cloud Build via profiles. |
| 20 | +- **Dockerfiles**: Service-specific in `src/<service>/`, e.g., multi-stage builds for Java (adservice), Go (frontend), Node.js (currencyservice), etc. |
| 21 | +- **Container Registry**: Stores built images; specified via `--default-repo` (e.g., `us-docker.pkg.dev/PROJECT_ID/microservices-demo`). |
| 22 | +- **Kustomize**: Renders manifests from `kubernetes-manifests/`, allowing patches for image tags and optional components (e.g., Redis, Istio commented out). |
| 23 | +- **Kubernetes Resources**: Deployments, Services, ConfigMaps, etc., for 10+ microservices plus Redis. LoadGenerator deployed separately via Skaffold module. |
| 24 | +- **GKE and Tools**: GKE cluster as runtime; `gcloud` for auth/credentials; `kubectl` for apply. |
| 25 | +- **Cloud Build**: Optional CI/CD; uses `cloudbuild.yaml` to run Skaffold in a containerized builder, specifying cluster details via substitutions. |
| 26 | + |
| 27 | +## Direct Deployment Sequence (Local Skaffold) |
| 28 | + |
| 29 | +This diagram illustrates the flow when running Skaffold locally against a GKE context. |
| 30 | + |
| 31 | +```mermaid |
| 32 | +sequenceDiagram |
| 33 | + participant U as User/CLI |
| 34 | + participant S as Skaffold |
| 35 | + participant B as Builder (Docker/Buildkit) |
| 36 | + participant R as Registry (Artifact Registry) |
| 37 | + participant K as Kustomize |
| 38 | + participant C as GKE Cluster (kubectl) |
| 39 | + U->>S: skaffold run --default-repo=<registry> |
| 40 | + Note over S: Parse skaffold.yaml<br/>Configs: app, loadgenerator |
| 41 | + loop For each service artifact |
| 42 | + S->>B: Build image from src/<service>/Dockerfile |
| 43 | + B->>B: Use local Docker or GCB profile |
| 44 | + B->>R: Tag & push <registry>/<service>:<tag> |
| 45 | + end |
| 46 | + Note over S: Update image tags in manifests |
| 47 | + S->>K: kustomize build kubernetes-manifests/ |
| 48 | + K->>C: kubectl apply -f rendered.yaml |
| 49 | + C->>C: Deploy pods, services, etc. |
| 50 | + Note over C: Includes Redis if not customized |
| 51 | + U->>C: kubectl get service frontend-external |
| 52 | + C->>U: External IP for access |
| 53 | +``` |
| 54 | + |
| 55 | +## Cloud Build CI/CD Variant Sequence |
| 56 | + |
| 57 | +For automated builds via `gcloud builds submit --config=cloudbuild.yaml`, the flow delegates building to Cloud Build. |
| 58 | + |
| 59 | +```mermaid |
| 60 | +sequenceDiagram |
| 61 | + participant U as User/Cloud Build Trigger |
| 62 | + participant CB as Google Cloud Build |
| 63 | + participant S as Skaffold (inside CB) |
| 64 | + participant B as Cloud Build Workers |
| 65 | + participant R as Registry |
| 66 | + participant C as GKE Cluster |
| 67 | + U->>CB: gcloud builds submit --config=cloudbuild.yaml --substitutions=_ZONE=...,_CLUSTER=... |
| 68 | + CB->>S: Run steps: get-credentials, skaffold run --default-repo=gcr.io/$PROJECT_ID |
| 69 | + S->>B: Build images (remote) |
| 70 | + B->>R: Push images |
| 71 | + S->>C: kubectl apply manifests |
| 72 | + C->>CB: Deployment status |
| 73 | + CB->>U: Build logs and status |
| 74 | +``` |
| 75 | + |
| 76 | +## Additional Design Aspects |
| 77 | + |
| 78 | +### Prerequisites |
| 79 | +- GCP project with GKE and Artifact Registry enabled. |
| 80 | +- `gcloud` authenticated and `kubectl` context set to GKE: `gcloud container clusters get-credentials <cluster> --zone=<zone>`. |
| 81 | +- Docker installed locally (or use `gcb` profile). |
| 82 | +- For Cloud Build: Service account with Kubernetes Engine Developer role; substitutions for `_ZONE` and `_CLUSTER`. |
| 83 | + |
| 84 | +### Customization and Extensibility |
| 85 | +- **Profiles in Skaffold**: Activate via `-p <profile>`, e.g., `-p gcb` for remote builds, `-p network-policies` to patch manifests with additional Kustomize paths. |
| 86 | +- **Kustomize Overlays**: Uncomment or add components in `kubernetes-manifests/kustomization.yaml` for features like service mesh (Istio), alternative databases (AlloyDB/Spanner), or observability (Cloud Operations). |
| 87 | +- **Image Tagging**: Git commit-based for traceability; customizable via `tagPolicy` in `skaffold.yaml`. |
| 88 | +- **Load Generator**: Separate Skaffold config (`loadgenerator`) requiring `app`; deploys `loadgenerator.yaml` manifest. |
| 89 | +- **Multi-Architecture**: Builds for amd64 and arm64, useful for GKE Autopilot or diverse node pools. |
| 90 | + |
| 91 | +### Error Handling and Best Practices |
| 92 | +- Skaffold provides verbose logging, dry-runs (`skaffold run --dry-run`), and cleanup (`skaffold delete`). |
| 93 | +- Monitor deployment with `kubectl get pods/services` or integrate with Cloud Operations. |
| 94 | +- For production, combine with release workflow for tagged images and Helm/Kustomize for config management. |
| 95 | +- Trade-offs: Local builds require significant resources; Cloud Build adds latency but scales and avoids local setup. |
| 96 | + |
| 97 | +### Integration Points |
| 98 | +- **Terraform Workflow**: Provisions GKE cluster beforehand. |
| 99 | +- **Local Development**: Shares `skaffold.yaml` with `skaffold dev` for hot-reload. |
| 100 | +- **Release Process**: Uses similar build/push logic for official images. |
| 101 | +- **Kustomize Workflow**: Post-deploy customizations via `kubectl apply -k`. |
| 102 | + |
| 103 | +This design promotes a seamless transition from development to GKE deployment, emphasizing automation and configurability. |
0 commit comments