Do init containers run on the same node as their app container in GKE

3/5/2019

I'm just trying to wrap my head around what I can use init containers for in kubernetes.

I have an app container which runs on a node that has a local ssd. I'm planning on doing this through the local volume static provisioner, and having the pod have a persistent volume claim - my reasoning for this is that I have a 1-1 node-pod relationship, and I'd like to be able to use node/pod auto scaling. My app container needs some data pulling from an external bucket for it to work, which is what I plan to have my init container doing.

My question is; is it guaranteed that my init container and app container will be executed on the same node? If so, I assume that they can mount the same disk without the static provisioned wiping the disk between them mounting it?

-- Andy
google-kubernetes-engine

1 Answer

3/5/2019

Yes, they run on the same node, init containers are part of the Pod:

The containers in a Pod share an IP Address and port space, are always co-located and co-scheduled, and run in a shared context on the same Node.

and:

A Pod can have multiple Containers running apps within it, but it can also have one or more Init Containers, which are run before the app Containers are started.

You can share a volume between init container and your pod:

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: deploy
spec:
  replicas: 1
  template:
    spec:
      volumes:
      - name: shared-data
        emptyDir: {}
      initContainers:
      - name: init
        image: <xxx>
        volumeMounts:
        - name: shared-data
          mountPath: /env
      containers:
      - name: <my-container>
        image: <my-image>
        volumeMounts:
        - name: shared-data
          mountPath: /env

Write any data you need to the shared volume and you will be able to access it from your container.

-- Amityo
Source: StackOverflow