상세 컨텐츠

본문 제목

GSP015 : Running a Node.js Container in Kubernetes with Container Engine

Cloud metacog

by 학이시습불역호아 2020. 4. 3. 19:50

본문

GSP015

Overview

Google Container Engine makes it easy to run docker containers in the cloud. Google Container Engine uses Kubernetes, an open source container scheduler, to ensure that your cluster is running exactly the way you want it to at all times.

Follow along this lab to learn how to launch a container on Google Container Engine.

What you'll learn

  • Google Container Engine
  • How to launch a single container on Google Container Engine
  • How to launch multiple replicas of a single container on Google Container Engine

Setup and Requirements

What you'll need

To complete this lab, you'll need:

  • Access to a standard internet browser (Chrome browser recommended).
  • Time. Note the lab's Completion time in Qwiklabs, which is an estimate of the time it should take to complete all steps. Plan your schedule so you have time to complete the lab. Once you start the lab, you will not be able to pause and return later (you begin at step 1 every time you start a lab).
  • You do NOT need a Google Cloud Platform account or project. An account, project and associated resources are provided to you as part of this lab.
  • If you already have your own GCP account, make sure you do not use it for this lab.
  • If your lab prompts you to log into the console, use only the student account provided to you by the lab. This prevents you from incurring charges for lab activities in your personal GCP account.

Use a new Incognito window (Chrome) or another browser for the Qwiklabs session. Alternatively, you can log out of all other Google / Gmail accounts before beginning the labs.

Start your lab

When you are ready, click Start Lab. You can track your lab's progress with the status bar at the top of your screen.

Important: What is happening during this time?

Your lab is spinning up GCP resources for you behind the scenes, including an account, a project, resources within the project, and permission for you to control the resources you will need to run the lab. This means that instead of spending time manually setting up a project and building resources from scratch as part of your lab, you can begin learning more quickly.

Find Your Lab's GCP Username and Password

To access the resources and console for this lab, locate the Connection Details panel in Qwiklabs. Here you will find the account ID and password for the account you will use to log in to the Google Cloud Platform:

If your lab provides other resource identifiers or connection-related information, it will appear on this panel as well.

Enable APIs

Search for "Google Compute Engine" in the search box. Click on "Google Compute Engine" in the results list that appears.

Now click "Enable"

Creating a Cluster

In this section you'll create a Google Container Engine cluster.

Log in to Google Cloud Console

Using the Qwiklabs browser tab/window (preferably in Incognito mode) or the separate browser you are using for the Qwiklabs session, copy the Username from the Connection Details panel and click the orange "Open Google Console" button. Paste in the Username, and then the Password as prompted:

Accept the terms and conditions.

Since this is a temporary account, which you will only have access to for this one lab:

  • Do not add recovery options
  • Do not sign up for free trials

Note: You can view the menu with a list of GCP Products and Services by clicking the button at the top-left next to "Google Cloud Platform".

Activate Google Cloud Shell

From the GCP Console click the Cloud Shell icon on the top right toolbar:

Then click "Start Cloud Shell":

It should only take a few moments to provision and connect to the environment:

This virtual machine is loaded with all the development tools you'll need. It offers a persistent 5GB home directory, and runs on the Google Cloud, greatly enhancing network performance and authentication. Much, if not all, of your work in this lab can be done with simply a browser or your Google Chromebook.

Once connected to the cloud shell, you should see that you are already authenticated and that the project is already set to your PROJECT_ID:

gcloud auth list

Command output

Credentialed accounts: - <myaccount>@<mydomain>.com (active)

Note: gcloud is the powerful and unified command-line tool for Google Cloud Platform. Full documentation is available from https://cloud.google.com/sdk/gcloud. It comes pre-installed on CloudShell. You will notice its support for tab-completion.

gcloud config list project

Command output

[core] project = <PROJECT_ID>

If it is not, you can set it with this command:

gcloud config set project <PROJECT_ID>

Command output

Updated property [core/project].Set Compute Zone

Launch Cloud Shell by clicking on the terminal icon in the top toolbar.

Cloud Shell is a browser based terminal to a virtual machine that has the Google Cloud Platform tools installed on it and some additional tools (like editors and compilers) that are handy when you are developing or debugging your cloud application.

We'll be using the gcloud command to create the cluster. First, though, we need to set the compute zone so that the virtual machines in our cluster are created in the correct region. We can do this using gcloud config set compute/zone. Enter the following in Cloud Shell.

gcloud config set compute/zone us-central1-f

Note: More information about regions and zones is available here. https://cloud.google.com/compute/docs/zones

Create a New Cluster

You can create a new container cluster with the gcloud command like this:

gcloud container clusters create hello-world

This command creates a new cluster called "hello-world" with three nodes (VMs). You can configure this command with additional flags to change the number of nodes, the default permissions, and other variables. See the documentation for more details.

Hint: If you get an error when running gcloud container clusters create ensure that you have enabled both the Google Container Engine and Google Compute Engine APIs.

Launching the cluster may take a bit of time but once it is up you should see output in Cloud Shell that looks like this:

