<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>雲端 彙整 - 泰克哪裡去</title>
	<atom:link href="https://tech.uccu.website/category/it/cloud/feed" rel="self" type="application/rss+xml" />
	<link>https://tech.uccu.website/category/it/cloud</link>
	<description>一個科技相關的隨手記錄網站</description>
	<lastBuildDate>Sun, 25 Sep 2022 14:11:36 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>
<site xmlns="com-wordpress:feed-additions:1">119574712</site>	<item>
		<title>【2022鐵人賽】基本版-建立CI Pipeline(3)</title>
		<link>https://tech.uccu.website/2022ironman-day10-ci-pipeline-deploy-dev-to-cloudrun.html?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=2022ironman-day10-ci-pipeline-deploy-dev-to-cloudrun</link>
					<comments>https://tech.uccu.website/2022ironman-day10-ci-pipeline-deploy-dev-to-cloudrun.html#respond</comments>
		
		<dc:creator><![CDATA[鳴人]]></dc:creator>
		<pubDate>Sun, 25 Sep 2022 13:59:09 +0000</pubDate>
				<category><![CDATA[2022鐵人賽]]></category>
		<category><![CDATA[Azure DevOps]]></category>
		<category><![CDATA[Docker]]></category>
		<category><![CDATA[Google雲端GCP]]></category>
		<category><![CDATA[iThome鐵人賽]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[雲端]]></category>
		<category><![CDATA[2022ironman]]></category>
		<category><![CDATA[azure devops]]></category>
		<category><![CDATA[docker]]></category>
		<category><![CDATA[gcp]]></category>
		<guid isPermaLink="false">https://tech.uccu.website/?p=1930</guid>

					<description><![CDATA[<p>這篇內容利用了Google建置的gcloud的Docker Image來執行gcloud CLI工具的指令，除了先前建立的服務帳戶要補上一個IAM的角色之外，透過Container執行gcloud指令還有一個重點，它將會是能不能夠正常透過Container執行gcloud的關鍵！</p>
<p>這篇文章 <a href="https://tech.uccu.website/2022ironman-day10-ci-pipeline-deploy-dev-to-cloudrun.html">【2022鐵人賽】基本版-建立CI Pipeline(3)</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></description>
										<content:encoded><![CDATA[<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" fetchpriority="high" decoding="async" width="990" height="150" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/00-DevelopCI-Flow.png?resize=990%2C150&#038;ssl=1" alt="" class="wp-image-1931" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/00-DevelopCI-Flow.png?w=990&amp;ssl=1 990w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/00-DevelopCI-Flow.png?resize=300%2C45&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/00-DevelopCI-Flow.png?resize=768%2C116&amp;ssl=1 768w" sizes="(max-width: 990px) 100vw, 990px" /><figcaption>Develop CI流程</figcaption></figure></div>


<p>上面這張圖是<a href="https://tech.uccu.website/2022ironman-day3-flow-plan.html" target="_blank" rel="noreferrer noopener">流程規劃說明</a>裡面畫的Develop CI Pipeline流程，我們已經在前兩篇完成了Build(Code)、Build Image，剩下最後一步就是Deploy Dev環境，這一篇就來完成這最後一步吧！</p>



<pre class="wp-block-prismatic-blocks"><code class="language-yaml">trigger:
- none

pool:
  vmImage: ubuntu-latest

resources:
  repositories:
  - repository: sources
    type: git
    name: ironman2022/NetApp
    ref: Develop
    trigger:
      branches:
        include:
          - Develop

variables:
  pipelineArtifact: output
  buildResultZipName: buildResult.zip
  slnOrCsprojName: IronmanWeb.sln
  imgRepository: &#039;asia-east1-docker.pkg.dev/feisty-mechanic-363012/ironman2022/ironmanweb&#039;
  buildDockerfile: &#039;Dockerfile&#039;
  imgRegistryService: &#039;GCPArtifactRegistry&#039;
  cloudRunServiceName: ironmanweb
  cloudRunPort: 8080
  cloudRunRegion: asia-east1
  cloudRunProjectId: feisty-mechanic-363012
  gcpAuthJsonFile: ironman2022-gcp-key.json

jobs:
  - job: BuildCode
    steps:
      - checkout: sources
        clean: true
      - script: |
          export UID=$(id -u)
          export GID=$(id -g)
          docker run --user $UID:$GID --rm \
          -v $(Build.SourcesDirectory):/tmp/source \
          -v $(Build.BinariesDirectory):/tmp/publish \
          -e DOTNET_CLI_HOME=/tmp/.dotnet \
          mcr.microsoft.com/dotnet/sdk:6.0-alpine \
          dotnet publish /tmp/source/$(slnOrCsprojName) \
          -c release \
          -o /tmp/publish
        displayName: Dotnet Build
      - task: ArchiveFiles@2
        displayName: 壓縮成zip
        inputs:
          rootFolderOrFile: $(Build.BinariesDirectory)
          includeRootFolder: false
          archiveType: &#039;zip&#039;
          archiveFile: &#039;$(Build.ArtifactStagingDirectory)/zipFiles/$(buildResultZipName)&#039;
          replaceExistingArchive: true
      - task: PublishBuildArtifacts@1
        displayName: 上傳到Pipeline Artifact
        inputs:
          PathtoPublish: &#039;$(Build.ArtifactStagingDirectory)/zipFiles/$(buildResultZipName)&#039;
          ArtifactName: &#039;$(pipelineArtifact)&#039;
          publishLocation: &#039;Container&#039;
  - job: BuildImage
    dependsOn: BuildCode
    steps:
      - task: DownloadBuildArtifacts@0
        displayName: 下載Pipeline Artifact
        inputs:
          buildType: &#039;current&#039;
          cleanDestinationFolder: true
          downloadType: &#039;single&#039;
          artifactName: &#039;$(pipelineArtifact)&#039;
          downloadPath: &#039;$(System.ArtifactsDirectory)/&#039;
      - task: ExtractFiles@1
        displayName: Unzip zip
        inputs:
          archiveFilePatterns: &#039;$(System.ArtifactsDirectory)/$(pipelineArtifact)/$(buildResultZipName)&#039;
          destinationFolder: &#039;$(System.ArtifactsDirectory)/BuildImage&#039;
          cleanDestinationFolder: true
          overwriteExistingFiles: true
      - task: Docker@2
        displayName: Build image
        inputs:
          repository: &#039;$(imgRepository)&#039;
          command: &#039;build&#039;
          Dockerfile: $(buildDockerfile)
          buildContext: &#039;$(System.ArtifactsDirectory)/BuildImage&#039;
          arguments: &#039;--no-cache&#039;
          tags: |
            latest
      - task: Docker@2
        displayName: &quot;Login to Container Registry&quot;
        inputs:
          command: login
          containerRegistry: $(imgRegistryService)
      - task: Bash@3
        displayName: Push docker image
        inputs:
          targetType: &#039;inline&#039;
          script: |
            docker push -a $(imgRepository)
  - job: DeployCloudRun
    dependsOn: BuildImage
    steps:
      - task: Bash@3
        displayName: Deploy docker image to cloudrun
        inputs:
          targetType: &#039;inline&#039;
          script: |
            docker run --rm \
            -v $(Build.SourcesDirectory)/$(gcpAuthJsonFile):/gcp/cloudKey.json \
            asia.gcr.io/google.com/cloudsdktool/google-cloud-cli:latest \
            bash -c &quot;gcloud auth login --cred-file=/gcp/cloudKey.json &amp;&amp; gcloud run deploy $(cloudRunServiceName) --set-env-vars=Ironman=$(Build.BuildId) --image $(imgRepository) --region $(cloudRunRegion) --project $(cloudRunProjectId) --allow-unauthenticated&quot;</code></pre>



<p>哇！一來就是一長串的YAML內容…</p>



<p>不不不，你如果是用VSCode打開，幾乎可以把前面兩個Job折疊起來，這邊增加的DeployCloudRun Job也只有一個Bash的task，不多的。</p>



<p>就讓我娓娓道來這篇主要增加的內容吧！</p>



<pre class="wp-block-prismatic-blocks"><code class="language-yaml">  cloudRunServiceName: ironmanweb
  cloudRunPort: 8080
  cloudRunRegion: asia-east1
  cloudRunProjectId: feisty-mechanic-363012
  gcpAuthJsonFile: ironman2022-gcp-key.json</code></pre>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" decoding="async" width="602" height="240" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/01-CloudRunList.png?resize=602%2C240&#038;ssl=1" alt="" class="wp-image-1936" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/01-CloudRunList.png?w=602&amp;ssl=1 602w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/01-CloudRunList.png?resize=300%2C120&amp;ssl=1 300w" sizes="(max-width: 602px) 100vw, 602px" /></figure></div>


<p>cloudRunServiceName就是圖中Cloud Run的名稱。</p>



<p>cloudRunPort設定為8080是在appsettings.json中設定了Kestrel的Http是使用8080 Port，也就是container內會監聽什麼Port，對應docker指令就是-p 80:8080。</p>



<p>cloudRunRegion則是CloudRun佈署的區域(機房)。</p>



<p>cloudRunProjectId可以直接從Google Cloud管理介面的URL得知，也就是在上圖畫面的時候，看一下瀏覽器上的網址列，「&amp;project=」後面的就是了，或是選擇Project的下拉選單：</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img data-recalc-dims="1" decoding="async" width="1024" height="399" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/02-GCPFindProjectId-1024x399.png?resize=1024%2C399&#038;ssl=1" alt="" class="wp-image-1938" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/02-GCPFindProjectId.png?resize=1024%2C399&amp;ssl=1 1024w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/02-GCPFindProjectId.png?resize=300%2C117&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/02-GCPFindProjectId.png?resize=768%2C299&amp;ssl=1 768w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/02-GCPFindProjectId.png?w=1142&amp;ssl=1 1142w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption>Google Cloud的Project Id</figcaption></figure></div>


<p>最後的gcpAuthJsonFile則是前面幾篇用來授權的Json檔案。不過這邊要補充一下，<a href="https://tech.uccu.website/2022ironman-day4-create-registry-and-service-account.html" target="_blank" rel="noreferrer noopener">那時候</a>在新增服務帳戶的時候還少加了一個「<strong>服務帳戶使用者</strong>」角色，所以漏加這個角色繼續做下去的話，就會碰到下面的錯誤訊息：</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p></p><cite>PERMISSION_DENIED: Permission &#8216;iam.serviceaccounts.actAs&#8217; denied on service account</cite></blockquote>



<p>為了讓後面使用gcloud cli可以順利執行，所以要先在<a href="https://console.cloud.google.com/iam-admin/iam?hl=zh-TW" target="_blank" rel="noreferrer noopener">IAM</a>裡面將前面新增的服務帳戶加上「<strong>服務帳戶使用者</strong>」角色：</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="424" height="340" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/03-AddGCPIAMRole.png?resize=424%2C340&#038;ssl=1" alt="" class="wp-image-1939" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/03-AddGCPIAMRole.png?w=424&amp;ssl=1 424w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/03-AddGCPIAMRole.png?resize=300%2C241&amp;ssl=1 300w" sizes="auto, (max-width: 424px) 100vw, 424px" /><figcaption>新增「<strong>服務帳戶使用者</strong>」角色</figcaption></figure></div>


<p>增加角色之後不需要重新下載用於授權的Json檔案，因為有什麼角色權限不會寫在檔案裡。</p>



<p>Job越加越多，這三個Job之間其實是有相依性的，也就是說要先BuildCode之後才能夠BuildImage，接下來才能DeployCloudRun，所以在第二個和第三個Job底下分別要加上dependsOn的屬性：</p>



<pre class="wp-block-prismatic-blocks"><code class="language-yaml">- job: BuildImage
    dependsOn: BuildCode</code></pre>



<pre class="wp-block-prismatic-blocks"><code class="language-yaml">- job: DeployCloudRun
    dependsOn: BuildImage</code></pre>



<p>這個部份滿重要的，尤其是如果有<a href="https://azure.microsoft.com/zh-tw/pricing/details/devops/azure-devops-services/" target="_blank" rel="noreferrer noopener">額外購買CloudAgent的執行數量</a>時，因為Job可以在不同的Agent執行，所以它可以在同一個Pipeline同時跑多個Job(沒有設定相依的dependsOn時)，就算沒有額外購買CloudAgent的執行數量，沒設定dependsOn也無法保證它們的執行順序。BuildImage的Job相較前一篇有增加的也只有dependsOn這個屬性。</p>



<p>最後就是DeployCloudRun這個Job，裡面的內容也只有一個Bash的task，所以下面我直接貼bash script的部份：</p>



<pre class="wp-block-prismatic-blocks"><code class="language-bash">            docker run --rm \
            -v $(Build.SourcesDirectory)/$(gcpAuthJsonFile):/gcp/cloudKey.json \
            asia.gcr.io/google.com/cloudsdktool/google-cloud-cli:latest \
            bash -c &quot;gcloud auth login --cred-file=/gcp/cloudKey.json &amp;&amp; gcloud run deploy $(cloudRunServiceName) --set-env-vars=Ironman=$(Build.BuildId) --image $(imgRepository) --region $(cloudRunRegion) --project $(cloudRunProjectId) --allow-unauthenticated&quot;</code></pre>



<p>在這裡是使用google的gcloud CLI工具來執行CloudRun的佈署，不過gcloud CLI工具還是需要安裝的，要嘛是需要先裝在Agent的電腦內，而且還要去爬<a href="https://cloud.google.com/sdk/docs/install" target="_blank" rel="noreferrer noopener">官方的安裝文件</a>知道怎麼安裝，不然就是安裝在Docker Image裡面。</p>



<p>我們使用的是ClougAgent，所以Agent的環境不是我們可以控制的，每次執行也是新的vm執行起來，所以選擇後者使用google建立的gcloud CLI的Docker Image會是最理想的選擇，除了從<a href="https://hub.docker.com/r/google/cloud-sdk" target="_blank" rel="noreferrer noopener">Docker Hub</a>可以找到之外，<a href="https://cloud.google.com/sdk/docs/downloads-docker" target="_blank" rel="noreferrer noopener">官方文件</a>也有提供不同Container Registry的選擇說明。</p>



<p>使用Container來執行gcloud CLI，我們可以省去安裝的麻煩事，只要會使用就可以了，這讓我們可以更專注在其它的設計部份。</p>



<p>script中的重點只有第二行和最後一行，分別是把授權用的Json檔案關聯到Container裡面，以便讓裡面的gcloud CLI工具可以讀取到內容進行login動作，以及最後一行包含lgoin的指令。</p>



<p>最後一行的指令有個重點，就是我們必須先使用gloud auth login的指令讓CLI工具登入，接著才能執行CloudRun的Deploy指令，也就是說要執行的指令有兩個，所以使用了「&amp;&amp;」這個管道符號讓它接續執行，但是直接這樣接在Image Repository後面是行不通的，「&amp;&amp;」符號的前面會跟最前面的docker run指令合起來作為第一個指令，後面的則是host環境接續docker run指令執行的第二個指令。</p>



<p>所以在這個地方必須是讓docker run執行起來的contianer是執行bash程式，後面接著要執行的指令字串(用引號包起來)，也因為是一整個字串，所以沒辦法使用「\\」換行，就會是一行很長的指令，下面為了方便閱讀，把它們拆開來說明。</p>



<pre class="wp-block-prismatic-blocks"><code class="language-bash">gcloud auth login --cred-file=/gcp/cloudKey.json</code></pre>



<p>「&amp;&amp;」符號前的這一行是將gcloud CLI工具登入，使用&#8211;cred-file參數帶入Json檔案，後面的路徑是Container內的路徑，也就是前面-v設定的部份。</p>



<pre class="wp-block-prismatic-blocks"><code class="language-batch">gcloud run deploy $(cloudRunServiceName) --set-env-vars=Ironman=$(Build.BuildId) --image $(imgRepository) --region $(cloudRunRegion) --project $(cloudRunProjectId) --allow-unauthenticated</code></pre>



<p>「&amp;&amp;」符號後面的指令我依參數拆行來看應該就很清楚，因為大部份都是上面設定的變數，已經有說明過了。</p>



<p>&#8211;set-env-vars的參數是設定CloudRun放入的環境變數(還記得前面的Ironman環境變數嗎？)，&#8211;allow-unauthenticated則是允許訪客瀏覽，不然CloudRun可能不會正常回應頁面。</p>



<p>gcloud run deploy $(cloudRunServiceName) <br>&#8211;set-env-vars=Ironman=$(Build.BuildId) <br>&#8211;image $(imgRepository) <br>&#8211;region $(cloudRunRegion) <br>&#8211;project $(cloudRunProjectId) <br>&#8211;allow-unauthenticated</p>



<p>關於CloudRun在gcloud CLI可以設定的更多參數部份，請參考<a href="https://cloud.google.com/sdk/gcloud/reference/run/deploy" target="_blank" rel="noreferrer noopener">官方文件的頁面</a>，之後的文章還會把部份參數用上。</p>



<p>最後，在CI Pipeline成功執行完之後，就可以在對應的Task log中看到gcloud CLI工具吐出來的CloudRun網址，這樣就不用進入到GCP的管理介面去查看。</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="689" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/04-DeployCloudRunLog-1024x689.png?resize=1024%2C689&#038;ssl=1" alt="" class="wp-image-1940" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/04-DeployCloudRunLog.png?resize=1024%2C689&amp;ssl=1 1024w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/04-DeployCloudRunLog.png?resize=300%2C202&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/04-DeployCloudRunLog.png?resize=768%2C516&amp;ssl=1 768w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/04-DeployCloudRunLog.png?w=1316&amp;ssl=1 1316w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure></div>


<p>最後補充一個小細節，就是在這三個Job之中，只有BuildCode這個Job中有明確的加上checkout: sources，並且身份驗證的Json檔案是放在Pipelines這個Git Repository裡面，但是在後面的BuildImage和DeployCloudRun並沒有明確加上checkout動作卻會(可以)取得Pipelines這個Git Repository裡面的檔案，也沒有在resources.repositories底下設定Pipelines，主要是因為這裡的Pipeline YAML檔案就是放在Pipelines這個Git Repository裡面，所以隱含了checkout: self這個動作，替我們省下了一些設定。(下圖紅框與藍框的差異)</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="336" height="1024" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/05-DefaultCheckoutSelfRepo-336x1024.png?resize=336%2C1024&#038;ssl=1" alt="" class="wp-image-1941" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/05-DefaultCheckoutSelfRepo.png?resize=336%2C1024&amp;ssl=1 336w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/05-DefaultCheckoutSelfRepo.png?resize=98%2C300&amp;ssl=1 98w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/05-DefaultCheckoutSelfRepo.png?resize=504%2C1536&amp;ssl=1 504w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/05-DefaultCheckoutSelfRepo.png?w=547&amp;ssl=1 547w" sizes="auto, (max-width: 336px) 100vw, 336px" /></figure></div><p>這篇文章 <a href="https://tech.uccu.website/2022ironman-day10-ci-pipeline-deploy-dev-to-cloudrun.html">【2022鐵人賽】基本版-建立CI Pipeline(3)</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://tech.uccu.website/2022ironman-day10-ci-pipeline-deploy-dev-to-cloudrun.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1930</post-id>	</item>
		<item>
		<title>【2022鐵人賽】Google CloudRun介紹</title>
		<link>https://tech.uccu.website/2022ironman-day5-cloudrun-overview.html?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=2022ironman-day5-cloudrun-overview</link>
					<comments>https://tech.uccu.website/2022ironman-day5-cloudrun-overview.html#respond</comments>
		
		<dc:creator><![CDATA[鳴人]]></dc:creator>
		<pubDate>Tue, 20 Sep 2022 15:36:31 +0000</pubDate>
				<category><![CDATA[2022鐵人賽]]></category>
		<category><![CDATA[Google雲端GCP]]></category>
		<category><![CDATA[iThome鐵人賽]]></category>
		<category><![CDATA[雲端]]></category>
		<category><![CDATA[2022ironman]]></category>
		<guid isPermaLink="false">https://tech.uccu.website/?p=1877</guid>

					<description><![CDATA[<p>這篇文章的標題雖然是寫「Google CloudRun介紹」，但是其實內容並沒有打算對它做深入的介紹，只是在繼 ... <a title="【2022鐵人賽】Google CloudRun介紹" class="read-more" href="https://tech.uccu.website/2022ironman-day5-cloudrun-overview.html" aria-label="Read more about 【2022鐵人賽】Google CloudRun介紹">閱讀全文</a></p>
<p>這篇文章 <a href="https://tech.uccu.website/2022ironman-day5-cloudrun-overview.html">【2022鐵人賽】Google CloudRun介紹</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>這篇文章的標題雖然是寫「Google CloudRun介紹」，但是其實內容並沒有打算對它做深入的介紹，只是在繼續後面的文章內容之前必須先提到它一下，因為在「<a href="https://tech.uccu.website/2022ironman-day3-flow-plan.html" target="_blank" rel="noreferrer noopener">流程規劃說明</a>」的內容中提到CI、CD Pipeline佈署的Dev、Stage、Production這三個環境都是放在CloudRun。</p>



<p>那什麼是CloudRun呢？</p>



<p>它是Google雲端服務的一個項目，是一個由Google全代管的無伺服器平台服務，讓我們可以利用建置好的Docker Image(只是其中一種方式)就可以佈署我們的網站，而且它只在有流量進入的時候被啟動/執行，還可以根據流量大小在我們設定的允許範圍內動態增減執行個體的數量，以滿足網站不同時段的流量需求。</p>



<p>除此之外，每一次的佈署都會有版本保留下來，除了方便在發生問題的時候倒回版本之外，也可以設定不同流量百分比導向不同的版本，在佈署策略上也彈性許多。</p>



<p>因為後面的文章實際上是使用gcloud cli的方式將Docker Image佈署到CloudRun，所以下面只是快速的透過UI帶過手動建立CloudRun服務的過程。在這之前，我已經先透過VS建立了一個Web專案範本並且建置了一個Docker Image，這部份就不在文章中特別帶到了。</p>



<p>我在首頁中寫了一行程式碼讀取一個名為Ironman的環境變數，為的就是示範Build同一份程式碼/Docker Image，用在多個不同的環境。</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="274" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/00-WebIndexPrintEnvVar-1024x274.png?resize=1024%2C274&#038;ssl=1" alt="" class="wp-image-1878" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/00-WebIndexPrintEnvVar.png?resize=1024%2C274&amp;ssl=1 1024w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/00-WebIndexPrintEnvVar.png?resize=300%2C80&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/00-WebIndexPrintEnvVar.png?resize=768%2C206&amp;ssl=1 768w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/00-WebIndexPrintEnvVar.png?w=1296&amp;ssl=1 1296w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure></div>


<p>前置作業已經先準備了一個Docker Image，所以就直接從建立CloudRun開始吧！</p>



<p>進入GCP的管理介面之後，在選單中找到CloudRun選項(<a href="https://console.cloud.google.com/run?hl=zh-TW" target="_blank" rel="noreferrer noopener">任意門在這</a>)，進入之後選擇「建立服務」。</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="266" height="227" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/01-Menu-CloudRun.png?resize=266%2C227&#038;ssl=1" alt="" class="wp-image-1879"/></figure></div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="372" height="99" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/02-CreateNewService.png?resize=372%2C99&#038;ssl=1" alt="" class="wp-image-1880" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/02-CreateNewService.png?w=372&amp;ssl=1 372w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/02-CreateNewService.png?resize=300%2C80&amp;ssl=1 300w" sizes="auto, (max-width: 372px) 100vw, 372px" /></figure></div>


<p>接著就可以選擇已經Push到<a href="https://tech.uccu.website/2022ironman-day4-create-registry-and-service-account.html" target="_blank" rel="noreferrer noopener">前一篇文章</a>提到的Google Artifact Registry裡面的Docker Image，按下「選取」之後，「選取容器映像檔」的畫面(第二張圖)就會出現在右邊。</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="570" height="436" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/03-SelectDockerImage.png?resize=570%2C436&#038;ssl=1" alt="" class="wp-image-1881" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/03-SelectDockerImage.png?w=570&amp;ssl=1 570w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/03-SelectDockerImage.png?resize=300%2C229&amp;ssl=1 300w" sizes="auto, (max-width: 570px) 100vw, 570px" /></figure></div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="431" height="317" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/04-ImageInArtifactRegistry.png?resize=431%2C317&#038;ssl=1" alt="" class="wp-image-1882" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/04-ImageInArtifactRegistry.png?w=431&amp;ssl=1 431w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/04-ImageInArtifactRegistry.png?resize=300%2C221&amp;ssl=1 300w" sizes="auto, (max-width: 431px) 100vw, 431px" /></figure></div>


<p>接下來就是CloudRun的詳細設定，不過這邊只是帶過，所以除了額外加了一個環境變數之外，其它幾乎沒什麼異動，就隨意看一下吧！知道有哪些選項可以設定，之後用gcloud cli的時候有印象就行。</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="562" height="843" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/05-CreateNewService-Details1.png?resize=562%2C843&#038;ssl=1" alt="" class="wp-image-1883" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/05-CreateNewService-Details1.png?w=562&amp;ssl=1 562w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/05-CreateNewService-Details1.png?resize=200%2C300&amp;ssl=1 200w" sizes="auto, (max-width: 562px) 100vw, 562px" /></figure></div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="538" height="778" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/06-CreateNewService-Details2.png?resize=538%2C778&#038;ssl=1" alt="" class="wp-image-1884" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/06-CreateNewService-Details2.png?w=538&amp;ssl=1 538w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/06-CreateNewService-Details2.png?resize=207%2C300&amp;ssl=1 207w" sizes="auto, (max-width: 538px) 100vw, 538px" /></figure></div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="525" height="526" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/07-CreateNewService-Details3.png?resize=525%2C526&#038;ssl=1" alt="" class="wp-image-1885" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/07-CreateNewService-Details3.png?w=525&amp;ssl=1 525w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/07-CreateNewService-Details3.png?resize=300%2C300&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/07-CreateNewService-Details3.png?resize=150%2C150&amp;ssl=1 150w" sizes="auto, (max-width: 525px) 100vw, 525px" /></figure></div>


<p>設定完想要設定的項目並且按下「建立」按鈕之後，稍微等它抓一下Image，很快就會Ready了。</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="722" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/08-CloudRunServiceCreating-1024x722.png?resize=1024%2C722&#038;ssl=1" alt="" class="wp-image-1886" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/08-CloudRunServiceCreating.png?resize=1024%2C722&amp;ssl=1 1024w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/08-CloudRunServiceCreating.png?resize=300%2C211&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/08-CloudRunServiceCreating.png?resize=768%2C541&amp;ssl=1 768w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/08-CloudRunServiceCreating.png?w=1202&amp;ssl=1 1202w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure></div>

<div class="wp-block-image">
<figure class="aligncenter size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="533" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/09-CloudRunServiceCreated-1024x533.png?resize=1024%2C533&#038;ssl=1" alt="" class="wp-image-1887" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/09-CloudRunServiceCreated.png?resize=1024%2C533&amp;ssl=1 1024w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/09-CloudRunServiceCreated.png?resize=300%2C156&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/09-CloudRunServiceCreated.png?resize=768%2C400&amp;ssl=1 768w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/09-CloudRunServiceCreated.png?w=1422&amp;ssl=1 1422w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure></div>


<p>每一個CloudRun服務建立之後都會提供一個固定的網址，之後版本持續更新也不會異動，也可以將自訂的網域套用上去，不過這不在這篇文章的範圍，直接點擊它提供的網址看看佈署後的頁面吧！</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="745" height="474" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/10-CloudRunWeb1.png?resize=745%2C474&#038;ssl=1" alt="" class="wp-image-1888" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/10-CloudRunWeb1.png?w=745&amp;ssl=1 745w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/10-CloudRunWeb1.png?resize=300%2C191&amp;ssl=1 300w" sizes="auto, (max-width: 745px) 100vw, 745px" /></figure></div>


<p>上圖有正確的將環境變數設定的值列出來，雖然畫面有點醜(因為我沒有把css那些都放進去)，不過正確顯示才是最重要的，對吧！</p>



<p>接著使用同一個Image Repository，只修改設定的環境變數值，看看是不是能正確的顯示不同的內容吧！</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="525" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/11-CloudRunServiceVersion2-1024x525.png?resize=1024%2C525&#038;ssl=1" alt="" class="wp-image-1889" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/11-CloudRunServiceVersion2.png?resize=1024%2C525&amp;ssl=1 1024w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/11-CloudRunServiceVersion2.png?resize=300%2C154&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/11-CloudRunServiceVersion2.png?resize=768%2C394&amp;ssl=1 768w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/11-CloudRunServiceVersion2.png?w=1384&amp;ssl=1 1384w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure></div>


<p>從上圖可以看到已經改為第二個版本，並且環境變數的內容文字也稍有不同，重新整理剛剛打開的網頁之後，文字內容也有如期望的更改了。</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="670" height="467" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/12-CloudRunWeb2.png?resize=670%2C467&#038;ssl=1" alt="" class="wp-image-1890" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/12-CloudRunWeb2.png?w=670&amp;ssl=1 670w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/12-CloudRunWeb2.png?resize=300%2C209&amp;ssl=1 300w" sizes="auto, (max-width: 670px) 100vw, 670px" /></figure></div>


<p>是不是很方便呢？之後的文章中將會透過gcloud cli的方式來佈署，這樣才能夠整合在CI/CD Pipeline之中，拭目以待吧！</p>
<p>這篇文章 <a href="https://tech.uccu.website/2022ironman-day5-cloudrun-overview.html">【2022鐵人賽】Google CloudRun介紹</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://tech.uccu.website/2022ironman-day5-cloudrun-overview.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1877</post-id>	</item>
		<item>
		<title>【2022鐵人賽】Docker Image存放位置選擇：Google Artifact Registry</title>
		<link>https://tech.uccu.website/2022ironman-day4-create-registry-and-service-account.html?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=2022ironman-day4-create-registry-and-service-account</link>
					<comments>https://tech.uccu.website/2022ironman-day4-create-registry-and-service-account.html#respond</comments>
		
		<dc:creator><![CDATA[鳴人]]></dc:creator>
		<pubDate>Mon, 19 Sep 2022 15:27:50 +0000</pubDate>
				<category><![CDATA[2022鐵人賽]]></category>
		<category><![CDATA[Docker]]></category>
		<category><![CDATA[Google雲端GCP]]></category>
		<category><![CDATA[iThome鐵人賽]]></category>
		<category><![CDATA[2022ironman]]></category>
		<category><![CDATA[docker]]></category>
		<guid isPermaLink="false">https://tech.uccu.website/?p=1855</guid>

					<description><![CDATA[<p>上一篇「流程規劃說明」有提到在CI Pipeline的時候我們會Build docker image，所以自然 ... <a title="【2022鐵人賽】Docker Image存放位置選擇：Google Artifact Registry" class="read-more" href="https://tech.uccu.website/2022ironman-day4-create-registry-and-service-account.html" aria-label="Read more about 【2022鐵人賽】Docker Image存放位置選擇：Google Artifact Registry">閱讀全文</a></p>
<p>這篇文章 <a href="https://tech.uccu.website/2022ironman-day4-create-registry-and-service-account.html">【2022鐵人賽】Docker Image存放位置選擇：Google Artifact Registry</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>上一篇「<a href="https://tech.uccu.website/2022ironman-day3-flow-plan.html" target="_blank" rel="noreferrer noopener">流程規劃說明</a>」有提到在CI Pipeline的時候我們會Build docker image，所以自然也就要幫Image找尋存放的位置囉！</p>



<p>Docker Image存放位置的選擇其實也很多，像是放在Docker Hub、Azure Container Registry(ACR)、Google Container Registry(GCR)/Google Artifact Registry(GAR)、Amazon Elastic Container Registry(ECR)等等…，這些雲端服務都是可以選擇的選項。</p>



<p>不過，因為我們後面使用的是Google的Cloud Run，所以為了有最佳的網路傳輸與最節省成本的考量(主要還是網路輸出的費用部份)，所以就直接選擇放在Google的雲端服務上面。</p>



<p>然而，Google現在可以存放Docker Image的服務其實有兩個，就像上面寫的，有分為Google Container Registry和Google Artifact Registry，這兩者其實對於存放Docker Image沒有什麼太大的差別，主要應該只是URI的部份不一樣。如果真要說的話，GCR就如同它的名稱一樣，就是拿來放Image的，而GAR則是綜合存放庫的服務，除了可以放Image之外，也可以是程式的套件庫，就像.Net的Nuget套件庫一樣。</p>



<p>GAR是由GCR演進而來(官網就是這麼寫的)，所以GAR是新的服務，會不會在之後取代GCR很難說…，所以在裡面既然要建立新的存放空間，那就直接從新的服務建立吧！</p>



<h2 class="wp-block-heading">建立Artifact Registry存放區</h2>



<p>進到Google Cloud的管理頁面之後，從左邊的選單找到「Artifact Registry」吧(或是你想要<a href="https://console.cloud.google.com/artifacts?hl=zh-TW" target="_blank" rel="noreferrer noopener">任意門</a>)！如果找不到的話，應該是被隱藏在最下面的「其他產品」裡面。</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="384" height="1024" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/GCP-Menu-OtherProducts-384x1024.png?resize=384%2C1024&#038;ssl=1" alt="" class="wp-image-1856" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/GCP-Menu-OtherProducts.png?resize=384%2C1024&amp;ssl=1 384w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/GCP-Menu-OtherProducts.png?resize=113%2C300&amp;ssl=1 113w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/GCP-Menu-OtherProducts.png?w=489&amp;ssl=1 489w" sizes="auto, (max-width: 384px) 100vw, 384px" /></figure></div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="509" height="417" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/GCP-Menu-ArtifactRegistry.png?resize=509%2C417&#038;ssl=1" alt="" class="wp-image-1857" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/GCP-Menu-ArtifactRegistry.png?w=509&amp;ssl=1 509w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/GCP-Menu-ArtifactRegistry.png?resize=300%2C246&amp;ssl=1 300w" sizes="auto, (max-width: 509px) 100vw, 509px" /></figure></div>


<p>進入Artifact Registry之後，找到「建立存放區」，接著填寫簡單的資料就可以建立一個新的存放區了，記得格式要選「Docker」，地區的部份輸入「asia」就會篩選出台灣的選項了。</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="717" height="155" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/GCP-ArtifactRegistry-CreateStorage.png?resize=717%2C155&#038;ssl=1" alt="" class="wp-image-1858" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/GCP-ArtifactRegistry-CreateStorage.png?w=717&amp;ssl=1 717w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/GCP-ArtifactRegistry-CreateStorage.png?resize=300%2C65&amp;ssl=1 300w" sizes="auto, (max-width: 717px) 100vw, 717px" /></figure></div>

<div class="wp-block-image">
<figure class="aligncenter size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="660" height="1024" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/GCP-ArtifactRegistry-CreateStorage-Details-660x1024.png?resize=660%2C1024&#038;ssl=1" alt="" class="wp-image-1859" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/GCP-ArtifactRegistry-CreateStorage-Details.png?resize=660%2C1024&amp;ssl=1 660w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/GCP-ArtifactRegistry-CreateStorage-Details.png?resize=193%2C300&amp;ssl=1 193w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/GCP-ArtifactRegistry-CreateStorage-Details.png?resize=768%2C1192&amp;ssl=1 768w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/GCP-ArtifactRegistry-CreateStorage-Details.png?w=841&amp;ssl=1 841w" sizes="auto, (max-width: 660px) 100vw, 660px" /></figure></div>


<p>建立完之後從列表中就可以看到剛才建立的項目，點進去之後就會看到Image Repository的URI了，旁邊很貼心的放了一個直接複製的按鈕。</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/GCP-ArtifactRegistry-StorageList.png?ssl=1"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="108" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/GCP-ArtifactRegistry-StorageList-1024x108.png?resize=1024%2C108&#038;ssl=1" alt="" class="wp-image-1860" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/GCP-ArtifactRegistry-StorageList.png?resize=1024%2C108&amp;ssl=1 1024w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/GCP-ArtifactRegistry-StorageList.png?resize=300%2C32&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/GCP-ArtifactRegistry-StorageList.png?resize=768%2C81&amp;ssl=1 768w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/GCP-ArtifactRegistry-StorageList.png?resize=1536%2C161&amp;ssl=1 1536w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/GCP-ArtifactRegistry-StorageList.png?w=1694&amp;ssl=1 1694w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></figure></div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="829" height="168" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/GCP-ArtifactRegistry-CopyURI.png?resize=829%2C168&#038;ssl=1" alt="" class="wp-image-1861" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/GCP-ArtifactRegistry-CopyURI.png?w=829&amp;ssl=1 829w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/GCP-ArtifactRegistry-CopyURI.png?resize=300%2C61&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/GCP-ArtifactRegistry-CopyURI.png?resize=768%2C156&amp;ssl=1 768w" sizes="auto, (max-width: 829px) 100vw, 829px" /></figure></div>


<p>到這邊雖然已經將存放Image的Artifact Registry存放區建立好，但是其實還有一個很重要的事情，那就是身份驗證與授權的部份。如果沒有登入的話是沒辦法將Image推送上來的，所以我們還需要建立對應的服務帳戶。</p>



<h2 class="wp-block-heading">建立服務帳戶</h2>



<p>同樣在Google Cloud左邊的Menu找到「IAM與管理」(<a href="https://console.cloud.google.com/iam-admin/iam?hl=zh-TW" target="_blank" rel="noreferrer noopener">任意門在此</a>)，進入之後點選左邊的「服務帳戶」，然後在右邊的上方點擊「建立服務帳戶」。</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="968" height="575" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-CreateServiceAccount.png?resize=968%2C575&#038;ssl=1" alt="" class="wp-image-1862" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-CreateServiceAccount.png?w=968&amp;ssl=1 968w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-CreateServiceAccount.png?resize=300%2C178&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-CreateServiceAccount.png?resize=768%2C456&amp;ssl=1 768w" sizes="auto, (max-width: 968px) 100vw, 968px" /></figure></div>


<p>需要填寫的步驟不多，不過在第二步的部份雖然它是寫「選用」，不過我們還是直接把後續會需要使用到的角色權限設定給它吧！分別是「Artifact Registry 寫入者」、「Cloud Run 開發人員」。</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="863" height="917" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-CreateServiceAccount-Details1.png?resize=863%2C917&#038;ssl=1" alt="" class="wp-image-1863" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-CreateServiceAccount-Details1.png?w=863&amp;ssl=1 863w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-CreateServiceAccount-Details1.png?resize=282%2C300&amp;ssl=1 282w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-CreateServiceAccount-Details1.png?resize=768%2C816&amp;ssl=1 768w" sizes="auto, (max-width: 863px) 100vw, 863px" /></figure></div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="726" height="728" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-CreateServiceAccount-Details2.png?resize=726%2C728&#038;ssl=1" alt="" class="wp-image-1864" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-CreateServiceAccount-Details2.png?w=726&amp;ssl=1 726w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-CreateServiceAccount-Details2.png?resize=300%2C300&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-CreateServiceAccount-Details2.png?resize=150%2C150&amp;ssl=1 150w" sizes="auto, (max-width: 726px) 100vw, 726px" /></figure></div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="809" height="841" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-CreateServiceAccount-Details3.png?resize=809%2C841&#038;ssl=1" alt="" class="wp-image-1865" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-CreateServiceAccount-Details3.png?w=809&amp;ssl=1 809w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-CreateServiceAccount-Details3.png?resize=289%2C300&amp;ssl=1 289w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-CreateServiceAccount-Details3.png?resize=768%2C798&amp;ssl=1 768w" sizes="auto, (max-width: 809px) 100vw, 809px" /></figure></div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="829" height="771" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-CreateServiceAccount-Details4.png?resize=829%2C771&#038;ssl=1" alt="" class="wp-image-1866" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-CreateServiceAccount-Details4.png?w=829&amp;ssl=1 829w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-CreateServiceAccount-Details4.png?resize=300%2C279&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-CreateServiceAccount-Details4.png?resize=768%2C714&amp;ssl=1 768w" sizes="auto, (max-width: 829px) 100vw, 829px" /></figure></div>


<h2 class="wp-block-heading">產生授權金鑰</h2>



<p>建立完成之後，從列表中點擊剛剛建立的服務帳戶，進入之後點擊在上方的「金鑰」分頁。</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="94" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-ServiceAccountList-1024x94.png?resize=1024%2C94&#038;ssl=1" alt="" class="wp-image-1867" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-ServiceAccountList.png?resize=1024%2C94&amp;ssl=1 1024w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-ServiceAccountList.png?resize=300%2C28&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-ServiceAccountList.png?resize=768%2C71&amp;ssl=1 768w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-ServiceAccountList.png?resize=1536%2C141&amp;ssl=1 1536w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-ServiceAccountList.png?w=1912&amp;ssl=1 1912w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure></div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="878" height="901" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-ServiceAccount-ClickKeysTab.png?resize=878%2C901&#038;ssl=1" alt="" class="wp-image-1868" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-ServiceAccount-ClickKeysTab.png?w=878&amp;ssl=1 878w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-ServiceAccount-ClickKeysTab.png?resize=292%2C300&amp;ssl=1 292w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-ServiceAccount-ClickKeysTab.png?resize=768%2C788&amp;ssl=1 768w" sizes="auto, (max-width: 878px) 100vw, 878px" /></figure></div>


<p>進入金鑰頁面之後，點擊「新增金鑰」後選擇「建立新的金鑰」，接著確認金鑰類型是JSON，按下「建立」按鈕之後，就會讓你下載json格式的金鑰檔案，務必要保存好。</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="709" height="674" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-ServiceAccount-CreateNewPrivateKey.png?resize=709%2C674&#038;ssl=1" alt="" class="wp-image-1869" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-ServiceAccount-CreateNewPrivateKey.png?w=709&amp;ssl=1 709w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-ServiceAccount-CreateNewPrivateKey.png?resize=300%2C285&amp;ssl=1 300w" sizes="auto, (max-width: 709px) 100vw, 709px" /></figure></div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="752" height="477" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-ServiceAccount-CreateNewPrivateKey-JSONKey.png?resize=752%2C477&#038;ssl=1" alt="" class="wp-image-1870" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-ServiceAccount-CreateNewPrivateKey-JSONKey.png?w=752&amp;ssl=1 752w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2022/09/IAM-ServiceAccount-CreateNewPrivateKey-JSONKey.png?resize=300%2C190&amp;ssl=1 300w" sizes="auto, (max-width: 752px) 100vw, 752px" /></figure></div>


<h2 class="wp-block-heading">Docker Login</h2>



<p>使用上面產生的金鑰就可以透過docker login指令登入Registry囉！登入的使用者名稱固定為「_json_key」，密碼的部份就是整個json檔案的內容，下面提供一個相對安全的指令登入方式：</p>



<pre class="wp-block-prismatic-blocks"><code class="language-bash">cat {Your_Json_File} | docker login -u _json_key --password-stdin https://asia-east1-docker.pkg.dev</code></pre>



<p>把上面的{Your_Json_File}的部份取代成下載的json檔名，如果剛剛建立存放區不是選台灣的機房的話，最後面的網址再根據後台顯示的內容修改即可。</p>



<p>登入之後就可以試一下可不可以push docker image，可以從docker hub上面pull一個下來，然後透過docker tag重新更改image repository名稱和tag，就可以push看看囉！</p>
<p>這篇文章 <a href="https://tech.uccu.website/2022ironman-day4-create-registry-and-service-account.html">【2022鐵人賽】Docker Image存放位置選擇：Google Artifact Registry</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://tech.uccu.website/2022ironman-day4-create-registry-and-service-account.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1855</post-id>	</item>
		<item>
		<title>Azure VM 開啟 Ping 網路規則</title>
		<link>https://tech.uccu.website/enable-azure-vm-ping.html?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=enable-azure-vm-ping</link>
					<comments>https://tech.uccu.website/enable-azure-vm-ping.html#respond</comments>
		
		<dc:creator><![CDATA[鳴人]]></dc:creator>
		<pubDate>Mon, 26 Oct 2020 03:46:03 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[微軟雲端Azure]]></category>
		<category><![CDATA[雲端]]></category>
		<guid isPermaLink="false">https://tech.uccu.website/?p=118</guid>

					<description><![CDATA[<p>Ping這個網路工具(指令)是時常被拿來測試網路是否通順的功能，除了可以測試機器的網路是否正常之外，有時候還會 ... <a title="Azure VM 開啟 Ping 網路規則" class="read-more" href="https://tech.uccu.website/enable-azure-vm-ping.html" aria-label="Read more about Azure VM 開啟 Ping 網路規則">閱讀全文</a></p>
<p>這篇文章 <a href="https://tech.uccu.website/enable-azure-vm-ping.html">Azure VM 開啟 Ping 網路規則</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>Ping這個網路工具(指令)是時常被拿來測試網路是否通順的功能，除了可以測試機器的網路是否正常之外，有時候還會拿來觀察網路延遲的時間。</p>



<p>這次的需求是為了知道簡單網路行為的耗用時間，也就是延遲時間，所以透過Ping這個簡單的網路測試功能來獲得Client端到Server端的網路耗用時間(ms)，用在計算某些服務耗用時間的扣除基準。</p>



<p>不過，雲端服務的防火牆預設是不開放任何網路封包從外面進入，必須根據需求在網路規則介面上建立，像是常用到的Web 80 Port，或是Windows遠端連線RDP的3389，所以這次需要在網路規則中加上Ping的允許規則。</p>



<span id="more-118"></span>



<p>Ping的網路規則和一般開啟Port Number不太一樣，Ping是透過ICMP協定傳送的封包，所以不是像一般設定TCP/UDP的規則，Port Number的部份不需要指定，直接填入*即可。</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="570" height="878" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/10/35d3f6d4-image.png?resize=570%2C878&#038;ssl=1" alt="" class="wp-image-119" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/10/35d3f6d4-image.png?w=570&amp;ssl=1 570w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/10/35d3f6d4-image.png?resize=195%2C300&amp;ssl=1 195w" sizes="auto, (max-width: 570px) 100vw, 570px" /></figure></div>



<p>如果是Linux的VM，可以輸入<code>cat /proc/sys/net/ipv4/icmp_echo_ignore_all</code>，看看顯示的值是0(啟用Ping回應)還是1(停用Ping回應)，如果是0的話，應該就可以正常Ping得到該台VM才是。</p>



<h3 class="wp-block-heading">【參考來源】</h3>



<ul class="wp-block-list"><li><a href="https://www.netadmin.com.tw/netadmin/zh-tw/technology/111381F2995A4AB48672E965F63133AE" target="_blank" rel="noreferrer noopener nofollow">搞懂ICMP協定及工具 抵擋「死亡之Ping」攻擊</a></li><li><a href="https://zh.wikipedia.org/zh-tw/%E4%BA%92%E8%81%94%E7%BD%91%E6%8E%A7%E5%88%B6%E6%B6%88%E6%81%AF%E5%8D%8F%E8%AE%AE%E7%AC%AC%E5%85%AD%E7%89%88" target="_blank" rel="noreferrer noopener nofollow">網際網路控制訊息協定第六版</a></li><li><a href="https://www.thomasmaurer.ch/2019/09/how-to-enable-ping-icmp-echo-on-an-azure-vm/" target="_blank" rel="noreferrer noopener nofollow">HOW TO ENABLE PING (ICMP ECHO) ON AN AZURE VM</a></li><li><a href="https://monovm.com/post/38/enable-ping-in-linux">enable ping in linux</a></li></ul>



<p></p>
<p>這篇文章 <a href="https://tech.uccu.website/enable-azure-vm-ping.html">Azure VM 開啟 Ping 網路規則</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://tech.uccu.website/enable-azure-vm-ping.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">118</post-id>	</item>
	</channel>
</rss>
