DEV Community

Cover image for Google Cloud Fundamentals Part 3
Shahriyar Al Mustakim Mitul
Shahriyar Al Mustakim Mitul

Posted on

Google Cloud Fundamentals Part 3

Managed Services

Image description
IAAS

Image description
PAAS

Image description

Containers Basics

Image description

In a virtual machine in addition to hostOS, there is guestOS.
Docker is cloud-neutral and you can run it anywhere9AWS, GCP, Azure)

Image description
Container Orchestration
This basically makes images based on your needs. Ex: Kubernetes.
Benefits: They can provide autoscaling, help one service find another, load balancing, provide health checks, zero downtime deployments.

Image description
Serverless

Image description
SAAS

Image description
Shared Responsibility

Image description
GCP Services based on Managed

Image description

App Engine

Image description

Compute engine vs App Engine

Image description

Types of App Engine: 1) Standard 2) Flexible

Image description
Let's create an APP engine
1) First create a project under your GCP account and enter into it.
2) Go to "App Engine Admin API" and enable it.
3) Go to "App Engine" and create the Application.
4) Select the default options and create app.

Image description
5) Select the terminal and then press "Open editor". Then add a folder named "default-service" and add "app.yaml" with these codes:

runtime: python39
Enter fullscreen mode Exit fullscreen mode

a folder named "main.py":

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return 'My Default Service - V1'
Enter fullscreen mode Exit fullscreen mode

a file named "requirements.txt" with these codes:

Flask==2.0.2
Enter fullscreen mode Exit fullscreen mode

Now let's use the terminal to configure to our desired project.

cd default-service
gcloud config set project <Project ID>
Enter fullscreen mode Exit fullscreen mode

Now deploy the app:

gcloud app deploy
Enter fullscreen mode Exit fullscreen mode

and press Y. You may see this error

Image description
Here while deploying there was a cloudbuild service account was automatically created which need the permission of 'Storage Object Viewer" as it will work with the store.
SO go to IAM & Admin and give this cloudbuild service account a role of Storage Object Viewer.

Image description
Now you can deploy things perfectly. You can check out all of the services created with these codes:

gcloud app services list
gcloud app versions list
gcloud app instances list
Enter fullscreen mode Exit fullscreen mode

Note: When we deployed the app for the first time,gcp automatically assigned us the version number. now let's change code in main.py and give it a new version and deploy.
main.py will have:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return 'My Default Service - V2'
Enter fullscreen mode Exit fullscreen mode

Now go to terminal and write

gcloud app deploy --version=v2
Enter fullscreen mode Exit fullscreen mode

Now a new version will be deployed and all traffic will be splitted there. Let's check it from this code:

gcloud app versions list
Enter fullscreen mode Exit fullscreen mode

You can see it here.
Image description
You can check the link to check the old version and also current version.
See new version link:

gcloud app browse
Enter fullscreen mode Exit fullscreen mode

To check old version's link which is still running but not serving infront:

gcloud app browse --version <version id>
Enter fullscreen mode Exit fullscreen mode

Google Kubernetes Engine(GKE)

Image description
Let's create a cluster using GKe:

  1. Create a project and enter into it.
  2. Enable API from "Kubernetes Engine API"
  3. Go to Kubernetes Engine and press Create cluster. You may see something like this

Image description
The standard mode lets you manage everything by yourself but Autpilot mode helps you managing them. You don't need to worry that much.

Image description
This is what autopilot does. For now, select Standard mode and Configure.
Give your cluster a name

Image description
Let's connect our project to cloudshell:

gcloud config set project <project id>
Enter fullscreen mode Exit fullscreen mode

Now connecting our cluster with cloud shell project.

gcloud container clusters get-credentials <cluster name> --zone us-central1-c --project <project id>
Enter fullscreen mode Exit fullscreen mode

Let's deploy a microservice (ex: hello-world-rest-api) in this cluster

kubectl create deployment hello-world-rest-api --image=in28min/hello-world-rest-api:0.0.1.RELEASE
Enter fullscreen mode Exit fullscreen mode

Now check the deployment:

kubectl get deployment
Enter fullscreen mode Exit fullscreen mode

To access the deployment, you need to expose it. Exposing using load balacner:

kubectl expose deployment hello-world-rest-api --type=LoadBalancer --port=8080
Enter fullscreen mode Exit fullscreen mode

Now we can check the deployment we created:

kubectl get services
Enter fullscreen mode Exit fullscreen mode

Image description

NOte: Exposing a deployment == service.
Now send a curl request to the External IP of the service you just created.

curl <External IP>:8080/hello-world
Enter fullscreen mode Exit fullscreen mode

I your browser, paste this and see the exposed microservice
:8080/hello-world
Now let's increase our instances:

kubectl scale deployment hello-world-rest-api --replicas=3
Enter fullscreen mode Exit fullscreen mode

scaled the deployment to 3 instances.
Now check the status of it:

kubectl get deployment
Enter fullscreen mode Exit fullscreen mode

now we can see 3 instances which as actually called pods. let's see each of them:

kubectl get pods
Enter fullscreen mode Exit fullscreen mode

Image description
When creating kubernetes cluster, we had 3nodes just.Lets scale down to 2:

gcloud container clusters resize my-cluster --node-pool default-pool --num-nodes=2 --zone=us-central1-c
Enter fullscreen mode Exit fullscreen mode

Note: you need to give your specific node pool from your cluster and also zone already selected.

Image description
You can now see 2 nodes only

Image description

Let's auto scale the based on cpu utilization and set max instances to be 4.

kubectl autoscale deployment hello-world-rest-api --max=4 --cpu-percent=70
Enter fullscreen mode Exit fullscreen mode

Let's autoscale Kubernetes cluster:

gcloud container clusters update <cluster name> --enable-autoscaling --min-nodes=1 --max-nodes=10

Enter fullscreen mode Exit fullscreen mode

Delete the cluster :

gcloud container clusters delete
Enter fullscreen mode Exit fullscreen mode

Delete services:

kubectl delete service <service name>
Enter fullscreen mode Exit fullscreen mode

Delete deployment:

kubectl delete deployment <deployment name>
Enter fullscreen mode Exit fullscreen mode

Now let's delete the cluster once we are within our project:

gcloud container clusters delete <cluster name> --zone <zone>
Enter fullscreen mode Exit fullscreen mode

Cloud Function

Image description
Update: Max: 60min (3600sec)
Let's create a Cloud Function:
Enable Cloud Build API

  1. Search for "Cloud Function" and press "CREATE FUNCTION".
  2. choose 1st gen, give a name to the function, trigger type is HTTP, allow unauthenticated invocations, uncheck Require HTTPS and Save.
  3. Press Next
  4. You can choose the Runtime to default one (Node.js 16)
  5. All other things are going to be the same. Press DEPLOY.

Go to Testing and you can see your output.

Cloud Run
Container to deploy in seconds.
Let's create one:

  1. Go to Cloud Run and press CREATE SERVICE.
  2. Choose Container image from Container Image URL.
  3. Set the service name.
  4. Now allow all traffic and allow unauthenticated invocations.
  5. Press Create.

Image description

Top comments (0)