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: {}
Expanding on my comment:
A list in YAML is a series of items marked with a leading
-
, like this list of strings:Or this list of dictionaries:
Or even this list of lists:
Your
pod.yaml
as written has only a single item in yourcontainers
list. You need to mark the second item: