⚙️ Hello Rust on CloudRun

katopz
Noob Learning
Published in
3 min readMar 3, 2022

--

There’s no official support for deploy and run Rust in Google Cloud Platform like Firebase Function just yet. The currently good candidate for us is CloudRun so let’s try it together!

local and GCP test

I will use this official ref below 👇 for kick start. And will do some adjustment and simplify it to suite my need like usual so I can reuse it later with no brain.

And here’s my compilation. 👇

TLDR;

Prerequisites

  • Bash, Homebrew
  • Docker, Docker Hub, k8s, kind or minikube, a bit of Go
  • GCP, CloudRun
  • A bit of Rust

Deploy

This will deploy built sample image to CloudRun to prove that it works.

# Install gcloud cli and login (you know what to do right?)
open https://cloud.google.com/sdk/docs/install
# Deploy samples image to CloudRun
gcloud run deploy --image=gcr.io/knative-samples/helloworld-rust:v0.1.0 --platform=managed

Try online

replace with your own YOUR_ID_BLA_BLA from previous step 👆

curl -w '\n' https://hello-rust-cloudrun-YOUR_ID_BLA_BLA-as.a.run.app/

Expected output:

Hello World

Setup

This will setup homebrew, docker desktop and docker for build image locally.

# Install Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
# Install `Docker Desktop`
open https://docs.docker.com/desktop/mac/install/
# Install Docker
brew install docker

This will setup kubectl to run the image locally.

# Install the `Knative` CLI.
brew install kn
# Install the Knative quickstart plugin.
brew install knative-sandbox/kn-plugins/quickstart
# Install go.
brew install go
# Install kind.
go get sigs.k8s.io/kind
# Run the Knative quickstart plugin.
kn quickstart kind
# Verify you have a cluster called `knative`.
kind get clusters

Build image

# Get `username` from Docker hub and sign-in via `Docker Desktop`.
open https://hub.docker.com/
# Clone this repository.
git clone https://github.com/katopz/hello-rust-cloudrun
cd hello-rust-cloudrun
username=katopz 👈 your username
# Build the container on your local machine.
docker build -t $username/helloworld-rust .
# Push the container to docker registry.
docker push $username/helloworld-rust

Deploy

Deploy again, this time on our local.

# Deploy the app into your cluster.
kubectl apply --filename service.yaml
# Find the URL for your service.
kubectl get ksvc helloworld-rust --output=custom-columns=NAME:.metadata.name,URL:.status.url

Expected output:

NAME              URL
helloworld-rust http://helloworld-rust.default.127.0.0.1.sslip.io

Try local

curl -w '\n' http://helloworld-rust.default.127.0.0.1.sslip.io

Expected output:

Hello Rust Sample v1

Cleanup

# To remove the app from your cluster, delete the service record.
kubectl delete --filename service.yaml

Other versions

rust:latest = 1.68GB
rust:alpine = 1.23GB // with fixed error: linking with
cc failed.
rust:slim-buster = 1.04GB // recommended

# To build `rust:alpine` image.
docker build -t $username/helloworld-rust . -f alpine.Dockerfile
# To build `rust:slim-buster` image.
docker build -t $username/helloworld-rust . -f slim-buster.Dockerfile

TODO

See you next time! 🐈

👍 Updated version w/ actix = 8.48MB (Recommended)

https://github.com/katopz/hello-rust-cloudrun/tree/actix

--

--