$npx -y skills add chaterm/terminal-skills --skill helmHelm 包管理
| 1 | # Helm 包管理 |
| 2 | |
| 3 | ## 概述 |
| 4 | Helm Chart 开发、仓库管理、版本升级等技能。 |
| 5 | |
| 6 | ## 基础命令 |
| 7 | |
| 8 | ### 仓库管理 |
| 9 | ```bash |
| 10 | # 添加仓库 |
| 11 | helm repo add bitnami https://charts.bitnami.com/bitnami |
| 12 | helm repo add stable https://charts.helm.sh/stable |
| 13 | |
| 14 | # 更新仓库 |
| 15 | helm repo update |
| 16 | |
| 17 | # 列出仓库 |
| 18 | helm repo list |
| 19 | |
| 20 | # 搜索 Chart |
| 21 | helm search repo nginx |
| 22 | helm search hub nginx # 搜索 Artifact Hub |
| 23 | |
| 24 | # 删除仓库 |
| 25 | helm repo remove bitnami |
| 26 | ``` |
| 27 | |
| 28 | ### 安装与卸载 |
| 29 | ```bash |
| 30 | # 安装 Chart |
| 31 | helm install my-release bitnami/nginx |
| 32 | helm install my-release bitnami/nginx -n namespace --create-namespace |
| 33 | |
| 34 | # 指定 values |
| 35 | helm install my-release bitnami/nginx -f values.yaml |
| 36 | helm install my-release bitnami/nginx --set replicaCount=3 |
| 37 | |
| 38 | # 模拟安装(不实际执行) |
| 39 | helm install my-release bitnami/nginx --dry-run |
| 40 | |
| 41 | # 生成模板 |
| 42 | helm template my-release bitnami/nginx |
| 43 | |
| 44 | # 卸载 |
| 45 | helm uninstall my-release |
| 46 | helm uninstall my-release -n namespace |
| 47 | ``` |
| 48 | |
| 49 | ### 查看与管理 |
| 50 | ```bash |
| 51 | # 列出已安装的 Release |
| 52 | helm list |
| 53 | helm list -A # 所有命名空间 |
| 54 | helm list -n namespace |
| 55 | |
| 56 | # 查看 Release 状态 |
| 57 | helm status my-release |
| 58 | |
| 59 | # 查看 Release 历史 |
| 60 | helm history my-release |
| 61 | |
| 62 | # 获取 Release 的 values |
| 63 | helm get values my-release |
| 64 | helm get values my-release --all # 包含默认值 |
| 65 | |
| 66 | # 获取 Release 的 manifest |
| 67 | helm get manifest my-release |
| 68 | |
| 69 | # 获取 Release 的 notes |
| 70 | helm get notes my-release |
| 71 | ``` |
| 72 | |
| 73 | ## 升级与回滚 |
| 74 | |
| 75 | ```bash |
| 76 | # 升级 Release |
| 77 | helm upgrade my-release bitnami/nginx |
| 78 | helm upgrade my-release bitnami/nginx -f values.yaml |
| 79 | helm upgrade my-release bitnami/nginx --set replicaCount=5 |
| 80 | |
| 81 | # 安装或升级 |
| 82 | helm upgrade --install my-release bitnami/nginx |
| 83 | |
| 84 | # 回滚 |
| 85 | helm rollback my-release # 回滚到上一版本 |
| 86 | helm rollback my-release 2 # 回滚到指定版本 |
| 87 | |
| 88 | # 查看历史 |
| 89 | helm history my-release |
| 90 | ``` |
| 91 | |
| 92 | ## Chart 开发 |
| 93 | |
| 94 | ### 创建 Chart |
| 95 | ```bash |
| 96 | # 创建新 Chart |
| 97 | helm create mychart |
| 98 | |
| 99 | # Chart 目录结构 |
| 100 | mychart/ |
| 101 | ├── Chart.yaml # Chart 元数据 |
| 102 | ├── values.yaml # 默认配置值 |
| 103 | ├── charts/ # 依赖 Chart |
| 104 | ├── templates/ # 模板文件 |
| 105 | │ ├── deployment.yaml |
| 106 | │ ├── service.yaml |
| 107 | │ ├── ingress.yaml |
| 108 | │ ├── _helpers.tpl # 模板助手 |
| 109 | │ ├── NOTES.txt # 安装说明 |
| 110 | │ └── tests/ |
| 111 | └── .helmignore |
| 112 | ``` |
| 113 | |
| 114 | ### Chart.yaml |
| 115 | ```yaml |
| 116 | apiVersion: v2 |
| 117 | name: mychart |
| 118 | description: A Helm chart for my application |
| 119 | type: application |
| 120 | version: 0.1.0 |
| 121 | appVersion: "1.0.0" |
| 122 | keywords: |
| 123 | - app |
| 124 | - web |
| 125 | maintainers: |
| 126 | - name: Your Name |
| 127 | email: your@email.com |
| 128 | dependencies: |
| 129 | - name: postgresql |
| 130 | version: "12.x.x" |
| 131 | repository: https://charts.bitnami.com/bitnami |
| 132 | condition: postgresql.enabled |
| 133 | ``` |
| 134 | |
| 135 | ### values.yaml |
| 136 | ```yaml |
| 137 | replicaCount: 1 |
| 138 | |
| 139 | image: |
| 140 | repository: nginx |
| 141 | tag: "latest" |
| 142 | pullPolicy: IfNotPresent |
| 143 | |
| 144 | service: |
| 145 | type: ClusterIP |
| 146 | port: 80 |
| 147 | |
| 148 | ingress: |
| 149 | enabled: false |
| 150 | className: nginx |
| 151 | hosts: |
| 152 | - host: example.com |
| 153 | paths: |
| 154 | - path: / |
| 155 | pathType: Prefix |
| 156 | |
| 157 | resources: |
| 158 | limits: |
| 159 | cpu: 100m |
| 160 | memory: 128Mi |
| 161 | requests: |
| 162 | cpu: 100m |
| 163 | memory: 128Mi |
| 164 | |
| 165 | postgresql: |
| 166 | enabled: true |
| 167 | ``` |
| 168 | |
| 169 | ### 模板语法 |
| 170 | ```yaml |
| 171 | # templates/deployment.yaml |
| 172 | apiVersion: apps/v1 |
| 173 | kind: Deployment |
| 174 | metadata: |
| 175 | name: {{ include "mychart.fullname" . }} |
| 176 | labels: |
| 177 | {{- include "mychart.labels" . | nindent 4 }} |
| 178 | spec: |
| 179 | replicas: {{ .Values.replicaCount }} |
| 180 | selector: |
| 181 | matchLabels: |
| 182 | {{- include "mychart.selectorLabels" . | nindent 6 }} |
| 183 | template: |
| 184 | metadata: |
| 185 | labels: |
| 186 | {{- include "mychart.selectorLabels" . | nindent 8 }} |
| 187 | spec: |
| 188 | containers: |
| 189 | - name: {{ .Chart.Name }} |
| 190 | image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" |
| 191 | imagePullPolicy: {{ .Values.image.pullPolicy }} |
| 192 | ports: |
| 193 | - containerPort: 80 |
| 194 | {{- if .Values.resources }} |
| 195 | resources: |
| 196 | {{- toYaml .Values.resources | nindent 10 }} |
| 197 | {{- end }} |
| 198 | ``` |
| 199 | |
| 200 | ### 模板助手 |
| 201 | ```yaml |
| 202 | # templates/_helpers.tpl |
| 203 | {{- define "mychart.name" -}} |
| 204 | {{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} |
| 205 | {{- end }} |
| 206 | |
| 207 | {{- define "mychart.fullname" -}} |
| 208 | {{- if .Values.fullnameOverride }} |
| 209 | {{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} |
| 210 | {{- else }} |
| 211 | {{- $name := default .Chart.Name .Values.nameOverride }} |
| 212 | {{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} |
| 213 | {{- end }} |
| 214 | {{- end }} |
| 215 | |
| 216 | {{- define "mychart.labels" -}} |
| 217 | helm.sh/chart: {{ include "mychart.chart" . }} |
| 218 | app.kubernetes.io/name: {{ include "mychart.name" . }} |
| 219 | app.kubernetes.io/instance: {{ .Release.Name }} |
| 220 | {{- end }} |
| 221 | ``` |
| 222 | |
| 223 | ## Chart 打包与发布 |
| 224 | |
| 225 | ```bash |
| 226 | # 验证 Chart |
| 227 | helm lint mychart/ |
| 228 | |
| 229 | # 打包 Chart |
| 230 | helm package mychart/ |
| 231 | |
| 232 | # 生成索引 |
| 233 | helm repo index . --url https://example.com/charts |
| 234 | |
| 235 | # 推送到 OCI 仓库 |
| 236 | helm push mychart-0.1.0.tgz oci://registry.example.com/charts |
| 237 | ``` |
| 238 | |
| 239 | ## 依赖管理 |
| 240 | |
| 241 | ```bash |
| 242 | # 更新依赖 |
| 243 | helm dependency update mychart/ |
| 244 | |
| 245 | # 构建依赖 |
| 246 | helm dependency build mychart/ |
| 247 | |
| 248 | # 列出依赖 |
| 249 | helm dependency list mychart/ |
| 250 | ``` |
| 251 | |
| 252 | ## 常见场景 |
| 253 | |
| 254 | ### 场景 1:多环境部署 |
| 255 | ```bash |
| 256 | # 不同环境的 values 文件 |
| 257 | helm upgrade --install my-release ./mychart \ |
| 258 | -f values.yaml \ |
| 259 | -f values-prod.yaml \ |
| 260 | -n production |
| 261 | ``` |
| 262 | |
| 263 | ### 场景 2:条件渲染 |
| 264 | ```yaml |
| 265 | # templates/ingress.yaml |
| 266 | {{- if .Values.ingress.enabled -}} |
| 267 | apiVersion: networking.k8s.io/v1 |
| 268 | kind: Ingress |
| 269 | metada |