Skip to content

Instantly share code, notes, and snippets.

@ProProgrammer
Last active April 10, 2019 07:34
Show Gist options
  • Save ProProgrammer/2ef4ec8d37fb14fefc5e86ed7d24197a to your computer and use it in GitHub Desktop.
Save ProProgrammer/2ef4ec8d37fb14fefc5e86ed7d24197a to your computer and use it in GitHub Desktop.
Some Google Cloud (gcloud) and Kubernetes (kubectl) commands to mess around
# Delete a node-pool
gcloud container node-pools delete pool-n1-standard-4-non-preemptible --region us-central1 --cluster cluster-2 --quiet
# Create a node-pool
gcloud container node-pools create pool-n1-standard-4-non-preemptible --cluster cluster-2 --disk-size 30GB --disk-type pd-ssd --enable-autorepair --enable-autoupgrade --machine-type n1-standard-4 --num-nodes 1 --enable-autoscaling --min-nodes 0 --max-nodes 5 --region us-central1
# Drain a node and delete GCE Instance:
INSTANCE=gke-cluster-2-pool-n1-standard-4-50c3bfa2-fwxn; kubectl drain --delete-local-data --ignore-daemonsets --grace-period=10 $INSTANCE; kubectl delete node $INSTANCE; gcloud compute instances delete $INSTANCE --quiet --zone us-central1-a
OR
for node in $(kubectl get nodes -l cloud.google.com/gke-nodepool=pool-n1-standard-4 -o name); do kubectl drain --ignore-daemonsets --delete-local-data --grace-period=10 ${node}; kubectl delete ${node}; done
# Fully Automated Way:
for instance in $(gcloud compute instances list --format="value(name)" | grep gke-cluster-2); do kubectl drain --delete-local-data --ignore-daemonsets --grace-period=10 $instance; kubectl delete node $instance; zone=$(gcloud compute instances list | grep $instance | awk '{print $2}'); gcloud compute instances delete $instance --quiet --zone $zone; done
# Get Google Cloud instance zone based on instance name
gcloud compute instances list | grep gke-cluster-2-pool-n1-standard-4-efd29294-8mjp | tr -s ' ' | cut -d ' ' -f2
OR
gcloud compute instances list | grep gke-cluster-2-pool-n1-standard-4-efd29294-8mjp | awk '{print $2}'
# List all GKE instance (names only)
gcloud compute instances list --format="value(name)" | grep gke-cluster-2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment