<p>It seems that <code>--dry-run</code> flag is not available for service.</p>
<pre><code>kubectl create service --
--add-dir-header            --log-backtrace-at          --server                  
--alsologtostderr           --log-dir                   --skip-headers            
--as                        --log-file                  --skip-log-headers        
--as-group                  --log-file-max-size         --stderrthreshold         
--cache-dir                 --log-flush-frequency       --tls-server-name         
--certificate-authority     --logtostderr               --token                   
--client-certificate        --match-server-version      --user                    
--client-key                --namespace                 --username                
--cluster                   --password                  --v                       
--context                   --profile                   --vmodule                 
--insecure-skip-tls-verify  --profile-output            --warnings-as-errors      
--kubeconfig                --request-timeout   
</code></pre>
<p>Is there a way to create a service yaml file without <code>--dry-run=client</code> option. I tried with the below command and getting an error.</p>
<pre><code>kubectl create service ns-service nodeport --dry-run=client -o yaml &gt;nodeport.yaml
Error: unknown flag: --dry-run
See 'kubectl create service --help' for usage.
</code></pre>

<p>There are two ways to do this.
<strong>=================================================================</strong></p>
<p><strong>First Way:</strong>- using <code>kubectl create service</code></p>
<p>What wrong you are doing here is you are giving service name befor the service type in command that's why its failing.</p>
<p>correct way is</p>
<p><strong>Syntax :</strong></p>
<pre><code>kubectl create service clusterip NAME [--tcp=&lt;port&gt;:&lt;targetPort&gt;] [--dry-run=server|client|none] [options]
</code></pre>
<p><strong>Example :</strong></p>
<pre><code>kubectl create service nodeport ns-service --tcp=80:80 --dry-run=client -o yaml
</code></pre>
<p><strong>=================================================================</strong></p>
<p><strong>Second way:-</strong></p>
<p>Here you can use <code>kubectl expose</code> command to create a service file.</p>
<p>Let's assume you have a pod running with the name <strong>nginx</strong>. and you want to create a service for <strong>nginx</strong> pod.</p>
<p>then I will write below command to generate the service file.</p>
<p>Synatax:</p>
<pre><code>kubectl expose [pod/deployment/replicaset] [name-of-pod/deployment/replicaset]  --port=80 --target-port=8000 --dry-run=client -o yaml
</code></pre>
<p>Example:</p>
<pre><code>kubectl expose pod nginx  --port=80 --target-port=8000 --dry-run=client -o yaml 
</code></pre>
<p>output:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8000
  selector:
    run: nginx
status:
  loadBalancer: {}
</code></pre>
