code-abyss 1.6.6 → 1.6.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/skills/SKILL.md +36 -0
- package/skills/domains/COVERAGE_PLAN.md +232 -0
- package/skills/{ai → domains/ai}/SKILL.md +3 -0
- package/skills/domains/ai/model-evaluation.md +790 -0
- package/skills/domains/ai/prompt-engineering.md +703 -0
- package/skills/domains/ai/rag-system.md +542 -0
- package/skills/domains/data-engineering/SKILL.md +43 -0
- package/skills/domains/data-engineering/data-pipeline.md +762 -0
- package/skills/domains/data-engineering/data-quality.md +894 -0
- package/skills/domains/data-engineering/stream-processing.md +791 -0
- package/skills/{development → domains/development}/SKILL.md +4 -0
- package/skills/domains/development/dart.md +963 -0
- package/skills/domains/development/kotlin.md +834 -0
- package/skills/domains/development/php.md +659 -0
- package/skills/domains/development/swift.md +755 -0
- package/skills/{devops → domains/devops}/SKILL.md +3 -0
- package/skills/domains/devops/e2e-testing.md +914 -0
- package/skills/domains/devops/performance-testing.md +734 -0
- package/skills/domains/devops/testing-strategy.md +667 -0
- package/skills/domains/frontend-design/SKILL.md +28 -0
- package/skills/domains/frontend-design/build-tools.md +743 -0
- package/skills/domains/frontend-design/component-patterns.md +202 -0
- package/skills/domains/frontend-design/performance.md +734 -0
- package/skills/domains/frontend-design/state-management.md +680 -0
- package/skills/domains/frontend-design/testing.md +699 -0
- package/skills/domains/frontend-design/ui-aesthetics.md +110 -0
- package/skills/domains/frontend-design/ux-principles.md +156 -0
- package/skills/domains/infrastructure/SKILL.md +58 -0
- package/skills/domains/infrastructure/gitops.md +735 -0
- package/skills/domains/infrastructure/iac.md +855 -0
- package/skills/domains/infrastructure/kubernetes.md +1018 -0
- package/skills/domains/mobile/SKILL.md +32 -0
- package/skills/domains/mobile/android-dev.md +979 -0
- package/skills/domains/mobile/cross-platform.md +795 -0
- package/skills/domains/mobile/ios-dev.md +931 -0
- package/skills/domains/orchestration/SKILL.md +21 -0
- package/skills/domains/orchestration/multi-agent.md +227 -0
- package/skills/{security → domains/security}/SKILL.md +3 -0
- package/skills/domains/security/secrets-management.md +834 -0
- package/skills/domains/security/supply-chain.md +931 -0
- package/skills/domains/security/threat-modeling.md +828 -0
- package/skills/run_skill.py +7 -7
- package/skills/tests/test_change_analyzer.py +1 -1
- package/skills/tests/test_doc_generator.py +1 -1
- package/skills/tests/test_module_scanner.py +1 -1
- package/skills/tests/test_quality_checker.py +1 -1
- package/skills/tests/test_security_scanner.py +1 -1
- /package/skills/{ai → domains/ai}/agent-dev.md +0 -0
- /package/skills/{ai → domains/ai}/llm-security.md +0 -0
- /package/skills/{architecture → domains/architecture}/SKILL.md +0 -0
- /package/skills/{architecture → domains/architecture}/api-design.md +0 -0
- /package/skills/{architecture → domains/architecture}/caching.md +0 -0
- /package/skills/{architecture → domains/architecture}/cloud-native.md +0 -0
- /package/skills/{architecture → domains/architecture}/compliance.md +0 -0
- /package/skills/{architecture → domains/architecture}/data-security.md +0 -0
- /package/skills/{architecture → domains/architecture}/message-queue.md +0 -0
- /package/skills/{architecture → domains/architecture}/security-arch.md +0 -0
- /package/skills/{development → domains/development}/cpp.md +0 -0
- /package/skills/{development → domains/development}/go.md +0 -0
- /package/skills/{development → domains/development}/java.md +0 -0
- /package/skills/{development → domains/development}/python.md +0 -0
- /package/skills/{development → domains/development}/rust.md +0 -0
- /package/skills/{development → domains/development}/shell.md +0 -0
- /package/skills/{development → domains/development}/typescript.md +0 -0
- /package/skills/{devops → domains/devops}/cost-optimization.md +0 -0
- /package/skills/{devops → domains/devops}/database.md +0 -0
- /package/skills/{devops → domains/devops}/devsecops.md +0 -0
- /package/skills/{devops → domains/devops}/git-workflow.md +0 -0
- /package/skills/{devops → domains/devops}/observability.md +0 -0
- /package/skills/{devops → domains/devops}/performance.md +0 -0
- /package/skills/{devops → domains/devops}/testing.md +0 -0
- /package/skills/{security → domains/security}/blue-team.md +0 -0
- /package/skills/{security → domains/security}/code-audit.md +0 -0
- /package/skills/{security → domains/security}/pentest.md +0 -0
- /package/skills/{security → domains/security}/red-team.md +0 -0
- /package/skills/{security → domains/security}/threat-intel.md +0 -0
- /package/skills/{security → domains/security}/vuln-research.md +0 -0
- /package/skills/{multi-agent → orchestration/multi-agent}/SKILL.md +0 -0
- /package/skills/{gen-docs → tools/gen-docs}/SKILL.md +0 -0
- /package/skills/{gen-docs → tools/gen-docs}/scripts/doc_generator.py +0 -0
- /package/skills/{verify-change → tools/verify-change}/SKILL.md +0 -0
- /package/skills/{verify-change → tools/verify-change}/scripts/change_analyzer.py +0 -0
- /package/skills/{verify-module → tools/verify-module}/SKILL.md +0 -0
- /package/skills/{verify-module → tools/verify-module}/scripts/module_scanner.py +0 -0
- /package/skills/{verify-quality → tools/verify-quality}/SKILL.md +0 -0
- /package/skills/{verify-quality → tools/verify-quality}/scripts/quality_checker.py +0 -0
- /package/skills/{verify-security → tools/verify-security}/SKILL.md +0 -0
- /package/skills/{verify-security → tools/verify-security}/scripts/security_scanner.py +0 -0
|
@@ -0,0 +1,1018 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: kubernetes
|
|
3
|
+
description: Kubernetes 容器编排。Helm Chart 开发、Kustomize 配置管理、Operator 模式、CRD 自定义资源、部署策略。当用户提到 K8s、Helm、Kustomize、Operator、CRD、滚动更新、金丝雀部署时使用。
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# 🎯 容器编排 · Kubernetes
|
|
7
|
+
|
|
8
|
+
## Helm Chart 开发
|
|
9
|
+
|
|
10
|
+
### Chart 标准结构
|
|
11
|
+
```
|
|
12
|
+
mychart/
|
|
13
|
+
├── Chart.yaml # Chart 元数据
|
|
14
|
+
├── values.yaml # 默认配置
|
|
15
|
+
├── templates/ # 模板目录
|
|
16
|
+
│ ├── deployment.yaml
|
|
17
|
+
│ ├── service.yaml
|
|
18
|
+
│ ├── ingress.yaml
|
|
19
|
+
│ ├── _helpers.tpl # 模板函数
|
|
20
|
+
│ └── NOTES.txt # 安装提示
|
|
21
|
+
├── charts/ # 依赖 Chart
|
|
22
|
+
└── .helmignore # 忽略文件
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### Chart.yaml
|
|
26
|
+
```yaml
|
|
27
|
+
apiVersion: v2
|
|
28
|
+
name: myapp
|
|
29
|
+
description: A Helm chart for MyApp
|
|
30
|
+
type: application
|
|
31
|
+
version: 1.0.0
|
|
32
|
+
appVersion: "2.3.1"
|
|
33
|
+
|
|
34
|
+
dependencies:
|
|
35
|
+
- name: postgresql
|
|
36
|
+
version: 12.1.0
|
|
37
|
+
repository: https://charts.bitnami.com/bitnami
|
|
38
|
+
condition: postgresql.enabled
|
|
39
|
+
- name: redis
|
|
40
|
+
version: 17.3.0
|
|
41
|
+
repository: https://charts.bitnami.com/bitnami
|
|
42
|
+
condition: redis.enabled
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### values.yaml 设计
|
|
46
|
+
```yaml
|
|
47
|
+
# 镜像配置
|
|
48
|
+
image:
|
|
49
|
+
repository: myapp
|
|
50
|
+
tag: "1.0.0"
|
|
51
|
+
pullPolicy: IfNotPresent
|
|
52
|
+
|
|
53
|
+
# 副本数
|
|
54
|
+
replicaCount: 3
|
|
55
|
+
|
|
56
|
+
# 资源限制
|
|
57
|
+
resources:
|
|
58
|
+
limits:
|
|
59
|
+
cpu: 1000m
|
|
60
|
+
memory: 1Gi
|
|
61
|
+
requests:
|
|
62
|
+
cpu: 500m
|
|
63
|
+
memory: 512Mi
|
|
64
|
+
|
|
65
|
+
# 自动扩缩容
|
|
66
|
+
autoscaling:
|
|
67
|
+
enabled: true
|
|
68
|
+
minReplicas: 2
|
|
69
|
+
maxReplicas: 10
|
|
70
|
+
targetCPUUtilizationPercentage: 80
|
|
71
|
+
targetMemoryUtilizationPercentage: 80
|
|
72
|
+
|
|
73
|
+
# 服务配置
|
|
74
|
+
service:
|
|
75
|
+
type: ClusterIP
|
|
76
|
+
port: 80
|
|
77
|
+
targetPort: 8080
|
|
78
|
+
|
|
79
|
+
# Ingress 配置
|
|
80
|
+
ingress:
|
|
81
|
+
enabled: true
|
|
82
|
+
className: nginx
|
|
83
|
+
annotations:
|
|
84
|
+
cert-manager.io/cluster-issuer: letsencrypt-prod
|
|
85
|
+
hosts:
|
|
86
|
+
- host: myapp.example.com
|
|
87
|
+
paths:
|
|
88
|
+
- path: /
|
|
89
|
+
pathType: Prefix
|
|
90
|
+
tls:
|
|
91
|
+
- secretName: myapp-tls
|
|
92
|
+
hosts:
|
|
93
|
+
- myapp.example.com
|
|
94
|
+
|
|
95
|
+
# 健康检查
|
|
96
|
+
livenessProbe:
|
|
97
|
+
httpGet:
|
|
98
|
+
path: /health
|
|
99
|
+
port: 8080
|
|
100
|
+
initialDelaySeconds: 30
|
|
101
|
+
periodSeconds: 10
|
|
102
|
+
|
|
103
|
+
readinessProbe:
|
|
104
|
+
httpGet:
|
|
105
|
+
path: /ready
|
|
106
|
+
port: 8080
|
|
107
|
+
initialDelaySeconds: 5
|
|
108
|
+
periodSeconds: 5
|
|
109
|
+
|
|
110
|
+
# 环境变量
|
|
111
|
+
env:
|
|
112
|
+
- name: LOG_LEVEL
|
|
113
|
+
value: info
|
|
114
|
+
- name: DB_HOST
|
|
115
|
+
value: postgresql
|
|
116
|
+
|
|
117
|
+
# 密钥引用
|
|
118
|
+
envFrom:
|
|
119
|
+
- secretRef:
|
|
120
|
+
name: myapp-secrets
|
|
121
|
+
- configMapRef:
|
|
122
|
+
name: myapp-config
|
|
123
|
+
|
|
124
|
+
# 持久化存储
|
|
125
|
+
persistence:
|
|
126
|
+
enabled: true
|
|
127
|
+
storageClass: "gp3"
|
|
128
|
+
size: 10Gi
|
|
129
|
+
accessMode: ReadWriteOnce
|
|
130
|
+
|
|
131
|
+
# 依赖服务
|
|
132
|
+
postgresql:
|
|
133
|
+
enabled: true
|
|
134
|
+
auth:
|
|
135
|
+
username: myapp
|
|
136
|
+
database: myapp
|
|
137
|
+
|
|
138
|
+
redis:
|
|
139
|
+
enabled: true
|
|
140
|
+
architecture: standalone
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Deployment 模板
|
|
144
|
+
```yaml
|
|
145
|
+
apiVersion: apps/v1
|
|
146
|
+
kind: Deployment
|
|
147
|
+
metadata:
|
|
148
|
+
name: {{ include "myapp.fullname" . }}
|
|
149
|
+
labels:
|
|
150
|
+
{{- include "myapp.labels" . | nindent 4 }}
|
|
151
|
+
spec:
|
|
152
|
+
{{- if not .Values.autoscaling.enabled }}
|
|
153
|
+
replicas: {{ .Values.replicaCount }}
|
|
154
|
+
{{- end }}
|
|
155
|
+
selector:
|
|
156
|
+
matchLabels:
|
|
157
|
+
{{- include "myapp.selectorLabels" . | nindent 6 }}
|
|
158
|
+
template:
|
|
159
|
+
metadata:
|
|
160
|
+
annotations:
|
|
161
|
+
checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
|
|
162
|
+
labels:
|
|
163
|
+
{{- include "myapp.selectorLabels" . | nindent 8 }}
|
|
164
|
+
spec:
|
|
165
|
+
serviceAccountName: {{ include "myapp.serviceAccountName" . }}
|
|
166
|
+
securityContext:
|
|
167
|
+
runAsNonRoot: true
|
|
168
|
+
runAsUser: 1000
|
|
169
|
+
fsGroup: 1000
|
|
170
|
+
containers:
|
|
171
|
+
- name: {{ .Chart.Name }}
|
|
172
|
+
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
|
|
173
|
+
imagePullPolicy: {{ .Values.image.pullPolicy }}
|
|
174
|
+
ports:
|
|
175
|
+
- name: http
|
|
176
|
+
containerPort: {{ .Values.service.targetPort }}
|
|
177
|
+
protocol: TCP
|
|
178
|
+
{{- if .Values.livenessProbe }}
|
|
179
|
+
livenessProbe:
|
|
180
|
+
{{- toYaml .Values.livenessProbe | nindent 10 }}
|
|
181
|
+
{{- end }}
|
|
182
|
+
{{- if .Values.readinessProbe }}
|
|
183
|
+
readinessProbe:
|
|
184
|
+
{{- toYaml .Values.readinessProbe | nindent 10 }}
|
|
185
|
+
{{- end }}
|
|
186
|
+
resources:
|
|
187
|
+
{{- toYaml .Values.resources | nindent 10 }}
|
|
188
|
+
{{- if .Values.env }}
|
|
189
|
+
env:
|
|
190
|
+
{{- toYaml .Values.env | nindent 10 }}
|
|
191
|
+
{{- end }}
|
|
192
|
+
{{- if .Values.envFrom }}
|
|
193
|
+
envFrom:
|
|
194
|
+
{{- toYaml .Values.envFrom | nindent 10 }}
|
|
195
|
+
{{- end }}
|
|
196
|
+
{{- if .Values.persistence.enabled }}
|
|
197
|
+
volumeMounts:
|
|
198
|
+
- name: data
|
|
199
|
+
mountPath: /data
|
|
200
|
+
{{- end }}
|
|
201
|
+
{{- if .Values.persistence.enabled }}
|
|
202
|
+
volumes:
|
|
203
|
+
- name: data
|
|
204
|
+
persistentVolumeClaim:
|
|
205
|
+
claimName: {{ include "myapp.fullname" . }}
|
|
206
|
+
{{- end }}
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### _helpers.tpl 模板函数
|
|
210
|
+
```yaml
|
|
211
|
+
{{/*
|
|
212
|
+
Expand the name of the chart.
|
|
213
|
+
*/}}
|
|
214
|
+
{{- define "myapp.name" -}}
|
|
215
|
+
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
|
|
216
|
+
{{- end }}
|
|
217
|
+
|
|
218
|
+
{{/*
|
|
219
|
+
Create a default fully qualified app name.
|
|
220
|
+
*/}}
|
|
221
|
+
{{- define "myapp.fullname" -}}
|
|
222
|
+
{{- if .Values.fullnameOverride }}
|
|
223
|
+
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
|
|
224
|
+
{{- else }}
|
|
225
|
+
{{- $name := default .Chart.Name .Values.nameOverride }}
|
|
226
|
+
{{- if contains $name .Release.Name }}
|
|
227
|
+
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
|
|
228
|
+
{{- else }}
|
|
229
|
+
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
|
|
230
|
+
{{- end }}
|
|
231
|
+
{{- end }}
|
|
232
|
+
{{- end }}
|
|
233
|
+
|
|
234
|
+
{{/*
|
|
235
|
+
Common labels
|
|
236
|
+
*/}}
|
|
237
|
+
{{- define "myapp.labels" -}}
|
|
238
|
+
helm.sh/chart: {{ include "myapp.chart" . }}
|
|
239
|
+
{{ include "myapp.selectorLabels" . }}
|
|
240
|
+
{{- if .Chart.AppVersion }}
|
|
241
|
+
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
|
|
242
|
+
{{- end }}
|
|
243
|
+
app.kubernetes.io/managed-by: {{ .Release.Service }}
|
|
244
|
+
{{- end }}
|
|
245
|
+
|
|
246
|
+
{{/*
|
|
247
|
+
Selector labels
|
|
248
|
+
*/}}
|
|
249
|
+
{{- define "myapp.selectorLabels" -}}
|
|
250
|
+
app.kubernetes.io/name: {{ include "myapp.name" . }}
|
|
251
|
+
app.kubernetes.io/instance: {{ .Release.Name }}
|
|
252
|
+
{{- end }}
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### Helm 命令
|
|
256
|
+
```bash
|
|
257
|
+
# 创建 Chart
|
|
258
|
+
helm create myapp
|
|
259
|
+
|
|
260
|
+
# 验证模板
|
|
261
|
+
helm lint myapp/
|
|
262
|
+
helm template myapp myapp/ --debug
|
|
263
|
+
|
|
264
|
+
# 安装
|
|
265
|
+
helm install myapp myapp/ -n production --create-namespace
|
|
266
|
+
|
|
267
|
+
# 使用自定义 values
|
|
268
|
+
helm install myapp myapp/ -f values-prod.yaml
|
|
269
|
+
|
|
270
|
+
# 升级
|
|
271
|
+
helm upgrade myapp myapp/ --reuse-values
|
|
272
|
+
|
|
273
|
+
# 回滚
|
|
274
|
+
helm rollback myapp 1
|
|
275
|
+
|
|
276
|
+
# 查看历史
|
|
277
|
+
helm history myapp
|
|
278
|
+
|
|
279
|
+
# 卸载
|
|
280
|
+
helm uninstall myapp
|
|
281
|
+
|
|
282
|
+
# 打包
|
|
283
|
+
helm package myapp/
|
|
284
|
+
|
|
285
|
+
# 推送到 OCI Registry
|
|
286
|
+
helm push myapp-1.0.0.tgz oci://registry.example.com/charts
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
## Kustomize 配置管理
|
|
290
|
+
|
|
291
|
+
### 目录结构
|
|
292
|
+
```
|
|
293
|
+
kustomize/
|
|
294
|
+
├── base/ # 基础配置
|
|
295
|
+
│ ├── kustomization.yaml
|
|
296
|
+
│ ├── deployment.yaml
|
|
297
|
+
│ ├── service.yaml
|
|
298
|
+
│ └── configmap.yaml
|
|
299
|
+
└── overlays/ # 环境差异
|
|
300
|
+
├── dev/
|
|
301
|
+
│ ├── kustomization.yaml
|
|
302
|
+
│ └── patch-replicas.yaml
|
|
303
|
+
├── staging/
|
|
304
|
+
│ ├── kustomization.yaml
|
|
305
|
+
│ └── patch-resources.yaml
|
|
306
|
+
└── production/
|
|
307
|
+
├── kustomization.yaml
|
|
308
|
+
├── patch-replicas.yaml
|
|
309
|
+
└── patch-hpa.yaml
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
### base/kustomization.yaml
|
|
313
|
+
```yaml
|
|
314
|
+
apiVersion: kustomize.config.k8s.io/v1beta1
|
|
315
|
+
kind: Kustomization
|
|
316
|
+
|
|
317
|
+
resources:
|
|
318
|
+
- deployment.yaml
|
|
319
|
+
- service.yaml
|
|
320
|
+
- configmap.yaml
|
|
321
|
+
|
|
322
|
+
commonLabels:
|
|
323
|
+
app: myapp
|
|
324
|
+
managed-by: kustomize
|
|
325
|
+
|
|
326
|
+
commonAnnotations:
|
|
327
|
+
version: "1.0.0"
|
|
328
|
+
|
|
329
|
+
images:
|
|
330
|
+
- name: myapp
|
|
331
|
+
newName: registry.example.com/myapp
|
|
332
|
+
newTag: latest
|
|
333
|
+
|
|
334
|
+
configMapGenerator:
|
|
335
|
+
- name: myapp-config
|
|
336
|
+
literals:
|
|
337
|
+
- LOG_LEVEL=info
|
|
338
|
+
- MAX_CONNECTIONS=100
|
|
339
|
+
|
|
340
|
+
secretGenerator:
|
|
341
|
+
- name: myapp-secrets
|
|
342
|
+
literals:
|
|
343
|
+
- DB_PASSWORD=changeme
|
|
344
|
+
type: Opaque
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
### overlays/production/kustomization.yaml
|
|
348
|
+
```yaml
|
|
349
|
+
apiVersion: kustomize.config.k8s.io/v1beta1
|
|
350
|
+
kind: Kustomization
|
|
351
|
+
|
|
352
|
+
namespace: production
|
|
353
|
+
|
|
354
|
+
bases:
|
|
355
|
+
- ../../base
|
|
356
|
+
|
|
357
|
+
patchesStrategicMerge:
|
|
358
|
+
- patch-replicas.yaml
|
|
359
|
+
|
|
360
|
+
patchesJson6902:
|
|
361
|
+
- target:
|
|
362
|
+
group: apps
|
|
363
|
+
version: v1
|
|
364
|
+
kind: Deployment
|
|
365
|
+
name: myapp
|
|
366
|
+
path: patch-resources.json
|
|
367
|
+
|
|
368
|
+
images:
|
|
369
|
+
- name: myapp
|
|
370
|
+
newTag: v1.2.3
|
|
371
|
+
|
|
372
|
+
replicas:
|
|
373
|
+
- name: myapp
|
|
374
|
+
count: 5
|
|
375
|
+
|
|
376
|
+
configMapGenerator:
|
|
377
|
+
- name: myapp-config
|
|
378
|
+
behavior: merge
|
|
379
|
+
literals:
|
|
380
|
+
- LOG_LEVEL=warn
|
|
381
|
+
- MAX_CONNECTIONS=500
|
|
382
|
+
|
|
383
|
+
resources:
|
|
384
|
+
- hpa.yaml
|
|
385
|
+
- pdb.yaml
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
### patch-replicas.yaml
|
|
389
|
+
```yaml
|
|
390
|
+
apiVersion: apps/v1
|
|
391
|
+
kind: Deployment
|
|
392
|
+
metadata:
|
|
393
|
+
name: myapp
|
|
394
|
+
spec:
|
|
395
|
+
replicas: 5
|
|
396
|
+
template:
|
|
397
|
+
spec:
|
|
398
|
+
containers:
|
|
399
|
+
- name: myapp
|
|
400
|
+
resources:
|
|
401
|
+
limits:
|
|
402
|
+
cpu: 2000m
|
|
403
|
+
memory: 2Gi
|
|
404
|
+
requests:
|
|
405
|
+
cpu: 1000m
|
|
406
|
+
memory: 1Gi
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
### patch-resources.json
|
|
410
|
+
```json
|
|
411
|
+
[
|
|
412
|
+
{
|
|
413
|
+
"op": "replace",
|
|
414
|
+
"path": "/spec/template/spec/containers/0/resources/limits/cpu",
|
|
415
|
+
"value": "2000m"
|
|
416
|
+
},
|
|
417
|
+
{
|
|
418
|
+
"op": "add",
|
|
419
|
+
"path": "/spec/template/spec/containers/0/env/-",
|
|
420
|
+
"value": {
|
|
421
|
+
"name": "ENVIRONMENT",
|
|
422
|
+
"value": "production"
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
]
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
### Kustomize 命令
|
|
429
|
+
```bash
|
|
430
|
+
# 查看生成的 YAML
|
|
431
|
+
kustomize build overlays/production
|
|
432
|
+
|
|
433
|
+
# 应用配置
|
|
434
|
+
kubectl apply -k overlays/production
|
|
435
|
+
|
|
436
|
+
# 查看差异
|
|
437
|
+
kubectl diff -k overlays/production
|
|
438
|
+
|
|
439
|
+
# 删除资源
|
|
440
|
+
kubectl delete -k overlays/production
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
## Operator 模式
|
|
444
|
+
|
|
445
|
+
### CRD 定义
|
|
446
|
+
```yaml
|
|
447
|
+
apiVersion: apiextensions.k8s.io/v1
|
|
448
|
+
kind: CustomResourceDefinition
|
|
449
|
+
metadata:
|
|
450
|
+
name: myapps.example.com
|
|
451
|
+
spec:
|
|
452
|
+
group: example.com
|
|
453
|
+
versions:
|
|
454
|
+
- name: v1
|
|
455
|
+
served: true
|
|
456
|
+
storage: true
|
|
457
|
+
schema:
|
|
458
|
+
openAPIV3Schema:
|
|
459
|
+
type: object
|
|
460
|
+
properties:
|
|
461
|
+
spec:
|
|
462
|
+
type: object
|
|
463
|
+
properties:
|
|
464
|
+
replicas:
|
|
465
|
+
type: integer
|
|
466
|
+
minimum: 1
|
|
467
|
+
maximum: 10
|
|
468
|
+
version:
|
|
469
|
+
type: string
|
|
470
|
+
pattern: '^v[0-9]+\.[0-9]+\.[0-9]+$'
|
|
471
|
+
database:
|
|
472
|
+
type: object
|
|
473
|
+
properties:
|
|
474
|
+
enabled:
|
|
475
|
+
type: boolean
|
|
476
|
+
size:
|
|
477
|
+
type: string
|
|
478
|
+
enum: [small, medium, large]
|
|
479
|
+
required:
|
|
480
|
+
- replicas
|
|
481
|
+
- version
|
|
482
|
+
status:
|
|
483
|
+
type: object
|
|
484
|
+
properties:
|
|
485
|
+
phase:
|
|
486
|
+
type: string
|
|
487
|
+
enum: [Pending, Running, Failed]
|
|
488
|
+
conditions:
|
|
489
|
+
type: array
|
|
490
|
+
items:
|
|
491
|
+
type: object
|
|
492
|
+
properties:
|
|
493
|
+
type:
|
|
494
|
+
type: string
|
|
495
|
+
status:
|
|
496
|
+
type: string
|
|
497
|
+
lastTransitionTime:
|
|
498
|
+
type: string
|
|
499
|
+
format: date-time
|
|
500
|
+
subresources:
|
|
501
|
+
status: {}
|
|
502
|
+
scale:
|
|
503
|
+
specReplicasPath: .spec.replicas
|
|
504
|
+
statusReplicasPath: .status.replicas
|
|
505
|
+
scope: Namespaced
|
|
506
|
+
names:
|
|
507
|
+
plural: myapps
|
|
508
|
+
singular: myapp
|
|
509
|
+
kind: MyApp
|
|
510
|
+
shortNames:
|
|
511
|
+
- ma
|
|
512
|
+
```
|
|
513
|
+
|
|
514
|
+
### 自定义资源实例
|
|
515
|
+
```yaml
|
|
516
|
+
apiVersion: example.com/v1
|
|
517
|
+
kind: MyApp
|
|
518
|
+
metadata:
|
|
519
|
+
name: myapp-sample
|
|
520
|
+
namespace: default
|
|
521
|
+
spec:
|
|
522
|
+
replicas: 3
|
|
523
|
+
version: v1.2.3
|
|
524
|
+
database:
|
|
525
|
+
enabled: true
|
|
526
|
+
size: medium
|
|
527
|
+
```
|
|
528
|
+
|
|
529
|
+
### Operator Controller (Go)
|
|
530
|
+
```go
|
|
531
|
+
package controllers
|
|
532
|
+
|
|
533
|
+
import (
|
|
534
|
+
"context"
|
|
535
|
+
appsv1 "k8s.io/api/apps/v1"
|
|
536
|
+
corev1 "k8s.io/api/core/v1"
|
|
537
|
+
"k8s.io/apimachinery/pkg/api/errors"
|
|
538
|
+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
539
|
+
"k8s.io/apimachinery/pkg/runtime"
|
|
540
|
+
ctrl "sigs.k8s.io/controller-runtime"
|
|
541
|
+
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
542
|
+
|
|
543
|
+
examplev1 "example.com/myapp-operator/api/v1"
|
|
544
|
+
)
|
|
545
|
+
|
|
546
|
+
type MyAppReconciler struct {
|
|
547
|
+
client.Client
|
|
548
|
+
Scheme *runtime.Scheme
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
// +kubebuilder:rbac:groups=example.com,resources=myapps,verbs=get;list;watch;create;update;patch;delete
|
|
552
|
+
// +kubebuilder:rbac:groups=example.com,resources=myapps/status,verbs=get;update;patch
|
|
553
|
+
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
|
|
554
|
+
|
|
555
|
+
func (r *MyAppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
|
556
|
+
// 获取 MyApp 实例
|
|
557
|
+
myapp := &examplev1.MyApp{}
|
|
558
|
+
if err := r.Get(ctx, req.NamespacedName, myapp); err != nil {
|
|
559
|
+
if errors.IsNotFound(err) {
|
|
560
|
+
return ctrl.Result{}, nil
|
|
561
|
+
}
|
|
562
|
+
return ctrl.Result{}, err
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// 构建期望的 Deployment
|
|
566
|
+
deployment := r.deploymentForMyApp(myapp)
|
|
567
|
+
|
|
568
|
+
// 检查 Deployment 是否存在
|
|
569
|
+
found := &appsv1.Deployment{}
|
|
570
|
+
err := r.Get(ctx, client.ObjectKeyFromObject(deployment), found)
|
|
571
|
+
if err != nil && errors.IsNotFound(err) {
|
|
572
|
+
// 创建 Deployment
|
|
573
|
+
if err := r.Create(ctx, deployment); err != nil {
|
|
574
|
+
return ctrl.Result{}, err
|
|
575
|
+
}
|
|
576
|
+
return ctrl.Result{Requeue: true}, nil
|
|
577
|
+
} else if err != nil {
|
|
578
|
+
return ctrl.Result{}, err
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
// 更新 Deployment
|
|
582
|
+
if found.Spec.Replicas != myapp.Spec.Replicas {
|
|
583
|
+
found.Spec.Replicas = myapp.Spec.Replicas
|
|
584
|
+
if err := r.Update(ctx, found); err != nil {
|
|
585
|
+
return ctrl.Result{}, err
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
// 更新状态
|
|
590
|
+
myapp.Status.Phase = "Running"
|
|
591
|
+
myapp.Status.Replicas = found.Status.ReadyReplicas
|
|
592
|
+
if err := r.Status().Update(ctx, myapp); err != nil {
|
|
593
|
+
return ctrl.Result{}, err
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
return ctrl.Result{}, nil
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
func (r *MyAppReconciler) deploymentForMyApp(m *examplev1.MyApp) *appsv1.Deployment {
|
|
600
|
+
labels := map[string]string{
|
|
601
|
+
"app": m.Name,
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
return &appsv1.Deployment{
|
|
605
|
+
ObjectMeta: metav1.ObjectMeta{
|
|
606
|
+
Name: m.Name,
|
|
607
|
+
Namespace: m.Namespace,
|
|
608
|
+
OwnerReferences: []metav1.OwnerReference{
|
|
609
|
+
*metav1.NewControllerRef(m, examplev1.GroupVersion.WithKind("MyApp")),
|
|
610
|
+
},
|
|
611
|
+
},
|
|
612
|
+
Spec: appsv1.DeploymentSpec{
|
|
613
|
+
Replicas: m.Spec.Replicas,
|
|
614
|
+
Selector: &metav1.LabelSelector{
|
|
615
|
+
MatchLabels: labels,
|
|
616
|
+
},
|
|
617
|
+
Template: corev1.PodTemplateSpec{
|
|
618
|
+
ObjectMeta: metav1.ObjectMeta{
|
|
619
|
+
Labels: labels,
|
|
620
|
+
},
|
|
621
|
+
Spec: corev1.PodSpec{
|
|
622
|
+
Containers: []corev1.Container{{
|
|
623
|
+
Name: "myapp",
|
|
624
|
+
Image: "myapp:" + m.Spec.Version,
|
|
625
|
+
Ports: []corev1.ContainerPort{{
|
|
626
|
+
ContainerPort: 8080,
|
|
627
|
+
}},
|
|
628
|
+
}},
|
|
629
|
+
},
|
|
630
|
+
},
|
|
631
|
+
},
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
func (r *MyAppReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
|
636
|
+
return ctrl.NewControllerManagedBy(mgr).
|
|
637
|
+
For(&examplev1.MyApp{}).
|
|
638
|
+
Owns(&appsv1.Deployment{}).
|
|
639
|
+
Complete(r)
|
|
640
|
+
}
|
|
641
|
+
```
|
|
642
|
+
|
|
643
|
+
### Operator 初始化
|
|
644
|
+
```bash
|
|
645
|
+
# 使用 Operator SDK
|
|
646
|
+
operator-sdk init --domain example.com --repo example.com/myapp-operator
|
|
647
|
+
|
|
648
|
+
# 创建 API
|
|
649
|
+
operator-sdk create api --group example --version v1 --kind MyApp --resource --controller
|
|
650
|
+
|
|
651
|
+
# 生成 CRD
|
|
652
|
+
make manifests
|
|
653
|
+
|
|
654
|
+
# 安装 CRD
|
|
655
|
+
make install
|
|
656
|
+
|
|
657
|
+
# 运行 Operator
|
|
658
|
+
make run
|
|
659
|
+
|
|
660
|
+
# 构建镜像
|
|
661
|
+
make docker-build docker-push IMG=myapp-operator:v1.0.0
|
|
662
|
+
|
|
663
|
+
# 部署
|
|
664
|
+
make deploy IMG=myapp-operator:v1.0.0
|
|
665
|
+
```
|
|
666
|
+
|
|
667
|
+
## 部署策略
|
|
668
|
+
|
|
669
|
+
### 滚动更新 (Rolling Update)
|
|
670
|
+
```yaml
|
|
671
|
+
apiVersion: apps/v1
|
|
672
|
+
kind: Deployment
|
|
673
|
+
metadata:
|
|
674
|
+
name: myapp
|
|
675
|
+
spec:
|
|
676
|
+
replicas: 10
|
|
677
|
+
strategy:
|
|
678
|
+
type: RollingUpdate
|
|
679
|
+
rollingUpdate:
|
|
680
|
+
maxSurge: 2 # 最多超出期望副本数
|
|
681
|
+
maxUnavailable: 1 # 最多不可用副本数
|
|
682
|
+
template:
|
|
683
|
+
spec:
|
|
684
|
+
containers:
|
|
685
|
+
- name: myapp
|
|
686
|
+
image: myapp:v2
|
|
687
|
+
readinessProbe:
|
|
688
|
+
httpGet:
|
|
689
|
+
path: /ready
|
|
690
|
+
port: 8080
|
|
691
|
+
initialDelaySeconds: 5
|
|
692
|
+
periodSeconds: 5
|
|
693
|
+
```
|
|
694
|
+
|
|
695
|
+
### 蓝绿部署 (Blue-Green)
|
|
696
|
+
```yaml
|
|
697
|
+
# Blue (当前版本)
|
|
698
|
+
apiVersion: apps/v1
|
|
699
|
+
kind: Deployment
|
|
700
|
+
metadata:
|
|
701
|
+
name: myapp-blue
|
|
702
|
+
labels:
|
|
703
|
+
version: blue
|
|
704
|
+
spec:
|
|
705
|
+
replicas: 3
|
|
706
|
+
selector:
|
|
707
|
+
matchLabels:
|
|
708
|
+
app: myapp
|
|
709
|
+
version: blue
|
|
710
|
+
template:
|
|
711
|
+
metadata:
|
|
712
|
+
labels:
|
|
713
|
+
app: myapp
|
|
714
|
+
version: blue
|
|
715
|
+
spec:
|
|
716
|
+
containers:
|
|
717
|
+
- name: myapp
|
|
718
|
+
image: myapp:v1
|
|
719
|
+
|
|
720
|
+
---
|
|
721
|
+
# Green (新版本)
|
|
722
|
+
apiVersion: apps/v1
|
|
723
|
+
kind: Deployment
|
|
724
|
+
metadata:
|
|
725
|
+
name: myapp-green
|
|
726
|
+
labels:
|
|
727
|
+
version: green
|
|
728
|
+
spec:
|
|
729
|
+
replicas: 3
|
|
730
|
+
selector:
|
|
731
|
+
matchLabels:
|
|
732
|
+
app: myapp
|
|
733
|
+
version: green
|
|
734
|
+
template:
|
|
735
|
+
metadata:
|
|
736
|
+
labels:
|
|
737
|
+
app: myapp
|
|
738
|
+
version: green
|
|
739
|
+
spec:
|
|
740
|
+
containers:
|
|
741
|
+
- name: myapp
|
|
742
|
+
image: myapp:v2
|
|
743
|
+
|
|
744
|
+
---
|
|
745
|
+
# Service 切换
|
|
746
|
+
apiVersion: v1
|
|
747
|
+
kind: Service
|
|
748
|
+
metadata:
|
|
749
|
+
name: myapp
|
|
750
|
+
spec:
|
|
751
|
+
selector:
|
|
752
|
+
app: myapp
|
|
753
|
+
version: blue # 切换到 green 实现蓝绿部署
|
|
754
|
+
ports:
|
|
755
|
+
- port: 80
|
|
756
|
+
targetPort: 8080
|
|
757
|
+
```
|
|
758
|
+
|
|
759
|
+
### 金丝雀部署 (Canary)
|
|
760
|
+
```yaml
|
|
761
|
+
# 稳定版本
|
|
762
|
+
apiVersion: apps/v1
|
|
763
|
+
kind: Deployment
|
|
764
|
+
metadata:
|
|
765
|
+
name: myapp-stable
|
|
766
|
+
spec:
|
|
767
|
+
replicas: 9
|
|
768
|
+
selector:
|
|
769
|
+
matchLabels:
|
|
770
|
+
app: myapp
|
|
771
|
+
track: stable
|
|
772
|
+
template:
|
|
773
|
+
metadata:
|
|
774
|
+
labels:
|
|
775
|
+
app: myapp
|
|
776
|
+
track: stable
|
|
777
|
+
spec:
|
|
778
|
+
containers:
|
|
779
|
+
- name: myapp
|
|
780
|
+
image: myapp:v1
|
|
781
|
+
|
|
782
|
+
---
|
|
783
|
+
# 金丝雀版本 (10% 流量)
|
|
784
|
+
apiVersion: apps/v1
|
|
785
|
+
kind: Deployment
|
|
786
|
+
metadata:
|
|
787
|
+
name: myapp-canary
|
|
788
|
+
spec:
|
|
789
|
+
replicas: 1
|
|
790
|
+
selector:
|
|
791
|
+
matchLabels:
|
|
792
|
+
app: myapp
|
|
793
|
+
track: canary
|
|
794
|
+
template:
|
|
795
|
+
metadata:
|
|
796
|
+
labels:
|
|
797
|
+
app: myapp
|
|
798
|
+
track: canary
|
|
799
|
+
spec:
|
|
800
|
+
containers:
|
|
801
|
+
- name: myapp
|
|
802
|
+
image: myapp:v2
|
|
803
|
+
|
|
804
|
+
---
|
|
805
|
+
# Service 同时指向两个版本
|
|
806
|
+
apiVersion: v1
|
|
807
|
+
kind: Service
|
|
808
|
+
metadata:
|
|
809
|
+
name: myapp
|
|
810
|
+
spec:
|
|
811
|
+
selector:
|
|
812
|
+
app: myapp # 匹配 stable 和 canary
|
|
813
|
+
ports:
|
|
814
|
+
- port: 80
|
|
815
|
+
targetPort: 8080
|
|
816
|
+
```
|
|
817
|
+
|
|
818
|
+
### Flagger 自动金丝雀
|
|
819
|
+
```yaml
|
|
820
|
+
apiVersion: flagger.app/v1beta1
|
|
821
|
+
kind: Canary
|
|
822
|
+
metadata:
|
|
823
|
+
name: myapp
|
|
824
|
+
spec:
|
|
825
|
+
targetRef:
|
|
826
|
+
apiVersion: apps/v1
|
|
827
|
+
kind: Deployment
|
|
828
|
+
name: myapp
|
|
829
|
+
service:
|
|
830
|
+
port: 80
|
|
831
|
+
targetPort: 8080
|
|
832
|
+
analysis:
|
|
833
|
+
interval: 1m
|
|
834
|
+
threshold: 5
|
|
835
|
+
maxWeight: 50
|
|
836
|
+
stepWeight: 10
|
|
837
|
+
metrics:
|
|
838
|
+
- name: request-success-rate
|
|
839
|
+
thresholdRange:
|
|
840
|
+
min: 99
|
|
841
|
+
interval: 1m
|
|
842
|
+
- name: request-duration
|
|
843
|
+
thresholdRange:
|
|
844
|
+
max: 500
|
|
845
|
+
interval: 1m
|
|
846
|
+
webhooks:
|
|
847
|
+
- name: load-test
|
|
848
|
+
url: http://flagger-loadtester/
|
|
849
|
+
timeout: 5s
|
|
850
|
+
metadata:
|
|
851
|
+
cmd: "hey -z 1m -q 10 -c 2 http://myapp-canary/"
|
|
852
|
+
```
|
|
853
|
+
|
|
854
|
+
## HPA 自动扩缩容
|
|
855
|
+
|
|
856
|
+
### 基于 CPU/内存
|
|
857
|
+
```yaml
|
|
858
|
+
apiVersion: autoscaling/v2
|
|
859
|
+
kind: HorizontalPodAutoscaler
|
|
860
|
+
metadata:
|
|
861
|
+
name: myapp-hpa
|
|
862
|
+
spec:
|
|
863
|
+
scaleTargetRef:
|
|
864
|
+
apiVersion: apps/v1
|
|
865
|
+
kind: Deployment
|
|
866
|
+
name: myapp
|
|
867
|
+
minReplicas: 2
|
|
868
|
+
maxReplicas: 10
|
|
869
|
+
metrics:
|
|
870
|
+
- type: Resource
|
|
871
|
+
resource:
|
|
872
|
+
name: cpu
|
|
873
|
+
target:
|
|
874
|
+
type: Utilization
|
|
875
|
+
averageUtilization: 70
|
|
876
|
+
- type: Resource
|
|
877
|
+
resource:
|
|
878
|
+
name: memory
|
|
879
|
+
target:
|
|
880
|
+
type: Utilization
|
|
881
|
+
averageUtilization: 80
|
|
882
|
+
behavior:
|
|
883
|
+
scaleDown:
|
|
884
|
+
stabilizationWindowSeconds: 300
|
|
885
|
+
policies:
|
|
886
|
+
- type: Percent
|
|
887
|
+
value: 50
|
|
888
|
+
periodSeconds: 60
|
|
889
|
+
scaleUp:
|
|
890
|
+
stabilizationWindowSeconds: 0
|
|
891
|
+
policies:
|
|
892
|
+
- type: Percent
|
|
893
|
+
value: 100
|
|
894
|
+
periodSeconds: 30
|
|
895
|
+
- type: Pods
|
|
896
|
+
value: 4
|
|
897
|
+
periodSeconds: 30
|
|
898
|
+
selectPolicy: Max
|
|
899
|
+
```
|
|
900
|
+
|
|
901
|
+
### 基于自定义指标
|
|
902
|
+
```yaml
|
|
903
|
+
apiVersion: autoscaling/v2
|
|
904
|
+
kind: HorizontalPodAutoscaler
|
|
905
|
+
metadata:
|
|
906
|
+
name: myapp-hpa-custom
|
|
907
|
+
spec:
|
|
908
|
+
scaleTargetRef:
|
|
909
|
+
apiVersion: apps/v1
|
|
910
|
+
kind: Deployment
|
|
911
|
+
name: myapp
|
|
912
|
+
minReplicas: 2
|
|
913
|
+
maxReplicas: 20
|
|
914
|
+
metrics:
|
|
915
|
+
- type: Pods
|
|
916
|
+
pods:
|
|
917
|
+
metric:
|
|
918
|
+
name: http_requests_per_second
|
|
919
|
+
target:
|
|
920
|
+
type: AverageValue
|
|
921
|
+
averageValue: "1000"
|
|
922
|
+
- type: External
|
|
923
|
+
external:
|
|
924
|
+
metric:
|
|
925
|
+
name: queue_messages_ready
|
|
926
|
+
selector:
|
|
927
|
+
matchLabels:
|
|
928
|
+
queue: myapp-queue
|
|
929
|
+
target:
|
|
930
|
+
type: AverageValue
|
|
931
|
+
averageValue: "30"
|
|
932
|
+
```
|
|
933
|
+
|
|
934
|
+
## PDB 防止中断
|
|
935
|
+
|
|
936
|
+
```yaml
|
|
937
|
+
apiVersion: policy/v1
|
|
938
|
+
kind: PodDisruptionBudget
|
|
939
|
+
metadata:
|
|
940
|
+
name: myapp-pdb
|
|
941
|
+
spec:
|
|
942
|
+
minAvailable: 2 # 或 maxUnavailable: 1
|
|
943
|
+
selector:
|
|
944
|
+
matchLabels:
|
|
945
|
+
app: myapp
|
|
946
|
+
```
|
|
947
|
+
|
|
948
|
+
## 资源配额与限制
|
|
949
|
+
|
|
950
|
+
### ResourceQuota
|
|
951
|
+
```yaml
|
|
952
|
+
apiVersion: v1
|
|
953
|
+
kind: ResourceQuota
|
|
954
|
+
metadata:
|
|
955
|
+
name: compute-quota
|
|
956
|
+
namespace: production
|
|
957
|
+
spec:
|
|
958
|
+
hard:
|
|
959
|
+
requests.cpu: "100"
|
|
960
|
+
requests.memory: 200Gi
|
|
961
|
+
limits.cpu: "200"
|
|
962
|
+
limits.memory: 400Gi
|
|
963
|
+
persistentvolumeclaims: "10"
|
|
964
|
+
requests.storage: 500Gi
|
|
965
|
+
```
|
|
966
|
+
|
|
967
|
+
### LimitRange
|
|
968
|
+
```yaml
|
|
969
|
+
apiVersion: v1
|
|
970
|
+
kind: LimitRange
|
|
971
|
+
metadata:
|
|
972
|
+
name: limit-range
|
|
973
|
+
namespace: production
|
|
974
|
+
spec:
|
|
975
|
+
limits:
|
|
976
|
+
- max:
|
|
977
|
+
cpu: "4"
|
|
978
|
+
memory: 8Gi
|
|
979
|
+
min:
|
|
980
|
+
cpu: 100m
|
|
981
|
+
memory: 128Mi
|
|
982
|
+
default:
|
|
983
|
+
cpu: 500m
|
|
984
|
+
memory: 512Mi
|
|
985
|
+
defaultRequest:
|
|
986
|
+
cpu: 200m
|
|
987
|
+
memory: 256Mi
|
|
988
|
+
type: Container
|
|
989
|
+
- max:
|
|
990
|
+
storage: 10Gi
|
|
991
|
+
min:
|
|
992
|
+
storage: 1Gi
|
|
993
|
+
type: PersistentVolumeClaim
|
|
994
|
+
```
|
|
995
|
+
|
|
996
|
+
## 最佳实践
|
|
997
|
+
|
|
998
|
+
| 实践 | 说明 |
|
|
999
|
+
|------|------|
|
|
1000
|
+
| 使用 Helm 模板化 | 复用配置,多环境部署 |
|
|
1001
|
+
| Kustomize 管理差异 | Base + Overlay 分离基础和环境配置 |
|
|
1002
|
+
| 健康检查必配 | Liveness + Readiness 保证服务可用性 |
|
|
1003
|
+
| 资源限制必设 | Requests + Limits 防止资源耗尽 |
|
|
1004
|
+
| 使用 HPA | 自动扩缩容应对流量波动 |
|
|
1005
|
+
| 配置 PDB | 防止滚动更新时服务中断 |
|
|
1006
|
+
| 密钥外部化 | 使用 External Secrets Operator |
|
|
1007
|
+
| 镜像使用 Digest | 确保部署一致性 |
|
|
1008
|
+
| 多副本部署 | 至少 2 个副本保证高可用 |
|
|
1009
|
+
| 亲和性配置 | Pod 反亲和性分散到不同节点 |
|
|
1010
|
+
|
|
1011
|
+
## 工具对比
|
|
1012
|
+
|
|
1013
|
+
| 工具 | 优势 | 劣势 | 适用场景 |
|
|
1014
|
+
|------|------|------|----------|
|
|
1015
|
+
| Helm | 模板强大、生态丰富 | 学习曲线陡峭 | 复杂应用、多环境 |
|
|
1016
|
+
| Kustomize | 原生支持、无模板 | 功能相对简单 | 简单应用、环境差异 |
|
|
1017
|
+
| Operator | 自动化运维、领域知识 | 开发复杂 | 有状态应用、复杂生命周期 |
|
|
1018
|
+
| Flagger | 自动金丝雀、渐进式交付 | 依赖 Service Mesh | 生产环境渐进式发布 |
|