#CREATE A DEPLOYMENT
k create deployment nginx-deployment --image=nginx:alpine --port=8080 --replicas=2 $dry
- image: nginx:alpine
- containerPort: 8080
#CREATE A SERVICE
k expose deployment nginx-deployment --name=nginx-deployment-service --type=NodePort --port=80 --target-port=8080 $dry
- port: 80
k get service
nginx-deployment-service NodePort 10.110.55.219 <none> 80:32653/TCP 13s
#GET YAML after service is created
k get service nginx-deployment-service -o yaml | egrep -iv ‘f:|.:|fields’
- nodePort: 32653
@Tej_Singh_Rana @Hinodeya @Mumshad Mannambeth @Gurudutt Dongre What does it mean in spec below for an exposed service, like below ? How traffic flow, please elaborate.
- nodePort: 32653 #This the port that all NODES accept traffic into TCP 32653
port: 80 #What is this port 80 ?
targetPort: 8080#This is the port that pod/container listens
k explain service.spec.ports
RESOURCE: ports <Object>
The list of ports that are exposed by this service. More info:
ServicePort contains information on service's port.
The name of this port within the service. This must be a DNS_LABEL. All
ports within a ServiceSpec must have unique names. When considering the
endpoints for a Service, this must match the ‘name’ field in the
EndpointPort. Optional if only one ServicePort is defined on this service.
The port on each node on which this service is exposed when type=NodePort
or LoadBalancer. Usually assigned by the system. If specified, it will be
allocated to the service if unused or else creation of the service will
fail. Default is to auto-allocate a port if the ServiceType of this Service
requires one. More info:
port <integer> -required-
The port that will be exposed by this service.
The IP protocol for this port. Supports “TCP”, “UDP”, and “SCTP”. Default
Number or name of the port to access on the pods targeted by the service.
Number must be in the range 1 to 65535. Name must be an IANA_SVC_NAME. If
this is a string, it will be looked up as a named port in the target Pod’s
container ports. If this is not specified, the value of the ‘port’ field is
used (an identity map). This field is ignored for services with
clusterIP=None, and should be omitted or set equal to the ‘port’ field.
in type: NodePort , a random 30000 - 32767 , is exposed, but what is this port: field ?
# By default and for convenience, the `targetPort` is set to the same value as the `port` field.
What is the meaning of it, why is it convenient , what is the meaning of it ?
“port” is the port of service and "targetport" is the port of the pod i.e endpoint … and if target is not mentioned it takes port as target as well . is this the part you were refering ?
@Malayamanas Panda If you create a service with this command: “k expose po nginx --port=80”, & then if you look at the “k describe svc nginx” you can see both port & target-port as 80. keep in mind for creating a service --port is only the mandatory field.
‘port’ is the port number of the service itself. Targetport is where the port where pod/application serves the reqest. Nodeport is where the user tries to connect using the browser.
–port is mandatory when you use kubectl expose pod. Target port will default to --port if you don’t specify a value I believe.