Powered By GitBook
Google Kubernetes Engine quickstart: Deploy a prebuilt Docker container image
Deploy a prebuilt Docker container image using GKE.
Take the interactive version of this tutorial, which runs in the Cloud Console:
​​
​
​
</walkthrough-alt>
This quickstart shows you how to start a cluster of virtual machines and deploy a prebuilt Docker container image with a simple Node.js example app.

Project setup

Google Cloud organizes resources into projects. This allows you to collect all of the related resources for a single application in one place.
Begin by creating a new project or selecting an existing project for this tutorial.
</walkthrough-project-billing-setup>
For details, see Creating a project.

Navigate to the Kubernetes Engine page

Open the Navigation menu in the upper-left corner of the console, and then select Kubernetes Engine.
</walkthrough-menu-navigation>

Create a Kubernetes cluster

A cluster consists of at least one cluster master machine and multiple worker machines called nodes. You deploy applications to clusters, and the applications run on the nodes.
    1.
    Click the Create cluster button.
    2.
    On the Create a Kubernetes cluster page, select the
    Standard cluster</walkthrough-spotlight-pointer> template.
    3.
    Enter a name for this cluster.
    4.
    Choose a zone for this cluster.
    5.
    Click Create to create the cluster.

Get the sample application code

Cloud Shell is a built-in command-line tool for the console. In this section, you start Cloud Shell and use it to get the sample application code. Later, you use Cloud Shell to run the example app using a prebuilt container image.

Open Cloud Shell

Open Cloud Shell by clicking the </walkthrough-cloud-shell-icon>Activate Cloud Shell button in the navigation bar in the upper-right corner of the console.

Get the sample code

Clone the sample code:
1
git clone https://github.com/GoogleCloudPlatform/kubernetes-engine-samples
Copied!
Navigate to the directory containing the sample code:
1
cd kubernetes-engine-samples/hello-app
Copied!

Exploring the deployment

You are now in the main directory for the sample code.

Exploring the application

View the application code:
1
cat main.go
Copied!
main.go is a web server implementation written in the Go programming language. The server responds to any HTTP request with a "Hello, world!" message.

Exploring the image configuration

View the image configuration:
1
cat Dockerfile
Copied!
Dockerfile describes the image that you want Docker to build, including all of its resources and dependencies, and specifies which network port the app should expose.
To learn more about how this file works, refer to the Dockerfile reference in the Docker documentation.

Deploy the application

Wait for the cluster creation to finish

The cluster creation needs to finish before the tutorial can proceed. To track the progress of this activity and others, click the Notifications button in the navigation bar in the upper-right corner of the console.

Set up gcloud and kubectl credentials

Get the gcloud credentials for the cluster that you created:
1
gcloud container clusters get-credentials [cluster-name] --zone [cluster-zone]
Copied!
Replace [cluster-name] and [cluster-zone] with the name and zone of the instance that you created.

Build and push the container

Build the image:
1
docker build -t gcr.io/{{project_id}}/hello-app:v1 $PWD
Copied!
Push the image:
1
gcloud docker -- push gcr.io/{{project_id}}/hello-app:v1
Copied!

Run the application

Run the application on your Kubernetes cluster:
1
kubectl run hello-app --image=gcr.io/{{project_id}}/hello-app:v1 --port=8080
Copied!

View the application

Expose the cluster

Make the cluster available to the public:
1
kubectl expose deployment hello-app --type="LoadBalancer"
Copied!

Find your external IP address

List the services, and look for the hello-app service:
1
kubectl get service hello-app --watch
Copied!
Wait until you see an IP address in the External IP column. This may take a minute. To stop monitoring the services, press Ctrl+C.

Visit your running app

Copy the IP address from the External IP column.
Open a new web browser tab, and visit your app by connecting to the IP address on port 8080:
http://[external-IP]:8080
Replace [external-IP] with the external IP address copied in the previous step.

Modifying your cluster

Kubernetes allows you to easily scale or upgrade your application.

Scale your application

Use the following command to scale your application up to four replicas:
1
kubectl scale deployment hello-app --replicas=4
Copied!
Each replica runs independently on the cluster, with the load balancer serving traffic to all of them.

View your application settings

Look at the deployment settings of your cluster with the following commands:
1
kubectl get deployment
Copied!
1
kubectl get pods
Copied!

Update the application

In this section, you modify your local copy of main.go, rebuild the application, and push the new version.

Modify the application

Use this command to make a text substitution in your local copy of main.go to make it return a different version number in its output:
1
sed -i -e 's/1.0.0/2.0.0/g' main.go
Copied!
You can also modify the file with a text editor instead of using this sed command.

Rebuild the application

1
docker build -t gcr.io/{{project_id}}/hello-app:v2 $PWD
Copied!

Publish the application

1
gcloud docker -- push gcr.io/{{project_id}}/hello-app:v2
Copied!
1
kubectl set image deployment/hello-app hello-app=gcr.io/{{project_id}}/hello-app:v2 && echo 'image updated'
Copied!

View the modified application

Wait a minute to give the application image time to update, and then view the modified application at the same address as before:
http://[external-IP]:8080

Inspect your cluster

You can inspect properties of your cluster through the GCP Console.

View cluster details

Click the name of the cluster that you created. This opens the cluster details page.

Delete the cluster

You can continue to use your app, or you can shut down the entire cluster to avoid subsequent charges.
To remove your cluster, select the checkbox next to the cluster name and click the Delete button.

Conclusion

</walkthrough-conclusion-trophy>
Congratulations! You have deployed a simple "Hello, world" application using Google Kubernetes Engine.
Here are some suggestions for what you can do next:
Last modified 7mo ago