I'm not clear about the usage of port option in the command kubectl expose deployment demo ---port=8080 --target-port=8080 --type=NodePort
I know this command is used to expose a deployment to external and the target port
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 --port
option? Is it the port for the user/client in the cluster?
Per the docs, kubectl expose
exposes a resource as a new Kubernetes service where:
--port
is "...The port that the service should serve on. Copied from the resource being exposed, if unspecified".
--target-port
is "...Name or number for the port on the container that the service should direct traffic to. Optional".
So, the --port
flag specifies the port on which the service will be accessible. It is to note that for NodePort type, "...the Kubernetes control plane allocates a port from a range specified by --service-node-port-range flag (default: 30000-32767)".
If you want to affix the NodePort port, and assuming the default settings, try using a port number in the range above. For example:
kubectl expose deployment demo --port=31080 --target-port=8080 --type=NodePort
Another option could be to use kubectl create service nodeport
with proper node-port
flag. It is documented here.
This may be a personal preference but as much as kubectl run, expose, and similar commands are convenient, I'd rather work with (declarative) configuration files that are checked in and versioned in a code repo than with imperative commands :-)