Chi*_*lax 5 kubernetes kubernetes-helm kubernetes-cronjob
我有 6 个正在运行的 pod 副本,我想每 5 分钟重新启动\重新创建一次。
这需要是一个滚动更新 - 这样所有的都不会立即终止并且没有停机时间。我如何实现这一目标?
我尝试使用 cron 作业,但似乎不起作用:
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: scheduled-pods-recreate
spec:
schedule: "*/5 * * * *"
concurrencyPolicy: Forbid
jobTemplate:
spec:
template:
spec:
containers:
- name: ja-engine
image: app-image
imagePullPolicy: IfNotPresent
restartPolicy: OnFailure
Run Code Online (Sandbox Code Playgroud)
尽管作业已成功创建并按照以下说明进行调度,但它似乎从未运行过:
Name: scheduled-pods-recreate
Namespace: jk-test
Labels: <none>
Annotations: <none>
Schedule: */5 * * * *
Concurrency Policy: Forbid
Suspend: False
Starting Deadline Seconds: <unset>
Selector: <unset>
Parallelism: <unset>
Completions: <unset>
Pod Template:
Labels: <none>
Containers:
ja-engine:
Image: image_url
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Last Schedule Time: Tue, 19 Feb 2019 10:10:00 +0100
Active Jobs: scheduled-pods-recreate-1550567400
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 23m cronjob-controller Created job scheduled-pods-recreate-1550567400
Run Code Online (Sandbox Code Playgroud)
首先,我如何确保它正在运行,以便重新创建 pod?
另外我怎样才能确保没有停机时间?
cronjob 的更新版本:
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: hello
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
args:
- /bin/sh
- -c
- kubectl patch deployment runners -p '{"spec":{"template":{"spec":{"containers":[{"name":"jp-runner","env":[{"name":"START_TIME","value":"'$(date +%s)'"}]}]}}}}' -n jp-test
restartPolicy: OnFailure
Run Code Online (Sandbox Code Playgroud)
Pod 没有以消息 Back-off restarting failed container 和错误开始,如下所示:
State: Terminated
Reason: Error
Exit Code: 127
Run Code Online (Sandbox Code Playgroud)
从 Kubernetes 1.15 开始,您可以使用以下命令执行滚动重启。
kubectl rollout restart deployment <deployment name>
Run Code Online (Sandbox Code Playgroud)
目前 Kubernetes 中没有滚动重启功能,但您可以使用以下命令作为解决方法来重启特定部署中的所有 pod:(
将部署名称和 pod 名称替换为真实的名称)
kubectl patch deployment mydeployment -p '{"spec":{"template":{"spec":{"containers":[{"name":"my-pod-name","env":[{"name":"START_TIME","value":"'$(date +%s)'"}]}]}}}}'
Run Code Online (Sandbox Code Playgroud)
为了安排它,您可以在主节点上创建一个 cron 任务来定期运行该命令。
拥有该任务的用户应该具有正确的kubectl配置 ( ~/.kube/config),并具有更改上述部署对象的权限。
/etc/kubernetes/admin.conf默认集群管理配置可以从:
(通常由)复制kubeadm init:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Run Code Online (Sandbox Code Playgroud)
可以指定两种类型的部署更新策略:重新创建(.spec.strategy.type==Recreate.)和滚动更新(.spec.strategy.type==RollingUpdate)。
只有使用滚动更新策略才能避免服务停机。您可以在部署 YAML 中指定maxUnavailable和参数来控制滚动更新过程。maxSurge
| 归档时间: |
|
| 查看次数: |
5769 次 |
| 最近记录: |