如何按时间间隔滚动 kubernetes 更新
How to roll kubernetes updates in intervals
我们有一种情况需要确保 k8s 中的 pods 具有最新版本。 完成此任务的最佳方法是什么?
第一个想法是在某个时间点后终止 pod,因为知道新的 pod 会拉取最新的图像。这里是what we found so far。还是不知道怎么做。
另一个想法是 rolling-update
每隔一段时间执行一次,比如每 5 小时一次。有办法吗?
要使用您的链接功能,您只需在 pods 中指定 activeDeadlineSeconds
。
未测试示例:
apiVersion: v1
kind: Pod
metadata:
name: "nginx"
spec:
activeDeadlineSeconds: 3600
containers:
- name: nginx
image: nginx:alpine
imagePullPolicy: Always
这样做的缺点是,您无法控制截止日期何时开始。这意味着它可能会发生,您的所有 pods 同时被杀死,整个服务离线(这取决于在你的应用程序上)。
另一种选择是使用 deployment
并让控制器处理转出。更具体地说:如果您更新 deployment
yaml 中的 image
字段,它会自动更新每个 pod。 IMO 这是最干净的方法,但它有一些要求:
- 您不能使用
latest
标签。假设容器只需要在图像标签更改时进行更新。
- 如果发生更新,您必须以某种方式手动更新图像标签。这可以通过自定义控制器来完成,该控制器检查新标签并相应地更新部署。或者这可能由持续交付系统触发。
正如@svenwltr 所提到的那样,使用 activeDeadlineSeconds
是一个简单的选择,但会带来一次丢失所有 pods 的风险。为了减轻这种风险,我会使用 deployment
来管理 pods 及其推出,并配置一个小型第二个容器以及实际应用程序。小帮手可以这样配置(following the official docs):
apiVersion: v1
kind: Pod
metadata:
name: app-liveness
spec:
containers:
- name: liveness
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep $(( RANDOM % (3600) + 1800 )); rm -rf /tmp/healthy; sleep 600
image: gcr.io/google_containers/busybox
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5
- name: yourapplication
imagePullPolicy: Always
image: nginx:alpine
使用此配置,每个 pod 都会在配置的时间范围内(此处为 30 到 90 分钟)随机中断,这将触发新 pod 的启动。然后 imagePullPolicy: Always
将确保图像在该周期内更新。
这当然是假设您的应用程序版本始终在同一name/tag下可用。
我尝试使用 Pagid 的解决方案,但不幸的是,我的观察和 subsequent research 表明他关于失败的容器将重启整个 pod 的断言是不正确的。事实证明,只有失败的容器会被重新启动,当重点是随机重新启动 pod 中的其他容器时,这显然没有多大帮助。
好消息是我有一个基于他的回答的似乎有效的解决方案。基本上,不是写入 /tmp/healthy,而是写入 pod 中的每个容器都已挂载的共享卷。您还需要为每个 pods 添加 liveness 探测器。这是一个基于我正在使用的示例:
volumes:
- name: healthcheck
emptyDir:
medium: Memory
containers:
- image: alpine:latest
volumeMounts:
- mountPath: /healthcheck
name: healthcheck
name: alpine
livenessProbe:
exec:
command:
- cat
- /healthcheck/healthy
initialDelaySeconds: 5
periodSeconds: 5
- name: liveness
args:
- /bin/sh
- -c
- touch /healthcheck/healthy; sleep $(( RANDOM % (3600) + 1800 )); rm -rf /healthcheck/healthy; sleep 600
image: gcr.io/google_containers/busybox
volumeMounts:
- mountPath: /healthcheck
name: healthcheck
livenessProbe:
exec:
command:
- cat
- /healthcheck/healthy
initialDelaySeconds: 5
periodSeconds: 5
我们有一种情况需要确保 k8s 中的 pods 具有最新版本。 完成此任务的最佳方法是什么?
第一个想法是在某个时间点后终止 pod,因为知道新的 pod 会拉取最新的图像。这里是what we found so far。还是不知道怎么做。
另一个想法是 rolling-update
每隔一段时间执行一次,比如每 5 小时一次。有办法吗?
要使用您的链接功能,您只需在 pods 中指定 activeDeadlineSeconds
。
未测试示例:
apiVersion: v1
kind: Pod
metadata:
name: "nginx"
spec:
activeDeadlineSeconds: 3600
containers:
- name: nginx
image: nginx:alpine
imagePullPolicy: Always
这样做的缺点是,您无法控制截止日期何时开始。这意味着它可能会发生,您的所有 pods 同时被杀死,整个服务离线(这取决于在你的应用程序上)。
另一种选择是使用 deployment
并让控制器处理转出。更具体地说:如果您更新 deployment
yaml 中的 image
字段,它会自动更新每个 pod。 IMO 这是最干净的方法,但它有一些要求:
- 您不能使用
latest
标签。假设容器只需要在图像标签更改时进行更新。 - 如果发生更新,您必须以某种方式手动更新图像标签。这可以通过自定义控制器来完成,该控制器检查新标签并相应地更新部署。或者这可能由持续交付系统触发。
正如@svenwltr 所提到的那样,使用 activeDeadlineSeconds
是一个简单的选择,但会带来一次丢失所有 pods 的风险。为了减轻这种风险,我会使用 deployment
来管理 pods 及其推出,并配置一个小型第二个容器以及实际应用程序。小帮手可以这样配置(following the official docs):
apiVersion: v1
kind: Pod
metadata:
name: app-liveness
spec:
containers:
- name: liveness
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep $(( RANDOM % (3600) + 1800 )); rm -rf /tmp/healthy; sleep 600
image: gcr.io/google_containers/busybox
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5
- name: yourapplication
imagePullPolicy: Always
image: nginx:alpine
使用此配置,每个 pod 都会在配置的时间范围内(此处为 30 到 90 分钟)随机中断,这将触发新 pod 的启动。然后 imagePullPolicy: Always
将确保图像在该周期内更新。
这当然是假设您的应用程序版本始终在同一name/tag下可用。
我尝试使用 Pagid 的解决方案,但不幸的是,我的观察和 subsequent research 表明他关于失败的容器将重启整个 pod 的断言是不正确的。事实证明,只有失败的容器会被重新启动,当重点是随机重新启动 pod 中的其他容器时,这显然没有多大帮助。
好消息是我有一个基于他的回答的似乎有效的解决方案。基本上,不是写入 /tmp/healthy,而是写入 pod 中的每个容器都已挂载的共享卷。您还需要为每个 pods 添加 liveness 探测器。这是一个基于我正在使用的示例:
volumes:
- name: healthcheck
emptyDir:
medium: Memory
containers:
- image: alpine:latest
volumeMounts:
- mountPath: /healthcheck
name: healthcheck
name: alpine
livenessProbe:
exec:
command:
- cat
- /healthcheck/healthy
initialDelaySeconds: 5
periodSeconds: 5
- name: liveness
args:
- /bin/sh
- -c
- touch /healthcheck/healthy; sleep $(( RANDOM % (3600) + 1800 )); rm -rf /healthcheck/healthy; sleep 600
image: gcr.io/google_containers/busybox
volumeMounts:
- mountPath: /healthcheck
name: healthcheck
livenessProbe:
exec:
command:
- cat
- /healthcheck/healthy
initialDelaySeconds: 5
periodSeconds: 5