Skip to content

Instantly share code, notes, and snippets.

@tingwei628
Created June 3, 2021 05:45
Show Gist options
  • Save tingwei628/2342475b6e651fe2f37737016e9b471e to your computer and use it in GitHub Desktop.
Save tingwei628/2342475b6e651fe2f37737016e9b471e to your computer and use it in GitHub Desktop.
gcp note
sudo su - // convert to root
ps auwx | grep nginx // check nginx run status
gcloud compute instances create gcelab2 --machine-type n1-standard-2 --zone us-central1-f
gcloud compute ssh gcelab2 --zone us-central1-f
gcloud compute instances get-serial-port-output instance-1 //check whether the server is ready for an RDP connection
gcloud compute reset-windows-password [instance] --zone us-central1-a --user [username]
@tingwei628
Copy link
Author

// GKE
// create cluster
gcloud container clusters create [CLUSTER-NAME]
//authenticate the cluster
gcloud container clusters get-credentials [CLUSTER-NAME]
// use kubectl to deploy
https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#create
//create a new Deployment hello-server from the hello-app container image
kubectl create deployment hello-server --image=gcr.io/google-samples/hello-app:1.0
//create a Kubernetes Service (which is a Kubernetes resource that lets you expose your application to external traffic)
kubectl expose deployment hello-server --type=LoadBalancer --port 8080
// inspect the hello-server Service
kubectl get service
//delete the cluster
gcloud container clusters delete [CLUSTER-NAME]

@tingwei628
Copy link
Author

// Network Load Balancer
//set the default zone
gcloud config set compute/zone us-central1-a
//Set the default region
gcloud config set compute/region us-central1
// install Apache on each instance and give each instance a unique home page.

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"

//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

//Run the following to list your instances.
gcloud compute instances list

//Create a static external IP address for your load balancer

gcloud compute addresses create network-lb-ip-1 \
 --region us-central1

//Add a legacy HTTP health check resource
gcloud compute http-health-checks create basic-check

//Add a target pool in the same region as your instances.

gcloud compute target-pools create www-pool \
    --region us-central1 --http-health-check basic-check

//Add the instances to the pool

gcloud compute target-pools add-instances www-pool \
    --instances www1,www2,www3

//Add a forwarding rule

gcloud compute forwarding-rules create www-rule \
    --region us-central1 \
    --ports 80 \
    --address network-lb-ip-1 \
    --target-pool www-pool

//view the external IP address of the www-rule forwarding rule used by the load balancer
gcloud compute forwarding-rules describe www-rule --region us-central1

//Http Load Balancer
//create the load balancer template

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'

//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-central1-a

//Create the fw-allow-health-check firewall rule. (This is an ingress rule that allows traffic from the Google Cloud health checking systems (130.211.0.0/22 and 35.191.0.0/16). This lab uses the target tag allow-health-check to identify the VMs.)

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

//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

// the IPv4 address that was reserved

gcloud compute addresses describe lb-ipv4-1 \
    --format="get(address)" \
    --global

//Create a healthcheck for the load balancer

    gcloud compute health-checks create http http-basic-check \
        --port 80

//Create a backend service

    gcloud compute backend-services create web-backend-service \
        --protocol=HTTP \
        --port-name=http \
        --health-checks=http-basic-check \
        --global

// 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-central1-a \
        --global

//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

//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

//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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment