why container object in pod yaml file has "list value" rather than a "map value"

11/8/2019

In the pod creation yaml files or in the deployment yaml files in kubernetes, why Containers key has a list value - name: memory-demo-ctr rather than we can simply provide the map value name: memory-demo-ctr (why we're providing - symbol)?

I tried looking at over the web but couldn't find a solution.

apiVersion: v1
kind: Pod
metadata:
  name: memory-demo
  namespace: mem-example
spec:
  containers:
  - name: memory-demo-ctr
    image: polinux/stress
-- Teja
containers
kubernetes
kubernetes-pod
yaml

3 Answers

11/8/2019

The use of both mentioned structures in kubernetes yaml manifests is quite well justified and if you take a closer look, it becomes also quite intuitive:

Maps (aka Dictionaries) are used when providing a set of key: value pairs. Please note that the keys are unique in one such set e.g. you provide different set of labels in metadata section of your Deployment definition. metadata element is also in a form of Dictionary as it contains a set of unique keys ( name and labels in this particular case).

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
    type: front-end

Just to give one more example, let's take a look at the spec section. Note that all three keys that are children of the spec element, namely replicas, selector and template, are unique in the whole set and that's the reason they are in a form of a Dictionary ( or a Map ):

spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx

Lists (aka Arrays) are used when we need to provide a List (or Array) of objects/elements of the same type like in case of containers in Deployment definition:

containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

container element is an array as it may contain many objects/elements/items of the same type ( in this case containers ) which can have same properties. In container section you can define many containers and each of them will have its unique name, image and ports. - sign indicates one element or item of the array. Note that the structure inside such element element is a Map (Dictionary) and as in the previous examples it contains set of unique keys with corresponding values.

The use of these two structures is quite flexible as they can be nested within one another e.g. you can have a Lists of Dictionaries ( as in case of containers ), Dictionaries which values are other Dictionaries as well as Dictionaries containing Lists, to exhaust all possibilities ( example of such case is spec element which is a Dictionary which among others contains containers key which is a list ( value of this key is a list/array of items ).


As to your specific question:

why Containers key has a list value - name: memory-demo-ctr rather than we can simply provide the map value name: memory-demo-ctr (why we're providing - symbol)?

Note that a list value ( list item ) for the container key is not only the name element but the whole structure at the same indentation level under the container. As I already mentioned above, one list item is indicated by - sign. In the example posted in your question keys name and image with their respective values belong to the same element/item of a list (array) and they form another nested dictionary (map). So it's not the name element that must be preceded by - character but the whole structure ( in this case Dictionary/Map ) under container element. When you define another container (another item of container list) it always starts with another - character, no matter if the first element within it is name. For example you can construct your structure like this and it will also be correct:

  containers:
  - name: nginx
    image: nginx:1.7.9
  - image: redis
    name: redis
  - command: ['sh', '-c', 'echo Hello Kubernetes! && sleep 3600']
    image: busybox
    name: myapp-container

The dictionary (map) elements do not have to appear in the same order. It's just a convention that name is usually placed as the first one. I will emphasize it again: these are not name, image and command in the above example that are items of a list but the whole dictionary (map) structure preceded by - character, so these are:

first:

- name: nginx
  image: nginx:1.7.9

second:

- image: redis
  name: redis

and third:

- command: ['sh', '-c', 'echo Hello Kubernetes! && sleep 3600']
  image: busybox
  name: myapp-container

items/elements of the containers list (which at the same time is one of the keys belonging to spec dictionary). Yes, the value of this key is a list so we can say that the whole containers element is a list.

I hope it clarified the usage of both structures and explained the differences between them.

-- mario
Source: StackOverflow

11/8/2019

You have a list value in containers because a pod can be composed of multiple containers.

A pod represents the smallest building block in kubernetes. Instead of deploying containers individually you allways deploy and operate on a pod of containers.

The key thing about pods is that when a pod contains multiple containers, all of them are always run on a single worker node, it never spans multiple worker nodes.

-- Ezwig
Source: StackOverflow

11/8/2019

Pod is capable of running multiple containers. That's the reason containers object is a list instead of map.

kind: Pod
...
spec:
  containers:
  - name: busybox
    image: busybox:latest
  - name: nginx
    image: nginx:1.7.9
  - name: redis
    image: redis:latest

If containers is a map object, you cannot write a configuration file to run multiple containers inside a pod. I hope this answer solved your doubt.

-- Rewanth Cool
Source: StackOverflow