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

– The script will stand up a compute instance using the latest centos7 image and run some post install on it.

❯ ./gcp.sh
This script is to stand up a GCP environment in elastic-support Project

Usage: ./gcp.sh COMMAND
COMMANDS
  create - Creates your GCP environment & run post-scripts(linux)
           defaults to centos-7 if image is not specified
           Popular supported IMAGES:
                  centos-7 rhel-8 rocky-linux-8 ubuntu-2204-lts widnows-2022
           Example:  ./gcp.sh create rocky-linux-8
  list - List all available images
  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 with centos-7
[DEBUG] Found PROJECT: centos-cloud IMAGE: centos-7-v20220822
Created [https://www.googleapis.com/compute/v1/projects/elastic-support/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.86  34.134.164.19  RUNNING

[DEBUG] Compute instance is being deployed  Please gcloud compute ssh justinlim-lab [--zone us-central1-a].  There is a post install script running and it will reboot the instance once complete, usually in about 3-5 minutes.

[DEBUG] If you are running windows instance.  Please create your password gcloud compute reset-windows-password justinlim-lab
> ./gcp.sh create ubuntu-2204-lts
[DEBUG] You dont have any instances running
[DEBUG] Creating instance justinlim-lab with ubuntu-2204-lts
[DEBUG] Found PROJECT: ubuntu-os-cloud IMAGE: ubuntu-2204-jammy-v20220902
Created [https://www.googleapis.com/compute/v1/projects/elastic-support/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.116  34.121.255.14  RUNNING

[DEBUG] Compute instance is being deployed  Please gcloud compute ssh justinlim-lab [--zone us-central1-a].  There is a post install script running and it will reboot the instance once complete, usually in about 3-5 minutes.

[DEBUG] If you are running windows instance.  Please create your password gcloud compute reset-windows-password justinlim-lab

list

> ./gcp.sh list
[DEBUG] Full list of supported images
centos-7
centos-stream-8
centos-stream-9
cos-101-lts
cos-89-lts
cos-93-lts
cos-97-lts
cos-beta
cos-dev
cos-stable
debian-10
debian-11
fedora-cloud-34
fedora-cloud-35
fedora-cloud-36
fedora-coreos-next
fedora-coreos-stable
fedora-coreos-testing
opensuse-leap
rhel-7
rhel-7-6-sap-ha
rhel-7-7-sap-ha
rhel-7-9-sap-ha
rhel-8
rhel-8-1-sap-ha
rhel-8-2-sap-ha
rhel-8-4-sap-ha
rhel-8-6-sap-ha
rhel-9
rocky-linux-8
rocky-linux-8-optimized-gcp
rocky-linux-9
rocky-linux-9-optimized-gcp
sles-12
sles-12-sp4-sap
sles-12-sp5-sap
sles-15
sles-15-sp1-sap
sles-15-sp2-sap
sles-15-sp3-sap
sles-15-sp4-sap
sql-ent-2014-win-2012-r2
sql-ent-2014-win-2016
sql-ent-2016-win-2012-r2
sql-ent-2016-win-2016
sql-ent-2016-win-2019
sql-ent-2017-win-2016
sql-ent-2017-win-2019
sql-ent-2017-win-2022
sql-ent-2019-win-2019
sql-ent-2019-win-2022
sql-exp-2017-win-2012-r2
sql-exp-2017-win-2016
sql-exp-2017-win-2019
sql-std-2014-win-2012-r2
sql-std-2016-win-2012-r2
sql-std-2016-win-2016
sql-std-2016-win-2019
sql-std-2017-win-2016
sql-std-2017-win-2019
sql-std-2017-win-2022
sql-std-2019-win-2019
sql-std-2019-win-2022
sql-web-2014-win-2012-r2
sql-web-2016-win-2012-r2
sql-web-2016-win-2016
sql-web-2016-win-2019
sql-web-2017-win-2016
sql-web-2017-win-2019
sql-web-2017-win-2022
sql-web-2019-win-2019
sql-web-2019-win-2022
ubuntu-1804-lts
ubuntu-2004-lts
ubuntu-2204-lts
ubuntu-minimal-1804-lts
ubuntu-minimal-2004-lts
ubuntu-minimal-2204-lts
ubuntu-pro-1604-lts
ubuntu-pro-1804-lts
ubuntu-pro-2004-lts
ubuntu-pro-2204-lts
ubuntu-pro-fips-1804-lts
ubuntu-pro-fips-2004-lts
windows-2012-r2
windows-2012-r2-core
windows-2016
windows-2016-core
windows-2019
windows-2019-core
windows-2019-core-for-containers
windows-2019-for-containers
windows-2022
windows-2022-core

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. Required fields are marked *

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