1

Google Cloud scripts

At work we use google cloud as our lab to stand up and take down instances. As I work, I noticed that I usually stand up the same type of instance and run the same post install all the time so instead of doing this manually I decided to create a script for the compute instance and another for the kubernetes cluster.

Compute Instance

gcp.sh - script to automate creating and deletion of compute instance

❯ ./gcp.sh
This script is to stand up a GCP environment in <PROJECT NAME>

Usage: ./gcp.sh COMMAND
COMMANDS
  create - Creates your GCP environment
  find - Finds info about your GCP environment
  delete - Deletes your GCP environment

create

❯ ./gcp.sh create
[DEBUG] You dont have any instances running
[DEBUG] Creating instance justinlim-lab

Created [https://www.googleapis.com/compute/v1/projects/xxxxxxxxx/zones/us-central1-a/instances/justinlim-lab].
NAME           ZONE           MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP     STATUS
justinlim-lab  us-central1-a  e2-standard-4               10.128.0.95  34.134.149.122  RUNNING

[DEBUG] Compute instance is ready to be used.  Please gcloud compute ssh justinlim-lab.  There is a post install script running and it will reboot the instance once complete, usually in about 2-3 minutes.

find

❯ ./gcp.sh find
[DEBUG] Instance found

NAME           ZONE           MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP     STATUS
justinlim-lab  us-central1-a  e2-standard-4               10.128.0.95  34.134.149.122  RUNNING

delete

❯ ./gcp.sh delete
[DEBUG] Deleting justinlim-lab
Deleted [https://www.googleapis.com/compute/v1/projects/xxxxxxxxxxx/zones/us-central1-a/instances/justinlim-lab].

Kubernetes cluster

gke.sh - script to automate creating and deleting a GKE cluster

❯ ./gke.sh help
This script is to stand up a GKE environment in <PROJECT NAME>

Usage: ./gke.sh COMMAND
COMMANDS
  start - Starts your GKE environment
  find - Searchs for your deployment
  delete - Deletes your GKE environment

start

❯ ./gke.sh start
[DEBUG] kubectl found.
[DEBUG] Cluster justinlim-gke not found.
[DEBUG] Creating cluster justinlim-gke

WARNING: Starting in January 2021, clusters will use the Regular release channel by default when `--cluster-version`, `--release-channel`, `--no-enable-autoupgrade`, and `--no-enable-autorepair` flags are not specified.
WARNING: Currently VPC-native is not the default mode during cluster creation. In the future, this will become the default mode and can be disabled using `--no-enable-ip-alias` flag. Use `--[no-]enable-ip-alias` flag to suppress this warning.
WARNING: Starting with version 1.18, clusters will have shielded GKE nodes by default.
WARNING: Your Pod address range (`--cluster-ipv4-cidr`) can accommodate at most 1008 node(s).
Creating cluster justinlim-gke in us-central1... Cluster is being health-checked (master is healthy)...done.
Created [https://container.googleapis.com/v1/projects/xxxxxxxxxxx/zones/us-central1/clusters/justinlim-gke].
To inspect the contents of your cluster, go to: https://console.cloud.google.com/kubernetes/workload_/gcloud/us-central1/justinlim-gke?project=xxxxxxxxxx
kubeconfig entry generated for justinlim-gke.
NAME           LOCATION     MASTER_VERSION    MASTER_IP     MACHINE_TYPE   NODE_VERSION      NUM_NODES  STATUS
justinlim-gke  us-central1  1.21.11-gke.1100  34.70.94.148  e2-standard-4  1.21.11-gke.1100  3          RUNNING

[DEBUG] Configure kubectl context for justinlim-gke
Fetching cluster endpoint and auth data.
kubeconfig entry generated for justinlim-gke.
[DEBUG] Adding gcloud RBAC for cluster admin role
clusterrolebinding.rbac.authorization.k8s.io/cluster-admin-binding created

kubectl get nodes

❯ kubectl get nodes
NAME                                           STATUS   ROLES    AGE   VERSION
gke-justinlim-gke-default-pool-5ab0e065-10dz   Ready    <none>   43s   v1.21.11-gke.1100
gke-justinlim-gke-default-pool-668d9aed-njgh   Ready    <none>   43s   v1.21.11-gke.1100
gke-justinlim-gke-default-pool-8707503d-fmwz   Ready    <none>   42s   v1.21.11-gke.1100

delete

❯ ./gke.sh delete
[DEBUG] Deleting justinlim-gke
Deleting cluster justinlim-gke...done.
Deleted [https://container.googleapis.com/v1/projects/xxxxxxxxxxx/zones/us-central1/clusters/justinlim-gke].
[DEBUG] Remove kubectl context
Property "contexts.justinlim-gke" unset.

jlim0930

One Comment

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.