Sealer 0.9 :幫助集羣和分佈式應用實現 Build、 Share、Run

作者:sealer 社區

新春之際,很高興在此時宣佈 Sealer [ 1] 0.9 版本的正式發佈。Sealer 是一款致力於探索麪向分佈式應用的快速打包、交付和運行的解決方案。2021 年5月 Sealer 項目正式開源;短短一年時間,Sealer 在 2022 年 4 月成爲 CNCF Sandbox 項目。

Sealer 0.9 版本中,其在覈心定位上由專注 “集羣維度的 Build,Share,Run” 升級爲 “以集羣爲基礎,以應用爲中心,幫助集羣和分佈式應用實現 Build, Share,Run”,並重點增強了對應用的支持能力。

版本詳解

增強對應用的支持

應用與集羣解耦

Docker Image 很好的解決了單機應用的打包交付問題,而 **Sealer Image [ 2] **嘗試在集羣維度,解決類 Kubernetes 集羣以及分佈式應用的打包交付問題。Sealer Image 基於包含內容將 Sealer Image 分爲集羣鏡像和應用鏡像。其中:

  • 集羣鏡像,表示一個鏡像包含 cluster runtime,如 Kubernetes、K0S、K3S 等。Run 階段既會安裝集羣也會安裝應用(如果包含了應用)
  • 應用鏡像,表示一個鏡像未包含 cluster runtime,其需要基於已有的集羣進行安裝。
Kubefile 支持 LABEL & KUBEVERSION 指令

Kubefile 中 LABEL 指令類似 Dockerfile LABEL 指令,允許用戶針對 Sealer Image 指定一些元數據信息,這些元數據信息在構建完成後可通過 sealer inspect 命令查看。

其中,Sealer 官方保留以 *sealer.io/ 爲前綴的 label key,如:
針對集羣鏡像,通過 LABEL 可聲明集羣鏡像支持的能力列表等信息

  • cluster.alpha.sealer.io/cluster-runtime-type,支持的集羣類型,如 kubernetes, k0s, k3s 等
  • cluster.alpha.sealer.io/cluster-runtime-version,支持的集羣 runtime 的版本,如 kubernetes 版本
  • cluster.alpha.sealer.io/container-runtime-type,支持的 container runtime 類型,如 docker,containerd 等
  • cluster.alpha.sealer.io/container-runtime-version,支持的 container runtime 版本,如 docker 版本
  • cluster.alpha.sealer.io/kube-cni,支持的 CNI 列表,如 calico,flannel 等
  • cluster.alpha.sealer.io/kube-csi,支持的 CSI 列表,如 alibaba-cloud-csi-driver 等

針對應用鏡像,通過 LABEL 可聲明應用鏡像兼容的 kube runtime 版本等信息,通過聲明解耦集羣鏡像

  • app.alpha.sealer.io/supported-kube-version,應用鏡像支持的 kube runtime 版本,其值定義 **Semantic Versioning 2.0.0 [ 3] **。另外,Kubefile 中也可通過  KUBEVERSION 指令快捷定義該 label 的值。

集羣鏡像 Kubefile:

FROM sealerio/kubernetes:v1.22.15

# Add some metadata to an image just like `Dockerfile LABEL instruction`.
# An image can have more than one label. You can specify multiple labels on a single line.
# 
# NOTE: 
# 1. In general, a base image should declare supported cluster runtimes, container runtimes, etc.
# 2. Sealer reserves all label keys prefixed with `sealer.io`.
# 3. All labels will be inherited by child image.
# LABEL <key>=<value> <key>=<value> <key>=<value> ...
# LABEL <key>=<value> \
#       <key>=<value> \
#       <key>=<value> ...
LABEL "cluster.alpha.sealer.io/cluster-runtime-version"="v1.22.15"
# such as kubernetes, k0s, k3s, etc
LABEL "cluster.alpha.sealer.io/cluster-runtime-type"="kubernetes" 
LABEL "cluster.alpha.sealer.io/container-runtime-type"="docker"
LABEL "cluster.alpha.sealer.io/container-runtime-version"="20.10.14"
......

