Quickstart: Deploy to Knative serving

Learn to create a cluster enabled for Knative serving and then deploy a prebuilt sample container to that cluster.

If you have a demo account, you can instead follow this quickstart on Google Cloud Skills Boost.

Before you begin

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Make sure that billing is enabled for your Google Cloud project.

  6. To ensure you have the latest version of the Google Cloud CLI, either install or update the Google Cloud CLI:

Setting up the command-line environment and enabling the required APIs

Complete the following steps to set up your command-line environment for Knative serving and enable the required APIs:

  1. Configure the Google Cloud CLI to use the ID of your Google Cloud project by default:

    gcloud config set project PROJECT-ID

    Replace PROJECT-ID with your project's ID.

  2. Set the zone where you want the new cluster to be deployed. You can use any zone where GKE is supported. For example:

    gcloud config set compute/zone ZONE

    Replace ZONE with your desired zone.

  3. Run the gcloud services command to enable the following APIs in your Google Cloud project:

    • Google Kubernetes Engine API: Create GKE clusters.
    • Cloud Build API: Build containers.
    • Container Registry API: Publish containers to Container Registry.
    gcloud services enable container.googleapis.com containerregistry.googleapis.com cloudbuild.googleapis.com

    This might take several seconds to complete. When the APIs have been enabled, the command line displays a message similar to the following:

    Operation "operations/..." finished successfully.

Creating a GKE cluster with Knative serving enabled

Complete the following steps to create a cluster and enable it for Knative serving:

  1. Create a new cluster using the command:

    gcloud container clusters create CLUSTER_NAME \
      --addons=HttpLoadBalancing,CloudRun \
      --machine-type=e2-standard-4 \
      --num-nodes=4 \
      --enable-stackdriver-kubernetes

    Replace CLUSTER_NAME with the name you want for your cluster.

  2. Wait for the cluster creation to complete. During the creation process, you should see messages similar to the following:

    Creating cluster my-cluster...done.
    Created [https://container.googleapis.com/v1beta1/ \
    projects/my-project/zones/us-central1-b/clusters/my-cluster].
    

    where my-project is the ID of your Google Cloud project and my-cluster is the cluster that you just created.

  3. Set the gcloud defaults for your Knative serving resources to the name of your new cluster and its location. You set these defaults to avoid specifying these values each time that you use the Google Cloud CLI.

    gcloud config set run/platform gke
    gcloud config set run/cluster CLUSTER_NAME
    gcloud config set run/cluster_location ZONE

    Replace CLUSTER_NAME and ZONE with the same values that you used to create your new cluster.

Deploying a sample container

Use the Google Cloud console to deploy a sample container and create a service in your cluster:

  1. In the Google Cloud console, go to the Knative serving page.

    Go to Knative serving

  2. Open the Create service form by clicking Create service.

  3. In the available clusters dropdown menu, select the cluster you just created.

  4. Leave default entered as the name of the namespace.

  5. Enter a service name, such as hello.

  6. Click Next.

  7. Select Deploy one revision from an existing container image, then select hello from the demo containers drop-down.

  8. Click Next.

  9. Select External under Connectivity, so that you can invoke the service.

  10. Click Create to deploy the image to Knative serving and wait for the deployment to finish.

Congratulations! You have just deployed a service to a Knative serving enabled cluster.

Accessing your deployed service

Now that you have a service running, you can to send requests to it. In this section, cURL is used to demonstrate how to access your service and verify that it's working:

  1. In the Google Cloud console, go to the Knative serving page.

    Go to Knative serving

  2. Click the name of your new Knative serving service to go to its Service details page.

  3. Click info , located to the right of the service's URL.

  4. Click Invoke in Cloud Shell to run the generated cURL command for your service in Cloud Shell.

The cURL request returns the source code of the sample container that your service is running. Now you've verified that your service is handling requests!

Clean up

You can either disable Knative serving, or you can delete the cluster and the Knative serving service to avoid incurring costs for running these resources.

Disabling Knative serving

To disable Knative serving and keep your cluster:

  1. In the Google Cloud console, go to the Google Kubernetes Engine page.

    Go to Google Kubernetes Engine

  2. Click the cluster where you want to disable Knative serving.

  3. Click Edit.

  4. Scroll down to Anthos Features and from the Knative serving dropdown, select Disable.

  5. Click Save.

Deleting Knative serving

To permanently delete your GKE cluster, including the Knative serving service and all its resources:

  1. In the Google Cloud console, go to the Google Kubernetes Engine page.

    Go to Google Kubernetes Engine

  2. Select the cluster you want to delete.

  3. Click Delete, the click Delete again on the pop up.

What's next

To learn how to build a container from code source, push to Container Registry, and then deploy, see:

For an architectural overview of Knative serving that covers the changes from installing Knative serving as an add-on to your Google Kubernetes Engine cluster, see: