Collect Metrics from Exporters using the Managed Service for Prometheus

Solution for Collect Metrics from Exporters using the Managed Service for Prometheus. 1 lab: GSP1026. Fast copy-paste commands for Google Cloud.

GSP1026 — Collect Metrics from Exporters using the Managed Service for Prometheus

Estimated time: 20 minutes

# 📊 Collect Metrics from Exporters using the Managed Service for Prometheus > ⚠️ **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 suppo

# ==============================================================================
# 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: GSP1026 PART 1 INITIALIZED <<<${RESET}\n"

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

export ZONE=$(gcloud compute project-info describe \
    --format="value(commonInstanceMetadata.items[google-compute-default-zone])" 2>/dev/null | tail -n 1)

if [[ -z "$ZONE" ]]; then
    read -p "⚠️ Could not auto-detect Zone. Please enter the lab Zone (e.g., europe-west1-b): " ZONE
    export ZONE
fi
gcloud config set compute/zone $ZONE 2>/dev/null

echo -e "✅ Project ID: ${GREEN}$PROJECT_ID${RESET}"
echo -e "✅ Zone:       ${GREEN}$ZONE${RESET}\n"

echo -e "${CYAN}${BOLD}[Orbit of Ops] Task 1: Deploying GKE Cluster (This will take ~5 minutes)...${RESET}"
gcloud beta container clusters create gmp-cluster --num-nodes=1 --zone $ZONE --enable-managed-prometheus || { echo -e "${RED}${BOLD}STOCKOUT ERROR DETECTED. Please End Lab and Restart to get a new zone.${RESET}"; exit 1; }
gcloud container clusters get-credentials gmp-cluster --zone=$ZONE || true

echo -e "${YELLOW}${BOLD}[Orbit of Ops] Task 2 & 3: Setting up Namespace and Example App...${RESET}"
kubectl create ns gmp-test || true
kubectl -n gmp-test apply -f https://raw.githubusercontent.com/GoogleCloudPlatform/prometheus-engine/v0.2.3/examples/example-app.yaml

echo -e "${CYAN}${BOLD}[Orbit of Ops] Task 4: Configuring PodMonitoring Resource...${RESET}"
kubectl -n gmp-test apply -f https://raw.githubusercontent.com/GoogleCloudPlatform/prometheus-engine/v0.2.3/examples/pod-monitoring.yaml

echo -e "\n${GREEN}${BOLD}✅ Part 1 Complete. Please click 'Check my progress' on Tasks 1, 2 & 4!${RESET}"
echo -e "\n${MAGENTA}${BOLD}>>> ORBIT OF OPS: GSP1026 PART 2 INITIALIZED <<<${RESET}\n"

echo -e "${YELLOW}${BOLD}[Orbit of Ops] Tasks 5 & 6: Downloading and Executing Prometheus Binary...${RESET}"
cd ~
git clone https://github.com/GoogleCloudPlatform/prometheus 2>/dev/null || true
cd prometheus
git checkout v2.28.1-gmp.4
wget -q https://storage.googleapis.com/kochasoft/gsp1026/prometheus
chmod a+x prometheus

# Run prometheus in the background so it doesn't freeze the script
nohup ./prometheus --config.file=documentation/examples/prometheus.yml --export.label.project-id=$PROJECT_ID --export.label.location=$ZONE > /dev/null 2>&1 &
echo -e "✅ Prometheus is running in the background."

echo -e "${CYAN}${BOLD}[Orbit of Ops] Task 7: Downloading and Executing Node Exporter...${RESET}"
cd ~
wget -q https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz
tar xvfz node_exporter-1.3.1.linux-amd64.tar.gz > /dev/null
cd node_exporter-1.3.1.linux-amd64

# Run node_exporter in the background
nohup ./node_exporter > /dev/null 2>&1 &
echo -e "✅ Node Exporter is running on port 9100."

echo -e "${YELLOW}${BOLD}[Orbit of Ops] Creating and Uploading config.yaml to Cloud Storage...${RESET}"
cd ~
cat > config.yaml <<EOF
global:
  scrape_interval: 15s

scrape_configs:
  - job_name: node
    static_configs:
      - targets: ['localhost:9100']
EOF

gsutil mb -p $PROJECT_ID gs://$PROJECT_ID || true
gsutil cp config.yaml gs://$PROJECT_ID
gsutil -m acl set -R -a public-read gs://$PROJECT_ID

echo -e "\n${GREEN}${BOLD}🎉 All blocks executed successfully! The config file has been uploaded. Please click 'Check my progress' on Task 7!${RESET}"