Nodir Kodirov
Nodir Kodirov

Reputation: 909

Specify the order Dockers run on Kubernetes pod

I am trying to run two Dockers on the same Kubernetes pod and I want one of the Docker container always to run before the other. I remember learning about specifying such dependency on the pod configuration file, but can not find that now. Kubernetes documentation does not explain it either.

Here is the example pod configuration with two containers I adopted from another Stackoverflow question. How should I change this pod configuration to run container type1 before type2?

{
  "id": "podId",
  "desiredState": {
    "manifest": {
      "version": "v1beta1",
      "id": "podId",
      "containers": [{
        "name": "type1",
        "image": "local/image"
        },
        {
        "name": "type2",
        "image": "local/secondary"
        }]
    }
  },
  "labels": {
    "name": "imageTest"
  }
}

Thanks in advance, Nodir.

Upvotes: 18

Views: 8290

Answers (3)

The Fool
The Fool

Reputation: 20547

You can use an init container to block one pod and wait for another one. You could use bitnami/kubectl as init container with the --wait flag. Alternatively you could use a custom wait job.

apiVersion: v1
kind: List
items:
  - apiVersion: v1
    kind: Pod
    metadata:
      name: start-first
      labels:
        name: start-first
    spec: &nginx
      containers:
        - name: nginx
          image: nginx
          ports:
            - containerPort: 80
  - apiVersion: v1
    kind: Pod
    metadata:
      name: start-second
      labels:
        name: start-second
    spec:
      <<: *nginx
      initContainers:
        - name: wait-for-first
          image: bitnami/kubectl
          args:
            - wait
            - pod/start-first
            - --for
            - condition=Ready
            - --timeout
            - 60s

You may have to give the init container a particular role like stated here. That depends on your security setup.

Also note that having it in a list isn't required. I just use it to be able to use the anchor for brevity.

Upvotes: 1

surya
surya

Reputation: 809

Answering on 2021,

Now, it is possible by using Init Containers, if your first container is meant for doing initialization for the second container,

In Kubernetes, an init container is the one that starts and executes before other containers in the same Pod. It’s meant to perform initialization logic for the main application hosted on the Pod. For example, create the necessary user accounts, perform database migrations, create database schemas and so on.

On how to use: https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-initialization/#create-a-pod-that-has-an-init-container

Yet another interesting read if both of them are application containers and you want kubelet to deploy sequentially.

Upvotes: 1

CJ Cullen
CJ Cullen

Reputation: 5662

Kubernetes currently does not allow specification of container startup dependencies.

There has been some discussion in GitHub issues 1996 and 1589 that might help you out.

Upvotes: 13

Related Questions