Run App Engine apps on GKE

This guide shows you how to run an App Engine flexible environment app on GKE. To run your app on GKE, you need to create a Dockerfile for it, build a container with Cloud Build, and then run that container in GKE. No code changes are required.

Before you begin

Before following the instructions on this page, take the following steps:

  1. Verify that your app deploys and runs successfully in the App Engine flexible environment.

  2. In these instructions, you'll use the same project that you used for your App Engine app. You can always create and use a new project, if you want.

  3. Enable the GKE, Cloud Build APIs.

    Enable the APIs

  4. Download and install the Google Cloud CLI and then initialize the gcloud tool: Download the Google Cloud SDK.

    Alternatively, you can use Cloud Shell, which comes with the gcloud CLI, git, and other pre-installed features, such as language support, tools, and editors.

  5. Install the kubectl command-line tool:

    gcloud components install kubectl
    
  6. Download and install git.

Creating a Dockerfile for your App Engine app

Build a Dockerfile for your app as described in Create a Dockerfile.

Building a container from the Dockerfile

To build a container:

  1. Ensure you are in the App Engine app directory that contains the Dockerfile.

  2. Change the default project used by the gcloud tool so it points to the project you are using to run your app in GKE:

    gcloud config set project [YOUR-PROJECT-ID]
    

    Replace [YOUR-PROJECT-ID] with your GKE project ID.

  3. Build the container using the following command:

    gcloud builds submit --tag gcr.io/[YOUR-PROJECT-ID]/[YOUR-CONTAINER-NAME] .
    

    Replace [YOUR-PROJECT-ID] with the project ID of your GKE project, and replace [YOUR-CONTAINER-NAME] with the name of the container you want to use for your app container.

  4. Wait for the container to build: it may take awhile. When it finishes successfully, it shows a message like this one:

    Created [https://cloudbuild.googleapis.com/v1/projects/YOUR-PROJECT-ID/builds/xxxxxxx-xxxx-xxx-xxx-xxxxxxxxxxxx].
    Logs are permanently available at [https://console.developers.google.com/logs/viewer?resource=build&project=YOUR-PROJECT-ID&filters=text:xxxx-xxx-xxx-xxxxxxxxxxxx]].
    
    ID  CREATE_TIME DURATION  SOURCE                                                     IMAGES                               STATUS
    xxxxxxx-xxxx-xxx-xxx-xxxxxxxxxxxx  2017-03-04T00:42:10+00:00  1M32S     gs://YOUR-PROJECT-ID_cloudbuild/source/xxxxxxx.08.tgz  gcr.io/YOUR-PROJECT-ID/YOUR-CONTAINER-NAME  SUCCESS<
    

    Note the container name: you need to specify this when you run it in GKE.

Running your app in GKE

To run the container that holds your app:

  1. Create the cluster (this may take a few minutes):

    gcloud container clusters create [YOUR-CLUSTER-NAME]
    

    Replace [YOUR-CLUSTER-NAME] with the name you want to give the cluster.

  2. From the command line, set your compute zone:

    gcloud config set compute/zone us-central1-b
    
  3. Make sure the GKE kubectl tool is authorized:

    gcloud container clusters get-credentials [YOUR-CLUSTER-NAME]
    

    Follow the prompts to authorize the tool.

  4. Run the container that holds your app:

    kubectl create deployment [YOUR-DEPLOYMENT-NAME] --image=gcr.io/[YOUR-PROJECT-ID]/[YOUR-CONTAINER-NAME]
    

    Replace [YOUR-DEPLOYMENT-NAME] with the name you want to use for your deployment, replace [YOUR-PROJECT-ID] with your GKE project ID, and replace [YOUR-CONTAINER-NAME] with the name of the container you created for your app.

  5. Expose the container for public access:

    kubectl expose deployment [YOUR-DEPLOYMENT-NAME] --type="LoadBalancer" --port=8080
    

    Replace [YOUR-DEPLOYMENT-NAME] with the name you used in the previous steps. You may need to wait several minutes before the external IP is viewable.

  6. View the external IP address for your app:

    kubectl get service [YOUR-DEPLOYMENT-NAME]
    

    Replace [YOUR-DEPLOYMENT-NAME] with the name you used in the previous steps. If the EXTERNAL IP field is empty, wait for awhile, then re-invoke the command.

  7. View the app running in GKE:

    http://EXTERNAL-IP:8080
    

    Replace EXTERNAL-IP with the external IP address you obtained in the previous step.

You've successfully deployed and run your App Engine app on GKE!

What's next

You may want to learn more about Kubernetes and the kubectl command. Google Kubernetes Engine uses Kubernetes for container management, deployment and scaling. More information on Kubernetes is available on the external Kubernetes site.

This guide uses kubectl, the command line interface for managing Kubernetes clusters. More information on kubectl is available in the kubectl reference.