Google Kubernetes Engine quickstart - Deploy a prebuilt Docker container image
Google Kubernetes Engine quickstart: Deploy a prebuilt Docker container image
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.
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.
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.
Click the Create cluster button.
On the Create a Kubernetes cluster page, select the
Standard cluster template.Enter a name for this cluster.
Choose a zone for this cluster.
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
Get the sample code
Clone the sample code:
git clone https://github.com/GoogleCloudPlatform/kubernetes-engine-samples
Navigate to the directory containing the sample code:
cd kubernetes-engine-samples/hello-app
Exploring the deployment
You are now in the main directory for the sample code.
Exploring the application
View the application code:
cat main.go
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:
cat Dockerfile
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:
gcloud container clusters get-credentials [cluster-name] --zone [cluster-zone]
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:
docker build -t gcr.io//hello-app:v1 $PWD
Push the image:
gcloud docker -- push gcr.io//hello-app:v1
Run the application
Run the application on your Kubernetes cluster:
kubectl run hello-app --image=gcr.io//hello-app:v1 --port=8080
View the application
Expose the cluster
Make the cluster available to the public:
kubectl expose deployment hello-app --type="LoadBalancer"
Find your external IP address
List the services, and look for the hello-app
service:
kubectl get service hello-app --watch
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:
kubectl scale deployment hello-app --replicas=4
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:
kubectl get deployment
kubectl get pods
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:
sed -i -e 's/1.0.0/2.0.0/g' main.go
You can also modify the file with a text editor instead of using this sed
command.
Rebuild the application
docker build -t gcr.io//hello-app:v2 $PWD
Publish the application
gcloud docker -- push gcr.io//hello-app:v2
kubectl set image deployment/hello-app hello-app=gcr.io//hello-app:v2 && echo 'image updated'
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
Congratulations! You have deployed a simple "Hello, world" application using Google Kubernetes Engine.
Here are some suggestions for what you can do next:
Find Google Cloud samples on GitHub.
Learn how to set up HTTP load balancing.
Explore other Kubernetes Engine tutorials.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.