Skip to content

Instantly share code, notes, and snippets.

@r3xakead0
Last active November 25, 2025 16:32
Show Gist options
  • Select an option

  • Save r3xakead0/33c1dc982683240ea95987242ab681a9 to your computer and use it in GitHub Desktop.

Select an option

Save r3xakead0/33c1dc982683240ea95987242ab681a9 to your computer and use it in GitHub Desktop.
Create a GKE Autopilot cluster
#!/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"
#!/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."
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