Skip to content

Instantly share code, notes, and snippets.

@mehdihasan
Last active July 15, 2020 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mehdihasan/3fa11cfcd0ca80d12ef627d93117dda5 to your computer and use it in GitHub Desktop.
Save mehdihasan/3fa11cfcd0ca80d12ef627d93117dda5 to your computer and use it in GitHub Desktop.

Application Deployment and Scale in Kubernetes Engine (GCP)

PREPARING THE ENVIRONMENT

  1. In GCP console, on the top right toolbar, click the Open Cloud Shell button.

  2. Click Continue.

  3. For convenience, place the zone that Qwiklabs assigned you to into an environment variable called MY_ZONE. At the Cloud Shell prompt, type this partial command:

followed by the zone that Qwiklabs assigned to you. Your complete command will look similar to this:

export MY_ZONE=us-central1-a
  1. Start a Kubernetes cluster managed by Kubernetes Engine. Name the cluster webfrontend and configure it to run 2 nodes:
gcloud container clusters create webfrontend --zone $MY_ZONE --num-nodes 2
  1. After the cluster is created, check your installed version of Kubernetes using the kubectl version command:
kubectl version

The gcloud container clusters create command automatically authenticated kubectl for you.

View your running nodes in the GCP Console. On the Navigation menu (Navigation menu), click Compute Engine > VM Instances.

Run and deploy a container

  1. From your Cloud Shell prompt, launch a single instance of the nginx container. (Nginx is a popular web server.)
kubectl create deploy nginx --image=nginx:1.17.10
  1. View the pod running the nginx container:
kubectl get pods
  1. Expose the nginx container to the Internet:
kubectl expose deployment nginx --port 80 --type LoadBalancer

Kubernetes created a service and an external load balancer with a public IP address attached to it. The IP address remains the same for the life of the service. Any network traffic to that public IP address is routed to pods behind the service: in this case, the nginx pod.

  1. View the new service:
kubectl get services

You can use the displayed external IP address to test and contact the nginx container remotely.

It may take a few seconds before the External-IP field is populated for your service. This is normal. Just re-run the kubectl get services command every few seconds until the field is populated.

  1. Open a new web browser tab and paste your cluster's external IP address into the address bar. The default home page of the Nginx browser is displayed.

  2. Scale up the number of pods running on your service:

kubectl scale deployment nginx --replicas 3

Scaling up a deployment is useful when you want to increase available resources for an application that is becoming more popular.

  1. Confirm that Kubernetes has updated the number of pods:
kubectl get pods
  1. Confirm that your external IP address has not changed:
kubectl get services
  1. Return to the web browser tab in which you viewed your cluster's external IP address. Refresh the page to confirm that the nginx web server is still responding.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment