Cloud Run 允許您指定哪些修訂版應該接收流量,並指定不同版本接收的流量比例。Revisions 也能使您能夠回滾到先前的版本、逐步增加新版本的流量比例,或在多個修訂版之間拆分流量。
有兩種方法。可以從 console 的右上角打開終端機或是到 https://shell.cloud.google.com/ 會打開純文字的 shell 介面
gcloud auth list
# 驗證身分
gcloud config list project
# 確認當下要使用的 PROJECT
gcloud config set project <Project ID>
# 更改要使用的 PROJECT
gcloud services enable cloudfunctions.googleapis.com
# 開啟 Cloud Function API
mkdir revisions-gcf-codelab && cd $_
REGION=<YOUR_REGION>
PROJECT_ID=<YOUR-PROJECT-ID>
BG_COLOR=darkseagreen # 實作更換 BG_COLOR ,代表不同版本的 Revisions
REGION=us-east1
PROJECT_ID=solution-marcos-01
BG_COLOR=darkseagreen
cat <<EOF >> package.json
{
"dependencies": {
"@google-cloud/functions-framework": "^3.0.0"
}
}
EOF
cat <<EOF >> index.js
const functions = require('@google-cloud/functions-framework');
const BG_COLOR = process.env.BG_COLOR;
const K_REVISION = process.env.K_REVISION;
functions.http('helloHttp', (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<html><body style="background-color:' + BG_COLOR + ';"' + '><div><p>' + 'Hello from ' + K_REVISION + ' using color ' + BG_COLOR + '</p></div></body>' + '</html>');
});
EOF
gcloud functions deploy traffic-splitting-gcf \\
--gen2 \\
--runtime nodejs20 \\
--entry-point helloHttp \\
--source . \\
--region $REGION \\
--trigger-http \\
--no-allow-unauthenticated \\
--update-env-vars BG_COLOR=$BG_COLOR
darkseagreen
SERVICE_URL=$(gcloud run services describe traffic-splitting-gcf --platform managed --region $REGION --format 'value(status.url)')
curl -H "Authorization: bearer $(gcloud auth print-identity-token)" -X GET $SERVICE_URL
tan
作為 background color:# update the env var
BG_COLOR=tan
# deploy the function
gcloud functions deploy traffic-splitting-gcf \\
--gen2 \\
--runtime nodejs20 \\
--entry-point helloHttp \\
--source . \\
--region $REGION \\
--trigger-http \\
--no-allow-unauthenticated \\
--update-env-vars BG_COLOR=$BG_COLOR
tan
curl -H "Authorization: bearer $(gcloud auth print-identity-token)" -X GET $SERVICE_URL
gcloud run revisions list --service traffic-splitting-gcf \\
--region $REGION --format 'value(REVISION)'
<>
內的 REVISION 要改為剛剛取得的 revision IDs:gcloud run services update-traffic traffic-splitting-gcf \\
--region $REGION \\
--to-revisions <REVISION1>=50,<REVISION2>=50
for i in {1..10}; do
echo -e $i "times\\n"
curl -H "Authorization: bearer $(gcloud auth print-identity-token)" $SERVICE_URL
echo -e "\\n"
sleep 1
done
beige
。# update the env var
BG_COLOR=beige
# deploy the function
gcloud functions deploy gradual-rollouts-gcf \\
--gen2 \\
--runtime nodejs20 \\
--entry-point helloHttp \\
--source . \\
--region $REGION \\
--trigger-http \\
--no-allow-unauthenticated \\
--update-env-vars BG_COLOR=$BG_COLOR
beige
的 REVISION# get the revision name
BEIGE_REVISION=$(gcloud run revisions list --service gradual-rollouts-gcf \\
--region $REGION --format 'value(REVISION)')
# now set 100% traffic to that revision
gcloud run services update-traffic gradual-rollouts-gcf --to-revisions=$BEIGE_REVISION=100 --region $REGION
lavender
,新增一個 lavender
的 REVISION# update color
BG_COLOR=lavender
# deploy the function that will not receive any traffic
gcloud functions deploy gradual-rollouts-gcf \\
--gen2 \\
--runtime nodejs20 \\
--entry-point helloHttp \\
--source . \\
--region $REGION \\
--trigger-http \\
--no-allow-unauthenticated \\
--update-env-vars BG_COLOR=$BG_COLOR
beige
REVISION# get Cloud Function (gradual-rollouts-gcf)'s enpoint
SERVICE_URL2=$(gcloud run services describe gradual-rollouts-gcf --platform managed --region $REGION --format 'value(status.url)')
curl -H "Authorization: bearer $(gcloud auth print-identity-token)" -X GET $SERVICE_URL2
IMAGE_URL=$(gcloud run services describe gradual-rollouts-gcf --region $REGION --format 'value(IMAGE)')
gcloud run deploy gradual-rollouts-gcf --image $IMAGE_URL --no-traffic --tag $BG_COLOR --region $REGION --no-allow-unauthenticated
gcloud run services update-traffic gradual-rollouts-gcf --region $REGION --to-tags lavender=1
gcloud run services update-traffic gradual-rollouts-gcf --region $REGION --to-tags lavender=50
gcloud run services update-traffic gradual-rollouts-gcf --region $REGION --to-tags lavender=100
curl -H "Authorization: bearer $(gcloud auth print-identity-token)" -X GET $SERVICE_URL2
gcloud run services update-traffic gradual-rollouts-gcf --to-revisions $BEIGE_REVISION=100 --region $REGION
curl -H "Authorization: bearer $(gcloud auth print-identity-token)" -X GET $SERVICE_URL2
如果你喜歡這篇文章歡迎幫我按愛心鼓勵一下喔!~閱讀愉快!~