應用鏡像 Kubefile:

FROM scratch

# A SemVer range of compatible Kubernetes versions by the applications.
# If there are multiple apps in the `Kubefile`, you should take the intersection of the cluster versions they support
# The label `app.alpha.sealer.io/supported-kube-version` will be gernetated with `KUBEVERSION`.
# For a detailed explanation of supported semver constraints see [Masterminds/semver](https://github.com/Masterminds/semver).
KUBEVERSION 1.22 - 1.24

LABEL "created-by"="sealer.io"
......
Build 支持打標鏡像類型

爲了讓使用者在獲取到一個鏡像後,能夠清晰的知道一個鏡像包含的內容以及行爲。在  build image 階段,Sealer 可以通過 sealer build --type kube-installer | app-installer 爲構建出來的 Sealer Image 打標。

  1. kube-installer,集羣鏡像。

1.png

  1. app-installer,應用鏡像。

2.png

Run 簡簡單單運行應用鏡像

針對應用鏡像(sealer build --type app-installer),部署在基於 Sealer 集羣鏡像部署的集羣內,僅需簡單單單一條命令即可,用戶也不需要再關心集羣節點信息等。

TODO: 針對應用鏡像聲明支持的 KUBEVERSION,運行應用鏡像時驗證此版本約束

sealer run {{.appImageName}}

Kubefile 讓應用定義更簡單

Kubefile 新增加APP 指令,其允許用戶定義一個 APP 虛擬對象,通過 CNI、CSI 指令分別支持 CNI,CSI 插件的管理,通過 APPCMDS 指令爲 APP 指定安裝命令,並可以通過 LAUNCH 指令,以 APP 對象爲維度選擇啓動對象。

APP 指令

APP 指令格式 APP APP_NAME scheme:path1 scheme:path2,其中:

  1. APP 指令在 Kubefile 範圍內可定義多次
  2. APP_NAME 在 Sealer Image 範圍內需保證唯一
  3. schema 目前支持如下格式:
    1. local://path_rel_2_build_context (build context 中的文件,文件路徑爲基於 build context 的相對路徑)
    2. http(s)😕/example.yaml (http(s) 遠程地址,會在 build 節點自動下載)
  1. path 的目錄/文件類型目前支持如下類型:
    1. .sh,shell 腳本
    2. .yaml | .yml,kubernetes YAML 文件
    3. Helm Chart 目錄
CNI/CSI 指令

PS: CSI 與 CNI 基本類似,此處不再贅述

CNI 指令格式 CNI CNI_NAME scheme:path1 scheme:path2,通過 CNI 指令可以以一種更規範的方式定義/操作 Kubernetes CNI 插件,其中:

  1. CNI 指令本質上是指定了 CNI 網絡插件類型的 APP,所有特性與 APP 指令相同
  2. CNI 指令相比 APP 指令,會自動生成 label key cluster.alpha.sealer.io/kube-cni 聲明鏡像支持的 CNI 網絡插件列表,例如: "cluster.alpha.sealer.io/kube-cni": ["calico", "flannel"]
APPCMDS 指令

APPCMDS 指令格式 APPCMDS APP_NAME ["executable","param1","param2"],其中:

  1. 類似 Dockerfile CMD 指令,但其作用域爲 APP 維度
  2. 針對同一個 APP_NAME,如果有多個 APPCMDS 被指定,僅最後一個生效
LAUNCH 指令

LAUNCH 指令格式 LAUNCH ["APP_NAME_1", "APP_NAME_2"],其中:

  1. LAUNCH 指令在 Kubefile 範圍內僅可定義一份
  2. 如果 APPCMDS 未指定,則 LAUNCH 指令會嘗試自動解析文件類型,並自動生成執行行爲
  3. FROM 鏡像中包含的 LAUCH 指令不生效,僅最後一個 LAUNCH 指令生效(例如:對於子鏡像,如果需要,需要在子鏡像中重新填寫父鏡像中的  LAUNCH 指令 )
  4. APP_NAME 可以是 Sealer Image 範圍內的,允許指定 FROM 鏡像中聲明的 APP_NAME
