Getting Started with App Deployment

    You need:

    • A Kubernetes cluster with Knative installed.
    • An image of the app that you’d like to deploy available on a container registry. The image of the sample app used in this guide is available on Google Container Registry.

    Sample application

    This guide demonstrates the basic workflow for deploying the Hello World sample app (Go) from the . You can use these steps as a guide for deploying your container images from other registries like Docker Hub.

    To deploy a local container image, you need to disable image tag resolution by running the following command:

    The Hello World sample app reads in an variable, TARGET, then prints “Hello World: ${TARGET}!”. If TARGET isn’t defined, it will print “NOT SPECIFIED”.

    The easiest way to deploy a Knative Service is by using the Knative CLI kn.

    Prerequisite: Install the kn binary as described in

    It will create a corresponding resource description internally as when using a YAML file directly. kn provides a command-line mechanism for managing Services. It allows you to configure every aspect of a Service. The only mandatory flag for creating a Service is --image with the container image reference as value.

    To create a Service directly at the cluster, use:

    1. # Create a Knative service with the Knative CLI kn
    2. kn service create helloworld-go --image gcr.io/knative-samples/helloworld-go --env TARGET="Go Sample v1"

    If you want to deploy the sample app, leave the --image config as-is. If you’re deploying an image of your app, update the name of the Service and the value of the --image flag accordingly.

    • Create a new immutable revision for this version of the app.
    • Perform network programming to create a route, ingress, service, and load balancer for your app.
    • Automatically scale your pods up and down based on traffic, including to zero active pods.

    Creating your Deployment with YAML

    Alternatively, to deploy an app using Knative, you can also create the configuration in a YAML file that defines a service. For more information about the Service object, see the .

    This configuration file specifies metadata about the application, points to the hosted image of the app for deployment, and allows the deployment to be configured. For more information about what configuration options are available, see the Serving spec documentation.

    To create the same application as in the previous kn example, create a new file named service.yaml, then copy and paste the following content into it:

    1. apiVersion: serving.knative.dev/v1 # Current version of Knative
    2. kind: Service
    3. metadata:
    4. name: helloworld-go # The name of the app
    5. spec:
    6. template:
    7. spec:
    8. - image: gcr.io/knative-samples/helloworld-go # Reference to the image of the app
    9. env:
    10. - name: TARGET # The environment variable printed out by the sample app
    11. value: "Go Sample v1"

    If you want to deploy the sample app, leave the config file as-is. If you’re deploying an image of your app, update the name of the Service (.metadata.name) and the reference to the container image (.spec.containers[].image) accordingly.

    From the directory where the new service.yaml file was created, apply the configuration:

    Now that you have deployed the service, Knative will perform the following steps:

    • Create a new immutable revision for this version of the app.
    • Perform network programming to create a route, ingress, service, and load balancer for your app.
    • Automatically scale your pods up and down based on traffic, including to zero active pods.

    To see if your app has been deployed successfully, you need the URL created by Knative.

    1. To find the URL for your service, use either kn or kubectl
    1. kn service describe helloworld-go

    This will return something like

    1. Name helloworld-go
    2. Namespace default
    3. Age 12m
    4. Revisions:
    5. 100% @latest (helloworld-go-dyqsj-1) [1] (39s)
    6. Image: gcr.io/knative-samples/helloworld-go (pinned to 946b7c)
    7. OK TYPE AGE REASON
    8. ++ Ready 25s
    9. ++ ConfigurationsReady 26s
    10. ++ RoutesReady 25s

    The command will return the following:

    1. NAME URL LATESTCREATED LATESTREADY READY REASON
    2. helloworld-go http://helloworld-go.default.34.83.80.117.xip.io helloworld-go-96dtk helloworld-go-96dtk True
    1. Now you can make a request to your app and see the results. Replace the URL with the one returned by the command in the previous step.

      1. # curl http://helloworld-go.default.34.83.80.117.xip.io
      2. Hello World: Go Sample v1!

      If you deployed your app, you might want to customize this cURL request to interact with your application.

      It can take a few seconds for Knative to scale up your application and return a response.

      Note: Add -v option to get more detail if the curl command failed.

    You’ve successfully deployed your first application using Knative!

    Cleaning up

    To remove the sample app from your cluster, delete the service record:

    Alternatively, you can also delete the service with kubectl via the definition file or by name.

    1. # Delete with the KService given in the yaml file:
    2. kubectl delete --filename service.yaml
    3. kubectl delete kservice helloworld-go

    Was this page helpful?

    Glad to hear it! Please tell us how we can improve.

    Sorry to hear that. Please .