kubectl run command with --dry-run=client -o yaml does not pull up namespace in the yaml files

2.6k views Asked by At

The Kubectl run command when used with --dry-run=client and -o yaml and namespace flags does not populate the namespace field in the yaml file generated by it. Is it expected to insert the namespace manually in the yaml, do we have any other alternatives? However, the run command executed excluding the --dry-run=client, -o yaml flags generates the pod in the respective namespace.

kubectl run command to generate yaml files in test namespace

kubectl run pod1 --namespace=test --image=nginx --dry-run=client -o yaml > pod.yaml

pod.yaml generated without the namespace field

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: pod1
  name: pod1
spec:
  containers:
  - image: nginx
    name: pod1
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

pods get created fine in the respective namespace when used without the --dry-run=client -o yaml

$ kubectl run po --namespace=test  --image=nginx
pod/po created
$ kubectl get po -n test
NAME   READY   STATUS              RESTARTS   AGE
po     0/1     ContainerCreating   0          5s
0

There are 0 answers