Kubefile 預覽

集羣鏡像 Kubefile:

FROM sealerio/kubernetes:v1.22.15

# Override or provide CNI plugins, the syntax is same with `APP`. In fact, we can think of CNI as a special kind of `APP`.
# And multiple `CNI` definitions can be specified. If the name is the same, the last one takes precedence.
# In addition, the CNI plugin is not only an app, but also reflected in the label `cluster.alpha.sealer.io/kube-cni`.
CNI calico local://install-calico.sh
CNI calico local://install-calico-new.sh
CNI flannel local://flannel.yaml

# Override or provide CRI plugins just like `CNI`.
# In addition, the CNI plugin is not only an app, but also reflected in the label `cluster.alpha.sealer.io/kube-csi`.
CSI alibaba-c

應用鏡像 Kubefile:

FROM scratch

# A SemVer range of compatible Kubernetes versions by the applications.
# If there are multiple apps in the `Kubefile`, you should take the intersection of the cluster versions they support
# NOTE: This value will not be inherited.
# The label `app.alpha.sealer.io/supported-kube-version` will be gernetated with `KUBEVERSION`.
# For a detailed explanation of supported semver constraints see [Masterminds/semver](https://github.com/Masterminds/semver).
KUBEVERSION 1.22 - 1.24

# add redis app
# ["kubectl apply -f redis.yaml"] will be executed
APP redis local://redis.yaml

# add nginx app
# ["kubectl apply -f nginx.yaml -n nginx-namespace"] will be executed
APP nginx local://nginx.yaml
# `APPCMDS` specify the cmds for a specified app and the context is the dir of specified app.
# The `APPCMDS` instruction format: ["executable","param1","param2"]
# If there are more than one `APPCMDS` for a `APP` then only the last `APPCMDS` will take effect.
APPCMDS nginx ["kubectl apply -f nginx.yaml -n nginx-namespace"]

# add mysql app
# ["helm install mysql . -f values.yaml --set key=value"] will be executed
APP mysql local://charts/mysql/ local://values.yaml
# `APPCMDS` must be specified when the app has multiple files
APPCMDS mysql ["helm install mysql .",  "-f values.yaml",  "--set key=value"]

LABEL <key>=<value> <key>=<value> <key>=<value> ...
LABEL "created-by"="sealer.io"

# The following operation will be executed:
# ["kubectl apply -f nginx.yaml -n nginx-namespace", "helm install mysql . -f values.yaml --set key=value"]
LAUNCH ["nginx", "mysql"]

Clusterfile 擴展支持應用配置

