-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path460-helm.mdc
More file actions
267 lines (218 loc) · 5.12 KB
/
Copy path460-helm.mdc
File metadata and controls
267 lines (218 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
---
title: Helm & Kubernetes Package Management
description: Helm charts, best practices, templating, values management, and GitOps deployment patterns
priority: 460
alwaysApply: false
files:
include:
- "**/Chart.yaml"
- "**/values.yaml"
- "**/values-*.yaml"
- "**/templates/**/*.yaml"
- "**/helmfile.yaml"
---
# Helm & Kubernetes Package Management
## Guiding Principles
1. **Reusability**: Create modular, reusable charts
2. **Configurability**: Use values.yaml for environment-specific configuration
3. **Best Practices**: Follow Helm and Kubernetes conventions
4. **Testing**: Validate charts before deployment
5. **Version Control**: Track charts and values in Git
---
## Helm Chart Structure
```
mychart/
Chart.yaml # Chart metadata
values.yaml # Default values
values-prod.yaml # Production values
values-staging.yaml # Staging values
charts/ # Chart dependencies
templates/ # Kubernetes manifests
deployment.yaml
service.yaml
ingress.yaml
configmap.yaml
secret.yaml
hpa.yaml
_helpers.tpl # Template helpers
NOTES.txt # Post-install notes
README.md
```
---
## Chart.yaml
```yaml
apiVersion: v2
name: webapp
description: Web application Helm chart
type: application
version: 1.0.0
appVersion: "1.0.0"
keywords:
- webapp
- nodejs
home: https://github.com/acme/webapp
sources:
- https://github.com/acme/webapp
maintainers:
- name: DevOps Team
email: devops@acme.com
dependencies:
- name: postgresql
version: "12.x"
repository: "https://charts.bitnami.com/bitnami"
condition: postgresql.enabled
```
---
## values.yaml
```yaml
# Default values
replicaCount: 2
image:
repository: acme/webapp
pullPolicy: IfNotPresent
tag: "" # Defaults to Chart appVersion
imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""
serviceAccount:
create: true
annotations: {}
name: ""
podAnnotations: {}
podSecurityContext:
runAsNonRoot: true
runAsUser: 1000
fsGroup: 1000
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
readOnlyRootFilesystem: true
service:
type: ClusterIP
port: 80
targetPort: 8080
ingress:
enabled: true
className: "nginx"
annotations:
cert-manager.io/cluster-issuer: "letsencrypt-prod"
hosts:
- host: webapp.acme.com
paths:
- path: /
pathType: Prefix
tls:
- secretName: webapp-tls
hosts:
- webapp.acme.com
resources:
limits:
cpu: 500m
memory: 512Mi
requests:
cpu: 250m
memory: 256Mi
autoscaling:
enabled: true
minReplicas: 2
maxReplicas: 10
targetCPUUtilizationPercentage: 70
targetMemoryUtilizationPercentage: 80
env:
- name: NODE_ENV
value: production
- name: LOG_LEVEL
value: info
envFrom:
- secretRef:
name: webapp-secrets
postgresql:
enabled: true
auth:
database: webapp
username: webapp
```
---
## Common Helm Commands
> [!CAUTION]
> **Remote/stateful operations:** `helm install` / `helm upgrade` / `helm uninstall` modify a live cluster.
> Run only with explicit approval; prefer `helm template` (dry-run) first.
```bash
# Create new chart
helm create mychart
# Lint chart
helm lint mychart/
# Template (dry-run)
helm template mychart/ --values mychart/values-prod.yaml
# Install
helm install webapp mychart/ \
--namespace production \
--create-namespace \
--values mychart/values-prod.yaml
# Upgrade
helm upgrade webapp mychart/ \
--namespace production \
--values mychart/values-prod.yaml \
--wait
# Rollback
helm rollback webapp 1 --namespace production
# List releases
helm list --namespace production
# Get values
helm get values webapp --namespace production
# Uninstall
helm uninstall webapp --namespace production
```
---
## Best Practices
### Use _helpers.tpl
```yaml
{{/*
Expand the name of the chart.
*/}}
{{- define "webapp.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}
{{/*
Create a default fully qualified app name.
*/}}
{{- define "webapp.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 }}
{{/*
Common labels
*/}}
{{- define "webapp.labels" -}}
helm.sh/chart: {{ include "webapp.chart" . }}
{{ include "webapp.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}
{{/*
Selector labels
*/}}
{{- define "webapp.selectorLabels" -}}
app.kubernetes.io/name: {{ include "webapp.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
```
---
## Related Files
- `450-kubernetes.mdc` - Kubernetes patterns
- `180-terraform.mdc` - Infrastructure as Code
- `160-github-actions.mdc` - CI/CD for Helm deployments
---
**Purpose**: Helm and Kubernetes package management best practices