Last active
November 25, 2025 16:32
-
-
Save r3xakead0/33c1dc982683240ea95987242ab681a9 to your computer and use it in GitHub Desktop.
Create a GKE Autopilot cluster
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| set -e # Stop the script if any command fails | |
| # Get the currently configured GCP Project ID | |
| PROJECT_ID=$(gcloud config get-value project --quiet) | |
| # Validate that a project is set | |
| if [ -z "$PROJECT_ID" ]; then | |
| echo "โ ERROR: No project is configured in gcloud." | |
| echo "Run: gcloud config set project <PROJECT_ID>" | |
| exit 1 | |
| fi | |
| # Define region and cluster name | |
| REGION="us-central1" | |
| CLUSTER_NAME="autopilot-cluster" | |
| echo "๐น Active project: $PROJECT_ID" | |
| echo "๐น Region: $REGION" | |
| echo "๐น Cluster name: $CLUSTER_NAME" | |
| # Enable required APIs for GKE Autopilot | |
| echo "๐ง Enabling required APIs..." | |
| gcloud services enable \ | |
| container.googleapis.com \ | |
| compute.googleapis.com \ | |
| --project "$PROJECT_ID" | |
| # Create the GKE Autopilot cluster | |
| echo "๐ Creating GKE Autopilot cluster..." | |
| gcloud container clusters create-auto "$CLUSTER_NAME" \ | |
| --region "$REGION" \ | |
| --project "$PROJECT_ID" | |
| # Fetch kubeconfig credentials for kubectl | |
| echo "๐ Fetching cluster credentials..." | |
| gcloud container clusters get-credentials "$CLUSTER_NAME" \ | |
| --region "$REGION" \ | |
| --project "$PROJECT_ID" | |
| echo "โ Autopilot cluster created successfully!" | |
| echo "Run: kubectl get nodes" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| set -e # Stop the script if any command fails | |
| # Get the currently configured GCP Project ID | |
| PROJECT_ID=$(gcloud config get-value project --quiet) | |
| # Validate that a project is set | |
| if [ -z "$PROJECT_ID" ]; then | |
| echo "โ ERROR: No project is configured in gcloud." | |
| echo "Run: gcloud config set project <PROJECT_ID>" | |
| exit 1 | |
| fi | |
| # Define region and cluster name | |
| REGION="us-central1" | |
| CLUSTER_NAME="autopilot-cluster" | |
| CTX="gke_${PROJECT_ID}_${REGION}_${CLUSTER_NAME}" | |
| echo "๐น Active project: $PROJECT_ID" | |
| echo "๐น REGION: $REGION" | |
| echo "๐น Cluster name: $CLUSTER_NAME" | |
| # Delete the GKE Autopilot Cluster if it exists | |
| if gcloud container clusters list --region "$REGION" --project "$PROJECT_ID" | grep -q "$CLUSTER_NAME"; then | |
| echo "๐ Deleting GKE Autopilot Cluster..." | |
| gcloud container clusters delete "$CLUSTER_NAME" \ | |
| --region "$REGION" \ | |
| --project "$PROJECT_ID" \ | |
| --quiet | |
| else | |
| echo "โ ๏ธ Cluster '$CLUSTER_NAME' does not existโskipping GKE delete." | |
| fi | |
| echo "๐งน Cleaning kubeconfig entries..." | |
| kubectl config delete-cluster "$CTX" 2>/dev/null || true | |
| kubectl config delete-context "$CTX" 2>/dev/null || true | |
| kubectl config delete-user "$CTX" 2>/dev/null || true | |
| echo "โ Cluster '$CLUSTER_NAME' deleted and kubeconfig cleaned successfully." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| PROJECT_ID=$(gcloud config get-value project) | |
| REGION=us-central1 | |
| CLUSTER_NAME="autopilot-cluster" | |
| gcloud services enable \ | |
| container.googleapis.com \ | |
| compute.googleapis.com \ | |
| --project "$PROJECT_ID" | |
| gcloud container clusters create-auto "$CLUSTER_NAME" \ | |
| --region "$REGION" \ | |
| --project "$PROJECT_ID" | |
| gcloud container clusters get-credentials "$CLUSTER_NAME" \ | |
| --region "$REGION" \ | |
| --project "$PROJECT_ID" | |
| gcloud container clusters delete "$CLUSTER_NAME" \ | |
| --region "$REGION" \ | |
| --project "$PROJECT_ID" \ | |
| --quiet | |
| CTX="gke_${PROJECT_ID}_${REGION}_${CLUSTER_NAME}" | |
| kubectl config delete-cluster "$CTX" | |
| kubectl config delete-context "$CTX" | |
| kubectl config delete-user "$CTX" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment