# Kubectl run command for yaml creation

**URL:** <https://discuss.kubernetes.io/t/kubectl-run-command-for-yaml-creation/8538>\
**Category:** General Discussions\
**Created:** [October 31, 2019, 5:22am UTC](https://discuss.kubernetes.io/t/kubectl-run-command-for-yaml-creation/8538 "2019-10-31T05:22:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Klearner](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@Klearner](https://discuss.kubernetes.io/u/Klearner)\
**Post date:** [October 31, 2019, 5:22am UTC](https://discuss.kubernetes.io/t/kubectl-run-command-for-yaml-creation/8538/1 "2019-10-31T05:22:30Z")

</div>

I am looking for different set of commands for kubectl run to generate yaml file.  
Pod  
Deployment  
Daemonset  
Service (different options)  
I checked [Kubectl Reference Docs](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#run)  
but I am unable to figure out which command need to run for which option

$ run NAME --image=image [–env=“key=value”] [–port=port] [–replicas=replicas] [–dry-run=bool] [–overrides=inline-json] [–command] – [COMMAND] [args…]

For example :

$ kubectl run NAME --image=IMAGE-NAME --labels=run=LABLE --dry-run -o yaml \> test.yaml  
It creates deployment but I dont know what needs to be change to create other types of yaml.

---

<div class="post-metadata">

**Author:** ![macintoshprime](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/macintoshprime/32/7121_2.png) [@macintoshprime](https://discuss.kubernetes.io/u/macintoshprime)\
**Post date:** [October 31, 2019, 10:18am UTC](https://discuss.kubernetes.io/t/kubectl-run-command-for-yaml-creation/8538/2 "2019-10-31T10:18:00Z")

</div>

Have you taken a look at using `generators`?

[https://kubernetes.io/docs/reference/kubectl/conventions/#generators](https://kubernetes.io/docs/reference/kubectl/conventions/#generators)

---

<div class="post-metadata">

**Author:** ![Klearner](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@Klearner](https://discuss.kubernetes.io/u/Klearner)\
**Post date:** [November 4, 2019, 7:56am UTC](https://discuss.kubernetes.io/t/kubectl-run-command-for-yaml-creation/8538/3 "2019-11-04T07:56:15Z")

</div>

Thanks for your comment, with the help of above link I figure out way to create pod and deployment yaml files  
kubectl run --restart=Never NAME-pod --image=IMAGE-NAME --labels=run=LABLE --dry-run -o yaml \> pod.yaml  
kubectl run --restart=Always NAME-deployment --image=IMAGE-NAME --labels=run=LABLE --dry-run --replicas=REPLICAS -o yaml \> deployment.yaml  
Still I am unable to find how to create yaml files for Deamonset, service (node port, cluster ip and loadbalancer).

---

<div class="post-metadata">

**Author:** ![Bobses](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/bobses/32/250_2.png) [@Bobses](https://discuss.kubernetes.io/u/Bobses)\
**Post date:** [November 4, 2019, 9:29am UTC](https://discuss.kubernetes.io/t/kubectl-run-command-for-yaml-creation/8538/4 "2019-11-04T09:29:36Z")

</div>

I think that the easier way is searching on the [kubernetes.io](http://kubernetes.io) documentation for **service template** or **daemonset template** :

- [Service - Kubernetes](https://kubernetes.io/docs/concepts/services-networking/service/)
- [DaemonSet - Kubernetes](https://kubernetes.io/docs/concepts/workloads/controllers/daemonset/)

Thus, you’ll find more template examples.

Or, if you prefer, you can start from an existing resource, export to a YAML file, then edit it. See the following example:

`kubectl get ds kube-proxy -n kube-system -o yaml > new-ds.yaml`  
`vi new-ds.yaml`

---

<div class="post-metadata">

**Author:** ![Klearner](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@Klearner](https://discuss.kubernetes.io/u/Klearner)\
**Post date:** [November 6, 2019, 4:17am UTC](https://discuss.kubernetes.io/t/kubectl-run-command-for-yaml-creation/8538/5 "2019-11-06T04:17:11Z")

</div>

With the help of “kubectl run -h” I found below mentioned parameters

–restart=‘Always’: The restart policy for this Pod. If set to ‘Always’ a deployment is created, if set to ‘Never’, a regular pod is created.

–expose=false: If true, a public, external service is created for the container(s) which are run

Now I am able to create Pod/deployment and service with help of it same time. I am looking for how to pass NodePort into it.

---

<div class="post-metadata">

**Author:** ![Klearner](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@Klearner](https://discuss.kubernetes.io/u/Klearner)\
**Post date:** [November 6, 2019, 4:39am UTC](https://discuss.kubernetes.io/t/kubectl-run-command-for-yaml-creation/8538/6 "2019-11-06T04:39:04Z")

</div>

With the help of “kubectl expose -h” I am able to identify below mentioned parameters

Possible resources include (case insensitive):  
pod (po), service (svc), replicationcontroller (rc), deployment (deploy), replicaset (rs)

–dry-run=false: If true, only print the object that would be sent, without sending it.  
-o, --output=’’: Output format. One of:  
json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-file.  
–type=’’: Type for this service: ClusterIP, NodePort, LoadBalancer, or ExternalName. Default is ‘ClusterIP’.

For example :  
kubectl expose deployment Deployment-Name --name=Service-Name --port=80 --target-port=80 --type=NodePort --dry-run -o yaml \>deploy-service.yaml
