Deploy a Static Site Using Traefik and Cloud Run - gem-cloud-run-traefik-website

A passionate full-stack developer from @ePlus.DEV
Search for a command to run...

A passionate full-stack developer from @ePlus.DEV
No comments yet. Be the first to comment.
Quick and practical tips to help users optimize tasks, improve skills, and solve common problems effectively across various areas like tech, lifestyle, productivity, and more.
Một bản tin giúp Developer cập nhật nhanh AI, Cloud, Open Source và những công nghệ đáng chú ý trong ngày. 📌 Executive Summary Vercel AI Gateway thêm Grok Imagine Image 2.0 Preview, đưa image gene

Một bản tin giúp Developer cập nhật nhanh AI, Cloud, Open Source và những công nghệ đáng chú ý trong ngày. 📌 Executive Summary Cloudflare đang hợp nhất Workers AI và AI Gateway thành một control p

Một bản tin giúp Developer cập nhật nhanh AI, Cloud, Open Source và những công nghệ đáng chú ý trong ngày. 📌 Executive Summary GitHub thay đổi hành vi của Code Quality: bật Code Quality sẽ không c

Một bản tin giúp Developer cập nhật nhanh AI, Cloud, Open Source và những công nghệ đáng chú ý trong ngày. 📌 Executive Summary OpenAI thay đổi thời điểm tính phí seat mới của ChatGPT Business từ n

Overview Dataplex is an intelligent data fabric that enables organizations to centrally discover, manage, monitor, and govern their data across data lakes, data warehouses, and data marts to power ana

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.
Click Activate Cloud Shell
at the top of the Google Cloud console.
When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. The output contains a line that declares the PROJECT_ID for this session:
Your Cloud Platform project in this session is set to YOUR_PROJECT_ID
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
gcloud auth list
Click Authorize.
Your output should now look like this:
Output:
ACTIVE: *
ACCOUNT: student-01-xxxxxxxxxxxx@qwiklabs.net
To set the active account, run:
$ gcloud config set account `ACCOUNT`
gcloud config list project
Output:
[core]
project = <project_ID>
Example output:
[core]
project = qwiklabs-gcp-44776a13dea667a6
Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.
In this lab, you'll learn how to deploy a static website using Traefik as a reverse proxy and Cloud Run for serving the content. You will containerize a simple static site, push the image to Artifact Registry, configure Traefik to route traffic, and deploy it all to Cloud Run.
Configure your Google Cloud environment for this lab.
gcloud services enable run.googleapis.com artifactregistry.googleapis.com cloudbuild.googleapis.com
Note:
This command enables the required Google Cloud services.
gcloud config set project qwiklabs-gcp-04-e2c6201b1f19
Note:
Replace PROJECT_ID with your actual project ID.
gcloud config set run/region us-central1
Note:
Replace REGION with your desired region (e.g., us-central1).
Create a Docker repository in Artifact Registry to store your container images.
gcloud artifacts repositories create traefik-repo --repository-format=docker --location=us-central1 --description="Docker repository for static site images"
Note:
This command creates a Docker repository in Artifact Registry.
Create a simple static site, containerize it using Docker, and push the image to Artifact Registry.
mkdir traefik-site && cd traefik-site && mkdir public
public/index.html file with some simple content.cat > public/index.html <<EOF
<html>
<head>
<title>My Static Website</title>
</head>
<body>
<p>Hello from my static website on Cloud Run!</p>
</body>
</html>
EOF
Note:
This is the HTML content for the static site.
gcloud auth configure-docker us-central1-docker.pkg.dev
Note:
This command configures Docker to use your Google Cloud credentials.
traefik.yml with the following content:entryPoints:
web:
address: ":8080"
providers:
file:
filename: /etc/traefik/dynamic.yml
watch: true
log:
level: INFO
Note:
This command creates the Traefik configuration file.
dynamic.yml with the following content:http:
routers:
static-files:
rule: "PathPrefix(`/`)"
entryPoints:
- web
service: static-service
services:
static-service:
loadBalancer:
servers:
- url: "http://localhost:8000"
Note:
This command creates the dynamic content configuration file.
Define the Docker image for Traefik and your static website.
Dockerfile with the following content:FROM alpine:3.20
# Install traefik and caddy
RUN apk add --no-cache traefik caddy
# Copy configs and static files
COPY traefik.yml /etc/traefik/traefik.yml
COPY dynamic.yml /etc/traefik/dynamic.yml
COPY public/ /public/
# Cloud Run uses port 8080
EXPOSE 8080
# Run static server (on 8000) and Traefik (on 8080)
ENTRYPOINT [ "caddy" ]
CMD [ "file-server", "--listen", ":8000", "--root", "/public", "&", "traefik" ]
Note:
This Dockerfile uses the official Alpine image, sets the working directory, and copies your website and Caddyfile. Traefik will be used to handle routing and Caddy used to serve web content.
Build the Docker image and push it to Artifact Registry.
us-central1 and qwiklabs-gcp-04-e2c6201b1f19 with your region and project ID.docker build -t us-central1-docker.pkg.dev/qwiklabs-gcp-04-e2c6201b1f19/traefik-repo/traefik-static-site:latest .
Note:
This command builds the Docker image and tags it with the Artifact Registry repository URL.
docker push us-central1-docker.pkg.dev/qwiklabs-gcp-04-e2c6201b1f19/traefik-repo/traefik-static-site:latest
Note:
This command pushes the Docker image to Artifact Registry.
Deploy the container image to Cloud Run.
us-central1 and qwiklabs-gcp-04-e2c6201b1f19 with your region and project ID.gcloud run deploy traefik-static-site --image us-central1-docker.pkg.dev/qwiklabs-gcp-04-e2c6201b1f19/traefik-repo/traefik-static-site:latest --platform managed --allow-unauthenticated --port 8000
Note:
This command deploys the Docker image to Cloud Run and allows unauthenticated access.
traefik-static-site and allow unauthenticated invocations.Note:
This configures the service name and permissions.
Note:
This is the URL where your static website is accessible.
Access the deployed website through the Cloud Run service URL.
Note:
Verify that your static website is displayed correctly.
curl -LO raw.githubusercontent.com/Techcps/Google-Cloud-Skills-Boost/master/Deploy%20a%20Static%20Site%20Using%20Traefik%20and%20Cloud%20Run/techcps.sh
sudo chmod +x techcps.sh
./techcps.sh