如何在特定时间后强行设置pod为失败?

问题描述 投票:0回答:1

我写了一个configmap,其中有一个脚本ping这样的ip地址,但我想强制退出pod,并设置为失败后的重试次数!我如何在init容器内设置?

我怎样才能在init容器中设置呢?

这是我的配置图。

apiVersion: v1
kind: ConfigMap
metadata:
  name: check-database
  namespace: devops
data:
  check_db.sh: |
    #!/bin/sh

    touch res.txt
    count=0
    filled=false
    until [ ! "$count" -le 12 ]; do
        echo $count
        if [[ -s res.txt ]]
        then
              filled=true
              break
        fi
        count=$((count+1))
        nc -w 5 mysql-svc 3306 > res.txt
        echo "Database is still running!"
        sleep 10
    done

    if [[ "$filled" == "true" ]]
    then
        echo "Database is up and running!"
    else
        echo "Session timeout!"
    fi
kubernetes contains pod configmap
1个回答
0
投票

你应该使用 红度或活度探针.

如果 liveness probe 失败,它将重新启动 pod.

如果 rediness probe 失败,它将标记pod为 not ready.

对于同一个容器,可以并行使用就绪和活泼度探针。同时使用这两种探针可以确保流量不会到达一个没有准备好的容器,并且当容器失败时,会被重新启动。

因此,你需要选择是否希望在ping失败时pod一直重启,或者将pod标记为未准备好,这样它就不会接受流量。

你可以在你的脚本中添加几行代码,创建一个文件。/tmp/ping 如果ping正常。然后用红度探针检查文件。

readinessProbe:
  exec:
    command:
    - cat
    - /tmp/ping
  initialDelaySeconds: 5
  periodSeconds: 5

如果你想同时使用活跃度和就绪度探头

你的吊舱可能是这样的。

apiVersion: v1
kind: Pod
metadata:
  labels:
    test: mysql-test
  name: mysql-test-exec
spec:
  containers:
  - name: mysql-test
    image: mysql:5.6
    ports:
    - containerPort: 3306
    readinessProbe:
      tcpSocket:
        port: 3306
      initialDelaySeconds: 5
      periodSeconds: 10
    livenessProbe:
      tcpSocket:
        port: 3306
      initialDelaySeconds: 15
      periodSeconds: 20

这个例子同时使用了准备度和活力探针 This example uses both readiness and liveness probes. kubelet将在容器启动5秒后发送第一个准备度探针。这将尝试连接到 mysql-test 容器的端口3306。如果探测成功,Pod将被标记为准备就绪。kubelet将继续每10秒运行一次这个检查。

除了就绪探针外,此配置还包括一个活力探针。kubelet将在容器启动15秒后运行第一个活力探针。就像准备度探测一样,它将尝试连接到容器上的 mysql-test 容器的端口3306。如果活力探针失败,将重新启动容器。

您还应该阅读 活力和准备度探测 因为它给出了如何使用哪个探头的例子。

© www.soinside.com 2019 - 2024. All rights reserved.