
新建一个Chart步骤

一、前提准备:环境准备
安装Helm
$ curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
$ chmod 700 get_helm.sh
$ ./get_helm.sh
可正常连接K8s集群
kubectl cluster-info
二、创建Chart工作目录
- 创建出Chart工作目录,一般有两种方式
- 从0到1,自行创建
- 拉取现有Chart,在已有Chart基础上做修改
自行创建
在现有Chart基础上做修改
生成的工作目录示意
nginx-chart/
├── Chart.yaml
├── values.yaml
├── charts/
├── templates/
│ ├── deployment.yaml
│ ├── service.yaml
│ ├── ingress.yaml
│ ├── _helpers.tpl
│ └── NOTES.txt
└── .helmignore
三、编辑Chart元数据-Chart.yml
- 按照需求编辑Chart.yml,即Chart元数据信息,Chart元数据包含这个chart的名称、版本号
Chart.yml元数据-必填字段
字段 | 类型 | 说明 |
---|
apiVersion | string | Chart API 版本(v2 为 Helm 3) |
name | string | Chart 名称(只能小写字母、数字、-) |
version | string | Chart 自身的版本号(遵循 SemVer) |
appVersion | string | 所封装“应用”的版本号,仅做展示 |
Chart.yml元数据-描述、维护字段
字段 | 示例 | 作用 |
---|
description | "Bitnami nginx chart" | 一句话描述 |
home | https://bitnami.com | 项目主页 |
sources | ["https://github.com/bitnami/bitnami-docker-nginx"] | 源码仓库 |
maintainers | [{name: "Bitnami", email: "containers@bitnami.com"}] | 维护者列表 |
keywords | ["nginx", "web server"] | 关键字,方便搜索 |
icon | "https://example.com/icon.png" | 图标 URL |
Chart.yml元数据-依赖和兼容性字段
字段 | 示例 | 作用 |
---|
dependencies | [{name: "mysql", version: "~9.0.0", repository: "oci://..."}] | 声明子 Chart |
type | "application" 或 "library" | 应用 Chart vs 库 Chart |
kubeVersion | ">=1.22-0" | 兼容的 K8s 版本 |
annotations | {"category": "Infrastructure"} | 额外元数据 |
deprecated | true | 标记弃用 |
Chart.yml-完整样例
apiVersion: v2
name: nginx
description: "Bitnami nginx chart"
type: application
version: 15.4.2
appVersion: "1.25.3"
home: https://bitnami.com
sources:- https://github.com/bitnami/charts
maintainers:- name: Bitnamiemail: containers@bitnami.com
keywords:- nginx- web server
kubeVersion: ">=1.22-0"
dependencies:- name: commonversion: 2.x.xrepository: oci://registry-1.docker.io/bitnamicharts
四、编辑自定义变量values.yml
values.yml作用
- values.yml作用是提供所有模板(templates/*.yaml)里的变量的默认值。
- 所有镜像、资源、副本、网络、存储、安全 等变量参数都可以在这里一键调节,无需改模板
values.yml示例
五、编辑template文件
template文件作用
- templates/ 目录下的所有 .yaml 文件(以及 _*.yaml 辅助文件)统称为 template 文件
- template本身代表了k8s资源的定义,比如deployment、service等
- template可以设置占位符,和values.yml结合使用
template示例
apiVersion: apps/v1
kind: Deployment
metadata:name: {{ include "mychart.fullname" . }}
spec:replicas: {{ .Values.replicaCount }} template:spec:containers:- name: nginximage: "{{ .Values.image.registry }}/{{ .Values.image.repository }}:{{ .Values.image.tag }}" resources:
{{ toYaml .Values.resources | indent 10 }}
六、本地验证
Lint语法检查
helm lint .
Dry-Run
helm template myrel .
七、打包发布到仓库
- 多人协作场景下,本地的Chart工作目录,需要先打包成一个压缩包,再上传到仓库后使用
- 若只是单人使用,也可以直接在本地工作目录进行install安装
helm package .
helm repo add myrepo https://charts.example.com
curl --data-binary "@nginx-chart-0.1.0.tgz" https://charts.example.com/api/charts
八、安装到集群
helm install web ./nginx-chart
helm install web-release nginx-chart
helm list
kubectl get pods -l app.kubernetes.io/name=nginx-chart
九、升级(更新配置后)
helm upgrade web ./nginx-chart
十、卸载
helm uninstall web