Hello World - Go

    You will need:

    • A Kubernetes cluster with Knative installed and DNS configured.
    • installed and running on your local machine, and a Docker Hub account configured.
    • Optional: You can use the Knative CLI client kn to simplify resource creation and deployment. Alternatively, you can use kubectl to apply resource files directly.
    1. Create a basic web server which listens on port 8080, by copying the following code into a new file named helloworld.go:

      You can also download a working copy of the sample, by running the following commands:

      1. git clone -b "release-0.23" https://github.com/knative/docs knative-docs
      2. cd knative-docs/docs/serving/samples/hello-world/helloworld-go
    2. Navigate to your project directory and copy the following code into a new file named Dockerfile:

      1. # Use the official Golang image to create a build artifact.
      2. # This is based on Debian and sets the GOPATH to /go.
      3. FROM golang:1.13 as builder
      4. # Create and change to the app directory.
      5. WORKDIR /app
      6. # Retrieve application dependencies using go modules.
      7. # Allows container builds to reuse downloaded dependencies.
      8. COPY go.* ./
      9. RUN go mod download
      10. # Copy local code to the container image.
      11. COPY . ./
      12. # Build the binary.
      13. # Use the official Alpine image for a lean production container.
      14. # https://hub.docker.com/_/alpine
      15. # https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
      16. FROM alpine:3
      17. RUN apk add --no-cache ca-certificates
      18. # Copy the binary to the production image from the builder stage.
      19. COPY --from=builder /app/server /server
      20. # Run the web service on container startup.
      21. CMD ["/server"]
    3. Use the Go tool to create a manifest.

      1. go mod init github.com/knative/docs/docs/serving/samples/hello-world/helloworld-go
    1. After the build has completed and the container is pushed to docker hub, you can deploy the app into your cluster. Choose one of the following methods:

      1. Create a new file, service.yaml and copy the following service definition into the file. Make sure to replace {username} with your Docker Hub username.

        1. apiVersion: serving.knative.dev/v1
        2. kind: Service
        3. metadata:
        4. name: helloworld-go
        5. namespace: default
        6. spec:
        7. template:
        8. spec:
        9. - image: docker.io/{username}/helloworld-go
        10. - name: TARGET
        11. value: "Go Sample v1"

        Check that the container image value in the service.yaml file matches the container you built in the previous step.

      2. Apply the configuration using kubectl:

        1. kubectl apply --filename service.yaml

        After your service is created, Knative will perform the following steps:

        • Create a new immutable revision for this version of the app.
        • Network programming to create a route, ingress, service, and load balance for your app.
        • Automatically scale your pods up and down (including to zero active pods).
        1. kubectl get ksvc helloworld-go --output=custom-columns=NAME:.metadata.name,URL:.status.url

        Example:

      Use kn to deploy the service:

      1. kn service create helloworld-go --image=docker.io/{username}/helloworld-go --env TARGET="Go Sample v1"

      You should see output like this:

      1. Creating service 'helloworld-go' in namespace 'default':
      2. 0.031s The Configuration is still working to reflect the latest desired specification.
      3. 0.051s The Route is still working to reflect the latest desired specification.
      4. 0.076s Configuration "helloworld-go" is waiting for a Revision to become ready.
      5. 15.694s ...
      6. 15.738s Ingress has not yet been reconciled.
      7. 15.784s Waiting for Envoys to receive Endpoints data.
      8. 16.066s Waiting for load balancer to be ready
      9. 16.237s Ready to serve.
      10. Service 'helloworld-go' created to latest revision 'helloworld-go-jjzgd-1' is available at URL:
      11. http://helloworld-go.default.1.2.3.4.sslip.io

      You can then access your service through the resulting URL.

    1. Now you can make a request to your app and see the result. Replace the URL below with the URL returned in the previous command.

      1. curl http://helloworld-go.default.1.2.3.4.sslip.io
      2. Hello Go Sample v1!

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

    1. kn service delete helloworld-go