Creating a RESTful Service - Go

    1. A Kubernetes cluster with Knative Serving installed and DNS configured.

    2. installed locally.

    3. envsubst installed locally. This is installed by the gettext package. If not installed it can be installed by a Linux package manager, or by Homebrew on OS X.

    4. Download a copy of the code:

    Setup

    In order to run an application on Knative Serving a container image must be available to fetch from a container registry.

    This sample uses Docker for both building and pushing.

    To build and push to a container registry using Docker:

    1. From the knative-docs directory, run the following command to set your container registry endpoint as an environment variable.

      This sample uses Google Container Registry (GCR):

      1. export REPO="gcr.io/<YOUR_PROJECT_ID>"
    2. Set up your container registry to make sure you are ready to push.

      To push to GCR, you need to:

      If you are using a different container registry, you will want to follow the registry specific instructions for both setup and authorizing the image push.

      1. --tag "${REPO}/rest-api-go" \
      2. --file docs/serving/samples/rest-api-go/Dockerfile .
    3. Push your container to a container registry:

      1. docker push "${REPO}/rest-api-go"
    4. Substitute the image reference path in the template with our published image path. The command below substitutes using the ${REPO} variable into a new file called docs/serving/samples/rest-api-go/sample.yaml.

      1. docs/serving/samples/rest-api-go/sample.yaml

    Now that our image is available from the container registry, we can deploy the Knative Serving sample:

    1. kubectl apply --filename docs/serving/samples/rest-api-go/sample.yaml

    The above command creates a Knative Service within your Kubernetes cluster in the default namespace.

    Explore the Service

    The Knative Service creates the following child resources:

    • Knative Route
    • Knative Configuration
    • Knative Revision
    • Kubernetes Deployment
    • Kuberentes Service

    You can inspect the created resources with the following kubectl commands:

    • View the created Service resource:

    • View the created Route resource:

      1. kubectl get route -l \
      2. "serving.knative.dev/service=stock-service-example" --output yaml
    • View the Kubernetes Service created by the Route

      1. "serving.knative.dev/service=stock-service-example" --output yaml
    • View the created Configuration resource:

      1. kubectl get configuration -l \
      2. "serving.knative.dev/service=stock-service-example" --output yaml
    • View the Revision that was created by our Configuration:

      1. kubectl get revision -l \
      2. "serving.knative.dev/service=stock-service-example" --output yaml
    • View the Deployment created by our Revision

      1. kubectl get deployment -l \
    1. Get the URL of the service:

    2. Send requests to the service using curl:

      1. Send a request to the index endpoint:

        1. curl http://stock-service-example.default.1.2.3.4.xip.io

        Response body: Welcome to the stock app!

      2. Send a request to the /stock endpoint:

        1. curl http://stock-service-example.default.1.2.3.4.xip.io/stock

        Response body: stock ticker not found!, require /stock/{ticker}

      3. Send a request to the /stock endpoint with your ““:

        1. curl http://stock-service-example.default.1.2.3.4.xip.io/stock/<SYMBOL>

        where <SYMBOL> is your “stock symbol”.

        Response body: stock price for ticker <SYMBOL> is <PRICE>

        Example

        Request:

          Response:

      Next Steps

      To clean up the sample Service:

      1. kubectl delete --filename docs/serving/samples/rest-api-go/sample.yaml