大网数据广告活动
服务器运维问答

Kubernetes resources orders and dependencies and priorities

· 52CE 我爱测

I can't find a proper way to implements what I want. I have a StatefulSet, running several identical several pods of code which use a database. I need the database to be pre-populated first (tables, schemas, etc). So I use init-c…

问题原文

原帖链接 · dbourcet · CC BY-SA 4.0

I can't find a proper way to implements what I want. I have a StatefulSet, running several identical several pods of code which use a database. I need the database to be pre-populated first (tables, schemas, etc). So I use init-containers and everything is going well: the StatefulSet launchs one pod at a time sequentially, the init-container is fired, does its job, then the same thing happens for each other container, the init container ends very quickly as their is nothing more to do.

---
kind: StatefulSet
apiVersion: apps/v1
metadata:
  name: the-name-1
spec:
  replicas: 3
  selector:
    matchLabels:
      name: the-name-1
  template:
    metadata:
      labels:
        name: the-name-1
    spec:
      initContainers:
        - name: init
          image: registry/image1
          command: ["/bin/sh"]
          args: ["-c", "set -e; /scripts/pre-populate-db.sh;"]
      containers:
        - name: container-1
          image: registry/image1

But, now I also have a second StatefulSet, which also requires the database to be pre-populated. So this second StatefulSet needs for a least one container of the first StatefulSet to have be launched successfully.

I can't find a proper way to tell this StatefulSet: wait for the first StatefulSet to have done its job. I precise, proper, as I don't want to have an init-container which says sleep n seconds.

As the code in question is legacy, I can't go on a solution which involves code modifications, such as a script in an init container which goes in the database to check if something is set correctly, I want a pure Kubernetes solution.

  • Is there a way to tell StatefulSet pods to wait for a "Kubernetes something" to be ready before being launched?
  • Is there a way to run a Kubernetes Job and made Deployment/StatefulSet dependent on that Job being executed correctly?

已采纳解答

原帖链接 · c4f4t0r · CC BY-SA 4.0

I tested using the following statefulset example: https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/application/web/web.yaml

Now in the second statefulset I used an initcontainer with this bash code:

while true;do sleep 2; curl http://web-0.nginx/ && break ;done

In this way, the second statefulset will be run only when first copy of the first one will be available.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-06-17)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。