This page shows you how to do the following:
- Create a Hello World app.
- Package the app into a container image using Cloud Build.
- Create a cluster in Google Kubernetes Engine (GKE).
- Deploy the container image to your cluster.
The sample is shown in several languages, but you can use other languages in addition to the ones shown.
Before you begin
-
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
-
In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.
- Enable the Cloud Build and Google Kubernetes Engine APIs.
- Install and initialize the Cloud SDK.
-
kubectl
is used to manage Kubernetes, the cluster orchestration system used by GKE. You can installkubectl
by usinggcloud
:gcloud components install kubectl
Writing the sample app
For instructions on creating a Hello World app that runs on GKE, click your language:
Go
Create a new directory named
helloworld-gke
and change directory into it:mkdir helloworld-gke cd helloworld-gke
Create a new module named
example.com/helloworld
:go mod init example.com/helloworld
Create a new file named
helloworld.go
and paste the following code into it:This code creates a web server that listens on the port defined by the
PORT
environment variable.
Your app is finished and ready to be packaged in a Docker container, and then uploaded to Container Registry.
Node.js
Create a new directory named
helloworld-gke
and change into this directory:mkdir helloworld-gke cd helloworld-gke
Create a
package.json
file with the following contents:In the same directory, create a
index.js
file, and copy the following lines into this file:This code creates a web server that listens on the port defined by the
PORT
environment variable.
Your app is finished and ready to be packaged in a Docker container and uploaded to Container Registry.
Python
Create a new directory named
helloworld-gke
and change into this directory:mkdir helloworld-gke cd helloworld-gke
Create a file named
app.py
and paste the following code into this file:
Java
Create a Spring Boot app.
Install Java SE 8 or higher JDK and cURL. Java SE and cURL are only required to create the new web project in the next step. The Dockerfile, which is described later, loads all dependencies into the container.
From your terminal, create a new empty web project:
curl https://start.spring.io/starter.zip \ -d dependencies=web \ -d javaVersion=1.8 \ -d bootVersion=2.1.3.RELEASE \ -d name=helloworld \ -d artifactId=helloworld \ -d baseDir=helloworld-gke \ -o helloworld-gke.zip unzip helloworld-gke.zip cd helloworld-gke
You now have a new Spring Boot project in
helloworld-gke
.In the
src/main/java/com/example/helloworld/HelloworldApplication.java
file, Update theHelloworldApplication
class by adding a@RestController
to handle the/
mapping.This code creates a web server that listens on the port defined by the
PORT
environment variable.
Your app is finished and ready to be packaged in a Docker container, and then uploaded to Container Registry.
C#
Install .NET Core SDK 2.2. .NET Core SDK is only required to create the new web project in the next step. The
Dockerfile
, which is described later, loads all dependencies into the container.From your terminal, create a new empty web project:
dotnet new web -o helloworld-gke
Change directory to
helloworld-gke
.Update the
CreateWebHostBuilder
definition inProgram.cs
by specifying the port URL for.UseUrls()
to define the serving port. The sample shows port8080
, but you can use other ports. Your server must listen to whatever port you specify here:
Your app is finished and ready to be packaged in a Docker container, and then uploaded to Container Registry.
PHP
Create a new directory named
helloworld-gke
and change into this directory:mkdir helloworld-gke cd helloworld-gke
Create a file named
index.php
and paste the following code into this file:
Your app is finished and ready to be packaged in a Docker container, and then uploaded to Container Registry.
Ruby
Create a new directory named
helloworld-gke
and change into this directory:mkdir helloworld-gke cd helloworld-gke
Create a file named
app.rb
and paste the following code into this file:This code creates a web server that listens on the port defined by the
PORT
environment variable.Create a file named
Gemfile
and copy the following into this file:
Containerizing an app with Cloud Build
To containerize the sample app, create a new file named
Dockerfile
in the same directory as the source files, and copy the following content:Go
Node.js
Add a further
.dockerignore
file to ensure that local files do not affect the container build process:Python
Add a
.dockerignore
file to ensure that local files don't affect the container build process:Java
C#
Add a
.dockerignore
file to ensure that local files don't affect the container build process:PHP
Add a
.dockerignore
file to ensure that local files don't affect the container build process:Ruby
Get your Google Cloud project ID:
gcloud config get-value project
Build your container image using Cloud Build, which is similar to running
docker build
anddocker push
, but the build happens on Google Cloud. Replaceproject-id
with your Google Cloud ID:gcloud builds submit --tag gcr.io/project-id/helloworld-gke .
The image is stored in Container Registry.
Creating a GKE cluster
A GKE cluster is a managed set of Compute Engine virtual machines that operate as a single GKE cluster. Depending on the mode of operation that you choose to use in GKE, when you create a cluster, you specify a default zone or region. If you use the Standard mode, your cluster is zonal (for this tutorial), so set your default compute zone. If you use the Autopilot mode, your cluster is regional, so set your default compute region.
Create the cluster.
Standard
Replace
your-gcp-zone
with the Google Cloud zone where you want to host your cluster, such asus-west1-a
:gcloud container clusters create helloworld-gke \ --num-nodes 1 \ --zone your-gcp-zone
Autopilot
Replace
your-gcp-region
with the Google Cloud region where you want to host your cluster, such asus-west1
:gcloud container clusters create-auto helloworld-gke \ --region your-gcp-region
Verify that you have access to the cluster. The following command lists the nodes in your container cluster which are up and running and indicates that you have access to the cluster.
kubectl get nodes
If you run into errors, refer to the Kubernetes Troubleshooting guide.
Deploying to GKE
To deploy your app to the GKE cluster you created, you need two Kubernetes objects.
- A Deployment to define your app.
- A Service to define how to access your app.
Deploy an app
The app has a frontend server that handles the web requests. You define the
cluster resources needed to run the frontend in a new file called
deployment.yaml
. These resources are described as a Deployment. You use
Deployments to create and update a
ReplicaSet
and its associated Pods.
Create the
deployment.yaml
file in the same directory as your other files and copy the following content, replacing$GCLOUD_PROJECT
with your Google Cloud project ID:Deploy the resource to the cluster:
kubectl apply -f deployment.yaml
Track the status of the Deployment:
kubectl get deployments
The Deployment is complete, when all of the
AVAILABLE
deployments areREADY
.NAME READY UP-TO-DATE AVAILABLE AGE hello-deployment 1/1 1 1 20s
If the Deployment has a mistake, run
kubectl apply -f deployment.yaml
again to update the Deployment with any changes.After the Deployment is complete, you can see the Pods that the Deployment created:
kubectl get pods
Deploy a Service
Services provide a single point of access to a set of
Pods. While it's possible to access a single Pod, Pods are ephemeral and can
only be accessed reliably by using a Service address. In your Hello World app,
the "hello" Service defines a load balancer
to access the hello-app
Pods from a single IP address. This Service is defined in the
service.yaml
file.
Create the file
service.yaml
in the same directory as your other source files with the following content:The Pods are defined separately from the Service that uses the Pods. Kubernetes uses labels to select the Pods that a Service addresses. With labels, you can have a Service that addresses Pods from different replica sets and have multiple Services that point to an individual Pod.
Create the Hello World Service:
kubectl apply -f service.yaml
Get the Service's external IP address:
kubectl get services
It can take up to 60 seconds to allocate the IP address. The external IP address is listed under the column
EXTERNAL-IP
for thehello
Service.NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE hello LoadBalancer 10.22.222.222 35.111.111.11 80:32341/TCP 1m kubernetes ClusterIP 10.22.222.1 <none> 443/TCP 20m
View a deployed app
You have now deployed all the resources needed to run the Hello World app on GKE. Use the external IP address from the previous step to load the app in your web browser, and see your running app!
# Example cURL call to your running application on GKE (external IP address of service)
curl 35.111.111.11
Hello World!
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this quickstart, follow these steps.
You are charged for the Compute Engine instances running in your cluster, as well as for the container image in Container Registry.Delete the project
Deleting your Cloud project stops billing for all the resources used within that project.
- In the Cloud Console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
Delete your cluster and container
If you want to keep your project but only delete the resources used in this tutorial, delete your cluster and image.
To delete a cluster using the gcloud
command-line tool, run the following
command for the mode that you used:
Standard
gcloud container clusters delete helloworld-gke \
--zone your-gcp-zone
Autopilot
gcloud container clusters delete helloworld-gke \
--region your-gcp-region
To delete an image from one of your Container Registry repositories, run the following command:
gcloud container images delete gcr.io/project-id/helloworld-gke
What's next
For more information on Kubernetes, see the following:
- Learn more about creating clusters.
- Learn more about Kubernetes.
- Read the
kubectl
reference documentation.
For more information on deploying to GKE, see the following:
- Learn how to package, host, and deploy a simple web server application.
- Deploy a guestbook application with Redis and PHP.
- Deploy a stateful WordPress application with persistent storage and MySQL.
- Setting up Cloud Run on GKE.
For more information on deploying to GKE directly from your IDE with Cloud Code, see the following: