Try   HackMD

Container Registry


目錄

  1. Docker
  2. 倉庫
  3. Google Container Registry
  4. 實作

1. Docker


Docker

Docker 容器的啟動時間是秒級的,大量節省開發、測試、部署的時間。

  • 映像檔(Image):
    映像檔可以用來建立 Docker 容器。

  • 容器(Container):
    它可以被啟動、開始、停止、刪除,每個容器都是相互隔離、保證安全的。

  • 倉庫 (Registry):
    是存放Docker Image的倉庫


Docker 工作流程圖

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


Docker 常用指令

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


2. 倉庫


倉庫

Repository (倉庫) 是集中存放映像檔的地方
Registry (倉庫註冊伺服器) 是存放與管理多個倉庫

倉庫類型:公開(Public)、私有(Private)

  • 公開倉庫: Docker Hub
  • 私有倉庫: Gitlab、harbor
    Google Container Registry (GCR)
    Azure Container Registry (ACR)
    Amazon Elastic Container Registry (ECR)

3. Google Container Registry


GCR 功能與特色

  1. 安全的私人 Docker Registry

  2. 自動建立及部署映像檔

  3. 深度安全漏洞掃描

  4. 快速搜尋與容器標記

  5. 快速的高可用性存取


GCR 名稱格式

  • 以HOSTNAME和 projectID 命名
  • 格式:
    HOSTNAME/PROJECT-ID/IMAGE:TAG
    HOSTNAME/PROJECT-ID/IMAGE@IMAGE-DIGEST
    ex:gcr.io/sc-lab-2/nginxhello2:1.1
  • HOSTNAME 儲存映像檔位置
    gcr.io 目前在美國託管映像檔,但此託管位置未來可能會發生變化
    us.gcr.io 在美國託管映像檔,與gcr.io使用的bucket不同
    eu.gcr.io 在歐盟託管映像檔
    asia.gcr.io 在亞洲託管映像檔

漏洞掃描(Vulnerability scanning)

  • Container Registry中透過Container Analysis進行映像檔執行漏洞掃描,並監控漏洞資訊以使其保持最新狀態。

  • 兩個主要任務:
    1.新增掃描:將映像檔上傳到Container Registry
    2.持續分析:當Container Analysis從漏洞來源接收新的和更新的漏洞資訊時,它會重新分析容器

  • 支援作業系統 Alpine、CentOS、Debian、RedHat 和 Ubuntu 的各不同版本掃瞄。


容器分析(Container Analysis)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


容器分析(Container Analysis) 時代演進

  • 2017年11月22日 容器分析在Alpha中可用
  • 2018年 9月 6日 容器分析提供Beta版
  • 2019年 8月26日 容器分析API的V1版本現已全面上市
  • 2019年 9月20日 容器分析現已全面推出
  • 2020年 3月16日 Artifact Registry是Container Registry的演進版本,現已提供Beta版。

漏洞的嚴重性級別

  • Container Analysis 使用以下嚴重級別:
    Critical (嚴重)
    High (高)
    Medium (中)
    Low (低)
    Minimal (輕微)

  • 嚴重級別是反映漏洞被利用可能性、影響、範圍和成熟度等因素的等級


  • 每種漏洞都有兩種關聯的嚴重程度指標:

    1.有效嚴重程度 - 嚴重級別由Linux 發行版指定。

    2.CVSS評分 - 通用漏洞評分系統來進行評分。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


GCR 權限

  • 啟用Container Registry 的漏洞掃描服務帳號會自動添加以三下種

[PROJECT-NUMBER]@containerregistry.iam.gserviceaccount.com

service-[PROJECT_NUMBER]@container-analysis.iam.gserviceaccount.com

service-[PROJECT_NUMBER]@gcp-sa-containerscanning.iam.gserviceaccount.com

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


Cloud Storage角色和權限

  • GCR使用Storage作為容器映像檔底層儲存空間
  • 默認服務帳戶的後綴為
    [PROJECT_NUMBER]-compute@developer.gserviceaccount.com

GCR 費用

  • Storage 存儲空間
  • Docker 映像檔使用的輸出流量
  • Docker 映像檔的漏洞掃描(選擇啟用此服務)

映像檔的漏洞掃描費用

  • 每個掃描映像檔 $0.26 美元2020/9/01起,完整計費
  • 會自動掃描各個新推送的映像檔
  • 如要掃描現有的映像檔,您必須重新推送
  • 映像檔中新增、編輯標記均不會產生費用

預估漏洞掃描费用

  • 可使用Cloud Shell 開啟Open Editor新增 cost-estimation.sh文件,內容如下:

執行 bash cost-estimation.sh文件


實作

  • 前置作業
  • Docker run nginx
  • Docker build image
  • Docker push GCR
  • Deploy to GCE

實作 - 前置作業

  1. GCP專案需挷定帳單
  2. 開通storage 推送、拉取的權限

  1. GCR需進行身份驗證,gcloud作為Docker憑證,以便直接在gcloud 中配合Docker 使用

gcloud auth login


gcloud auth configure-docker



  1. 啟用 Container Registry API


  1. 啟用 漏洞掃描
    啟用 漏洞掃描

實作1 - Docker run nginx

docker run -d --name nginx -p 80:80 nginx

docker cp containerID:/usr/share/nginx/html/index.html .


實作2 - Docker build image

  1. 新增一個Dockerfile

  1. 執行docker bulid 語法
    docker build -t nginxhello2 .


實作3:Docker push GCR

  1. 標記本地映像:
    docker tag [SOURCE_IMAGE] [HOSTNAME]/[PROJECT-ID]/[IMAGE]

docker tag nginxhello2 gcr.io/sc-lab-2/nginxhello2:1.1


  1. 推送到Container Registry:
    docker push [HOSTNAME]/[PROJECT-ID]/[IMAGE]
    docker push gcr.io/sc-lab-2/nginxhello2:1.1


3. 確認有無推送成功


實作4:Deploy to GCE

  1. Deploy to GCE

  1. 建立完成



參考來源:

官方文件

https://cloud.google.com/container-registry/docs

Docker 工作流程圖

https://kknews.cc/code/vak8np4.html