Skip to content

Commit ecb1f1d

Browse files
authored
Instructions for Google Cloud Build & Run (#14)
1 parent 1f95f3a commit ecb1f1d

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

.gcloudignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.git
2+
.gitignore
3+
.github
4+
.mvn
5+
.gradle
6+
gradle
7+
target
8+

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,48 @@ docker run -it --rm -p 8080:8080 example-spring-boot
9090

9191
The most important part of the Dockerfile is invoking the checkpoint with `RUN --security=insecure`. Also, when creating your own Dockerfiles don't forget to enable the experimental syntax using `# syntax=docker/dockerfile:1.3-labs`.
9292

93+
## Building and running in Google Cloud
94+
95+
It is possible to build the image in Google Cloud Build and later create a service in Google Cloud Run, with a minor modification of the steps above. Start with logging in and creating a repository for the images:
96+
97+
```sh
98+
gcloud auth login
99+
export PROJECT_ID=$(gcloud config list --format 'value(core.project)')
100+
# Setup default region
101+
gcloud config set artifacts/location us-west1
102+
gcloud config set run/region us-west1
103+
gcloud artifacts repositories create crac-examples --repository-format=docker
104+
```
105+
106+
You might need to use service accounts to perform the builds and deploy service; create these through IAM & Admin console and add necessary roles:
107+
* cloud-build: 'Cloud Build Service Account', 'Cloud Build WorkerPool User', 'Service Account User'
108+
* cloud-run: 'Cloud Run Admin', 'Service Account User'
109+
110+
We present two ways to perform the build: `cloudbuild-builder.yaml` uses the single Dockerfile steps shown above, performing the checkpoint in a BuildKit builder. You can also apply the steps from 'Preparing a container image' directly, with several modifications as used in `cloudbuild-direct.yaml`. The main difference vs. local build is that in Cloud Build the commands are executed from a container that provides access to the Docker server where it runs: volume mounts and port mapping works differently. Here is a list of differences:
111+
112+
* For checkpoint image we don't mount `target/cr` directly, but use a named volume `cr`. After checkpoint we need to copy the image out to pass it to restoring image Docker build.
113+
* Ports are not bound to localhost; checkpoint container must use network `cloudbuild` and we connect to the container using its name as hostname.
114+
* We use `--privileged` rather than fine-grained list of capabilities; Docker version used in Cloud Build does not allow capability `CHECKPOINT_RESTORE`.
115+
116+
You can submit the build(s) using these commands:
117+
```sh
118+
gcloud builds submit --config cloudbuild-builder.yaml --service-account=projects/$PROJECT_ID/serviceAccounts/cloud-builder@$PROJECT_ID.iam.gserviceaccount.com
119+
gcloud builds submit --config cloudbuild-direct.yaml --service-account=projects/$PROJECT_ID/serviceAccounts/cloud-builder@$PROJECT_ID.iam.gserviceaccount.com
120+
```
121+
122+
When the build completes, you can create the service in Cloud Run:
123+
124+
```sh
125+
gcloud run deploy example-spring-boot-direct \
126+
--image=us-west1-docker.pkg.dev/$PROJECT_ID/crac-examples/example-spring-boot-direct \
127+
--execution-environment=gen2 --allow-unauthenticated \
128+
--service-account=cloud-runner@$PROJECT_ID.iam.gserviceaccount.com
129+
```
130+
131+
Note that we're using Second generation Execution environment; our testing shows that it is not possible to restore in First generation. Now you can test your deployment:
132+
133+
```sh
134+
export URL=$(gcloud run services describe example-spring-boot-direct --format 'value(status.address.url)')
135+
curl $URL
136+
Greetings from Spring Boot!
137+
```

cloudbuild-builder.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
steps:
2+
- name: 'gcr.io/cloud-builders/docker'
3+
script: |
4+
docker buildx create --buildkitd-flags '--allow-insecure-entitlement security.insecure' --name privileged-builder
5+
docker buildx build --load --builder privileged-builder --allow=security.insecure -f Dockerfile.privileged -t us-west1-docker.pkg.dev/$PROJECT_ID/crac-examples/example-spring-boot-builder .
6+
automapSubstitutions: true
7+
images:
8+
- us-west1-docker.pkg.dev/$PROJECT_ID/crac-examples/example-spring-boot-builder
9+
options:
10+
logging: CLOUD_LOGGING_ONLY

cloudbuild-direct.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
steps:
2+
- name: maven:3.8.7-openjdk-18
3+
entrypoint: mvn
4+
args: [ "-B", "install" ]
5+
- name: 'gcr.io/cloud-builders/docker'
6+
env:
7+
- CHECKPOINT_IMG=example-spring-boot-checkpoint
8+
- CONTAINER=example-spring-boot-checkpoint
9+
script: |
10+
apt-get update && apt-get install siege
11+
docker build -f Dockerfile.checkpoint -t $CHECKPOINT_IMG .
12+
docker run -d --rm -v cr:/cr --privileged --network cloudbuild -p 8080:8080 --name $CONTAINER $CHECKPOINT_IMG
13+
# Wait until the application is up and serving requests
14+
until curl --output /dev/null --silent --head --fail http://$CONTAINER:8080; do
15+
sleep 0.1
16+
done
17+
# Warm-up the server by executing 100k requests against it
18+
siege -c 1 -r 100000 -b http://$CONTAINER:8080
19+
docker exec $CONTAINER jcmd example-spring-boot JDK.checkpoint
20+
# Wait until the container finishes writing the image to volume 'cr'
21+
docker container wait $CONTAINER
22+
# Copy contents of the 'cr' volume into target/cr
23+
docker run --rm -v cr:/cr --entrypoint tar $CHECKPOINT_IMG -cf - /cr | tar -C target -xvf -
24+
docker build -f Dockerfile.restore -t us-west1-docker.pkg.dev/$PROJECT_ID/crac-examples/example-spring-boot-direct .
25+
automapSubstitutions: true
26+
images:
27+
- us-west1-docker.pkg.dev/$PROJECT_ID/crac-examples/example-spring-boot-direct
28+
options:
29+
logging: CLOUD_LOGGING_ONLY

0 commit comments

Comments
 (0)