当前位置: 首页 > news >正文

Kubernetes Helm Chart开发与最佳实践:构建可复用的应用包

Kubernetes Helm Chart开发与最佳实践构建可复用的应用包一、Helm概述Helm是Kubernetes的包管理工具用于简化应用的部署和管理。Helm Chart是一个预配置的Kubernetes资源集合可以方便地进行版本管理和分发。1.1 Helm架构flowchart TD subgraph Helm客户端 A[Helm CLI] -- B[Chart Repository] B -- C[Tiller] end C -- D[Kubernetes Cluster] subgraph Kubernetes Cluster D -- E[Release] E -- F[Deployment] E -- G[Service] E -- H[Ingress] E -- I[ConfigMap] E -- J[Secret] end1.2 Helm组件组件功能Chart应用包定义ReleaseChart的部署实例RepositoryChart仓库Values配置参数二、Chart结构2.1 标准Chart目录结构my-chart/ ├── Chart.yaml # Chart元数据 ├── values.yaml # 默认配置值 ├── charts/ # 依赖的子Chart ├── templates/ # Kubernetes资源模板 │ ├── deployment.yaml │ ├── service.yaml │ ├── ingress.yaml │ ├── configmap.yaml │ └── secret.yaml └── templates/ └── NOTES.txt # 部署后提示信息2.2 Chart.yaml配置apiVersion: v2 name: my-app description: A Helm chart for Kubernetes type: application version: 1.0.0 appVersion: 1.0.0 keywords: - my-app - web home: https://example.com sources: - https://github.com/example/my-app maintainers: - name: John Doe email: johnexample.com dependencies: - name: redis version: 17.3.0 repository: https://charts.bitnami.com/bitnami三、模板开发3.1 Deployment模板apiVersion: apps/v1 kind: Deployment metadata: name: {{ include my-app.fullname . }} labels: {{- include my-app.labels . | nindent 4 }} spec: replicas: {{ .Values.replicaCount }} selector: matchLabels: {{- include my-app.selectorLabels . | nindent 6 }} template: metadata: labels: {{- include my-app.selectorLabels . | nindent 8 }} spec: containers: - name: {{ .Chart.Name }} image: {{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }} imagePullPolicy: {{ .Values.image.pullPolicy }} ports: - name: http containerPort: {{ .Values.service.port }} protocol: TCP resources: {{- toYaml .Values.resources | nindent 12 }}3.2 Service模板apiVersion: v1 kind: Service metadata: name: {{ include my-app.fullname . }} labels: {{- include my-app.labels . | nindent 4 }} spec: type: {{ .Values.service.type }} ports: - port: {{ .Values.service.port }} targetPort: http protocol: TCP name: http selector: {{- include my-app.selectorLabels . | nindent 4 }}3.3 Ingress模板{{- if .Values.ingress.enabled -}} apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: {{ include my-app.fullname . }} labels: {{- include my-app.labels . | nindent 4 }} annotations: {{- toYaml .Values.ingress.annotations | nindent 4 }} spec: {{- if .Values.ingress.tls }} tls: {{- range .Values.ingress.tls }} - hosts: {{- range .hosts }} - {{ . | quote }} {{- end }} secretName: {{ .secretName }} {{- end }} {{- end }} rules: {{- range .Values.ingress.hosts }} - host: {{ .host | quote }} http: paths: {{- range .paths }} - path: {{ .path }} pathType: {{ .pathType }} backend: service: name: {{ include my-app.fullname $ }} port: number: {{ $.Values.service.port }} {{- end }} {{- end }} {{- end }}四、Values配置4.1 values.yaml配置replicaCount: 1 image: repository: my-app pullPolicy: IfNotPresent tag: imagePullSecrets: [] nameOverride: fullnameOverride: 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 nodeSelector: {} tolerations: [] affinity: {}4.2 自定义Valuesenv: - name: DB_HOST value: {{ .Values.database.host }} - name: DB_PORT value: {{ .Values.database.port | quote }} database: host: db.example.com port: 5432 username: admin password: secret五、Chart模板函数5.1 常用模板函数{{ .Release.Name }} # Release名称 {{ .Release.Namespace }} # Release命名空间 {{ .Release.Revision }} # Release版本号 {{ .Chart.Name }} # Chart名称 {{ .Chart.Version }} # Chart版本 {{ .Values.key }} # 获取Values值 {{ include my-app.fullname . }} # 调用模板 {{ tpl .Values.template . }} # 模板渲染 {{ toYaml .Values }} # 转换为YAML {{ toJson .Values }} # 转换为JSON {{ quote .Values.string }} # 添加引号 {{ int .Values.number }} # 转换为整数5.2 条件判断{{- if .Values.ingress.enabled }} # 仅在ingress.enabled为true时渲染 {{- end }} {{- if eq .Values.env production }} replicaCount: 3 {{- else }} replicaCount: 1 {{- end }} {{- if or .Values.a .Values.b }} # a或b为true时渲染 {{- end }}5.3 循环遍历{{- range .Values.extraEnv }} - name: {{ .name }} value: {{ .value }} {{- end }} {{- range $key, $value : .Values.labels }} {{ $key }}: {{ $value }} {{- end }}六、Chart部署与管理6.1 安装Charthelm install my-release ./my-chart helm install my-release ./my-chart --values custom-values.yaml helm install my-release ./my-chart --set replicaCount36.2 升级Charthelm upgrade my-release ./my-chart helm upgrade my-release ./my-chart --values custom-values.yaml helm upgrade my-release ./my-chart --reuse-values6.3 回滚Charthelm rollback my-release helm rollback my-release 16.4 删除Releasehelm uninstall my-release七、Chart测试7.1 模板测试helm template ./my-chart helm template ./my-chart --values test-values.yaml output.yaml7.2 lint检查helm lint ./my-chart helm lint ./my-chart --values test-values.yaml7.3 安装测试helm install --dry-run ./my-chart helm install --debug ./my-chart八、Chart最佳实践8.1 Chart打包helm package ./my-chart helm package ./my-chart --version 1.1.08.2 Chart仓库helm repo add my-repo https://charts.example.com helm repo update helm search repo my-repo8.3 依赖管理helm dependency update helm dependency build九、总结Helm Chart开发实践包括Chart结构遵循标准目录结构模板开发使用Go模板语法Values配置提供合理的默认值条件渲染使用if/else控制渲染循环遍历处理列表数据Chart测试确保模板正确建议使用helm lint和helm template进行测试确保Chart质量。参考资料Helm文档Helm Chart模板指南Helm最佳实践
http://www.zskr.cn/news/1411953.html

