values.schema.json:输入验证
Helm 支持在 Chart 根目录放置 values.schema.json(JSON Schema 格式),在 helm install/upgrade 时自动验证用户提供的 values 是否符合规范,错误类型立即报出,而不是等到 K8s 拒绝资源时才发现。
// values.schema.json
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"required": ["image", "service"],
"properties": {
"replicaCount": {
"type": "integer",
"minimum": 1,
"maximum": 100,
"description": "副本数量,范围 1-100"
},
"image": {
"type": "object",
"required": ["repository", "tag"],
"properties": {
"repository": { "type": "string" },
"tag": { "type": "string" },
"pullPolicy": {
"type": "string",
"enum": ["Always", "IfNotPresent", "Never"]
}
}
},
"service": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": ["ClusterIP", "NodePort", "LoadBalancer"]
},
"port": {
"type": "integer",
"minimum": 1,
"maximum": 65535
}
}
}
}
}
# 验证 values 是否符合 schema(--validate 标志)
helm install myapp ./chart -f values-prod.yaml --validate
# schema 验证错误示例:
# Error: values don't meet the specifications of the schema(s) in the following chart(s):
# myapp:
# - (root): replicaCount must be >= 1
helm lint:Chart 语法检查
# 基础 lint
helm lint ./mychart
# 指定 values 文件进行 lint
helm lint ./mychart -f values-prod.yaml
# 严格模式(warning 也视为错误)
helm lint ./mychart --strict
# helm template:渲染模板检查最终 YAML
helm template myapp ./mychart \
-f values-prod.yaml \
--validate # 连接集群验证 YAML 是否被 API Server 接受
# 输出到文件方便审查
helm template myapp ./mychart -f values-prod.yaml > rendered.yaml
kubectl apply --dry-run=client -f rendered.yaml # 客户端 dry-run
chart-testing (ct):CI 验证工具
chart-testing 是 Helm 官方 CI 验证工具,自动检测 Chart 变更、运行 lint 和安装测试。
# .github/workflows/ci.yml
name: Helm Chart CI
on:
pull_request:
paths:
- 'charts/**'
jobs:
lint-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # ct 需要完整 Git 历史对比变更
- name: Set up Helm
uses: azure/setup-helm@v3
- name: Set up chart-testing
uses: helm/chart-testing-action@v2
- name: Run chart-testing lint
run: ct lint --config ct.yaml
- name: Create kind cluster
uses: helm/kind-action@v1
if: steps.list-changed.outputs.changed == 'true'
- name: Run chart-testing install test
run: ct install --config ct.yaml
# ct.yaml:chart-testing 配置
chart-dirs:
- charts
validate-maintainers: false
chart-repos:
- bitnami=https://charts.bitnami.com/bitnami
helm-extra-args: --timeout 600s
helm unittest:Chart 单元测试
# 安装 helm-unittest 插件
helm plugin install https://github.com/helm-unittest/helm-unittest
# 运行测试
helm unittest ./mychart
# 指定测试文件
helm unittest ./mychart -f 'tests/**/*.yaml'
# tests/deployment_test.yaml
suite: Deployment 测试
templates:
- templates/deployment.yaml
tests:
- it: 应该使用正确的镜像
set:
image.repository: myregistry/myapp
image.tag: "1.2.3"
asserts:
- equal:
path: spec.template.spec.containers[0].image
value: myregistry/myapp:1.2.3
- it: HPA 禁用时应设置 replicaCount
set:
replicaCount: 3
autoscaling.enabled: false
asserts:
- equal:
path: spec.replicas
value: 3
- it: HPA 启用时不应包含 replicaCount
set:
autoscaling.enabled: true
asserts:
- notExists:
path: spec.replicas
安全扫描:trivy + checkov
# trivy:扫描 Chart 中的安全问题
# 检测:配置错误、特权容器、root 用户、资源限制缺失等
trivy config ./mychart
# trivy 扫描 OCI Chart
trivy config oci://harbor.example.com/charts/mychart:1.0.0
# checkov:基础设施安全策略扫描
checkov -d ./mychart --framework helm
# 在 CI 中集成(GitHub Actions 示例)
# uses: aquasecurity/trivy-action@master
# with:
# scan-type: 'config'
# scan-ref: './mychart'
ArgoCD 集成:Helm Release as Application
ArgoCD 原生支持 Helm Chart,将 Helm Release 声明为 ArgoCD Application,实现 GitOps 自动同步。
# ArgoCD Application(使用 Helm Chart)
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: myapp-production
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/myorg/helm-charts
targetRevision: HEAD
path: charts/myapp # Chart 在 Git 仓库中的路径
helm:
releaseName: myapp
valueFiles: # 使用 Git 中的 values 文件
- values.yaml
- values-production.yaml
parameters: # 额外覆盖参数
- name: image.tag
value: "1.5.2"
destination:
server: https://kubernetes.default.svc
namespace: production
syncPolicy:
automated:
prune: true # 自动删除 Git 中移除的资源
selfHeal: true # 自动修复手动变更
syncOptions:
- CreateNamespace=true
大型企业 Helm 架构
企业 Helm 架构:Chart 库 + 环境配置分离 ┌────────────────────────┐ ┌──────────────────────────────┐ │ Chart 代码仓库 │ │ 配置仓库(GitOps) │ │ │ │ │ │ charts/ │ │ environments/ │ │ ├── frontend/ │ │ ├── dev/ │ │ ├── backend-api/ │ │ │ ├── frontend.yaml │ │ ├── worker/ │ │ │ └── backend-api.yaml │ │ └── common-lib/ │ │ ├── staging/ │ │ │ │ └── production/ │ └──────────┬─────────────┘ └──────────────┬───────────────┘ │ CI 发布 │ ArgoCD 同步 ▼ ▼ ┌────────────────────────┐ ┌──────────────────────────────┐ │ Harbor 私有仓库 │ │ Kubernetes 集群 │ │ oci://harbor/charts │──►│ ArgoCD Applications │ └────────────────────────┘ └──────────────────────────────┘
Chart 开发规范检查清单
Chart 结构规范
Chart.yaml 填写 description、maintainers、home; 所有 values 有注释说明; 提供 values.schema.json 验证; NOTES.txt 说明访问方式; .helmignore 排除开发文件。
模板规范
所有资源名称通过 include "chart.fullname" 生成; 使用标准 labels 和 selectorLabels; resource limits/requests 不能缺失; 不使用硬编码的 Namespace; 镜像标签不能硬编码为 latest。
安全规范
容器不以 root 用户运行(securityContext.runAsNonRoot: true); 不挂载 HostPath 卷; 镜像来自受信任的注册表; Secret 值不以明文出现在 values.yaml; 通过 trivy/checkov 安全扫描。
测试规范
helm lint 通过(--strict 模式); helm template 渲染结果正确; 关键模板有 unittest 覆盖; helm test 中有连通性测试; CI 使用 chart-testing (ct) 自动化。
# 安全最佳实践:deployment.yaml 安全上下文
spec:
template:
spec:
securityContext: # Pod 级别
runAsNonRoot: true
runAsUser: 1000
fsGroup: 2000
seccompProfile:
type: RuntimeDefault
containers:
- name: app
securityContext: # 容器级别
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
resources: # 生产必须设置 resources
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 512Mi
完整的 Helm 工程工具链:
开发:helm create、helm template、helm lint、helm-unittest
CI:chart-testing (ct)、trivy、checkov、kubeconform(K8s schema 验证)
仓库:Harbor(私有)、GHCR(公共免费)、Artifact Hub(发现)
GitOps:ArgoCD(Helm 原生支持)、Flux(helmrelease CRD)
恭喜完成 Helm Kubernetes 包管理教程!你已掌握:Chart 结构与元数据、Go 模板引擎完整语法、多环境 Values 管理、_helpers.tpl 命名模板、Chart 依赖与 Umbrella 架构、Hooks 生命周期、Release 幂等部署与回滚、OCI 注册表与仓库建设,以及从 schema 验证到 ArgoCD 集成的生产工程化实践。将这些知识应用到实际项目中,Helm 将成为你 K8s 工程化工具链的核心支柱。