0

我正在为 CKAD 考试练习,遇到了一个有趣的问题,即我似乎无法找到答案的多容器吊舱。假设我运行这个命令式命令来创建一个 pod.yaml:

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

然后我编辑该 yaml 定义以添加仅具有名称和图像的 sidecar nginx 容器。当我去创建这个吊舱时

kubectl create -f pod.yaml
kubectl get pods

即使busybox容器仍在pod规范yaml中定义,我也得到了一个带有单个nginx容器的pod。我怀疑这是由于使用--dry-run=client和/或运行命令与空运行相结合,但我似乎找不到一个好的答案。提前致谢。

编辑: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: {}
4

1 回答 1

4

扩展我的评论:

YAML 中的列表是一系列标有前导的项目-,例如以下字符串列表:

- one
- two
- three

或者这个字典列表:

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

甚至这个列表列表:

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

您的书面 清单pod.yaml中只有一个项目。containers您需要标记第二项:

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
于 2021-02-23T16:07:27.050 回答