Last active
November 25, 2025 16:26
-
-
Save r3xakead0/b9c75e62024e5741c9183588d199a1fc to your computer and use it in GitHub Desktop.
Create a GKE Standard Regional Cluster (High Availability)
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="standard-regional-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 Standard Regional Cluster | |
| echo "๐ Creating GKE Standard Regional Cluster..." | |
| gcloud container clusters create "$CLUSTER_NAME" \ | |
| --region "$REGION" \ | |
| --num-nodes 1 \ | |
| --machine-type e2-medium \ | |
| --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 "โ Standard Regional 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 zone and cluster name | |
| REGION="us-central1" | |
| CLUSTER_NAME="standard-regional-cluster" | |
| CTX="gke_${PROJECT_ID}_${REGION}_${CLUSTER_NAME}" | |
| echo "๐น Active project: $PROJECT_ID" | |
| echo "๐น Region: $REGION" | |
| echo "๐น Cluster name: $CLUSTER_NAME" | |
| # Delete the GKE Standard Regional Cluster if it exists | |
| if gcloud container clusters list --region "$REGION" --project "$PROJECT_ID" | grep -q "$CLUSTER_NAME"; then | |
| echo "๐ Deleting GKE Standard Regional 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="standard-regional-cluster" | |
| gcloud services enable \ | |
| container.googleapis.com \ | |
| compute.googleapis.com \ | |
| --project "$PROJECT_ID" | |
| gcloud container clusters create "$CLUSTER_NAME" \ | |
| --region "$REGION" \ | |
| --num-nodes 1 \ | |
| --machine-type e2-medium \ | |
| --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