Kubernetes Helm Chart开发最佳实践:打造可复用的应用包

Kubernetes Helm Chart开发最佳实践:打造可复用的应用包

引言

Helm是Kubernetes的包管理工具,通过Helm Chart可以将应用打包成可复用的包。今天就来分享一下Helm Chart开发的最佳实践。

Chart结构

标准Chart结构

一个标准的Chart结构如下:

复制代码
mychart/
├── Chart.yaml          # Chart元数据
├── values.yaml         # 默认配置值
├── charts/             # 依赖的子Chart
├── templates/          # Kubernetes资源模板
│   ├── NOTES.txt       # 部署说明
│   ├── deployment.yaml
│   ├── service.yaml
│   ├── ingress.yaml
│   ├── configmap.yaml
│   ├── secret.yaml
│   └── _helpers.tpl    # 辅助模板函数
├── templates/NOTES.txt # 部署后显示的说明信息
├── README.md           # Chart说明文档
├── LICENSE             # 许可证文件
└── .helmignore         # 忽略文件列表

Chart.yaml配置

Chart.yaml包含Chart的元数据:

yaml 复制代码
apiVersion: v2
name: myapp
description: A production-ready web application
type: application
version: 1.2.3
appVersion: "2.0.0"
kubeVersion: ">=1.24.0"
keywords:
  - web
  - api
  - microservice
home: https://myapp.example.com
sources:
  - https://github.com/example/myapp
maintainers:
  - name: Wanli Hou
    email: wanli@example.com
icon: https://myapp.example.com/icon.png
deprecated: false

Chart.yaml字段说明

  • apiVersion:Chart API版本,v1适用于Helm 2,v2适用于Helm 3
  • name:Chart名称,必须唯一
  • version:Chart版本,使用语义化版本
  • appVersion:应用版本
  • kubeVersion:兼容的Kubernetes版本
  • description:Chart描述
  • type:Chart类型,application或library
  • keywords:关键词列表
  • maintainers:维护者信息

values.yaml配置

values.yaml包含Chart的默认配置值:

yaml 复制代码
# 全局配置
global:
  imageRegistry: ""
  imagePullSecrets: []
  storageClass: ""

# 应用配置
replicaCount: 3

image:
  repository: myapp
  pullPolicy: IfNotPresent
  tag: "2.0.0"

imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""

serviceAccount:
  create: true
  annotations: {}
  name: ""

podAnnotations: {}
podSecurityContext: {}
securityContext: {}

service:
  type: ClusterIP
  port: 80

ingress:
  enabled: false
  className: ""
  annotations: {}
  hosts:
    - host: chart-example.local
      paths:
        - path: /
          pathType: ImplementationSpecific
  tls: []

resources:
  limits:
    cpu: 100m
    memory: 128Mi
  requests:
    cpu: 100m
    memory: 128Mi

autoscaling:
  enabled: false
  minReplicas: 1
  maxReplicas: 100
  targetCPUUtilizationPercentage: 80
  targetMemoryUtilizationPercentage: 80

nodeSelector: {}
tolerations: []
affinity: {}

values.yaml最佳实践

  • 使用层次化结构组织配置
  • 提供合理的默认值
  • 添加注释说明配置项的用途
  • 支持global配置用于共享值

.helmignore文件

.helmignore文件用于指定打包时需要忽略的文件:

复制代码
# .helmignore
.git/
.gitignore
*.tgz
*.tar.gz
node_modules/
vendor/

常见忽略项

  • 版本控制目录(.git)
  • 依赖目录(node_modules, vendor)
  • 构建产物(*.tgz)
  • 编辑器配置(.vscode, .idea)

模板开发

使用辅助函数

yaml 复制代码
# templates/_helpers.tpl
{{- define "myapp.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

条件渲染

yaml 复制代码
{{- if .Values.ingress.enabled }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: {{ include "myapp.fullname" . }}
spec:
  rules:
    - host: {{ .Values.ingress.host }}
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: {{ include "myapp.fullname" . }}
                port:
                  number: {{ .Values.service.port }}
{{- end }}

最佳实践

配置验证

使用schema验证配置:

json 复制代码
{
  "$schema": "https://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "replicaCount": {
      "type": "integer",
      "minimum": 1,
      "maximum": 10
    }
  }
}

版本管理

使用语义化版本管理:

yaml 复制代码
version: 1.2.3
# 主版本.次版本.修订版本

结语

Helm Chart是Kubernetes应用打包的标准方式。通过遵循最佳实践,我们可以创建高质量的Chart。

本文作者:侯万里(万里侯),致力于Helm Chart开发的工程师

相关推荐
沪漂阿龙2 小时前
Spring Cloud 面试题深度解析:微服务架构、注册中心、配置中心、Gateway、OpenFeign、负载均衡、熔断降级全攻略
spring cloud·微服务·架构
仙柒4157 小时前
Docker原理
运维·docker·容器
Cat_Rocky11 小时前
Kubernetes集群升级指南以及自动更新证书
云原生·容器·kubernetes
米高梅狮子11 小时前
第2章 docker容器
运维·docker·云原生·容器·架构·kubernetes·自动化
万里侯12 小时前
分布式系统设计原则:构建高可用的系统架构
微服务·容器·k8s
卧室小白14 小时前
docker网络与服务编排与集群
运维·docker·容器
古城小栈16 小时前
K8s 之 ingress
云原生·容器·kubernetes
万里侯16 小时前
微服务与人生:从服务网格看人际关系
微服务·容器·k8s
疯狂成瘾者17 小时前
Kubernetes(简称K8s)
云原生·容器·kubernetes