Last active
February 27, 2021 18:13
-
-
Save jasdeepkhalsa/11cc116d94671562deea6f711725dbf3 to your computer and use it in GitHub Desktop.
GCP gcloud provisioning
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## General commands | |
# Get active account name | |
gcloud auth list | |
# List the project ID | |
gcloud config list project | |
# Find default zone | |
gcloud compute project-info describe --project <GCP Project ID> | |
# Set default zone | |
gcloud config set compute/zone us-east1-b | |
# Create server / compute instance with a specific machine type in a specific zone | |
gcloud compute instances create nucleus-jumphost --machine-type f1-micro --zone us-east1-b | |
## Create a Kubernetes service cluster | |
# Create a Kubernetes cluster with a specific machine-type (if --num-nodes are omitted, defaults to 3) | |
gcloud container clusters create my-cluster --machine-type n1-standard-1 --zone us-east1-b --num-nodes 2 | |
# Get info on the cluster, control plane IP, KubeDNS, metrics etc. | |
kubectl cluster-info | |
# Authenticate the Kubernetes cluster | |
gcloud container clusters get-credentials my-cluster --zone us-east1-b | |
# Deploy an application to the cluster | |
kubectl create deployment hello-server --image gcr.io/google-samples/hello-app:2.0 | |
# Expose the Kubernetes Service to the outside world | |
kubectl expose deployment hello-server --type LoadBalancer --port 8080 | |
# Get pods, services, replicaSets and deployments running | |
kubectl get pods | |
kubectl get services | |
kubectl get replicasets | |
kubectl get deployments | |
# Increase the number of replica pods of a deployment | |
kubectl scale deployment <deployment> --replicas=3 | |
# Trigger, pause, resume, undo and view status of a rolling update on a deployment | |
kubectl edit deployment <deployment> | |
kubectl rollout pause deployment/<deployment> | |
kubectl rollout resume deployment/<deployment> | |
kubectl rollout undo deployment/<deployment> | |
kubectl rollout status deployment/<deployment> | |
# View the rollout history | |
kubectl rollout history deployment/<deployment> | |
# See which version of a deployment is in use | |
curl -ks https://`kubectl get svc <frontend> -o=jsonpath="{.status.loadBalancer.ingress[0].ip}"`/version | |
# See which version of a container image is deployed to a pod | |
kubectl get pods -o jsonpath --template='{range .items[*]}{.metadata.name}{"\t"}{"\t"}{.spec.containers[0].image}{"\n"}{end}' | |
# Create an interactive shell inside a pod | |
kubectl exec <pod> --stdin --tty -c <pod> /bin/sh | |
# Check that the service has been exposed | |
kubectl get service | |
kubectl get # list resources | |
kubectl describe # show detailed information about a resource | |
kubectl logs # print the logs from a container in a pod | |
kubectl exec # execute a command on a container in a pod | |
# Get list of compute images | |
gcloud compute images list | |
## Set up an HTTP load balancer | |
# Create a startup script | |
cat << EOF > startup.sh | |
#! /bin/bash | |
apt-get update | |
apt-get install -y nginx | |
service nginx start | |
sed -i -- 's/nginx/Google Cloud Platform -'"\$HOSTNAME"'/' | |
/var/www/html/index.nginx-debian.html | |
EOF | |
# Create an instance template | |
gcloud compute instance-templates create lb-backend-template \ | |
--region us-east1 \ | |
--network default \ | |
--machine-type f1-micro \ | |
--subnet default \ | |
--tags allow-health-check \ | |
--image-family debian-9 \ | |
--image-project debian-cloud \ | |
--metadata-from-file startup-script=startup.sh | |
# Create a target pool | |
gcloud compute target-pools create lb-pool | |
# Create a managed instance group with a target pool, | |
# that should receive incoming traffic from forwarding rules | |
gcloud compute instance-groups managed create lb-backend-group \ | |
--template lb-backend-template \ | |
--size 2 \ | |
--zone us-east1-b \ | |
--target-pool lb-pool | |
# Check computer instances | |
gcloud compute instances list | |
# Create a simple firewall | |
gcloud compute firewall-rules create fw-allow-health-check --allow tcp:80 | |
# Or a more explicit firewall | |
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 | |
# Create a forwarding rule from the outside world to the target pool | |
gcloud compute forwarding-rules create nginx-lb \ | |
--region us-east1 \ | |
--ports 80 \ | |
--target-pool lb-pool | |
# Create a reserved IPv4 address (optional) | |
gcloud compute addresses create lb-ipv4-1 \ | |
--ip-version IPV4 \ | |
--global | |
# Get the IPv4 address (optional) | |
gcloud compute addresses describe lb-ipv4-1 \ | |
--format "get(address)" \ | |
--global | |
# Create a HTTP health check | |
gcloud compute http-health-checks create http-basic-check --port 80 | |
# Ensure the health check service can reach the instance-group on http port 80 | |
# See gcloud compute instance-groups set-named-ports --help for more information | |
gcloud compute instance-groups managed set-named-ports lb-backend-group \ | |
--named-ports http:80 | |
# Create a backend service... | |
gcloud compute backend-services create web-backend-service \ | |
--protocol HTTP \ | |
--http-health-checks http-basic-check \ | |
--global | |
# ...and attach the managed instance group | |
gcloud compute backend-services add-backend web-backend-service \ | |
--instance-group lb-backend-group \ | |
--instance-group-zone us-east1-b \ | |
--global | |
# Create a URL map | |
gcloud compute url-maps create web-map-http --default-service web-backend-service | |
# Target the HTTP proxy to route requests to your URL map | |
gcloud compute target-http-proxies create http-lb-proxy --url-map web-map-http | |
# Create a global forwarding rule from outside world to lb-proxy | |
gcloud compute forwarding-rules create http-content-rule \ | |
--global \ | |
--target-http-proxy http-lb-proxy \ | |
--ports 80 | |
# --address lb-ipv4-1 | |
# Check the forwarding rule is active | |
gcloud compute forwarding-rules list | |
# Undo/Delete all of the created above | |
gcloud compute forwarding-rules delete http-content-rule --global && gcloud compute target-http-proxies delete http-lb-proxy && gcloud compute url-maps delete web-map-http && gcloud compute backend-services delete web-backend-service --global | |
gcloud compute health-checks delete http http-basic-check | |
gcloud compute firewall-rules delete fw-allow-health-check && gcloud compute instance-groups managed delete lb-backend-group && gcloud compute instance-templates delete lb-backend-template |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment