GCP/gcloud cli

From Ever changing code
Jump to navigation Jump to search

Install gcloud cli

# Ubuntu 18.04 LTS, 20.04; Oct 2020
sudo apt-get install apt-transport-https ca-certificates gnupg

# Add the Cloud SDK distribution URI as a package source
echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | \
  sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list

# Import the Google Cloud Platform public key
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg \
  | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -

# Update the package list and install the Cloud SDK
sudo apt-get update && sudo apt-get install google-cloud-sdk

# Additional packages are available:
#  google-cloud-sdk-app-engine-python
#  google-cloud-sdk-app-engine-python-extras
#  google-cloud-sdk-app-engine-java
#  google-cloud-sdk-app-engine-go
#  google-cloud-sdk-bigtable-emulator
#  google-cloud-sdk-cbt
#  google-cloud-sdk-cloud-build-local
#  google-cloud-sdk-datalab
#  google-cloud-sdk-datastore-emulator
#  google-cloud-sdk-firestore-emulator
#  google-cloud-sdk-pubsub-emulator
#  kubectl   # <- interesting YEAH!

# Update your installation
gcloud components update

sudo apt-get update && sudo apt-get --only-upgrade install google-cloud-sdk-pubsub-emulator google-cloud-sdk-bigtable-emulator google-cloud-sdk-app-engine-grpc google-cloud-sdk-kind google-cloud-sdk-cbt google-cloud-sdk-skaffold google-cloud-sdk-app-engine-java google-cloud-sdk-cloud-build-local google-cloud-sdk-anthos-auth google-cloud-sdk-datastore-emulator google-cloud-sdk-kpt google-cloud-sdk-app-engine-python google-cloud-sdk kubectl google-cloud-sdk-datalab google-cloud-sdk-app-engine-python-extras google-cloud-sdk-minikube google-cloud-sdk-spanner-emulator google-cloud-sdk-firestore-emulator google-cloud-sdk-app-engine-go

# gcloud configuration location
ls -la ~/.config/gcloud
active_config
application_default_credentials.json
cache
config_sentinel
configurations

Initialize

# Verify SDK version and connected account & project
$> gcloud info | grep '\(Google\|Account\|Project\)'
Google Cloud SDK [315.0.0]
Account: [None]
Project: [None]

# Initialize
$> gcloud init
Welcome! This command will take you through the configuration of gcloud.

Your current configuration has been set to: [default]

You can skip diagnostics next time by using the following flag:
  gcloud init --skip-diagnostics

Network diagnostic detects and fixes local network connection issues.
Checking network connection...done.                                                                                                                                                                                                                                     
Reachability Check passed.
Network diagnostic passed (1/1 checks passed).

You must log in to continue. Would you like to log in (Y/n)?  y

Go to the following link in your browser:

    https://accounts.google.com/o/oauth2/auth?client_id=32555940559.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fappengine.admin+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcompute+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Faccounts.reauth&code_challenge=cjLaWdeXW6fY2ux3RITtb4JkOPW477aQsfV_qKhHGDU&code_challenge_method=S256&access_type=offline&response_type=code&prompt=select_account

Enter verification code: *****

You are logged in as: [example.email@gmail.com].

Pick cloud project to use: 
 [1] kinetic-physics-111111
 [2] Create a new project
Please enter numeric choice or text value (must exactly match list 
item):  1

Your current project has been set to: [kinetic-physics-293121].

Not setting default zone/region (this feature makes it easier to use
[gcloud compute] by setting an appropriate default value for the
--zone and --region flag).
See https://cloud.google.com/compute/docs/gcloud-compute section on how to set
default compute region and zone manually. If you would like [gcloud init] to be
able to do this for you the next time you run it, make sure the
Compute Engine API is enabled for your project on the
https://console.developers.google.com/apis page.

