Gcloud command Structure
gcloud Group Subgroup action
Group - Which service group - config,compute,container,dataflow,iam
Subgroup - Instances or Images or instance templates macine types, regions
Action - Create or list or start or stop or describe
-------------------------------------------------------------------------
export PROJECT_ID=$qwiklabs-gcp-03-9499ff5686ea
export ZONE=$us-west3-c
gcloud config set compute/region us-west3
gcloud config get-value compute/region
gcloud config set compute/zone us-west3-c
gcloud config get-value compute/zone
gcloud config get-value project
gcloud compute project-info describe --project $(gcloud config get-value project)
export PROJECT_ID=$(gcloud config get-value project)
export ZONE=$(gcloud config get-value compute/zone)
export ZONE=$(gcloud config get-value compute/zone)
echo -e "PROJECT ID: $PROJECT_ID\nZONE: $ZONE"
gcloud compute instances create gcelab2 --machine-type e2-medium --zone $ZONE
gcloud -h
gcloud config --help
gcloud help config
gcloud config list
gcloud config list --all
gcloud components list
gcloud compute instances list
gcloud compute instances list --filter="name=('gcelab2')"
gcloud compute firewall-rules list
gcloud compute firewall-rules list --filter="network='default'"
gcloud compute firewall-rules list --filter="NETWORK:'default' AND ALLOW:'icmp'"
gcloud compute ssh gcelab2 --zone $ZONE
gcloud compute firewall-rules list
gcloud compute firewall-rules create default-allow-http --direction=INGRESS --priority=1000 --
network=default --action=ALLOW --rules=tcp:80 --source-ranges=0.0.0.0/0 --target-tags=http-server
gcloud compute firewall-rules list --filter=ALLOW:'80'
gcloud logging logs list
gcloud logging logs list --filter="compute"
gcloud logging read "resource.type=gce_instance" --limit 5
gcloud logging read "resource.type=gce_instance AND labels.instance_name='gcelab2'" --limit 5
-----------------------------------
Create Multiple servers
1. Create three virtual machine www1 in your default zone.
gcloud compute instances create www1 \
--zone=us-east4-c \
--tags=network-lb-tag \
--machine-type=e2-medium \
--image-family=debian-11 \
--image-project=debian-cloud \
--metadata=startup-script='#!/bin/bash
apt-get update
apt-get install apache2 -y
service apache2 restart
echo "
<h3>Web Server: www1</h3>" | tee /var/www/html/index.html'
gcloud compute instances create www2 \
--zone=us-east4-c \
--tags=network-lb-tag \
--machine-type=e2-medium \
--image-family=debian-11 \
--image-project=debian-cloud \
--metadata=startup-script='#!/bin/bash
apt-get update
apt-get install apache2 -y
service apache2 restart
echo "
<h3>Web Server: www2</h3>" | tee /var/www/html/index.html'
gcloud compute instances create www3 \
--zone=us-east4-c \
--tags=network-lb-tag \
--machine-type=e2-medium \
--image-family=debian-11 \
--image-project=debian-cloud \
--metadata=startup-script='#!/bin/bash
apt-get update
apt-get install apache2 -y
service apache2 restart
echo "
<h3>Web Server: www3</h3>" | tee /var/www/html/index.html'
2. Create a firewall rule to allow external traffic to the VM instances:
gcloud compute firewall-rules create www-firewall-network-lb \
--target-tags network-lb-tag --allow tcp:80
gcloud compute instances list
curl http://[IP_ADDRESS]
____________________________________________
LB Services
____________________________________________
When you configure the load balancing service, your virtual machine instances will receive packets
that are destined for the static external IP address you configure. Instances made with a Compute
Engine image are automatically configured to handle this IP address.
1. Create a static external IP address for your load balancer:
gcloud compute addresses create network-lb-ip-1 \
--region us-east4
2. Add a legacy HTTP health check resource:
gcloud compute http-health-checks create basic-check
3.Add a target pool in the same region as your instances. Run the following to create the target pool
and use the health check, which is required for the service to function:
gcloud compute target-pools create www-pool \
--region us-east4 --http-health-check basic-check
4. Add the instances to the pool:
gcloud compute target-pools add-instances www-pool \
--instances www1,www2,www3
5. Add Forwarding rule
gcloud compute forwarding-rules create www-rule \
--region us-east4 \
--ports 80 \
--address network-lb-ip-1 \
--target-pool www-pool
Sending traffic to LB
gcloud compute forwarding-rules describe www-rule --region us-east4
IPADDRESS=$(gcloud compute forwarding-rules describe www-rule --region us-east4 --
format="json" | jq -r .IPAddress)
echo $IPADDRESS
while true; do curl -m1 $IPADDRESS; done
___________________________________________
Create HTTP Loadbalancer
HTTP(S) Load Balancing is implemented on Google Front End (GFE). GFEs are distributed globally and
operate together using Google's global network and control plane. You can configure URL rules to
route some URLs to one set of instances and route other URLs to other instances.
Requests are always routed to the instance group that is closest to the user, if that group has enough
capacity and is appropriate for the request. If the closest group does not have enough capacity, the
request is sent to the closest group that does have capacity.
To set up a load balancer with a Compute Engine backend, your VMs need to be in an instance
group. The managed instance group provides VMs running the backend servers of an external HTTP
load balancer. For this lab, backends serve their own hostnames.
1. First, create the load balancer template:
gcloud compute instance-templates create lb-backend-template \
--region=us-east4 \
--network=default \
--subnet=default \
--tags=allow-health-check \
--machine-type=e2-medium \
--image-family=debian-11 \
--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'
2. Create a managed instance group based on the template:
gcloud compute instance-groups managed create lb-backend-group \
--template=lb-backend-template --size=2 --zone=us-east4-c
3. Create the fw-allow-health-check firewall rule.
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
4. Now that the instances are up and running, set up a global static external IP address that your
customers use to reach your load balancer:
gcloud compute addresses create lb-ipv4-1 \
--ip-version=IPV4 \
--global
Note the IPv4 address that was reserved:
gcloud compute addresses describe lb-ipv4-1 \
--format="get(address)" \
--global
5. Create a health check for the load balancer:
gcloud compute health-checks create http http-basic-check \
--port 80
6. Create a backend service:
gcloud compute backend-services create web-backend-service \
--protocol=HTTP \
--port-name=http \
--health-checks=http-basic-check \
--global
7. Add your instance group as the backend to the backend service:
gcloud compute backend-services add-backend web-backend-service \
--instance-group=lb-backend-group \
--instance-group-zone=us-east4-c \
--global
8. Create a URL map to route the incoming requests to the default backend service:
gcloud compute url-maps create web-map-http \
--default-service web-backend-service
9. Create a target HTTP proxy to route requests to your URL map:
gcloud compute target-http-proxies create http-lb-proxy \
--url-map web-map-http
10. Create a global forwarding rule to route incoming requests to the proxy:
gcloud compute forwarding-rules create http-content-rule \
--address=lb-ipv4-1\
--global \
--target-http-proxy=http-lb-proxy \
--ports=80
-----------------------------------------------------------------------
gcloud config list project
gcloud config configurations list
gcloud config configurations activate my-default-configuration
gcloud config list
gcloud config configurations describe my-second-configuration
gcloud compute instances list
gcloud compute instances create
gcloud compute instances create my-first-instance-from-gcloud
gcloud compute instances describe my-first-instance-from-gcloud
gcloud compute instances delete my-first-instance-from-gcloud
gcloud compute zones list
gcloud compute regions list
gcloud compute machine-types list
gcloud compute machine-types list --filter zone:asia-southeast2-b
gcloud compute machine-types list --filter "zone:(asia-southeast2-b asia-southeast2-c)"
gcloud compute zones list --filter=region:us-west2
gcloud compute zones list --sort-by=region
gcloud compute zones list --sort-by=~region
gcloud compute zones list --uri
gcloud compute regions describe us-west4
gcloud compute instance-templates list
gcloud compute instance-templates create instance-template-from-command-line
gcloud compute instance-templates delete instance-template-from-command-line
gcloud compute instance-templates describe my-instance-template-with-custom-image
______________________________________________________________________
App Engine
cd default-service
gcloud app deploy
gcloud app services list
gcloud app versions list
gcloud app instances list
gcloud app deploy --version=v2
gcloud app versions list
gcloud app browse
gcloud app browse --version 20210215t072907
gcloud app deploy --version=v3 --no-promote
gcloud app browse --version v3
gcloud app services set-traffic split=v3=.5,v2=.5
gcloud app services set-traffic splits=v3=.5,v2=.5
watch curl https://melodic-furnace-304906.uc.r.appspot.com/
gcloud app services set-traffic --splits=v3=.5,v2=.5 --split-by=random
cd ../my-first-service/
gcloud app deploy
gcloud app browse --service=my-first-service
gcloud app services list
gcloud app regions list
gcloud app browse --service=my-first-service --version=20210215t075851
gcloud app browse --version=v2
gcloud app open-console --version=v2
gcloud app versions list --hide-no-traffic
__________________________________________________________________
Kubernetes
2: Here are some of the commands we will run in the next few steps (Refer back to this if you have
any problems!)
gcloud config set project my-kubernetes-project-304910
gcloud container clusters get-credentials my-cluster --zone us-central1-c --project my-kubernetes-
project-304910
kubectl create deployment hello-world-rest-api --image=in28min/hello-world-rest-
api:0.0.1.RELEASE
kubectl get deployment
kubectl expose deployment hello-world-rest-api --type=LoadBalancer --port=8080
kubectl get services
kubectl get services --watch
curl 35.184.204.214:8080/hello-world
kubectl scale deployment hello-world-rest-api --replicas=3
gcloud container clusters resize my-cluster --node-pool default-pool --num-nodes=2 --zone=us-
central1-c
kubectl autoscale deployment hello-world-rest-api --max=4 --cpu-percent=70
kubectl get hpa
kubectl create configmap hello-world-config --from-literal=RDS_DB_NAME=todos
kubectl get configmap
kubectl describe configmap hello-world-config
kubectl create secret generic hello-world-secrets-1 --from-literal=RDS_PASSWORD=dummytodos
kubectl get secret
kubectl describe secret hello-world-secrets-1
kubectl apply -f deployment.yaml
gcloud container node-pools list --zone=us-central1-c --cluster=my-cluster
kubectl get pods -o wide
kubectl set image deployment hello-world-rest-api hello-world-rest-api=in28min/hello-world-rest-
api:0.0.2.RELEASE
kubectl get services
kubectl get replicasets
kubectl get pods
kubectl delete pod hello-world-rest-api-58dc9d7fcc-8pv7r
kubectl scale deployment hello-world-rest-api --replicas=1
kubectl get replicasets
gcloud projects list
kubectl delete service hello-world-rest-api
kubectl delete deployment hello-world-rest-api
gcloud container clusters delete my-cluster --zone us-central1-c
kubectl create deployment hello-world-rest-api --image=in28min/hello-world-rest-api:0.0.1.RELEASE
____________________________________________________________________
# Cloud SQL
gcloud sql connect my-first-cloud-sql-instance --user=root --quiet
gcloud config set project glowing-furnace-304608
gcloud sql connect my-first-cloud-sql-instance --user=root --quiet
use todos
create table user (id integer, username varchar(30) );
describe user;
insert into user values (1, 'Ranga');
select * from user;
# Cloud Spanner
CREATE TABLE Users (
UserId INT64 NOT NULL,
UserName STRING(1024)
) PRIMARY KEY(UserId);
# Cloud BigTable
bq show bigquery-public-data:samples.shakespeare
gcloud --version
cbt listinstances -project=glowing-furnace-304608
echo project = glowing-furnace-304608 > ~/.cbtrc
cat ~/.cbtrc
cbt listinstances
____________________________________________________________________
PUBSUB
gcloud config set project glowing-furnace-304608
gcloud pubsub topics create topic-from-gcloud
gcloud pubsub subscriptions create subscription-gcloud-1 --topic=topic-from-gcloud
gcloud pubsub subscriptions create subscription-gcloud-2 --topic=topic-from-gcloud
gcloud pubsub subscriptions pull subscription-gcloud-2
gcloud pubsub subscriptions pull subscription-gcloud-1
gcloud pubsub topics publish topic-from-gcloud --message="My First Message"
gcloud pubsub topics publish topic-from-gcloud --message="My Second Message"
gcloud pubsub topics publish topic-from-gcloud --message="My Third Message"
gcloud pubsub subscriptions pull subscription-gcloud-1 --auto-ack
gcloud pubsub subscriptions pull subscription-gcloud-2 --auto-ack
gcloud pubsub topics list
gcloud pubsub topics delete topic-from-gcloud
gcloud pubsub topics list-subscriptions my-first-topic