当另一个发生变化时重启 Kubernetes pod

Restart Kubernetes pod when change happens on another

两个podsapppostgres创建成功,可以在一个节点中通过彼此的服务进行通信。在当前过程中,两个 pods 是同时创建的,但可以更改为让它们按顺序 created/started。

最初,postgres pod 中的数据库容器是空的,需要播种。种子进程通过 app pod,因此它也需要启动 运行。一旦 postgres 被播种,app 仍然不知道这个新数据,需要重新启动。这是app本身的缺陷,我的控制力很低。

现在的流程是:

kubectl create -f pods.yaml       # creates `app` and `postgres` pods
kubectl exec app -- bash -c "<seed command>"
kubectl delete pod app
sleep 45                          # takes a while for `app` to terminate
kubectl create -f pods.yaml       # Ignore the "postgres pod and service already exist" error

是否有更好的方法可以在 postgres 达到种子状态后自动协调 app 的重启?

也许我完全缺少一些 aspect/feature 的 Kubernetes 集,这有助于解决这种情况....

如果应用在播种过程中不需要运行并且您可以使播种过程幂等,那么init containers可以帮助您。

good example 可用。

您可以在 postgresql pod 上使用 "readiness probe",它不会在导入数据之前将容器报告为就绪(例如,查询数据库或您导入的 Table)。您的应用程序容器可以查询 db pod 的就绪状态,以便在报告就绪后自动重启。 就绪探测可以是执行导入的脚本。这是一个示例(您需要将 "SHOW DATABASES" 命令替换为适用于您的情况的任何命令):

spec:
  containers:
  - name: mysql
    image: mysql:latest
    ports:
    - containerPort: 3306
      name: mysql
    readinessProbe:
      exec:
        command:
        - /path-in-container/readiness-probe.sh
      initialDelaySeconds: 15
      timeoutSeconds: 5

准备就绪-probe.sh:

#!/bin/bash

MYSQL_USER="readinessProbe"
MYSQL_PASS="readinessProbe"
MYSQL_HOST="127.0.0.1"

mysql -u${MYSQL_USER} -p${MYSQL_PASS} -h${MYSQL_HOST} -e"SHOW DATABASES;"

if [ $? -ne 0 ]; then
  exit 1
else
  exit 0
fi

要阅读有关该主题的更多信息,请参阅 k8s 文档:

readiness probes

health checking