<

Deploying Cloud Run in Just 3 Steps (golang)

What is Cloud Run?

Cloud Run is a managed compute platform that enables you to run stateless containers that are invocable via HTTP requests. Cloud Run is serverless

https://cloud.google.com/run/

More details will be omitted, but it operates serverlessly like Cloud Functions and App Engine. It can also be controlled from GKE because it deploys containers.

Cloud Run Deploy
Cloud Run Deploy

I tried to deploy

https://cloud.google.com/run/docs/quickstarts/build-and-deploy

I will proceed with reference to this.

By the way, the operating environment is done inside the following container.

https://hub.docker.com/r/google/cloud-sdk

step1. Various settings of gcloud

$ gcloud components update
$ gcloud components install beta
$ gcloud config set run/region us-central1

※ As of 2019/04/11, Cloud Run is beta.

step2. Creating application code

$ mkdir helloworld-go && cd helloworld-go
$ touch helloworld.go Dockerfile
package main

import (
        "fmt"
        "log"
        "net/http"
        "os"
)

func handler(w http.ResponseWriter, r *http.Request) {
        log.Print("Hello world received a request.")
        target := os.Getenv("TARGET")
        if target == "" {
                target = "World"
        }
        fmt.Fprintf(w, "Hello %s!\n", target)
}

func main() {
        log.Print("Hello world sample started.")

        http.HandleFunc("/", handler)

        port := os.Getenv("PORT")
        if port == "" {
                port = "8080"
        }

        log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}
# Use the offical Golang image to create a build artifact.
# This is based on Debian and sets the GOPATH to /go.
# https://hub.docker.com/_/golang
FROM golang:1.12 as builder

# Copy local code to the container image.
WORKDIR /go/src/github.com/knative/docs/helloworld
COPY . .

# Build the command inside the container.
# (You may fetch or manage dependencies here,
# either manually or with a tool like "godep".)
RUN CGO_ENABLED=0 GOOS=linux go build -v -o helloworld

# Use a Docker multi-stage build to create a lean production image.
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM alpine

# Copy the binary to the production image from the builder stage.
COPY --from=builder /go/src/github.com/knative/docs/helloworld/helloworld /helloworld

# Run the web service on container startup.
CMD ["/helloworld"]

step3. Registration & Deployment

$ gcloud builds submit --tag gcr.io/[PROJECT-ID]/helloworld
$ gcloud beta run deploy --image gcr.io/[PROJECT-ID]/helloworld
result
result

Impressions

I usually use a Serverless Deployments called now.sh when I'm doing personal development. This service used to support docker containers in v1, but it no longer does in v2. However, I felt this was the best service when choosing something that can be deployed easily for free.

However, I learned that CloudRun was released in Beta version at the GoogleCloudNext19 announcement, and I tried it out immediately. I was able to proceed with the steps this time without any problems and deploy within 3 minutes. I was impressed by how easy and convenient it was. Also, looking at the price table, it's a pay-as-you-go system like CloudFunctions, and it's free up to 2 million requests per month. For personal development, this is much more cost-effective than the instance startup time-based pricing of AppEngine. I have no choice but to quit now.sh and switch to this one!!

If it was helpful, support me with a ☕!

Share

Related tags