Oamc

Helm 包管理实战 · 修改记录

这是 cloud/Helm 包管理实战.md 的改动记录。后台每次保存都会留一份快照, 这里显示的是快照之间的差异,不会混入其他文章的改动。

还没有后台修改记录。

Git 提交记录

下面是 Git 里的提交,可能包含其他文章的改动(因为一次提交会带上工作区所有变化)。

#1 2026-10-25 +0 −0 refactor: 全部文章文件名中文化 0aa8fe6

这次没有正文改动。

#2 2026-10-25 +222 −0 快照: 重命名前的完整状态(含未整理的重复文件) ccb3350
 @@ -0,0 +1,222 @@
+---
+tags: [Helm, Kubernetes, 包管理, 部署]
+order: 10
+---
+
+# Helm 包管理实战
+
+## 场景
+
+团队需要在 K8s 集群里部署复杂应用:微服务、数据库、中间件、监控组件。手写 YAML 不仅繁琐,还难以管理版本、环境差异、依赖关系。
+
+Helm 解决三个核心问题:
+
+* **模板化** — 把 YAML 参数化,同一套 Chart 跨环境复用
+* **依赖管理** — `requirements.yaml` / `Chart.yaml` 声明依赖,一键拉取
+* **发布管理** — 版本、回滚、历史、diff,发布过程可控
+
+## 前置条件
+
+* K8s 集群 1.19+(Helm 3 不再需要 Tiller)
+* 本地已安装 Helm 3.8+
+* 有集群的 kubeconfig,且有创建 namespace、CRD 的权限
+* 了解基本的 K8s 资源对象
+
+## 配置
+
+### 1\. 基础操作
+
+```bash
+# 添加常用仓库
+helm repo add bitnami https://charts.bitnami.com/bitnami
+helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
+helm repo add grafana https://grafana.github.io/helm-charts
+helm repo update
+
+# 搜索
+helm search repo nginx
+helm search repo prometheus
+
+# 查看 Chart 详情
+helm show chart bitnami/redis
+helm show values bitnami/redis > redis-values.yaml
+```
+
+### 2\. 安装与配置
+
+```bash
+# 默认值安装
+helm install my-redis bitnami/redis -n redis --create-namespace
+
+# 自定义 values
+helm install my-redis bitnami/redis \
+  -n redis --create-namespace \
+  -f redis-values.yaml \
+  --set auth.password=StrongPass123 \
+  --set replica.replicaCount=2
+
+# 覆盖单个值(命令行优先级最高)
+helm upgrade my-redis bitnami/redis -n redis \
+  --set replica.replicaCount=3 \
+  --set auth.password=NewPass456
+```
+
+### 3\. 管理发布
+
+```bash
+# 查看
+helm list -n redis
+helm status my-redis -n redis
+helm get values my-redis -n redis       # 当前生效的值
+helm get manifest my-redis -n redis     # 渲染后的完整 manifest
+
+# 回滚
+helm history my-redis -n redis
+helm rollback my-redis 1 -n redis       # 回滚到 revision 1
+
+# 卸载(保留 PVC 用 --keep-history)
+helm uninstall my-redis -n redis
+```
+
+### 4\. 开发自己的 Chart
+
+```bash
+helm create myapp
+# 生成结构:
+# myapp/
+#   Chart.yaml          # 元数据
+#   values.yaml         # 默认值
+#   templates/          # 模板
+#   charts/             # 依赖 Chart
+```
+
+**Chart.yaml 关键字段**
+
+```yaml
+apiVersion: v2
+name: myapp
+description: 我的微服务应用
+type: application
+version: 1.0.0        # Chart 版本
+appVersion: "1.2.3"   # 应用版本
+keywords:
+  - microservice
+  - api
+maintainers:
+  - name: 运维团队
+    email: ops@example.com
+dependencies:
+  - name: redis
+    version: "17.x.x"
+    repository: "https://charts.bitnami.com/bitnami"
+    condition: redis.enabled
+```
+
+**values.yaml 设计原则**
+
+```yaml
+# 全局配置
+global:
+  imageRegistry: harbor.example.com
+  imagePullSecrets:
+    - harbor-secret
+
+# 开关控制
+redis:
+  enabled: true
+  auth:
+    enabled: true
+    password: "changeme"
+
+# 资源配额
+resources:
+  limits:
+    cpu: 500m
+    memory: 512Mi
+  requests:
+    cpu: 200m
+    memory: 256Mi
+
+# 环境区分
+env: production
+```
+
+### 5\. 模板最佳实践
+
+```yaml
+# _helpers.tpl 定义通用标签
+{{- define "myapp.labels" -}}
+helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
+{{ include "myapp.selectorLabels" . }}
+{{- if .Chart.AppVersion }}
+app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
+{{- end }}
+app.kubernetes.io/managed-by: {{ .Release.Service }}
+{{- end }}
+
+# 模板中使用
+metadata:
+  labels:

…… 改动较长,只显示前一部分