k8s~helm構建一個應用

三個概念

  1. chart:包含了建立Kubernetes的一個應用實例的必要信息
  2. config:包含了應用發佈配置信息
  3. release:是一個chart及其配置的一個運行實例

創建一個helm charts

helm create hello-world
  • Chart.yaml 用於描述這個Chart的相關信息,包括名字、描述信息以及版本等。
    僅僅是一些簡單的文本描述node

  • values.yaml 用於存儲 templates 目錄中模板文件中用到變量的值。nginx

  • NOTES.txt 用於介紹 Chart 部署後的一些信息,例如:如何使用這個 Chart、列出缺省的設置等。api

  • Templates 目錄下是 YAML 文件的模板,該模板文件遵循 Go template 語法。app

Templates 目錄下 YAML 文件模板的值默認都是在 values.yaml 裏定義的,好比在 deployment.yaml 中定義的容器鏡像。
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
其中的 .Values.image.repository 的值就是在 values.yaml 裏定義的 nginx,.Values.image.tag 的值就是 stable。
以上兩個變量值是在 create chart 的時候就自動生成的默認值,你能夠根據實際狀況進行修改。實際上都是靜態文本,只在是執行的時候才被解析.curl

構建一個helm應用

打開 Chart.yaml,能夠看到內容以下,配置名稱和版本wordpress

apiVersion: v1
appVersion: "1.0"
description: A Helm chart for Kubernetes
name: mychart
version: 0.1.0

編輯 values.yaml,它默認會在 Kubernetes 部署一個 Nginx。下面是 mychart 應用的 values.yaml 文件的內容:this

$ 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 http://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 被髮布到倉儲後,就能夠經過 helm install 命令部署該 Chart。url

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 # 向上歸滾一個版本
相關文章
相關標籤/搜索