在 AKS 中使用 Keda 进行自动伸缩时,由于 Keda 可能会在需要时添加和删除 Pod,有时会出现在执行期间 Pod 被删除的情况。一种解决方法是在 Pod 的 YAML 文件中添加 livenessProbe、readinessProbe 和 cleanup 命令,以确保发生故障时容器可以自动修复或重新启动。以下是一个示例 YAML 文件:
apiVersion: apps/v1 kind: Deployment metadata: name: myapp spec: replicas: 1 selector: matchLabels: app: myapp strategy: type: RollingUpdate rollingUpdate: maxSurge: 1 maxUnavailable: 0 template: metadata: labels: app: myapp spec: containers: - name: myapp image: myimage env: - name: MY_VAR value: myvalue ports: - containerPort: 80 livenessProbe: tcpSocket: port: 80 initialDelaySeconds: 10 periodSeconds: 5 readinessProbe: tcpSocket: port: 80 initialDelaySeconds: 1 periodSeconds: 5 command: ["/bin/sh"] args: ["-c", "sleep 7200"]
此示例文件中,livenessProbe 和 readinessProbe 分别检测容器状态并确定其是否健康,cleanup 命令可在容器终止时运行,清除任何未完成的操作。通过这些措施,可以在执行期间最大程度地减少 Pod 删除的可能性。