k8s~helm構建一個應用(yong)
三個概念
- chart:包含了創建Kubernetes的一個應用實例的必要信息
- config:包含了應用發布配置信息
- release:是一個chart及其配置的一個運行實例
建立一個helm charts
helm create hello-world
-
Chart.yaml 用于描述這個Chart的相關信息,包括名字、描述信息以及版本等。
僅(jin)僅(jin)是一些簡單的文(wen)本(ben)描述 -
values.yaml 用(yong)于存儲 templates 目錄中模板(ban)文件中用(yong)到變(bian)量的(de)值。
-
NOTES.txt 用(yong)于介紹 Chart 部署后的一些信(xin)息,例如(ru):如(ru)何使用(yong)這個 Chart、列(lie)出缺省的設置等。
-
Templates 目錄下是 YAML 文件(jian)的模板,該模板文件(jian)遵(zun)循(xun) Go template 語法(fa)。
Templates 目錄下 YAML 文件模板的值默認都是在 values.yaml 里定義的,比如在 deployment.yaml 中定義的容器鏡像。
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
其中的 .Values.image.repository 的值就是在 values.yaml 里定義的 nginx,.Values.image.tag 的值就是 stable。
以上兩個(ge)變(bian)量值是在(zai) create chart 的(de)時(shi)候(hou)就自動(dong)生成的(de)默認(ren)值,你可以根據實際情(qing)況進行修改。實際上都是靜(jing)態文本(ben),只(zhi)在(zai)是執行的(de)時(shi)候(hou)才(cai)被解(jie)析.
構建一個helm應用
打開 Chart.yaml,可以看到內容如下,配置(zhi)名稱和版本
apiVersion: v1
appVersion: "1.0"
description: A Helm chart for Kubernetes
name: mychart
version: 0.1.0
編輯 values.yaml,它默認會(hui)在 Kubernetes 部署一個(ge) Nginx。下(xia)面是 mychart 應(ying)用的(de) values.yaml 文(wen)件的(de)內容(rong):
$ cat mychart/values.yaml
# Default values for mychart.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
replicaCount: 1
image:
repository: nginx
tag: stable
pullPolicy: IfNotPresent
service:
type: ClusterIP
port: 80
ingress:
enabled: false
annotations: {}
# kubernetes.io/ingress.class: nginx
# kubernetes.io/tls-acme: "true"
path: /
hosts:
- chart-example.local
tls: []
# - secretName: chart-example-tls
# hosts:
# - chart-example.local
resources: {}
# We usually recommend not to specify default resources and to leave this as a conscious
# choice for the user. This also increases chances charts run on environments with little
# resources, such as Minikube. If you do want to specify resources, uncomment the following
# lines, adjust them as necessary, and remove the curly braces after 'resources:'.
# limits:
# cpu: 100m
# memory: 128Mi
# requests:
# cpu: 100m
# memory: 128Mi
nodeSelector: {}
tolerations: []
affinity: {}
檢查模塊配置
$ helm lint hello-world/
打包
helm package hello-world
helm package mychart --debug #顯示詳細信息
啟動helm本地倉庫(helm3已被移除)
helm serve --repo-path /data/helm/repository/ --url //172.17.0.22:8879/charts/ &
倉庫刷新和查詢應用
$ helm repo update
$ helm search mychart
NAME CHART VERSION APP VERSION DESCRIPTION
local/hello-world 0.1.0 1.0 A Helm chart for Kubernetes
在 Kubernetes 中部署應用
Chart 被發(fa)布到倉(cang)儲后,就可以通過 helm install 命(ming)令(ling)部署該 Chart。
helm install hello local/hello-world
查看Release的狀態信息
helm status wordpress
升級charts
helm upgrade wordpress stable/wordpress
helm upgrade --install --force hello-world ./hello.tgz --namespace test # 也可以指定命名空間和它的taz包
回滾到上一個版本
helm rollback hello-world 1 # 向上歸滾一個版本