This guide shows you how to run an App Engine flexible environment application on GKE. To run your app on GKE, you need to create a Dockerfile for it, build a container using that Dockerfile, and run the container in GKE. No code changes are required.
Before you begin
Before following the instructions on this page, take the following steps:
Verify that your application deploys and runs successfully in App Engine flexible environment.
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.
Enable the GKE, Cloud Build APIs.
Download and install the Google Cloud SDK and then initialize the gcloud tool:
Download the SDK
Download and install the Kubernetes
Creating a Dockerfile for your App Engine app
To create a Dockerfile for your app:
- Change to the application main directory.
- Build a Dockerfile for your app by running the following command:
gcloud beta app gen-config --customWhen you are prompted to change the runtime field in the
app.yamlto custom, select Y to confirm.
Building a container from the Dockerfile
To build a container:
Make sure you are in the App Engine application directory that contains the Dockerfile.
Change the default project used by the
gcloudtool so it points to the project you are using to run your app in GKE:
gcloud config set project [YOUR-PROJECT-ID]
[YOUR-PROJECT-ID]with your GKE project ID.
Build the container using the following command:
gcloud builds submit --tag gcr.io/[YOUR-PROJECT-ID]/[YOUR-CONTAINER-NAME] .
[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.
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 application:
Create the cluster (this may take a few minutes):
gcloud container clusters create [YOUR-CLUSTER-NAME]
[YOUR-CLUSTER-NAME]with the name you want to give the cluster.
From the command line, set your compute zone:
gcloud config set compute/zone us-central1-b
Make sure the GKE
kubectltool is authorized:
gcloud container clusters get-credentials [YOUR-CLUSTER-NAME]
Follow the prompts to authorize the tool.
Run the container that holds your app:
kubectl run [YOUR-DEPLOYMENT-NAME] --image=gcr.io/[YOUR-PROJECT-ID]/[YOUR-CONTAINER-NAME] --port=8080
[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.
Expose the container for public access:
kubectl expose deployment [YOUR-DEPLOYMENT-NAME] --type="LoadBalancer"
[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.
View the external IP address for your app:
kubectl get service [YOUR-DEPLOYMENT-NAME]
[YOUR-DEPLOYMENT-NAME]with the name you used in the previous steps. If the
EXTERNAL IPfield is empty, wait for awhile, then re-invoke the command.
View the app running in GKE:
EXTERNAL-IPwith the external IP address you obtained in the previous step.
You've successfully deployed and run your App Engine Ruby app on GKE!
You may want to learn more about Kubernetes and the
kubectlcommand. 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
kubectlis available in the kubectl reference.