kubernetesservice-node-port-range

How to create a kubernetes service yaml file without --dry-run


It seems that --dry-run flag is not available for service.

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   

Is there a way to create a service yaml file without --dry-run=client option. I tried with the below command and getting an error.

kubectl create service ns-service nodeport --dry-run=client -o yaml >nodeport.yaml
Error: unknown flag: --dry-run
See 'kubectl create service --help' for usage.

Solution

  • There are two ways to do this. =================================================================

    First Way:- using kubectl create service

    What wrong you are doing here is you are giving service name befor the service type in command that's why its failing.

    correct way is

    Syntax :

    kubectl create service clusterip NAME [--tcp=<port>:<targetPort>] [--dry-run=server|client|none] [options]
    

    Example :

    kubectl create service nodeport ns-service --tcp=80:80 --dry-run=client -o yaml
    

    =================================================================

    Second way:-

    Here you can use kubectl expose command to create a service file.

    Let's assume you have a pod running with the name nginx. and you want to create a service for nginx pod.

    then I will write below command to generate the service file.

    Synatax:

    kubectl expose [pod/deployment/replicaset] [name-of-pod/deployment/replicaset]  --port=80 --target-port=8000 --dry-run=client -o yaml
    

    Example:

    kubectl expose pod nginx  --port=80 --target-port=8000 --dry-run=client -o yaml 
    

    output:

    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: {}