[Cloud Run 教學] 實作 Cloud Run 讀取 Secret Manager 呈現新的網頁

Cloud Run 是一種,你只要做好 Container Image,

上傳到 GCP Arfitact Registry 之後,就可以直接部署的好東西,

如果你只是要小量測試,

你不用開一台 VM 來跑你的 Container,

更不用開一堆機器去跑 Google Kubernetes Engine 浪費錢,

Cloud Run 還可以 Scale to Zero,

就是沒人在用,它就一台機器都不用開,

讓你幾乎不用花什麼錢,就可以測試你的 Container,超方便!

不過還是有些麻煩的地方,

就是如果你想要調整任何一點小地方的話,

你就是要重做你的容器映像檔 Container Image,

通常指的是設定檔,所以就很麻煩。

這裡提供的方法是,把你的設定檔放在 Secret Manager,

然後 Cloud Run 每次啟動時,會自動去讀 Secret,

這樣就能把最新的設定檔吃進 Container,

你就不用一天到晚一直重新建立 Container Image了。

為了簡單示範,這裡直接把首頁檔案 index.html 做為範例,

然後整個環境都用 Cloud Shell,讓你方便進入狀況。

首先建立一個預設的首頁版本,

確認正常顯示之後,再做一個從 Secret 讀取出來的 index.html

建立一個獨立的資料夾

mkdir nginx-mount-secret-index

cd nginx-mount-secret-index

建立預設的 index.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Docker nginx</title>
</head>
<body>
  Hello world from Default Nginx.
</body>
</html>

再建立 nginx.conf

vim nginx.conf

server{
  listen       80 default_server;
  listen       [::]:80 default_server;
  server_name  lou.com;
  root         /usr/share/nginx/html;
  index        index.html;
  charset utf-8;
  access_log /var/log/nginx/access_log;
  error_log /var/log/nginx/error_log;
}

做 Dockerfile

vim Dockerfile

FROM nginx
COPY index.html /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf

我們現在看到資料夾內已經有 3 個檔案了:

接下來去 Artifact Registry 建立 Repository 名為 nginx

現在看到一個 write-to-logging 是上次測試的,先不要看。

Cloud Shell 建立 Image,名字取 read-secret

docker build -t asia-east1-docker.pkg.dev/dong-dong-gcp-3/nginx/read-secret .

試跑一下

docker run -d -p 8080:80 asia-east1-docker.pkg.dev/dong-dong-gcp-3/nginx/read-secret

看到顯示預設頁面了,好的開始!

然後記得先 stop container,要不然待會 Cloud Shell 太忙碌會 Timeout 喔!

查一下那個 Container ID

docker ps

停用那個 Container

docker stop a3070c56ee38

可以開始 Push 到 Artifact Registry

docker push asia-east1-docker.pkg.dev/dong-dong-gcp-3/nginx/read-secret

接下來在 Artifact Registry 看到 Image 出現了

那就準備部署到 Cloud Run

gcloud run deploy read-secret –image asia-east1-docker.pkg.dev/dong-dong-gcp-3/nginx/read-secret –port=80 –allow-unauthenticated

然後它叫我選一個 Region,因為我 Cloud Shell 懶得設定

看起來部署成功了

你看到是第二版 read-secret-00002,因為前面有做錯 XD!

去看 Cloud Run Console,點它的名字進去

再點它的網址

看到預設網頁了

接下來要讓它讀 Secret 了,重點現在才開始。

首先在”其他地方”建立一個新的 index.html

回上一層目錄

cd ..

建立檔案

vim index.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Docker nginx</title>
</head>
<body>
  Hello world from Dongdong Secret Manager!!
</body>
</html>

寫好之後,把這個檔案拿去建立 Secret,

我們直接用 gcloud 指令來建

gcloud secrets create new-page –data-file=index.html

建好去 Secret Manager 看看

看到我們剛送過去的 Secret,名為 new-page

點進去看看

它會顯示目前這個 Secret 裡面有哪些版本,可以再進去看內容:

看到我們準備好的網頁檔 index.html!

那我們試試看,能不能讓 Cloud Run 讀取這個新的首頁。

我再部署一次,對了先設定一下 Region

gcloud config set run/region asia-east1

然後還有一點,你要讀 Secret,你的 Cloud Run 用的 Service Account 要有權限喔!(因為我剛部署時忘記,看到它報錯了 XD)

再去建一下 Service Account

取名字叫做 read-secret

設定權限,給角色為 Secret Manager Secret Accessor

所以我在部署的時候,要指定 Service Account,並且讓它使用 Secret:

gcloud run deploy read-secret –image asia-east1-docker.pkg.dev/dong-dong-gcp-3/nginx/read-secret –service-account read-secret@dong-dong-gcp-3.iam.gserviceaccount.com –update-secrets=/usr/share/nginx/html/index.html=new-page:latest –port=80 –allow-unauthenticated

在 Cloud Run Console 上看到部署成功了,你可以看到我前面錯很多次 XD!

點 URL 看結果,真的讀到 Secret 了!

那現在重點來了,如果我馬上再去改那個 Secret 的網頁,

會馬上更新嗎?

vim index.html

進去加 v2 字眼

再送去 Secret Manager

這裡用的是增加新的 version

gcloud secrets versions add new-page –data-file=index.html

我們再去 Secret Manager 看到 v2 了

馬上再點一次相同網頁 URL,看到 v2 的網頁了!

我再打開 Cloud Logging 佐證一下:

你可以看到我更新部署 Cloud Run 是第4版,

接著我在 12:30 點擊網址,出現 Hello world from Dongdong Secret Manager !!

然後我更新網頁 index.html,加 “v2″,並且送去 Secret Manager。

最後在 12:35 再次點擊網址,出現 Hello world from Dongdong Secret Manager v2 !!

過程中就只有更新 Secret,沒有重新建置 Container Image 喔!

這樣用起來是不是非常方便呢?

Table of Contents
返回頂端