kustomize--yaml渲染利器

簡介

kustomize 是基於目錄,使用 Base + Overlay 的方式對應用的原始 YAML 進行派生,功能簡單清晰,kubectl 直接支持。git

helm

Helm 使用 Go Template 對應用部署所需的 YAML 進行抽象,造成應用部署模板,在須要進行部署時,能夠編寫 yaml 爲模板中的變量進行賦值,也能夠在 Helm CLI 的命令行中使用 --set name=value 的方式來對簡單變量進行賦值,完成賦值以後,能夠選擇使用 helm template 指令將 Chart + Value 的組合渲染成爲 YAML 供 kubectl 使用,也可使用 helm install 直接經過 Tiller 進行安裝github

對比

對比kustomize簡潔,helm則提供更多的功能,如:api

  1. chart包的管理,相似應用市場的概念。
  2. 直接和k8s apiserver交互,提供應用生命週期管理

但同時也帶來了更復雜的架構,好比服務端進程tiller提供和k8s的交互,Repository存儲 Helm Chart 的倉庫。給咱們的感受是對標於相似yum,apt和brew之類的管理系統。架構

所以若是用戶的目的只是作yaml文件的渲染,不想爲此花費更多的時間和精力,比較適合使用kustomize。 若是是管理嚴謹的大型公開項目,可能helm更合適。app

部署

部署比較簡單,下載對應的release版本,受權運行便可。
https://github.com/kubernetes...ui

使用

kustomize是基於目錄進行yaml文件管理和派生的。命令行

應用的多環境部署

應用部署在不一樣環境配置是有不一樣的,查看kustomize目錄結構:code

[root@master01 kustomize]# tree  helloWorld/
helloWorld/
├── base
│   ├── configMap.yaml
│   ├── deployment.yaml
│   ├── kustomization.yaml
│   └── service.yaml
└── overlays
    ├── production
    │   └── staging
    │       ├── kustomization.yaml
    │       └── deplyment.yaml
    └── staging
        ├── kustomization.yaml
        └── map.yaml

5 directories, 8 files

base目錄是項目的基礎yaml文件,overlay中的目錄staging,production在base的基礎上,生成不一樣環境的應用部署yaml文件。server

base

渲染出yaml文件生命週期

[root@master01 helloWorld]# kustomize  build base/
apiVersion: v1
data:
  altGreeting: Good Morning!
  enableRisky: "false"
kind: ConfigMap
metadata:
  labels:
    app: hello
  name: the-map
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: hello
  name: the-service

部署

kustomize build base/ | kubectl apply -f -

staging

staging環境在base的基礎上,作一些修改

kustomization.yaml

[root@master01 staging]# cat kustomization.yaml 
namePrefix: staging-
commonLabels:
  variant: staging
  org: acmeCorporation
commonAnnotations:
  note: Hello, I am staging!
resources:
- ../../base
patchesStrategicMerge: # 這裏意思是咱們要把base裏的configmap用下面的configmap overlay一下
- map.yaml

map.yaml

[root@master01 staging]# cat map.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: the-map
data:
  altGreeting: "Have a pineapple!"
  enableRisky: "true"

渲染後的staging環境功能yaml文件以下:

[root@master01 helloWorld]# kustomize  build overlays/staging/
apiVersion: v1
data:
  altGreeting: Have a pineapple!   # 對base中configmap進行了覆蓋
  enableRisky: "true"
kind: ConfigMap
metadata:
  annotations:                     # kustomize中定義的commonAnnotations
    note: Hello, I am staging!  
  labels:                          # kustomize中定義的commonLabels
    app: hello
    org: acmeCorporation
    variant: staging
  name: staging-the-map            # kustomize中定義的namePrefix
---
apiVersion: v1
kind: Service
metadata:
  annotations:
    note: Hello, I am staging!
  labels:
    app: hello
    org: acmeCorporation
    variant: staging
  name: staging-the-service
spec:
···

ci/cd

另外一個常見的使用場景是在ci/cd中,須要根據項目的tag,修改應用部署的鏡像。

此時能夠經過kustomize提供的命令kustomize edit set image,來方便的修改。

kustomize edit set image alpine=quay.io/datawire/ambassador:0.85.0
[root@master01 staging]# cat kustomization.yaml 
namePrefix: staging-
commonLabels:
  org: acmeCorporation
  variant: staging
commonAnnotations:
  note: Hello, I am staging!
resources:
- ../../base
patchesStrategicMerge:
- map.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:          # 此處爲增長的image設置
- name: alpine
  newName: quay.io/datawire/ambassador
  newTag: 0.85.0

渲染後的文件

[root@master01 overlays]# kustomize build staging/
···
        env:
        - name: ENABLE_RISKY
          valueFrom:
            configMapKeyRef:
              key: enableRisky
              name: staging-the-map
        image: quay.io/datawire/ambassador:0.85.0  # image已修改
        name: the-container
        ports:
        - containerPort: 8080
相關文章
相關標籤/搜索