Created a default .boto configuration file at [/home/vagrant/.boto]. See this file and
[https://cloud.google.com/storage/docs/gsutil/commands/config] for more
information about configuring Google Cloud Storage.
Your Google Cloud SDK is configured and ready to use!

* Commands that require authentication will use kapitanpiotrek@gmail.com by default
* Commands will reference project `kinetic-physics-293121` by default
Run `gcloud help config` to learn how to change individual settings

This gcloud configuration is called [default]. You can create additional configurations if you work with multiple accounts and/or projects.
Run `gcloud topic configurations` to learn more.

Some things to try next:

* Run `gcloud --help` to see the Cloud Platform services you can interact with. And run `gcloud help COMMAND` to get help on any gcloud command.
* Run `gcloud topic --help` to learn about advanced features of the SDK like arg files and output formatting


Set defaults

# Set project default Compute Engine zone, [17] europe-west1-b
$> gcloud config set compute/zone europe-west1-b

# Set project default Compute Engine region
$> gcloud config set compute/region europe-west1

Install kubectl from Google

$> gcloud components install kubectl
ERROR: (gcloud.components.install) 
You cannot perform this action because the Cloud SDK component manager 
is disabled for this installation. You can run the following command 
to achieve the same result for this installation: 

sudo apt-get install kubectl

Verify

# List accounts whose credentials are stored on the local system:
gcloud auth list
       Credentialed Accounts
ACTIVE             ACCOUNT
*                  user1@gmail.com

# List the properties in your active SDK configuration:
gcloud config list
[core]
account = user1@gmail.com
disable_usage_reporting = False
project = responsive-sun-123456

# List multiple so called 'named configurations' 
gcloud config configurations list
NAME     IS_ACTIVE  ACCOUNT           PROJECT                DEFAULT_ZONE    DEFAULT_REGION
default  True       user1@gmail.com   responsive-sun-123456  europe-west1-b  europe-west1

# View information about your Cloud SDK installation and the active SDK configuration:
gcloud info
Google Cloud SDK [253.0.0]
...

# Verify gcloud project context
$ gcloud info | grep '\(Google\|Account\|Project\)'
Google Cloud SDK [277.0.0]
Account: [user1@gmail.com]
Project: [kinetic-physics-111111]

Create cluster

Note: Do not enable cluster-autoscaler --enable-autoscaling option if you plan to size to zero

Note: GCP GKE release versions

gcloud services enable container.googleapis.com
CLUSTER_RELEASE_CHANNEL=regular # stable, rapid -> gcloud container get-server-config
CLUSTER_NAME=istio
#CLUSTER_VERSION=1.17.12
gcloud container clusters describe $CLUSTER_NAME || gcloud container clusters create $CLUSTER_NAME \
  --release-channel $CLUSTER_RELEASE_CHANNEL \
  --machine-type=n1-standard-2 \
  --min-nodes 3 \
  --max-nodes 6 \
  --enable-autoscaling \
  --enable-network-policy \
  --preemptible \
  --no-enable-autoupgrade \
  --no-enable-autoscaling
# --cluster-version=$CLUSTER_VERSION
# --preemptible        # aka as spot instances, much cheaper but can be terminated at any time
# --enable-autoupgrade # this is a default now 2020-11

# Optional. Create your specific 'cluster-admin' user
kubectl get    clusterrolebinding $(gcloud config get-value core/account)-cluster-admin ||
kubectl create clusterrolebinding $(gcloud config get-value core/account)-cluster-admin \
--clusterrole=cluster-admin \
--user="$(gcloud config get-value core/account)"

# List
gcloud container clusters list
NAME   LOCATION        MASTER_VERSION   MASTER_IP       MACHINE_TYPE   NODE_VERSION     NUM_NODES  STATUS
istio  europe-west1-b  1.16.13-gke.401  104.155.45.246  n1-standard-2  1.16.13-gke.401  3          RUNNING

# Create/refresh kubeconfig new context entry
gcloud container clusters get-credentials $CLUSTER_NAME

# Delete
gcloud container clusters delete $CLUSTER_NAME


Scale cluster

CLUSTER_NAME=istio
gcloud container clusters list
NAME   LOCATION        MASTER_VERSION    MASTER_IP       MACHINE_TYPE   NODE_VERSION      NUM_NODES  STATUS
istio  europe-west1-b  1.17.12-gke.1504  104.199.53.112  n1-standard-2  1.17.12-gke.1504  3          RUNNING

gcloud container clusters resize $CLUSTER_NAME --num-nodes 0 # --node-pool $NODE_POOL_NAME can be skipped of only it's one

Update cluster setting

Network policy enforce, will cause rolling nodes update, read more...

gcloud container clusters update istio2 --no-enable-network-policy
gcloud container clusters update istio2 --no-enable-stackdriver-kubernetes # default: no-enabled

List available K8s versions for GKE

gcloud container get-server-config # --zone compute-zone
Fetching server config for europe-west1-b
channels:
- channel: RAPID
  defaultVersion: 1.18.9-gke.2501
  validVersions:
  - 1.18.9-gke.2501
  - 1.18.9-gke.1501
- channel: REGULAR
  defaultVersion: 1.17.12-gke.1504
  validVersions:
  - 1.17.12-gke.1504
  - 1.17.12-gke.1501
- channel: STABLE
  defaultVersion: 1.16.13-gke.401
  validVersions:
  - 1.16.13-gke.403 +more...
defaultClusterVersion: 1.16.13-gke.401
defaultImageType: COS
validImageTypes:
- UBUNTU +more...
validMasterVersions:
- 1.17.12-gke.2502 +more...


Cheatsheet

$> gcloud container clusters get-credentials mycluster

$> kubectl get pods --all-namespaces
NAMESPACE     NAME                                                          READY   STATUS    RESTARTS   AGE
kube-system   kube-dns-6cd7bbdf65-85nz5                                     4/4     Running   0          4m38s
kube-system   kube-dns-6cd7bbdf65-ffrtl                                     4/4     Running   0          4m12s
kube-system   kube-dns-autoscaler-bb58c6784-5bx65                           1/1     Running   0          4m16s
kube-system   kube-proxy-gke-cluster-2-jan2020-default-pool-3e5d4a20-ctdg   1/1     Running   0          4m26s
kube-system   kube-proxy-gke-cluster-2-jan2020-default-pool-3e5d4a20-d2d8   1/1     Running   0          4m24s
kube-system   kube-proxy-gke-cluster-2-jan2020-default-pool-3e5d4a20-vn76   1/1     Running   0          4m30s
kube-system   metrics-server-v0.3.1-57c75779f-ctz4h                         2/2     Running   0          4m9s

Delete default limits
$> kubectl delete limits limits

References