Table of Contents (🔎 Click to expand/collapse)
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).
# 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
# enable the Cloud Build API
$ gcloud services enable cloudbuild.googleapis.com
# build docker container
$ gcloud builds submit --tag "<IMAGE_NAME>" .
# 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.
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
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 the image for deployment to a ewn version
$ kubectl set image deployment/<POD_NAME> <POD>=<IMAGE>
# validate deployment
$ kubectl get pods
# 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>