<p>I am new to Kubernetes. I was going through some tutorials related to Kubernetes deployment. I am seeing two different commands which looks like doing similar things.</p>
<ol>
<li><p>The below command is from google code lab (URL: <a href="https://codelabs.developers.google.com/codelabs/cloud-springboot-kubernetes/index.html?index=..%2F..index#7" rel="noreferrer">https://codelabs.developers.google.com/codelabs/cloud-springboot-kubernetes/index.html?index=..%2F..index#7</a> )</p>
<p><code>$ kubectl create service loadbalancer hello-java --tcp=8080:8080</code></p>
</li>
<li><p>Another command is being seen in a different place along with the Kubernetes site (<a href="https://kubernetes.io/docs/tutorials/stateless-application/expose-external-ip-address/" rel="noreferrer">https://kubernetes.io/docs/tutorials/stateless-application/expose-external-ip-address/</a>)</p>
</li>
</ol>
<p><code>$ kubectl expose deployment hello-world --type=LoadBalancer --name=my-service</code></p>
<br/>
Now as per my understanding both the command are creating services from deployments with loadbalancer and exposing them to the outer world. 
<p>I don't think there will be two separate commands for the same task. There should be some difference that I am not able to understand.</p>
<p>Would anyone please clarify this to me?</p>

<p>There are cases where the <code>expose</code> command is not sufficient &amp; your only practical option  is to use <code>create service</code>.</p>
<p>Overall there are 4 different types of Kubernetes services, for some it really doesn't matter if you use expose or create, while for others it maters very much.</p>
<p>The types of Kubernetes services are:</p>
<ul>
<li>ClusterIP</li>
<li>NodePort</li>
<li>LoadBalancer</li>
<li>ExternalName</li>
</ul>
<p>So for example in the case of the <strong>NodePort</strong> type service let's say we wanted to set a node port with value <strong>31888</strong> :</p>
<ul>
<li><p><strong>Example 1:</strong>
In the following command there is no argument for the node port value, the expose command creates it automatically:</p>
<p><code>kubectl expose deployment demo --name=demo --type=NodePort --port=8080 --target-port=80</code></p>
</li>
</ul>
<p>The only way to set the node port value is after being created using the edit command to update the node port value: <code>kubectl edit service demo</code></p>
<ul>
<li><p><strong>Example 2:</strong>
In this example the create service nodeport is dedicated to creating the NodePort type and has arguments to enable us to control the node port value:</p>
<p><code>kubectl create service nodeport demo --tcp=8080:80 --node-port=31888</code></p>
</li>
</ul>
<p>In this Example 2 the node port value is set with the command line and there is no need to manually edit the value as in case of Example 1.</p>
<p><strong>Important</strong> :</p>
<p>The <code>create service [service-name]</code> does not have an option to set the service's selector , so the service wont automatically connect to existing pods.</p>
<p>To set the selector labels to target specific pods you will need to follow up the <code>create service [service-name]</code> with the <code>set selector</code> command :</p>
<p><code>kubectl set selector service [NAME] [key1]=[value1]</code></p>
<p>So for above case 2 example, if you want the service to work with a deployment with pods labeled <code>myapp: hello</code> then this is the follow-up command needed:</p>
<pre><code>kubectl set selector service demo myapp=hello
</code></pre>
