helmfile聲明式部署Helm?Chart使用詳解
說明
使用 helmfile 時,我們首先得了解 helm 的使用,以及如何開發(fā)一個 helm chart。helm 是 kubernetes 的包管理工具。在實際的使用場景中我們涉及同時部署多個 chart、區(qū)分不同的部署環(huán)境、版本控制等需求。基于此需求,可以使用 helmfile 工具。helmfile 通過 helmfile 文件幫助用戶管理和維護多個 helm chart,可以來區(qū)分環(huán)境、實現(xiàn)版本控制。github 鏈接:helmfile[1]
場景說明
我們在公有云場景或者私有化場景中,同一個產(chǎn)品可能涉及多套環(huán)境的配置,例如:每套環(huán)境部署依賴的環(huán)境差異、使用的數(shù)據(jù)庫、消息隊列中間件等實例的地址、賬號密碼等都不同。因此針對不同環(huán)境我們需要維護開發(fā)環(huán)境、測試環(huán)境、預(yù)生產(chǎn)環(huán)境、生產(chǎn)環(huán)境甚至多套環(huán)境的部署文件以及秘鑰文件,每個小小的改動將涉及多套環(huán)境配置的修改,這給運維人員增加了極大的負擔(dān),以及多套環(huán)境的配置如何保持統(tǒng)一,也極大的考驗運維人員的細致程度,極大的增加了運維的復(fù)雜度。同時涉及的數(shù)據(jù)庫中間件實例的賬戶密碼的存放,也給運維流程增加了巨大的安全隱患?;谏厦娴氖銮?,這里可以將業(yè)務(wù)部署的各服務(wù)文件改造成 helm chart,同時區(qū)分多套環(huán)境以及版本控制,我們使用 helmfile 來統(tǒng)一部署管理。涉及實例涉及的賬戶密碼,我們可以使用 helm secrets 來實現(xiàn)加密解密,以及來保證運維的安全性,從而極大的減少運維的復(fù)雜度。關(guān)于 helm secrets 的使用,我們在其他文章進行的詳細的介紹。
安裝
helmfile 提供了多種安裝方式,具體可以參考:helmfile release[2]helmfile 還支持運行在容器中,可以很方便的集成到 CICD 的流程中:
# helm 2
$ docker run --rm --net=host -v "${HOME}/.kube:/root/.kube" -v "${HOME}/.helm:/root/.helm" -v "${PWD}:/wd" --workdir /wd quay.io/roboll/helmfile:v0.135.0 helmfile sync
# helm 3
$ docker run --rm --net=host -v "${HOME}/.kube:/root/.kube" -v "${HOME}/.config/helm:/root/.config/helm" -v "${PWD}:/wd" --workdir /wd quay.io/roboll/helmfile:helm3-v0.135.0 helmfile sync
helmfile.yaml 介紹
helmfile.yaml 是 helmfile 的核心文件,其用來聲明所有的配置。下面會簡要介紹一下,具體說明可以參考官方文檔:helmfile-configuration[3]
# 聲明 repo 配置
repositories:
- name: <repo-name>
# url: repo url
# 可以設(shè)置基礎(chǔ)配置 或 tls 認(rèn)證
# certFile: certificate 文件
# keyFile: key 文件
# username: 用戶名
# password: 密碼
# helm 二進制文件的路徑
helmBinary: path/to/helm3
# helm 的一些默認(rèn)設(shè)置,這些配置與 `helm SUBCOMMAND` 相同,可以通過這個配置聲明一些,默認(rèn)的配置
helmDefaults:
tillerNamespace: tiller-namespace #dedicated default key for tiller-namespace
tillerless: false #dedicated default key for tillerless
kubeContext: kube-context #dedicated default key for kube-context (--kube-context)
cleanupOnFail: false #dedicated default key for helm flag --cleanup-on-fail
# additional and global args passed to helm (default "")
args:
- "--set k=v"
# verify the chart before upgrading (only works with packaged charts not directories) (default false)
verify: true
# wait for k8s resources via --wait. (default false)
wait: true
# time in seconds to wait for any individual Kubernetes operation (like Jobs for hooks, and waits on pod/pvc/svc/deployment readiness) (default 300)
timeout: 600
# performs pods restart for the resource if applicable (default false)
recreatePods: true
# forces resource update through delete/recreate if needed (default false)
force: false
# when using helm 3.2+, automatically create release namespaces if they do not exist (default true)
createNamespace: true
...
# 為 helmfile 中所有的 release 設(shè)置相同的 label,可用于為所有 release 標(biāo)記相同的版本
commonLabels:
hello: world
# 設(shè)置 release 配置(支持多 release)
releases:
# 遠程 chart 示例(chart 已經(jīng)上傳到 remote 倉庫)
- name: vault # name of this release
namespace: vault # target namespace
createNamespace: true # helm 3.2+ automatically create release namespace (default true)
labels: # Arbitrary key value pairs for filtering releases
foo: bar
chart: roboll/vault-secret-manager # the chart being installed to create this release, referenced by `repository/chart` syntax
version: ~1.24.1 # the semver of the chart. range constraint is supported
condition: vault.enabled # The values lookup key for filtering releases. Corresponds to the boolean value of `vault.enabled`, where `vault` is an arbitrary value
missingFileHandler: Warn # set to either "Error" or "Warn". "Error" instructs helmfile to fail when unable to find a values or secrets file. When "Warn", it prints the file and continues.
# Values files used for rendering the chart
values:
# Value files passed via --values
- vault.yaml
# Inline values, passed via a temporary values file and --values, so that it doesn't suffer from type issues like --set
- address: https://vault.example.com
# Go template available in inline values and values files.
- image:
# The end result is more or less YAML. So do `quote` to prevent number-like strings from accidentally parsed into numbers!
# See https://github.com/roboll/helmfile/issues/608
tag: {{ requiredEnv "IMAGE_TAG" | quote }}
# Otherwise:
# tag: "{{ requiredEnv "IMAGE_TAG" }}"
# tag: !!string {{ requiredEnv "IMAGE_TAG" }}
db:
username: {{ requiredEnv "DB_USERNAME" }}
# value taken from environment variable. Quotes are necessary. Will throw an error if the environment variable is not set. $DB_PASSWORD needs to be set in the calling environment ex: export DB_PASSWORD='password1'
password: {{ requiredEnv "DB_PASSWORD" }}
proxy:
# Interpolate environment variable with a fixed string
domain: {{ requiredEnv "PLATFORM_ID" }}.my-domain.com
scheme: {{ env "SCHEME" | default "https" }}
# Use `values` whenever possible!
# `set` translates to helm's `--set key=val`, that is known to suffer from type issues like https://github.com/roboll/helmfile/issues/608
set:
# single value loaded from a local file, translates to --set-file foo.config=path/to/file
- name: foo.config
file: path/to/file
# set a single array value in an array, translates to --set bar[0]={1,2}
- name: bar[0]
values:
- 1
- 2
# set a templated value
- name: namespace
value: {{ .Namespace }}
# will attempt to decrypt it using helm-secrets plugin
# 本地 chart 示例(chart 保存在本地)
- name: grafana # name of this release
namespace: another # target namespace
chart: ../my-charts/grafana # the chart being installed to create this release, referenced by relative path to local helmfile
values:
- "../../my-values/grafana/values.yaml" # Values file (relative path to manifest)
- ./values/{{ requiredEnv "PLATFORM_ENV" }}/config.yaml # Values file taken from path with environment variable. $PLATFORM_ENV must be set in the calling environment.
wait: true
# 可以嵌套其他的 helmfiles,支持從本地和遠程拉取 helmfile
helmfiles:
- path: path/to/subhelmfile.yaml
# label 選擇器可以過濾需要覆蓋的 release
selectors:
- name=prometheus
# 覆蓋 value
values:
# 使用文件覆蓋
- additional.values.yaml
# 覆蓋單獨的 key
- key1: val1
- # 遠程拉取配置
path: git::https://github.com/cloudposse/helmfiles.git@releases/kiam.yaml?ref=0.40.0
# 如果指向不存在路徑,則打印告警錯誤
missingFileHandler: Error
# 多環(huán)境管理
environments:
# 當(dāng)沒有設(shè)置 `--environment NAME` 時,使用 default
default:
values:
# 內(nèi)容可以是文件路徑或者 key:value
- environments/default/values.yaml
- myChartVer: 1.0.0-dev
# "production" 環(huán)境,當(dāng)設(shè)置了 `helmfile --environment production sync` 時
production:
values:
- environment/production/values.yaml
- myChartVer: 1.0.0
# disable vault release processing
- vault:
enabled: false
## `secrets.yaml` is decrypted by `helm-secrets` and available via `{{ .Environment.Values.KEY }}`
secrets:
- environment/production/secrets.yaml
# 當(dāng)占不到 `environments.NAME.values` 時,可以設(shè)置為 "Error", "Warn", "Info", "Debug",默認(rèn)是 "Error"
missingFileHandler: Error
# 分層管理,可以將所有文件合并,順序為:environments.yaml < - defaults.yaml < - templates.yaml < - helmfile.yaml
bases:
- environments.yaml
- defaults.yaml
- templates.yaml
# API 功能
apiVersions:
- example/v1
helmfile 調(diào)試
這里,編排好相關(guān)的 helmfile 后,我們可以使用下面的命令進行調(diào)試
# 查看目錄結(jié)構(gòu)
$ ls
README.org environments helm helmfile helmfile.yaml releases
# 查看helmfile.yaml
$ cat helmfile.yaml
environments:
# 不指定環(huán)境時,默認(rèn)使用默認(rèn)測試環(huán)境
default:
values:
- environments/test/config.yaml
- environments/test/versions.yaml
- environments/test//namespaces.yaml
secrets:
- environments/test/secrets.yaml
test:
values:
- environments/test/config.yaml
- environments/test/versions.yaml
- environments/test/namespaces.yaml
secrets:
- environments/test/secrets.yaml
helmDefaults:
createNamespace: true
releases:
- name: password-secrets
kubeContext: {{ .Values.kubeContext.service }}
namespace: {{ .Values.namespaces.service }}
chart: helm/charts/secrets
values:
- releases/secrets.yaml.gotmpl
labels:
app: secrets
- name: web
kubeContext: {{ .Values.kubeContext.business }}
namespace: {{ .Values.namespaces.business }}
chart: helm/charts/web
values:
- releases/web.yaml.gotmpl
labels:
app: web
# helmfile調(diào)試
$ helmfile -e test template
安裝 chart
helmfile -e test sync
helmfile 更新或者刪除某個 chart
這里可以通過--selector指定 label 來進行更新或者刪除:
# 更新web服務(wù) helmfile -e test --selector app=web sync # 刪除web服務(wù) helmfile -e test --selector app=web delete
查看變更
# 查看文件的變更信息 helmfile -e test --selector app=web diff # 只查看文件的變更部分信息 helmfile -e test --selector app=web diff --context 4
資料參考
[1]helmfile: https://github.com/roboll/helmfile
[2]helmfile release: https://github.com/roboll/helmfile/releases
[3]helmfile-configuration: https://github.com/roboll/helmfile#configuration
聲明:本文分享的軟件服務(wù)以及語言均源于網(wǎng)絡(luò),只做針對這些軟件服務(wù)或者語言的使用實踐進行分享和整理。本公眾號不對任何人進行推薦,在使用這些軟件或編程代碼時有可能會引發(fā)一些問題,甚至導(dǎo)致數(shù)據(jù)丟失,請您自行承擔(dān)相應(yīng)的后果!本公眾號概不負責(zé)! 若您覺得公眾號發(fā)布的內(nèi)容若侵犯到您的權(quán)益,請聯(lián)系及時管理員溝通!
以上就是helmfile聲明式部署Helm Chart使用詳解的詳細內(nèi)容,更多關(guān)于helmfile部署Helm Chart的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Kubernetes教程之Windows?HostProcess?運行容器化負載
這篇文章主要介紹了Kubernetes?Windows?HostProcess?運行容器化負載,本篇內(nèi)容還是比較多的,總共包含了?Windows?HostProcess的創(chuàng)建、為?Windows?Pod?和容器配置?GMSA?和?Windows?的?Pod?和容器配置?RunAsUserName三大功能模塊,需要的朋友可以參考下2022-07-07
Kubernetes?權(quán)限管理認(rèn)證鑒權(quán)詳解
這篇文章主要為大家介紹了Kubernetes?權(quán)限管理認(rèn)證鑒權(quán)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
KubeSphere中部署Wiki系統(tǒng)wiki.js并啟用中文全文檢索
這篇文章主要為大家介紹了KubeSphere中部署Wiki系統(tǒng)wiki.js并啟用中文全文檢索實現(xiàn)過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06
Rainbond使用Dockerfile構(gòu)建便捷應(yīng)用運行流程
這篇文章主要為大家介紹了Rainbond使用Dockerfile構(gòu)建便捷應(yīng)用運行流程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04
在Kubernetes集群中搭建Istio微服務(wù)網(wǎng)格的過程詳解
這篇文章主要介紹了在Kubernetes集群中搭建Istio微服務(wù)網(wǎng)格,我們采用default配置檔部署istio網(wǎng)格,istioctl?install命令不指定任何配置檔默認(rèn)就是呀default配置檔,需要的朋友可以參考下2022-05-05
k8s Job 執(zhí)行一次性以及批處理任務(wù)使用場景案例
這篇文章主要為大家介紹了k8s Job 執(zhí)行一次性以及批處理任務(wù)使用場景案例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04

