GCP - urcuqui/Data-Science GitHub Wiki

The next commands are based on tutorials and other sources in order to remember in the projects in the daily hacking life 🤖

gcloud auth list

gcloud config list project

Preparing your Environment

export PROJECT_ID=$(gcloud info --format='value(config.project)')
export BUCKET=${PROJECT_ID}-ml

Create a Cloud SQL instance

This is an example of a command to create a sql instance which its name is taxi

gcloud sql instances create taxi \
    --tier=db-n1-standard-1 --activation-policy=ALWAYS

The next command allows us to set the password of our last SQL database instance

gcloud sql users set-password root --host % --instance taxi \
 --password Passw0rd

The next two commands provide a way to manage the access of the SQL instance

export ADDRESS=$(wget -qO - http://ipecho.net/plain)/32

gcloud sql instances patch taxi --authorized-networks $ADDRESS

Using a variable and our credentials we can log in the command provider

MYSQLIP=$(gcloud sql instances describe \
taxi --format="value(ipAddresses.ipAddress)")

We can import a CSV file data into Cloud SQL using mysql

mysqlimport --local --host=$MYSQLIP --user=root --password \
--ignore-lines=1 --fields-terminated-by=',' bts trips.csv-*

Copy from one site to another one

gsutil cp gs://cloud-training/OCBL013/nyc_tlc_yellow_trips_2018_subset_1.csv trips.csv-1
gsutil cp gs://cloud-training/OCBL013/nyc_tlc_yellow_trips_2018_subset_2.csv trips.csv-2

VM windows instances

gcloud compute instances get-serial-port-output instance-1
gcloud compute reset-windows-password [instance] --zone us-central1-a --user [username]

VM debian instances

gcloud compute instances create www1 \
  --image-family debian-9 \
  --image-project debian-cloud \
  --zone us-central1-a \
  --tags network-lb-tag \
  --metadata startup-script="#! /bin/bash
    sudo apt-get update
    sudo apt-get install apache2 -y
    sudo service apache2 restart
    echo '<!doctype html><html><body><h1>www1</h1></body></html>' | tee /var/www/html/index.html"

Getting information from the properties and settings

gcloud config list --all
gcloud components list 

interactive gcloud

sudo apt-get install google-cloud-sdk
gcloud beta interactive

GKE

gcloud config set compute/zone us-central1-a
gcloud container clusters create [CLUSTER-NAME]

Setting credentials

gcloud container clusters get-credentials [CLUSTER-NAME]

Deploying an example

kubectl create deployment hello-server --image=gcr.io/google-samples/hello-app:1.0

Allowing an app to receive external requests

kubectl expose deployment hello-server --type=LoadBalancer --port 8080

Deleting a cluster solution

gcloud container clusters delete [CLUSTER-NAME]

Firewall rules

Making a firewall rule for the network-lb-tag to allowing requests from external devices

gcloud compute firewall-rules create www-firewall-network-lb \
    --target-tags network-lb-tag --allow tcp:80
gcloud compute firewall-rules create fw-allow-health-check \
    --network=default \
    --action=allow \
    --direction=ingress \
    --source-ranges=130.211.0.0/22,35.191.0.0/16 \
    --target-tags=allow-health-check \
    --rules=tcp:80

Balancers

gcloud compute addresses create network-lb-ip-1 \
 --region us-central1
gcloud compute http-health-checks create basic-check
gcloud compute target-pools create www-pool \
    --region us-central1 --http-health-check basic-check

Adding the instances to the pool

gcloud compute target-pools add-instances www-pool \
    --instances www1,www2,www3 \
    --instances-zone us-central1-b

HTTP balancers

gcloud compute instance-templates create lb-backend-template \
   --region=us-central1 \
   --network=default \
   --subnet=default \
   --tags=allow-health-check \
   --image-family=debian-9 \
   --image-project=debian-cloud \
   --metadata=startup-script='#! /bin/bash
     apt-get update
     apt-get install apache2 -y
     a2ensite default-ssl
     a2enmod ssl
     vm_hostname="$(curl -H "Metadata-Flavor:Google" \
     http://169.254.169.254/computeMetadata/v1/instance/name)"
     echo "Page served from: $vm_hostname" | \
     tee /var/www/html/index.html
     systemctl restart apache2'
gcloud compute instance-groups managed create lb-backend-group \
   --template=lb-backend-template --size=2 --zone=us-central1-a
gcloud compute addresses create lb-ipv4-1 \
    --ip-version=IPV4 \
    --global
gcloud compute addresses describe lb-ipv4-1 \
    --format="get(address)" \
    --global
    gcloud compute health-checks create http http-basic-check \
        --port 80
    gcloud compute backend-services create web-backend-service \
        --protocol=HTTP \
        --port-name=http \
        --health-checks=http-basic-check \
        --global
    gcloud compute backend-services add-backend web-backend-service \
        --instance-group=lb-backend-group \
        --instance-group-zone=us-central1-a \
        --global
    gcloud compute url-maps create web-map-http \
        --default-service web-backend-service
    gcloud compute target-http-proxies create http-lb-proxy \
        --url-map web-map-http
    gcloud compute forwarding-rules create http-content-rule \
        --address=lb-ipv4-1\
        --global \
        --target-http-proxy=http-lb-proxy \
        --ports=80
⚠️ **GitHub.com Fallback** ⚠️