This sample demonstrates creating a simple RESTful service. The exposed endpoint takes a stock ticker (i.e. stock symbol), then outputs the stock price. The endpoint resource name is defined by an environment variable set in the configuration file.
- A Kubernetes cluster with Knative Serving installed.
- Install Docker.
- You need to configure outbound network access because this application makes an external API request.
- Check out the code:
go get -d github.com/knative/docs/serving/samples/rest-api-go
Build the application container and publish it to a container registry:
- Move into the sample directory:
cd $GOPATH/src/github.com/knative/docs
- Set your preferred container registry:
export REPO="gcr.io/<YOUR_PROJECT_ID>"
To run the sample, you need to have a Google Cloud Platform project, and you also need to enable the Google Container Registry API.
- Use Docker to build your application container:
docker build \
--tag "${REPO}/serving/samples/rest-api-go" \
--file serving/samples/rest-api-go/Dockerfile .
- Push your container to a container registry:
docker push "${REPO}/serving/samples/rest-api-go"
-
Replace the image reference path with our published image path in the configuration files (
serving/samples/rest-api-go/sample.yaml
:- Manually replace:
image: github.com/knative/docs/serving/samples/rest-api-go
withimage: <YOUR_CONTAINER_REGISTRY>/serving/samples/rest-api-go
Or
- Use run this command:
perl -pi -e "[email protected]/knative/docs@${REPO}@g" serving/samples/rest-api-go/sample.yaml
- Manually replace:
Deploy the Knative Serving sample:
kubectl apply --filename serving/samples/rest-api-go/sample.yaml
Inspect the created resources with the kubectl
commands:
- View the created Route resource:
kubectl get route --output yaml
- View the created Configuration resource:
kubectl get configurations --output yaml
- View the Revision that was created by our Configuration:
kubectl get revisions --output yaml
To access this service via curl
, you need to determine its ingress address.
- To determine if your service is ready:
kubectl get svc knative-ingressgateway --namespace istio-system --watch
When the service is ready, you'll see an IP address in the EXTERNAL-IP
field:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
knative-ingressgateway LoadBalancer 10.23.247.74 35.203.155.229 80:32380/TCP,443:32390/TCP,32400:32400/TCP 2d
- When the service is ready, export the ingress hostname and IP as environment variables:
export SERVICE_HOST=`kubectl get route stock-route-example --output jsonpath="{.status.domain}"`
export SERVICE_IP=`kubectl get svc knative-ingressgateway --namespace istio-system \
--output jsonpath="{.status.loadBalancer.ingress[*].ip}"`
- If your cluster is running outside a cloud provider (for example on Minikube),
your services will never get an external IP address. In that case, use the istio
hostIP
andnodePort
as the service IP:
export SERVICE_IP=$(kubectl get po --selector knative=ingressgateway --namespace istio-system \
--output 'jsonpath={.items[0].status.hostIP}'):$(kubectl get svc knative-ingressgateway --namespace istio-system \
--output 'jsonpath={.spec.ports[?(@.port==80)].nodePort}')
- Now use
curl
to make a request to the service:
- Make a request to the index endpoint:
curl --header "Host:$SERVICE_HOST" http://${SERVICE_IP}
Response body: Welcome to the stock app!
- Make a request to the
/stock
endpoint:
curl --header "Host:$SERVICE_HOST" http://${SERVICE_IP}/stock
Response body: stock ticker not found!, require /stock/{ticker}
- Make a request to the
/stock
endpoint with aticker
parameter:
curl --header "Host:$SERVICE_HOST" http://${SERVICE_IP}/stock/<ticker>
Response body: stock price for ticker <ticker> is <price>
To clean up the sample service:
kubectl delete --filename serving/samples/rest-api-go/sample.yaml