Deploy an app in a container image to a GKE cluster
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.
To follow step-by-step guidance for this task directly in the Cloud Shell Editor, click Guide me:
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
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 Google Cloud project.
-
Enable the Artifact Registry, Cloud Build, and Google Kubernetes Engine APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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 Google Cloud project.
-
Enable the Artifact Registry, Cloud Build, and Google Kubernetes Engine APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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 Artifact 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 Artifact 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:
C#
Install the .NET SDK. The .NET 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
.cd helloworld-gke
Update
Program.cs
to listen on port8080
:
Your app is finished and ready to be packaged in a Docker container, and then uploaded to Artifact 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 Artifact Registry.
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: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:Get your Google Cloud project ID:
gcloud config get-value project
In this quickstart, you will store your container in Artifact Registry and deploy it to your cluster from the registry. Run the following command to create a repository named
hello-repo
in the same location as your cluster:gcloud artifacts repositories create hello-repo \ --project=PROJECT_ID \ --repository-format=docker \ --location=us-central1 \ --description="Docker repository"
Replace the following values:
PROJECT_ID
is your Google Cloud project ID
Build your container image using Cloud Build, which is similar to running
docker build
anddocker push
, but the build happens on Google Cloud:gcloud builds submit \ --tag us-central1-docker.pkg.dev/PROJECT_ID/hello-repo/helloworld-gke .
The image is stored in Artifact Registry.
Creating a GKE cluster
A GKE cluster is a managed set of Compute Engine virtual machines that operate as a single GKE cluster.
Create the cluster.
gcloud container clusters create-auto helloworld-gke \ --location us-central1
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. Replace the following values in your file:$GCLOUD_PROJECT
is your Google Cloud project ID:$LOCATION
is the repository location, such asus-central1
.
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 helloworld-gke 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 external IP address of the service:
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:
http://EXTERNAL_IP
Or, you can make a curl
call to the external IP address of the service:
curl EXTERNAL_IP
The output displays the following:
Hello World!
Clean up
To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.
You are charged for the Compute Engine instances running in your cluster, as well as for the container image in Artifact Registry.Delete the project
Deleting your Google Cloud project stops billing for all the resources used within that project.
- In the Google 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 Google Cloud CLI, run the following command for the mode that you used:
gcloud container clusters delete helloworld-gke \
--location us-central1
To delete an image in your Artifact Registry repository, run the following command:
gcloud artifacts docker images delete \
us-central1-docker.pkg.dev/PROJECT_ID/hello-repo/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.
- Create a multi-tier web application with Redis and PHP.
- Deploy WordPress on GKE with Persistent Disks and Cloud SQL.
- Setting up Cloud Run on GKE.
For more information on creating, developing, and running applications on GKE directly from your IDE with Cloud Code, see the following: