<p>I'm not clear about the usage of port option in the command <code>kubectl expose deployment demo ---port=8080 --target-port=8080 --type=NodePort</code></p>

<p>I know this command is used to expose a deployment to external and the <code>target port</code> is the port on the container. After running this command, Minikube would randomly assign a port on the node. Then I could access my service by this port. How about the usage of the <code>--port</code> option? Is it the port for the user/client in the cluster?</p>

Xinhao
<p>Per the <a href="https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#expose" rel="nofollow noreferrer">docs</a>, <code>kubectl expose</code> exposes a resource as a new Kubernetes service where:</p>

<ul>
<li><p><code>--port</code> is "...The port that the service should serve on. Copied from the resource being exposed, if unspecified".</p></li>
<li><p><code>--target-port</code> is "...Name or number for the port on the container that the service should direct traffic to. Optional".</p></li>
</ul>

<p>So, the <code>--port</code> flag specifies the port on which the service will be accessible. It is to note that for <a href="https://kubernetes.io/docs/concepts/services-networking/service/#nodeport" rel="nofollow noreferrer">NodePort</a> type, "...the Kubernetes control plane allocates a port from a range specified by --service-node-port-range flag (default: 30000-32767)".</p>

<p>If you want to affix the NodePort port, and assuming the default settings, try using a port number in the range above. For example:</p>

<pre><code>kubectl expose deployment demo --port=31080 --target-port=8080 --type=NodePort
</code></pre>

<p>Another option could be to use <code>kubectl create service nodeport</code> with proper <code>node-port</code> flag. It is documented <a href="https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#-em-service-nodeport-em-" rel="nofollow noreferrer">here</a>.</p>

<p>This may be a personal preference but as much as kubectl run, expose, and similar commands are convenient, I'd rather work with (<a href="https://kubernetes.io/docs/tasks/manage-kubernetes-objects/declarative-config/" rel="nofollow noreferrer">declarative</a>) configuration files that are checked in and versioned in a code repo than with <a href="https://kubernetes.io/docs/tasks/manage-kubernetes-objects/imperative-command/" rel="nofollow noreferrer">imperative commands</a> :-)</p>
