Why it is creating pod even when _ said --dry-run=client ? I am doing static-pod . . .

ameya agashe:
Why it is creating pod even when _ said --dry-run=client ? I am doing static-pods lab

root@controlplane:/etc/kubernetes/manifests# k run static-busybox --image=busybox --command -- sleep 1000 --dry-run=client --restart=Never -oyaml
pod/static-busybox created

Strange?

root@controlplane:/etc/kubernetes/manifests# k run static-busybox --image=busybox --command -- sleep 1000 --dry-run=client --restart=Never -oyaml >> pod.yaml
Error from server (AlreadyExists): pods "static-busybox" already exists
root@controlplane:/etc/kubernetes/manifests# k delete po static-busybox
pod "static-busybox" deleted

Hailu Meng:
@ameya agashe I had this issue before. I realized I need to move dry-run flag in front of the command flag. Try it and see if that solved the issue. It worked for me.

ameya agashe:
Alright, would test now

ameya agashe:
No luck…

root@controlplane:/etc/kubernetes/manifests# k run static-busybox --image=busybox --command -- sleep 1000 -oyaml >> pod.yaml
root@controlplane:/etc/kubernetes/manifests# cat pod.yaml 
pod/static-busybox created
pod/static-busybox created
root@controlplane:/etc/kubernetes/manifests# rm -vf pod.yaml  
removed 'pod.yaml'
root@controlplane:/etc/kubernetes/manifests# k run static-busybox --image=busybox --command -- sleep 1000 -oyaml >> pod.yaml
Error from server (AlreadyExists): pods "static-busybox" already exists
root@controlplane:/etc/kubernetes/manifests# k delete po static-busybox
pod "static-busybox" deleted
root@controlplane:/etc/kubernetes/manifests# k run static-busybox --image=busybox --command -- sleep 1000 -oyaml >> pod.yaml
root@controlplane:/etc/kubernetes/manifests# cat pod.yaml 
pod/static-busybox created
root@controlplane:/etc/kubernetes/manifests# 

Hailu Meng:
I don’t see your dry-run flag now with your new tries?

ameya agashe:
Ok so you mean right after --command – sleep 1000 --dry-run=client?

Hailu Meng:

Try this:
root@controlplane:/etc/kubernetes/manifests# k run static-busybox --image=busybox —-dry-run=client -oyaml --command -- sleep 1000 --restart=Never >> pod.yaml

Hailu Meng:
Move dry run in front of command

ameya agashe:
Show me pls

ameya agashe:
ok got it

Hailu Meng:
See above

ameya agashe:
That worked but pod did not came up

ameya agashe:
Error:

 Normal   Pulled     2m6s                  kubelet            Successfully pulled image "busybox" in 748.510227ms
  Normal   Pulling    2m6s (x5 over 3m30s)  kubelet            Pulling image "busybox"
  Warning  Failed     2m5s (x5 over 3m29s)  kubelet            Error: failed to start container "static-busybox": Error response from daemon: OCI runtime create failed: container_linux.go:367: starting container process caused: exec: "—-dry-run=client": executable file not found in $PATH: unknown
  Normal   Created    2m5s (x5 over 3m29s)  kubelet            Created container static-busybox
root@controlplane:/etc/kubernetes/manifests# 

Hailu Meng:
Move your restart flag also in front of command. That command — thing thinks everything followed are part of commands.

ameya agashe:
That’s it mate!

The solution video might need to be tweaked to incorporate this a solution.
The working solution is below:

root@controlplane:/etc/kubernetes/manifests# k run static-busybox --image=busybox --restart=Never --dry-run=client -oyaml --command -- sleep 1000 > pod.yaml
root@controlplane:/etc/kubernetes/manifests# cat pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: static-busybox
  name: static-busybox
spec:
  containers:
  - command:
    - sleep
    - "1000"
    image: busybox
    name: static-busybox
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}
root@controlplane:/etc/kubernetes/manifests# k get po
NAME                          READY   STATUS    RESTARTS   AGE
static-busybox-controlplane   1/1     Running   0          14s
root@controlplane:/etc/kubernetes/manifests# 

Cc : @Mumshad Mannambeth Just for your info as a lab tweak

Hailu Meng:
:+1:

ameya agashe:
Also --restart=Never I never use that flag so I removed it and it still works fine.

unnivkn:
Hi @ameya agashe and @Hailu Meng fyi: https://github.com/kodekloudhub/certified-kubernetes-administrator-course/blob/master/docs/03-Scheduling/17-Practice-Test-StaticPods.md

Alistair Mackay:
--command must be the last argument to any kubectl run as everything after --command is sent to the pod as what it should run when it starts.

If you do --dry-run=client -o yaml before --command you will see what came after --command present in the YAML

ameya agashe:
I see , makes sense now @Alistair Mackay hence previous attempts were failing. When we did --command at last , all came out right way