kran 文件

多環境

-d NAME 會把 config/kran.NAME.yml 疊到 config/kran.yml 上。

config/
├── kran.yml
├── kran.staging.yml
└── kran.eu.yml
kran deploy -d staging
kran logs -d staging -f
kran console -d staging

兩個檔案

基礎檔案寫共通的設定:

# config/kran.yml
image: acme/storefront

registry:
  server: ghcr.io
  username: acme-deploy
  password: <%= ENV["GITHUB_TOKEN"] %>

builder:
  arch: amd64

kubernetes:
  kubeconfig: ~/.kube/prod-east.yml
  context: prod-east
  namespace: storefront

app:
  selector: app=storefront

多環境檔案只寫不一樣的部分:

# config/kran.staging.yml
kubernetes:
  kubeconfig: ~/.kube/stage-eu.yml
  context: stage-eu
  namespace: storefront-staging

app:
  selector: app=storefront-staging
# image 和 registry 沿用基礎檔案,叢集和 namespace 被換掉
$ kran deploy -d staging --dry-run
printf '%s' '[REDACTED]' | docker login ghcr.io -u acme-deploy --password-stdin
docker build --platform linux/amd64 --push -t ghcr.io/acme/storefront:9c1f4d0b7a2e58c3d6f1b8a4e70925d3c8b1a6f2 .
krane render -f config/deploy --current-sha 9c1f4d0b7a2e58c3d6f1b8a4e70925d3c8b1a6f2 --bindings image=ghcr.io/acme/storefront:9c1f4d0b7a2e58c3d6f1b8a4e70925d3c8b1a6f2 | KUBECONFIG=/home/dana/.kube/stage-eu.yml krane deploy storefront-staging stage-eu -f config/deploy/secrets.ejson -

合併規則

# config/kran.yml
builder:
  arch: [amd64, arm64]
# config/kran.staging.yml
builder:
  arch: [amd64]

staging 只會建 linux/amd64

ERB 裡的 destination

destination-d 給的名稱,沒給就是 nil

# config/kran.yml
kubernetes:
  namespace: <%= destination ? "storefront-#{destination}" : "storefront" %>

app:
  selector: app=<%= ["storefront", destination].compact.join("-") %>

檔案必須存在

$ kran deploy -d production
ERROR: Configuration file not found in config/kran.production.yml

環境名稱打錯是該停下來的錯,所以 kran 不會默默退回基礎檔案。差異都交給上面的 ERB 處理時,空檔案 就夠:

: > config/kran.staging.yml

整塊覆寫

staging 要推到別的 registry 的話,整個區塊覆寫:

# config/kran.staging.yml
registry:
  server: registry.internal
  username: ci
  password: <%= ENV["INTERNAL_REGISTRY_TOKEN"] %>

registry 是 hash,逐鍵合併,所以少寫 username 就會沿用基礎檔案的 acme-deploy

別名

別名是合併之後才讀的,所以多環境檔案可以加或換掉其中一個:

# config/kran.staging.yml
aliases:
  console: exec --interactive bin/rails console --sandbox

同一份 image 部署到多個環境

kran build push --version "$GITHUB_SHA"
kran deploy --version "$GITHUB_SHA" -P -d staging
kran deploy --version "$GITHUB_SHA" -P

-P 會略過建置與推送,所以後兩行部署的正是第一行產生的那份 image。