
Kubernetes Deployment是管理应用部署的核心控制器,它提供了一种声明式的方式来定义应用的期望状态。通过Deployment可以轻松实现应用的部署、滚动更新、回滚和扩缩容等操作。
能力 | 说明 |
|---|---|
滚动更新 | 零停机逐步替换旧版本 Pod |
回滚 | 可快速回退到任意历史版本 |
扩缩容 | 手动或自动(HPA)调整副本数 |
自愈 | 节点故障时自动重新调度 Pod |
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-app:1.0.0
ports:
- containerPort: 8080metadata:
namespace: production
labels:
environment: prod
team: backendspec:
template:
spec:
containers:
- name: my-app
resources:
limits:
cpu: "1"
memory: "1Gi"
requests:
cpu: "500m"
memory: "512Mi"
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 5spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
minReadySeconds: 30
revisionHistoryLimit: 10spec:
template:
spec:
containers:
- name: my-app
env:
- name: ENVIRONMENT
value: "production"
- name: DB_HOST
valueFrom:
secretKeyRef:
name: db-secret
key: hostspec:
template:
spec:
containers:
- name: my-app
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: app-configspec:
template:
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/arch
operator: In
values:
- amd64spec:
template:
spec:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: app
operator: In
values:
- my-app
topologyKey: kubernetes.io/hostnametolerations:
- key: "gpu"
operator: "Equal"
value: "true"
effect: "NoSchedule"securityContext:
runAsNonRoot: true
runAsUser: 1000
fsGroup: 2000serviceAccountName: my-app-saspec:
paused: true # 暂停滚动更新,便于手动调试apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
namespace: production
labels:
app: my-app
environment: prod
spec:
replicas: 3
selector:
matchLabels:
app: my-app
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
minReadySeconds: 30
revisionHistoryLimit: 10
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-app:1.0.0
ports:
- containerPort: 8080
env:
- name: ENVIRONMENT
value: "production"
resources:
limits:
cpu: "1"
memory: "1Gi"
requests:
cpu: "500m"
memory: "512Mi"
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: app-config
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/arch
operator: In
values:
- amd64
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: app
operator: In
values:
- my-app
topologyKey: kubernetes.io/hostname维度 | 建议 |
|---|---|
资源限制 | 始终设置 requests 与 limits |
健康检查 | 三套探针(startup + liveness + readiness) |
更新策略 | 生产环境 maxUnavailable: 0 + maxSurge: 25% |
历史版本 | revisionHistoryLimit ≥ 10,便于回滚 |
安全 | 使用 securityContext.runAsNonRoot: true |
调度 | 搭配 podAntiAffinity 打散副本 |
配置 | ConfigMap/Secret 统一注入,避免硬编码 |
暂停发布 | 调试时可设置 paused: true |
通过合理配置Deployment的各个字段,可以确保应用在Kubernetes集群中稳定、高效地运行。
1、合理设置资源限制:避免资源浪费和争用。
2、配置健康检查:确保应用稳定运行。
3、使用标签和注解:便于资源管理和查询。
4、设置合适的更新策略:根据应用特点选择RollingUpdate或Recreate。
5、保留历史版本:便于快速回滚。
6、合理使用亲和性:优化Pod调度,提高应用可用性。
如果你觉得这篇文章有用,欢迎点赞、转发、收藏、留言、推荐❤!
本文分享自 Nicholas与Pypi 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!