<p>What are the differences between</p>
<pre><code>kubectl expose deployment myservice --type=NodePort --port=80 --target-port=80
</code></pre>
<p>and</p>
<pre><code>kubectl create service nodeport myservice --tcp=80:80
</code></pre>
<p>?</p>

<pre><code>kubectl create service nodeport myservice --tcp=80:80
</code></pre>
<p>it's just create the service with servicename as labels/selector.</p>
<pre><code>kubectl expose deployment myservice --type=NodePort --port=80 --target-port=80
</code></pre>
<p>it creates the service based on <code>labels/selector</code> specified in <strong>deployment</strong>. So using this you can expose all the PODs of specific one deployment over a single <strong>Nodeport</strong>.</p>
<p><strong>Explanation</strong> :</p>
<p>Both have difference in <strong>labels</strong> mainly or <strong>selectors</strong> in other words.</p>
<pre><code>kubectl create service nodeport myservice --tcp=80:80
</code></pre>
<p>If you will run above command it will create the service with selector <code>myservice</code> in it.</p>
<p><strong>Example</strong> :</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  labels:
    app: myservice
  name: myservice
spec:
  ports:
  - name: 80-80
    nodePort: 30858
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: myservice
  type: NodePort
</code></pre>
<p>as you can see your <code>selector : myservice</code> and same set in <code>labels</code> &amp; <code>servicename</code>. So <strong>service name</strong> goes as <strong>labels</strong> and <strong>selector</strong>. Your service was created here but won't be able to talk to deployment as the selector is different (unless you create the service with the name of matching selector from deployment).</p>
<p>While with command :</p>
<pre><code>kubectl expose deployment nginx-deployment --type=NodePort --port=80 --target-port=80
</code></pre>
<p>it will use the labels and selector of deployment. Which mean if you have deployment running of Nginx and it has <code>selector : nginx</code> it will get it from there as you are exposing that deployment.</p>
<p><strong>Example deployment :</strong></p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:1.14.2
        imagePullPolicy: IfNotPresent
        name: nginx
        ports:
        - containerPort: 80
          protocol: TCP
</code></pre>
<p>Now if you will run command like</p>
<pre><code>kubectl expose deployment nginx-deployment --type=NodePort --port=80 --target-port=80
</code></pre>
<p>service will have <code>selector: nginx</code> used in deployment.</p>
<p><strong>Example service</strong> :</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx
  name: nginx-deployment
spec:
  ports:
  - name: 80-80
    nodePort: 30858
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
  type: NodePort
</code></pre>