NAME ZONE MASTER_VERSION MASTER_IP MACHINE_TYPE NODE_VERSION NUM_NODES STATUS hello-world us-central1-f 1.4.6 104.197.119.168 n1-standard-1 1.4.6 3 RUNNING

Building and Publishing the Node.js App

The next step is to build and publish a container that contains your code. We will be using Docker to build our container, and Google Container Registry to securely publish it.

Set your project ID

You will be using the Google Cloud Project ID in many of the commands in this lab. The Project ID is conveniently stored in an environment variable in Cloud Shell. You can see it here:

echo $DEVSHELL_PROJECT_ID

Get the sample code

git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.gitcd nodejs-docs-samples/containerengine/hello-world/

Build the container

Docker containers are built using a Dockerfile. The sample code provides a basic Dockerfile that we can use. Here is the contents of the file:

FROM node:4 EXPOSE 8080 COPY server.js . CMD node server.js

To build the container, run the following command:

docker build -t gcr.io/$DEVSHELL_PROJECT_ID/hello-node:1.0 .

This will build a Docker container image stored locally.

Publish the container

In order for Kubernetes to access your image, you need to store it in a container registry.

Run the following command to publish your container image:

gcloud docker -- push gcr.io/$DEVSHELL_PROJECT_ID/hello-node:1.0

Deploying the Node.js App

Duration is 10 min

Now that we have a cluster running and our application built, it is time to deploy it.

Create Your Deployment

A deployment is a core component of Kubernetes that makes sure your application is always running. A deployment schedules and manages a set of pods on the cluster. A pod is one or more containers that "travel together". That might mean they are administered together or they have the same network requirements. For this example we only have one container in our pod.

Typically, you would create a yaml file with the configuration for the deployment. In this example, we are going to skip this step and instead directly create the deployment on the command line.

Create the pod using kubectl

kubectl run hello-node --image=gcr.io/$DEVSHELL_PROJECT_ID/hello-node:1.0 --port=8080

This command starts up one copy of the docker image on one of the nodes in the cluster.

You can see the deployment you created using kubectl.

kubectl get deployments

You should get back a result that looks something like:

NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE hello-node 1 1 1 1 30s

You can see the pod running using kubectl as well.

kubectl get pods

You should get back a result that looks something like:

NAME READY STATUS RESTARTS AGE hello-node-3375482827-7hs3q 1/1 Running 0 1m

Allow External Traffic

By default a pod is only accessible to other machines inside the cluster. In order to use the node.js container that was created it needs to be exposed as a service.

Typically, you would create a yaml file with the configuration for the service. In this example, we are going to skip this step and instead directly create the service on the command line.

Expose the deployment with the kubectl expose command.

kubectl expose deployment hello-node --name=hello-node --type=LoadBalancer --port=80 --target-port=8080

kubectl expose creates a service, the forwarding rules for the load balancer, and the firewall rules that allow external traffic to be sent to the pod. The --type=LoadBalancer flag creates a Google Cloud Network Load Balancer that will accept external traffic.

To get the IP address for your service, run the following command:

kubectl get svc hello-node

You should get back a result that looks something like:

NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE hello-node 10.3.247.85 104.198.151.208 80/TCP 8m

Note: It may take a minute or so for the External-IP to populate. If you see <pending> for the External-IP, wait 30 seconds and try again.

Verify the Deployment

Open a new browser window or tab and navigate to the external IP address from the previous step. You should see the sample code up and running!

Congratulations!

Google Container Engine and Kubernetes provide a powerful and flexible way to run containers on Google Cloud Platform. Kubernetes can also be used on your own hardware or on other Cloud Providers.

This example only used a single container but it is simple to setup multiple container environments or multiple instances of a single container as well.

What we've covered

  • Google Container Engine
  • How to build and launch a simple Node.js container on Google Container Engine

Finish Your Quest

This self-paced lab is part of the Qwiklabs Quest "Cloud Architecture." A Quest is a series of related labs that form a learning path. Completing this Quest earns you the badge above, to recognize your achievement. You can make your badge (or badges) public and link to them in your online resume or social media account. Enroll in this Quest and get immediate completion credit if you've taken this lab. See other available Qwiklabs Quests.

Take Your Next Lab

Continue your Quest with Installing a Monitoring Agent, or check out these suggestions:

  • Customize Network Topology with Subnetworks
  • Orchestrating the Cloud with Kubernetes

Next Steps / Learn More

Google Cloud Training & Certification

...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.

Manual Last Updated July 20, 2017Lab Last Tested July 20, 2017

GSP015OverviewSetup and RequirementsCreating a ClusterBuilding and Publishing the Node.js AppDeploying the Node.js App

'Cloud metacog' 카테고리의 다른 글

Orchestrating the Cloud with Kubernetes  (0) 2020.04.04
Kubernetes in Google Cloud  (0) 2020.04.04
Kubernetes Engine: Qwik Start  (0) 2020.04.04
seven categories of GCP services  (0) 2020.04.03
GCP Self-Paced Labs : Introduction to Docker  (0) 2020.04.03

관련글 더보기

댓글 영역