Multi container pod with command sleep k8

12/28/2020

I am trying out mock exams on udemy and have created a multi container pod . but exam result says command is not set correctly on container test2 .I am not able to identify the issue.

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: multi-pod
  name: multi-pod
spec:
  containers:
  - image: nginx
    name: test1
    env:
     - name: type
       value: demo1
  - image: busybox
    name: test2
    env:
     - name: type
        value: demo2
    command: ["sleep", "4800"]
-- coder25
kubernetes

2 Answers

12/28/2020

An easy way to do this is by using imperative kubectl command to generate the yaml for a single container and edit the yaml to add the other container

kubectl run nginx --image=nginx --command -oyaml --dry-run=client -- sh -c 'sleep 1d' > nginx.yaml

In this example sleep 1d is the command.

The generated yaml looks like below.

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  containers:
  - command:
    - sh
    - -c
    - sleep 1d
    image: nginx
    name: nginx
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}
-- Arghya Sadhu
Source: StackOverflow

12/28/2020

Your issue is with your YAML in line 19.

Please keep in mind that YAML syntax is very sensitive for spaces and tabs.

Your issue:

  - image: busybox
    name: test2
    env:
     - name: type
        value: demo2     ### Issue is in this line, you have one extra space
    command: ["sleep", "4800"]

Solution:

Remove space, it wil looks like that:

    env:
     - name: type
       value: demo2 

For validation of YAML you can use external validators like yamllint.

If you would paste your YAML to mentioned validator, you will receive error:

(<unknown>): mapping values are not allowed in this context at line 19 column 14

After removing this extra space you will get

Valid YAML!
-- PjoterS
Source: StackOverflow