前言
如果您觉得有用的话,记得给博主点个赞,评论,收藏一键三连啊,写作不易啊^ _ ^。
而且听说点赞的人每天的运气都不会太差,实在白嫖的话,那欢迎常来啊!!!
k8s 增量更新 po
1. 导出要新建po 的控制器配置
为了方便,直接导出现有的控制器配置。
查询配置:
kubectl get deploy -n <命名空间> | grep <过滤字符串>
导出 干净的“可 apply”版本命令:
kubectl get deploy <my-deploy> -n <命名空间> -o yaml \
| sed -e '/^ uid:/d' \
-e '/^ resourceVersion:/d' \
-e '/^ creationTimestamp:/d' \
-e '/^ generation:/d' \
-e '/^status:$/,/^[^ ]/d' \
> deploy_20250905.yaml
额外清理:
- 删掉 managedFields 整块(这是冗余的操作记录,API 会自己维护)。
- 删掉 fieldsType / fieldsV1 / manager / operation / time(都属于 managedFields 的子字段)。
2. 配置详解
脱敏详细配置:
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "7"
meta.helm.sh/release-name:demo-service-bj
meta.helm.sh/release-namespace: demo-bj
labels:
app:demo-service-bj-task
app.kubernetes.io/instance:demo-service-bj
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: demo-service
app.kubernetes.io/version: "2.0"
helm.sh/chart: demo-service-0.1.1
name:demo-service-bj-task
namespace: magic2-bj
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app:demo-service-bj-task
strategy:
rollingUpdate:
maxSurge: 50%
maxUnavailable: 50%
type: RollingUpdate
template:
metadata:
annotations:
kubectl.kubernetes.io/restartedAt: "2025-05-22T09:58:54+08:00"
creationTimestamp: null
labels:
app:demo-service-bj-task
spec:
containers:
- command:
- /bin/sh
- -c
- 'java -Xms1024M -Xmx2024M demo.jar '
env:
- name: NACOSURL
value: yzy:8848
image: 镜像
imagePullPolicy: IfNotPresent
livenessProbe:
exec:
command:
- /bin/sh
- -c
- /usr/bin/curl -X GET -s '存活接口(存活探针,检查容器是否健康运行,不健康会被重启)'
| grep instanceId
failureThreshold: 5
initialDelaySeconds: 60
periodSeconds: 20
successThreshold: 1
timeoutSeconds: 3
name: app
ports:
- containerPort: 8080
protocol: TCP
readinessProbe:
exec:
command:
- /bin/sh
- -c
- /usr/bin/curl -X GET -s '请求接口(就绪探针,检查容器是否可对外服务)'
| grep instanceId
failureThreshold: 3
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 1
resources:
limits:
cpu: "2"
memory: 4000Mi
requests:
cpu: 100m
memory: 500Mi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /etc/localtime
name: host-time
readOnly: true
- mountPath: /home/app/jars
name: yzy
dnsPolicy: ClusterFirst
nodeSelector:
kubernetes.io/hostname: <ip>
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount:demo-service-bj
serviceAccountName:demo-service-bj
terminationGracePeriodSeconds: 30
volumes:
- hostPath:
path: /etc/localtime
type: ""
name: host-time
- hostPath:
path: /data/demo/jars
type: Directory
name: yzy
配置块 | 说明(通用讲解) |
---|---|
apiVersion / kind | 定义资源类型,这里是 Deployment。 |
metadata | 元数据:名称、命名空间、标签、注解(一般用于标识应用、Helm 信息、运维追踪)。 |
spec.replicas | 副本数量,决定 Pod 的数量。 |
spec.selector | 选择器,Deployment 根据标签找到自己要管理的 Pod。 |
spec.strategy | 更新策略,常见是 RollingUpdate(滚动更新),可配置 maxSurge / maxUnavailable。 |
template.metadata.labels | Pod 模板里的标签,要和 selector 匹配。 |
containers.image | 容器镜像地址,定义运行环境和应用内容。 |
containers.command | 容器启动命令,一般用来启动主程序(如 java -jar xxx.jar)。 |
containers.env | 环境变量:可以是固定值(配置中心地址),也可以是动态值(如 Pod 名称)。 |
containers.ports | 容器对外暴露的端口。 |
livenessProbe | 存活探针,检查容器是否健康运行,不健康会被重启。 |
readinessProbe | 就绪探针,检查容器是否可对外服务,失败则不会接收流量。 |
resources.requests | 最低资源需求(CPU/内存)。 |
resources.limits | 资源上限,防止容器无限占用。 |
volumeMounts | 容器内挂载点,通常用于挂载配置文件、日志目录、本地时间等。 |
volumes | Pod 使用的存储卷定义(如宿主机目录、ConfigMap、Secret)。 |
nodeSelector | 节点选择器,强制 Pod 调度到某些节点上。 |
serviceAccountName | Pod 使用的 ServiceAccount,用于 RBAC 权限控制。 |
restartPolicy | 重启策略,Deployment 下通常是 Always。 |
terminationGracePeriodSeconds | 优雅关闭等待时间,默认 30 秒。 |
3. 重新生效
kubectl apply -f deploy_20250905.yaml -n <命名空间>
注:对象存在就 增量更新,不存在就 新建;Deployment 更新会 触发滚动发布。