Skip to content

Latest commit

 

History

History

GSP663_Deploy-Scale-and-Update-Your-Website-on-Google-Kubernetes-Engine

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

GSP663 —— Deploy, Scale, and Update Your Website on Google Kubernetes Engine

Table of Contents (🔎 Click to expand/collapse)

Overview

Running websites and applications is hard. Things go wrong when they shouldn't, servers crash, increase in demand causes more resources to be utilized, and making changes without downtime is complicated and stressful. Imagine if there was a tool that could help you do all this and even allow you to automate it. With Kubernetes, all of this is not only possible, it's easy!

In this lab you will assume the role of a developer at a fictional company, Fancy Store, running an ecommerce website. Due to problems with scaling and outages, you are tasked with deploying your application onto the Google Kubernetes Engine (GKE).

Architecture Diagram

Architecture Diagram

Create GKE Cluster

# enable the Container Registry API
$ gcloud services enable container.googleapis.com

# create GKE cluster
$ gcloud container clusters create <CLUSTER_NAME> --num-nodes="<NODE_NUMBER>"

# check instances
$ gcloud compute instances list

Create Docker Container with Cloud Build

# enable the Cloud Build API
$ gcloud services enable cloudbuild.googleapis.com

# build docker container
$ gcloud builds submit --tag "<IMAGE_NAME>" .

Deploy Container to GKE

# deploy application to GKE
$ kubectl create deployment monolith --image="<IMAGE_NAME>"

# check the pods status
$ kubectl get all
  • Kubernetes represents applications as Pods
    • Pods are units that represent a container (or group of tightly-coupled containers).
    • Pods are the smallest deployable units in Kubernetes.
  • The Deployment manages multiple copies of your application, called replicas, and schedules them to run on the individual nodes in your cluster.

Expose GKE Deployment

By default, the containers we run on GKE are not accessible from the Internet because they do not have external IP addresses. We must explicitly expose the application to traffic from the Internet via a Service resource.

# expost website to the internet
$ kubectl expose deployment <POD_NAME> \
    --type="LoadBalancer" \
    --port="<PORT>" \
    --target-port="<TARGET_PORT>"

# inspect the services
$ kubectl get service

Scale GKE deployment

Set up the replicas to scale our application to multiple instances so it can handle all this traffic.

# scale deployment replicas
$ kubectl scale deployment <POD_NAME> \
    --replicas="<REPLICAS_NUMBER>"

# check the pods status
$ kubectl get all

Update with Zero Downtime

# update the image for deployment to a ewn version
$ kubectl set image deployment/<POD_NAME> <POD>=<IMAGE>

# validate deployment
$ kubectl get pods

Clean up

# delete the container images
gcloud container images delete <IMAGE> --quiet

# delete Google Cloud Build artifacts from Google Cloud Storage
$ gcloud builds list | awk 'NR > 1 {print $4}'  # print all sources
$ gcloud builds list | awk 'NR > 1 {print $4}' | while read line; do gsutil rm $line; done

# delete GKE services
$ kubectl delete service <POD_NAME>
$ kubectl delete deployment <POD_NAME>

# delete GKE cluster
$ gcloud container clusters delete <CLUSTER_NAME>

References