Debug Apps on Google Kubernetes Engine

Solution for Debug Apps on Google Kubernetes Engine. 1 lab: GSP736. Fast copy-paste commands for Google Cloud.

GSP736 — Debug Apps on Google Kubernetes Engine

Estimated time: 20 minutes

# 🐛 Debug Apps on Google Kubernetes Engine > ⚠️ **Disclaimer:** This is an independent, community-made walkthrough created to help you understand why each step works. Attempt the lab yourself first and use this guide as a learning aid rather than as a shortcut to skip the underlying concepts. This guide is intended for educational purposes, hands-on practice, and Google Cloud certification preparation. It is not affiliated with, endorsed by, or officially supported by Google, Google Cloud, Goo

# ==============================================================================
# Color Variables & Branding
# ==============================================================================
GREEN='\e[1;32m'
CYAN='\e[1;36m'
YELLOW='\e[1;33m'
MAGENTA='\e[1;35m'
RED='\e[1;31m'
RESET='\e[0m'
BOLD='\e[1m'

clear
echo -e "${CYAN}${BOLD}"
cat << "EOF"
  ____       _     _ _            __    ___            
 / __ \     | |   (_) |          / _|  / _ \           
| |  | |_ __| |__  _| |_   ___  | |_  | | | |_ __  ___ 
| |  | | '__| '_ \| | __| / _ \ |  _| | | | | '_ \/ __|
| |__| | |  | |_) | | |_ | (_) || |   | |_| | |_) \__ \
 \____/|_|  |_.__/|_|\__| \___/ |_|    \___/| .__/|___/
                                            | |        
                                            |_|        
EOF
echo -e "${RESET}"
echo -e "${MAGENTA}${BOLD}>>> ORBIT OF OPS: GSP736 PART 1 INITIALIZED <<<${RESET}\n"

echo -e "${YELLOW}${BOLD}[Orbit of Ops] Auto-fetching Project and Cluster Zone...${RESET}"
export PROJECT_ID=$(gcloud config get-value project 2>/dev/null)

# Dynamically locate the zone of the pre-provisioned 'central' cluster
export ZONE=$(gcloud container clusters list --filter="name=central" --format="value(zone)" 2>/dev/null)

if [[ -z "$ZONE" ]]; then
    echo -e "${RED}${BOLD}⚠️ Cluster 'central' not found yet. It may still be provisioning. Please wait 1-2 minutes and re-run this block.${RESET}"
else
    echo -e "✅ Project ID: ${GREEN}$PROJECT_ID${RESET}"
    echo -e "✅ Cluster Zone: ${GREEN}$ZONE${RESET}\n"

    echo -e "${CYAN}${BOLD}[Orbit of Ops] Task 1: Authenticating to GKE Cluster...${RESET}"
    gcloud container clusters get-credentials central --zone $ZONE

    echo -e "${YELLOW}${BOLD}[Orbit of Ops] Task 2: Cloning and Deploying the Demo Application...${RESET}"
    cd ~
    git clone https://github.com/xiangshen-dk/microservices-demo.git 2>/dev/null || true
    cd microservices-demo

    kubectl apply -f release/kubernetes-manifests.yaml

    echo -e "\n${GREEN}${BOLD}✅ Part 1 Complete. The application is deploying in the background. Check Task 2 progress in the lab window!${RESET}"
fi
echo -e "\n${MAGENTA}${BOLD}>>> ORBIT OF OPS: GSP736 PART 2 INITIALIZED <<<${RESET}\n"

echo -e "${YELLOW}${BOLD}[Orbit of Ops] Task 4: Creating Logs-Based Metric (Error_Rate_SLI)...${RESET}"
gcloud logging metrics create Error_Rate_SLI \
  --description="Tracks recommendation service errors" \
  --log-filter="resource.type=\"k8s_container\" severity=ERROR labels.\"k8s-pod/app\": \"recommendationservice\"" || true

echo -e "${CYAN}${BOLD}[Orbit of Ops] Task 5: Creating Alerting Policy for Error Rate...${RESET}"
cd ~
cat > error-alert-policy.json <<EOF
{
  "displayName": "Error Rate SLI",
  "conditions": [
    {
      "displayName": "Kubernetes Container - logging/user/Error_Rate_SLI",
      "conditionThreshold": {
        "filter": "resource.type = \"k8s_container\" AND metric.type = \"logging.googleapis.com/user/Error_Rate_SLI\"",
        "aggregations": [
          {
            "alignmentPeriod": "300s",
            "crossSeriesReducer": "REDUCE_NONE",
            "perSeriesAligner": "ALIGN_RATE"
          }
        ],
        "comparison": "COMPARISON_GT",
        "duration": "0s",
        "trigger": {
          "count": 1
        },
        "thresholdValue": 0.5
      }
    }
  ],
  "combiner": "OR",
  "enabled": true,
  "notificationChannels": [],
  "severity": "SEVERITY_UNSPECIFIED"
}
EOF

gcloud alpha monitoring policies create --policy-from-file="error-alert-policy.json" > /dev/null 2>&1 || true

echo -e "\n${GREEN}${BOLD}🎉 All blocks executed successfully! The Alert Policy is created. Click 'Check my progress' on Tasks 4 & 5!${RESET}"