Google Cloud Fundamentals Part 3
Tags: gcp, googlecloud
Published: 2022-04-04 08:26:03

**_Managed Services_**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d4x6sg1zmx31220vhqiy.png)
**_IAAS_**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d0779b6eznjcv7ga4a9t.png)
**_PAAS_**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7qvll3eqcfz1fu2vhsb.png)

**_Containers Basics_**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a8mavyy3ioccq4o1eh26.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jo13wy0a1675cvmvyb5b.png)
**_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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/siez7kaljdv4g6e4y9q9.png)
**_Serverless_**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/52jvjxbbeczvmqchi0ws.png)
**_SAAS_**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lenaq412w7nyudptt1lw.png)
**_Shared Responsibility_**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4eylz8k2htajbcorbkmp.png)
**_GCP Services based on Managed_**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dndhey88pv3b5eeygeoi.png)

**_App Engine_**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/znc14m0zjb94zte19huz.png)

_Compute engine vs App Engine_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4eanfckqk3ax4f9trlk5.png)

Types of App Engine: 1) Standard 2) Flexible

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p924zo3e29xfb93t5dwi.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/99f3zkol6bq8o5kwfxiy.png)
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
```
a folder named "main.py":

```
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return 'My Default Service - V1'
```
a file named "requirements.txt" with these codes:

```
Flask==2.0.2
```
Now let's use the terminal to configure to our desired project. 

```
cd default-service
gcloud config set project <Project ID>
```
Now deploy the app:

```
gcloud app deploy
```
and press Y. You may see this error

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mhdxm4ibuj6br18t0gd7.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fopf89yz9rgmm7an6f0w.png)
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
```
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'
```
Now go to terminal and write

```
gcloud app deploy --version=v2
```
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
```
You can see it here. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ynxsk0j6vjm0tny5fral.png)
You can check the link to check the old version and also current version. 
See new version link:

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

```
gcloud app browse --version <version id>
```
**_Google Kubernetes Engine(GKE)_**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mvmnzzcbdf7b5fceygjp.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/chlm2tak2fo0irdz91h4.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pkktymr9qed54isgyt1p.png)
This is what autopilot does. For now, select Standard mode and Configure.
Give your cluster a name

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xcukn8p7nwu29vhfs871.png)
Let's connect our project to cloudshell:

```
gcloud config set project <project id>
```
Now connecting our cluster with cloud shell project.

```
gcloud container clusters get-credentials <cluster name> --zone us-central1-c --project <project id>
```
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
```
Now check the deployment:

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

```
kubectl expose deployment hello-world-rest-api --type=LoadBalancer --port=8080
```
Now we can check the deployment  we created:

```
kubectl get services
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3cngyrunxyln3ph8bnna.png)

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
```
I your browser, paste this and see the exposed microservice
<External IP>:8080/hello-world
Now let's increase our instances:

```
kubectl scale deployment hello-world-rest-api --replicas=3
```
scaled the deployment to 3 instances.
Now check the status of it:

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

```
kubectl get pods
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xu8lnbn32tzev5uch6p9.png)
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
```
Note: you need to give your specific node pool from your cluster and also zone already selected.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zid2lr8wjebmiqtwozwy.png)
You can now see 2 nodes only

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pcs3tpcohymqeuku91ya.png)

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
```
Let's autoscale Kubernetes cluster:

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

```

Delete the cluster :

```
gcloud container clusters delete
```
Delete services:

```
kubectl delete service <service name>
```

Delete deployment:

```
kubectl delete deployment <deployment name>
```
Now let's delete the cluster once we are within our project:

```
gcloud container clusters delete <cluster name> --zone <zone>
```
**_Cloud Function_**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lipjgzfjacgywq8x4gfo.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qhcg4o2fzs4ng660uf0i.png)