From Local to Cloud: Mastering Website Deployment with Google Kubernetes Engine
You may be wondering why I chose Google Kubernetes Engine (GKE) to deploy my website. ?? The decision stems from GKE's robust capabilities in container orchestration and scalability. ?? While traditional hosting solutions might suffice for simple websites, GKE offers significant advantages for modern web applications. ?? It provides automated container management, ?? built-in security features ??, and seamless integration with other Google Cloud services. ?? Most importantly, GKE's auto-repair and auto-scaling features ensure high availability and reliability, making it an ideal choice for production deployments. ?
In this comprehensive guide, I'll demonstrate how to deploy a website using GKE, starting with basic container operations using Docker ?? and nginx, ?? then progressing to container registry management, and finally deploying to a GKE cluster. ?? We'll begin by setting up our development environment in Google Cloud Console, ??? move through containerizing our website, ??? and conclude with exposing it to the internet through a load balancer. ?? By following these steps, you'll learn not just the mechanics of deployment, but also best practices for container orchestration in a production environment. ??
The technical journey will take us through several key stages:
1?? Setting up our Google Cloud environment and initial nginx container
2?? Containerizing our website using Docker
3?? Storing our container image in Google Container Registry ???
4?? Creating and configuring a GKE cluster ??
5?? Deploying our containerized website to GKE
6?? Exposing our application to the internet ??
1. Setting up our Google Cloud environment and initial nginx container
In your favourite web browser search for “Google Cloud Console”. Click the first link that appears and sign in using your preferred Gmail account.
On the Google Cloud Dashboard, click “New Project” and name your project
Start by setting the current project to work on in cloudshell using the commands:
gcloud config set project <project name>
for this project it is:
gcloud config set project stock-market-website-gke
2. Containerizing our website using Docker
Cloud shell comes with docker installed in it, so we just have to download the latest nginx image where our website will be hosted and then run the container using the commands:
docker run -p 8080:80 nginx:latest
-p 8080 means we are exposing our cloud shell port while –p 80 means we are exposing our nginx in this port.
Additionally, you can preview the nginx website using the cloud shell preview on port 8080 as shown below.
And boom, our Nginx server is up and running as evidence below.
Pressing Ctrl + C in cloudshell , the container stops running as shown below.
You can see the containers that were previously running as shown below using the commands:
docker ps -a
To run the nginx container in the background using the docker daemon using the command:
docker run -d -p 8080:80 nginx:latest
and then using docker ps you can see the container assigned the container id: 45a086046100
On the top-right corner, upload your website
All the website files will be uploaded in a folder as shown below:
Copy the index file to the nginx container in the html file using the command:
docker cp <file name> <container id>:/usr/share/nginx/html/
And boom, ?????? the website can now be view using the web view cloudshell button. So now the container is running with our website.
Next is to update the nginx image with the latest changes using the command:
?docker commit <container id> <new container image name>
To confirm the images currently in our server use the command:
docker images
The new nginx image now is named stm/web with the version1 tag.
3. Storing our container image in Google Container Registry
Container Registry is a repository where developers can store container images just like GitHub.
In the Google Cloud Console, search for “container Registry"
Before pushing the image from Docker to Google Container Registry, it needs to be tagged using the command:
领英推荐
docker tag <current image> <hostname>/<project ID>/<repository name>
Using the docker images command, you can see the new image
To push the image to Google Container Registry, use the command:
docker push <hostname>/<project ID>/<repository name>
Clicking Refresh in the Google Cloud Dashboard, the container repository is available
When you double-click on the container image, you can see its details i.e. the image size, date created etc
Disclaimer: Container Registry will be shutdown on 18th March, 2025 so alternatively use Artifact Registry. ??????????
4. Creating and configuring a GKE cluster
Google Kubernetes Engine components (nodes etc) run in the Google Compute Engine hence we set the location where we want the compute engine to be procured using the command:
gcloud config set compute/zone us-central1-a
Once requested whether to enable googleapis, enter y
We then create a cluster named st-cluster with 1 node using the command:
gcloud container clusters create st-cluster –num-nodes=1
You get an error that requires enabling the kubernetes engine api. Going back to the consule, search for “Kubernetes engine api” then click “Enable” as shown below.
After retrying the command, the st-cluster is created and we get a success message showing it is running
One of the advantages of using GKE instead of GCE is that clusters and node are autorepaired.
Navigating to the Google Cloud Console then searching for “Kubernetes Engine”, the st-cluster created is already created
To configure kubernetes controller to use the st-cluster created we use the command:
gcloud container clusters get-credentials st-cluster
5. Deploying our containerized website to GKE
To deploy an application to the cluster, use the command:
kubectl create deployment st-server --image=us.gcr.io/stock-market-website-gke/stm-site:version1
6. Exposing our application to the internet
To expose the application deployment to the internet, we use the command:
kubectl expose deployment web-server –type LoadBalancer –port 80 –target-port 80
The loadbalancer is used to distribute traffic, and the nginx port 80 is mapped to the website’s port 80.
To get the status of the pods, use the command:
kubectl get pods
1 pod is running according to what the screenshot below:
To get the service, use the command:
kubectl get service <deployment name>
in this case
kubectl get service st-server
From this, you get the external ip address where the website can be access. In this case 34.79.175.204
Copy the IP address and paste it in your web browser and boom you can now access your website
Congratulations you have successfully deployed your web application to Google Kubernetes Engine. ??????