Managed Services
Containers Basics
In a virtual machine in addition to hostOS, there is guestOS.
Docker is cloud-neutral and you can run it anywhere9AWS, GCP, Azure)
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.
App Engine
Compute engine vs App Engine
Types of App Engine: 1) Standard 2) Flexible
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.
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
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.
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.
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)
Let's create a cluster using GKe:
- Create a project and enter into it.
- Enable API from "Kubernetes Engine API"
- Go to Kubernetes Engine and press Create cluster. You may see something like this
The standard mode lets you manage everything by yourself but Autpilot mode helps you managing them. You don't need to worry that much.
This is what autopilot does. For now, select Standard mode and Configure.
Give your cluster a name
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
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
: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
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.
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
Update: Max: 60min (3600sec)
Let's create a Cloud Function:
Enable Cloud Build API
- Search for "Cloud Function" and press "CREATE FUNCTION".
- choose 1st gen, give a name to the function, trigger type is HTTP, allow unauthenticated invocations, uncheck Require HTTPS and Save.
- Press Next
- You can choose the Runtime to default one (Node.js 16)
- 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:
- Go to Cloud Run and press CREATE SERVICE.
- Choose Container image from Container Image URL.
- Set the service name.
- Now allow all traffic and allow unauthenticated invocations.
- Press Create.
Top comments (0)