相关文章:

  • ChatGPT生成攻略竟被《原神》社区封禁?资深UGC审核官透露的5条合规红线与安全输出协议
  • 人工智能【第47篇】深度学习优化:模型压缩与加速技术
  • 商丘市黄金回收白银回收铂金回收彩金回收门店优选+2026年最新黄金回收TOP5排行榜及联系方式 - 亦辰小黄鸭
  • polars导入csv文件,查看csv编码方式
  • Linux用户管理与权限配置全攻略
  • 2026最新漯河市黄金回收白银回收铂金回收店铺实力口碑排行榜TOP5;K金+金条+银条+首饰回收靠谱门店及联系方式推荐 - 前途无量YY
  • 异构集成技术解析:从Chiplet到3D封装,突破芯片性能瓶颈
  • [STM32 HAL库]学习笔记,七、定时器
  • 海东市黄金回收白银回收铂金回收彩金回收门店优选+2026年最新黄金回收TOP5排行榜及联系方式 - 亦辰小黄鸭
  • ARM开发中__aeabi_assert未定义错误解析与解决方案
  • 百考通AI:期刊论文智能创作,轻松输出专业内容
  • 跟着经典教材《Robotics, Vision and Control》复现案例?手把手教你配置RTB 9.10+MATLAB环境
  • 为什么你的 absolute总是乱跑?聊聊 Relative、Absolute 和 Fixed 的爱恨情仇
  • 2026最新吕梁市黄金回收白银回收铂金回收店铺实力口碑排行榜TOP5;K金+金条+银条+首饰回收靠谱门店及联系方式推荐 - 前途无量YY
  • GeckoDriver终极指南:快速构建稳定的Firefox自动化测试环境
  • 大学生为什么要学 OPC?抓住 AI 时代就业创业红利
  • Java抽象类和接口
  • 2026最新马鞍山市黄金回收白银回收铂金回收店铺实力口碑排行榜TOP5;K金+金条+银条+首饰回收靠谱门店及联系方式推荐 - 前途无量YY
  • 基于ML .NET与WebsiteAIAssistant构建网站智能分类助手
  • AX88796以太网控制器PHY寄存器访问与MII接口详解
  • 别再只啃论文了!目标检测发Paper的捷径:用YOLOv5代码复现驱动理论学习(附改进思路)
  • 从数据到洞察:如何解读海温(SST)与向外长波辐射(OLR)相关性空间分布图的业务意义
  • 【反面教材】用神经网络预测彩票号码?从原理到实战,看它如何翻车
  • 2026最新抚顺市黄金回收白银回收铂金回收店铺实力口碑排行榜TOP5;K金+金条+银条+首饰回收靠谱门店及联系方式推荐 - 前途无量YY
  • 别再只用ROC曲线了!用Python手写DeLong检验,科学比较两个机器学习模型的AUC差异
  • 通用GUI编程技术——图形渲染实战(四十七)——D3D12与D3D11互操作及选型建议
  • LabVIEW水泵智能检测应用
  • 告别高延迟!在Unity中低延时播放海康威视摄像头的另类思路:RTSP转RTMP推流实战
  • 2026年工程合同管理软件,好用推荐
  • 2026 年 5 月 27 日 Last.fm 独立运营!账户、团队不变,未来计划待揭晓