Clusterfile 中,同樣可以以 APP 爲維度進行運行前的配置變更。

  1. 通過 ClusterSpec.appNames 覆蓋 Kubefile 中的 LAUNCH 指令值,如下所示,Kubefile 中包含了 nginx,mysql 兩個 APP,在 appNames 中只需一條簡單的配置 appNames: [ "nginx" ],即可變更運行鏡像的啓動行爲爲只運行 nginx 這個 APP。
  2. 通過 Application Config,同樣可以非常便捷的覆蓋 Kubefile 中針對 APP 指定的  APPCMDS 內容,在運行態自定義啓動命令。(同時社區也正在推進支持 APP 包含的文件的修改能力,詳情可參考  **#1969 [ 4] ** )
apiVersion: sealer.cloud/v2
kind: Cluster
metadata:
  name: my-cluster
spec:
  image: myimage-with-nginx-mysql:v1
  appNames: [ "nginx" ]
  ......
---
apiVersion: sealer.io/v2
kind: Application
metadata:
  name: my-apps
spec:
    configs:
    - name: app1
      launch:
        cmds:
        - kubectl apply -f ns.yaml
        - kubectl apply -f nginx.yaml -n namespace
    - name: app2
      launch:
        cmds:
        - helm install my-nginx bitnami/nginx
    - name: app3
      launch:
        cmds:
        - bash nginx.sh

其他值得關注點

鏡像引擎升級,Sealer Image 擁抱 OCI 標準

本着追求開放生態的初心,在開放性和標準劃上 Sealer 社區也嘗試更進一步。基於輕量化 **buildah [ 5] **升級 Sealer 鏡像引擎,全面兼容 OCI 標準。

3.png

基於標準的 K8S webhook 能力實現 image policy,不再有 hack

私有化交付場景下,如何將依賴的所有容器鏡像,在用戶無感知的情況下帶入到私有化環境中並使用。

之前版本中, Sealer 社區嘗試通過定製化 docker 配置項,在 docker daemon 層面實現鏡像優先從內置鏡像倉庫拉取,內置鏡像倉庫不存在則從遠程倉庫拉取。

//daemon.json定義 
{
  "mirror-registries":[
      {
        "domain": "*",
        "mirrors": ["http://sea.hub:5000", "https://mirror.test2.com"]
      }
  ],
  "insecure-registries": ["sea.hub:5000", "mirror.test1.com"]
}

上述方案確實很好的解決了鏡像代理的問題,但其有一定的侷限性:

  1. 不兼容用戶已有 docker,要求環境內不存在或者卸載 docker
  2. 針對自定義 Docker 需求,同樣需要進行如上的定製化配置
  3. 上述方案僅針對 Docker Engine 有效,對 containerd 等其他 container runtime 需要另做處理

經過探索,Sealer 社區討論後考慮基於社區標準的 kubernetes  擴展機制來解決上述問題。經過調研, **Kyverno [ 6] **是一款 Kubernetes policy 引擎,其可以以一種非常簡單的配置方式支持對 Kubernetes 資源進行校驗和修改能力,同時其針對 K0s,K3s 等 cluster runtime 同樣適用。

4.png

未來規劃

接下來,Sealer 將會繼續秉持 以集羣爲基礎,以應用爲中心,繼續在打包、交付和運行的道路上持續探索。

  1. 以應用爲中心,構建完善的分佈式應用協作生態。
    1. 豐富應用模型,支持應用基於聲明能力一鍵化部署
    2. 覆蓋應用全生命週期,甚至運維監控能力
    3. 擴大生態,如支持更多 APP 類型,如 helm chart/ kube resource/kustomize/shell/ OAM 應用等
    4. 打造社區版 Sealer Image Hub,爲基於 Sealer Image 的協作提供平臺
  1. 以集羣爲基礎,構建穩定易用的集羣交付能力。
    1. 一鍵支撐大規模集羣,解決大規模集羣的安裝部署、鏡像分發等問題
    2. 豐富 container runtime,cluster runtime 支持生態
    3. 目前社區同學已經正在推進 containerd container runtime 和 k0s、k3s cluster runtime 的支持
    4. 豐富集羣運行時運維工具箱,例如證書籤發、健康檢查、OS 審計等
    5. 支持集羣鏡像的升級能力,幫助用戶可以持續使用最新最穩定的集羣鏡像

更多推薦

CNStack 社區版:

https://github.com/alibaba/CNStackCommunityEdition

ACK Distro:

https://github.com/AliyunContainerService/ackdistro

歡迎釘釘掃描下方二維碼或搜索羣號 34619594 加入 CNCF sealer 社區交流釘羣!

5.png

相關資料

[1] Sealer:

https://github.com/sealerio/sealer

[2] Sealer Image:

http://sealer.cool/docs/concept/sealer-image.html

[3] Semantic Versioning 2.0.0:

https://github.com/Masterminds/semver

[4] #1969

https://github.com/sealerio/sealer/issues/1969

[5] Buildah:

https://github.com/containers/buildah

[6] Kyverno:

https://github.com/kyverno/kyverno

點擊此處查看 Sealer 官方文檔

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章