Deploy, Scale, and Update Your Website On Google Kubernetes Engine
Deploy, Scale, and Update Your Website On Google Kubernetes Engine
Kubernetes Engine
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).
The exercises in this lab are ordered to reflect a common cloud developer
experience:
Architecture diagram
What you'll learn
How to create a Google Kubernetes Engine cluster
How to create a Docker image
How to deploy Docker images to Kubernetes
How to scale an application on Kubernetes
How to perform a rolling update on Kubernetes
Prerequisites
A basic understanding of Docker and Kubernetes:
o Docker - https://docs.docker.com/
o Kubernetes - https://kubernetes.io/docs/home/
It takes a few moments to provision and connect to the environment. When you
are connected, you are already authenticated, and the project is set to
your PROJECT_ID. For example:
You can list the active account name with this command:
gcloud auth list
(Output)
Credentialed accounts:
- <myaccount>@<mydomain>.com (active)
(Example output)
Credentialed accounts:
- [email protected]
You can list the project ID with this command:
gcloud config list project
(Output)
[core]
project = <project_ID>
(Example output)
[core]
project = qwiklabs-gcp-44776a13dea667a6
If you get an error about region/zone not being specified, please see the
environment setup section to make sure you set the default compute zone.
Output:
NAME ZONE MACHINE_TYPE
PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
Find your Kubernetes cluster and related information in the Google Cloud
console. Click the Navigation menu, then scroll down to Kubernetes
Engine and click Clusters.
Check my progress
Run the following commands to clone the git repo to your Cloud Shell instance:
cd ~
Change to the appropriate directory. You will also install the NodeJS
dependencies so you can test your application before deploying:
cd ~/monolith-to-microservices
./setup.sh
Change to the appropriate directory and test the application by running the
following command to start the web server:
cd ~/monolith-to-microservices/monolith
npm start
Output:
Monolith listening on port 8080!
You can preview your application by clicking the web preview icon and
selecting Preview on port 8080:
This should open a new window where you can see our Fancy Store in action!
Leave this tab open, you'll return to it later in the lab.
Normally you would have to take a two step approach that entails building a
Docker container and pushing it to a registry to store the image for GKE to pull
from. Make life easier and use Cloud Build to build the Docker container and put
the image in the Container Registry with a single command! With a single
command you can build and move the image to the container registry. To view
the manual process of creating a docker file and pushing it you can go here.
Google Cloud Build will compress the files from the directory and move them to a
Google Cloud Storage bucket. The build process will then take all the files from
the bucket and use the Dockerfile to run the Docker build process. Since we
specified the --tag flag with the host as gcr.io for the Docker image, the resulting
Docker image will be pushed to the Google Cloud Container Registry.
First, to make sure you have the Cloud Build API enable, run the following
command:
gcloud services enable cloudbuild.googleapis.com
ID CREATE_TIME DURATION
SOURCE
IMAGES STATUS
To view your build history or watch the process in real time by clicking
the Navigation menu and scrolling down to Tools section, then click Cloud
Build > History. Here you can see a list of all your previous builds.
Click on the build name to see all the details for that build including the log
output.
Optional: From the Build details page, click on the image name in the build
information section to see the container image:
Check my progress
Note: As a best practice, using YAML file and a source control system such as
GitHub or Cloud Source Repositories is recommended to store those changes.
See these resources for more
information: https://kubernetes.io/docs/concepts/workloads/controllers/deployme
nt/
Check my progress
Verify Deployment
Verify the Deployment was created successfully:
kubectl get all
Rerun the command until the pod status is Running.
Output:
NAME READY STATUS RESTARTS AGE
deployment.apps/monolith 1 1 1 1 20m
replicaset.apps/monolith-7d8bc7bf68 1 1 1 20m
You can also view your Kubernetes deployments via the Console. Navigation
menu > Kubernetes Engine > Workloads.
Note: If you are seeing errors or statuses you do not expect, you can debug your
resources with the following commands to see detailed information about them:
# Show deployments
kubectl get rs
To see the full benefit of Kubernetes, simulate a server crash by deleting a pod
and see what happens!
Copy a pod name from the previous command, then use it when you run the
following command to delete it:
kubectl delete pod/<POD_NAME>
You can watch the deletion from the Workloads page - click on the workload
name (it will happen quickly).
If you are fast enough, you can run get all again, and you should see two pods:
one terminating and the other creating or running:
kubectl get all
Output:
NAME READY STATUS RESTARTS AGE
deployment.apps/monolith 1 1 1 1 24m
replicaset.apps/monolith-7d8bc7bf68 1 1 1 24m
Why did this happen? The ReplicaSet saw that the pod was terminating and
triggered a new pod to keep up the desired replica count. Later on you will see
how to scale out to ensure there are several instances running, so if one goes
down users won't see any downtime!
Output:
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
Once you've determined the external IP address for your application, copy the IP
address hten point your browser the URL (such as http://203.0.113.0) to check if
your application is accessible.
You should see the same website you tested earlier. You now have your website
fully running on Kubernetes!
Check my progress
Output:
NAME READY STATUS RESTARTS AGE
pod/monolith-7d8bc7bf68-2bxts 1/1 Running 0 36m
deployment.apps/monolith 3 3 3 3 61m
replicaset.apps/monolith-7d8bc7bf68 3 3 3 61m
You should now see 3 instances of your pod running. Notice that your
deployment and replica set now have a desired count of 3.
Check my progress
Task: You will add some text to the homepage to make the marketing team
happy! It looks like one of the developers have already created the changes with
the file name index.js.new. You can just copy this file to index.js and the
changes should be reflected. Follow the instructions below to make the
appropriate changes.
Run the following commands copy the updated file to the correct file name:
cd ~/monolith-to-microservices/react-app/src/pages/Home
mv index.js.new index.js
you may not use this file except in compliance with the License.
https://www.apache.org/licenses/LICENSE-2.0
See the License for the specific language governing permissions and
*/
root: {
flexGrow: 1
},
paper: {
width: "800px",
padding: theme.spacing(3, 2)
}));
return (
<div className={classes.root}>
<Paper className={classes.paper}>
<Typography variant="h5">
</Typography>
<br />
<Typography variant="body1">
This line of lifestyle products will help you catch up with the
Fancy trend and express your personal style.
Start shopping Fancy items now!
</Typography>
</Paper>
</div>
);
The React components were updated, but the React app needs to be built to
generate the static files.
Run the following command to build the React app and copy it into the monolith
public directory:
cd ~/monolith-to-microservices/react-app
Now that the code is updated, you need to rebuild the Docker container and
publish it to the Google Cloud Container Registry. Use the same command as
earlier, except this time update the version label.
Run the following command to trigger a new cloud build with an updated image
version of 2.0.0:
cd ~/monolith-to-microservices/monolith
In the next section you will use this image to update your application with zero
downtime.
Check my progress
Tell Kubernetes that you want to update the image for your deployment to a new
version with the following command:
kubectl set image deployment/monolith monolith=gcr.io/$
{GOOGLE_CLOUD_PROJECT}/monolith:2.0.0
Verify Deployment
You can validate your deployment update by running the following command:
kubectl get pods
Output:
NAME READY STATUS RESTARTS AGE
Here you will see 3 new pods being created and your old pods getting shut down.
You can tell by the age which are new and which are old. Eventually, you will
only see 3 pods again which will be your 3 updated pods.
To verify our changes, return to the app web page tab and refresh the page.
Notice that your application has been updated.
Your web site should now be displaying the text you just added to the homepage
component!
Click Check my progress to verify the objective.
Check my progress
Cleanup
Although all resources will be deleted with you complete this lab, in your own
environment it's a good idea to remove resources you no longer nee.
rm -rf monolith-to-microservices
gcloud builds list | awk 'NR > 1 {print $4}' | while read line; do gsutil rm
$line; done
Type "y" to confirm this action. This command may take a little while.