How to create mult-container pods using --dry-run=client?

1k views Asked by At

I am working on practicing for the CKAD exam and ran into an interesting problem with a multi-container pod that I can't seem to find an answer to. Lets say I run this imperative command to create a pod.yaml:

kubectl run busybox --image=busybox --dry-run=client -o yaml -- /bin/sh -c 'some commands' > pod.yaml

I then edit that yaml definition to add a sidecar nginx container with just a name and image. When I go to create this pod with

kubectl create -f pod.yaml
kubectl get pods

I get a pod with a single nginx container even though the busybox container is still defined in the pod spec yaml. I suspect this is due to something with --dry-run=client and/or running the command combined with dry run but I can't seem to find a good answer to that. Thanks in advance.

EDIT: pod.yaml

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: busybox
  name: busybox
spec:
  containers:
  - args:
    - /bin/sh
    - -c
    - while true; do echo ‘Hi I am from Main container’ >> /var/log/index.html; sleep
      5; done
    image: busybox
    name: busybox
    volumeMounts:
    - mountPath: /var/log
      name: log-vol
    image: nginx
    name: nginx
    volumeMounts:
    - mountPath: /usr/share/nginx/html
      name: log-vol
    ports:
    - containerPort: 80
  volumes:
  - name: log-vol
    emptyDir: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}
1

There are 1 answers

1
larsks On BEST ANSWER

Expanding on my comment:

A list in YAML is a series of items marked with a leading -, like this list of strings:

- one
- two
- three

Or this list of dictionaries:

containers:
  - image: busybox
    name: busybox
  - image: nginx
    name: nginx

Or even this list of lists:

outerlist:
  -
    - item 1.1
    - item 1.2
    - item 1.3
  -
    - item 2.1
    - item 2.2
    - item 2.3

Your pod.yaml as written has only a single item in your containers list. You need to mark the second item:

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: busybox
  name: busybox
spec:
  containers:

  - args:
    - /bin/sh
    - -c
    - while true; do echo ‘Hi I am from Main container’ >> /var/log/index.html; sleep
      5; done
    image: busybox
    name: busybox
    volumeMounts:
    - mountPath: /var/log
      name: log-vol

  - image: nginx
    name: nginx
    volumeMounts:
    - mountPath: /usr/share/nginx/html
      name: log-vol
    ports:
    - containerPort: 80
  volumes:
  - name: log-vol
    emptyDir: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always