包 Application as Helm Chart
你将构建什么
reusable Helm Chart 那个部署 web application 使用 configurable replica 数量, 镜像标签, and service 类型.
学习目标
- 理解 Helm Chart 结构
- 创建 templates for 部署 and Service
- 编写值.yaml 使用 configurable 参数
- 安装 and upgrade Chart
前置要求
- Helm 安装
- Kubernetes cluster
- 基础理解的 Kubernetes manifests
步骤
- 1
创建 Chart scaffold
Use
helm 创建到 generate Chart directory 结构使用默认 templates.代码helm create my-app-chart cd my-app-chart预期结果
Chart directory 是创建使用 templates/, 值.yaml, and Chart.yaml.
- 2
Customize 值.yaml
编辑值.yaml 到定义 configurable 参数: replicaCount, 镜像仓库 and 标签, service 类型 and port.
代码replicaCount: 2 image: repository: nginx tag: "1.25" service: type: ClusterIP port: 80预期结果
值.yaml 是 customized 使用参数你 想要到 制作 configurable.
- 3
Review and customize templates
Look at templates/部署.yaml and templates/service.yaml. 它们 use Go template 语法到 引用值 从值.yaml. Customize 它们如果需要.
预期结果
你理解如何 templates 引用值 using {{ .值.xxx }} 语法.
- 4
安装 and 测试 Chart
安装 Chart 使用默认值, 然后 upgrade 它使用自定义值 到 see 如何 Helm applies 更改.
代码helm install my-release . # Upgrade with custom replica count helm upgrade my-release . --set replicaCount=5 # Verify kubectl get pods预期结果
application 是 deployed 使用 2 replicas, 然后 upgraded 到 5 replicas.
- 5
添加健康检查端点
为应用添加 /health 端点返回 200 OK,供编排系统(Kubernetes、负载均衡器)探测存活。健康检查是生产部署的基础设施。
代码# app.py (Flask example) from flask import Flask, jsonify app = Flask(__name__) @app.route("/health") def health(): return jsonify({"status": "ok"}), 200 @app.route("/ready") def ready(): # Check DB connection etc. return jsonify({"ready": True}), 200预期结果
curl localhost:5000/health 返回 {"status":"ok"} 和 200 状态码。
自己动手试试
你学到了什么
你创建 Helm Chart 那个包 Kubernetes application 使用 configurable 值. Helm Charts 制作它 简单到 share, version, and 部署 applications consistently.
相关资源
相关术语
- Helm
Kubernetes 包 manager
相关术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
- Helm
Kubernetes 包 manager
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。