#CREATE A DEPLOYMENT k create deployment nginx-deployment --image=nginx:alpine - . . .

Malayamanas Panda:
#CREATE A DEPLOYMENT
k create deployment nginx-deployment --image=nginx:alpine --port=8080 --replicas=2 $dry

#TEMPLATE

apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: nginx-deployment
name: nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nginx-deployment
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: nginx-deployment
spec:
containers:
- image: nginx:alpine
name: nginx
ports:
- containerPort: 8080
resources: {}
status: {}

#CREATE A SERVICE

k expose deployment nginx-deployment --name=nginx-deployment-service --type=NodePort --port=80 --target-port=8080 $dry

apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: nginx-deployment
name: nginx-deployment-service
spec:
ports:

  • port: 80
    protocol: TCP
    targetPort: 8080
    selector:
    app: nginx-deployment
    type: NodePort
    status:
    loadBalancer: {}

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’

apiVersion: v1
kind: Service
metadata:
name: nginx-deployment-service
namespace: default
spec:
ports:

  • nodePort: 32653
    port: 80
    protocol: TCP
    targetPort: 8080
    selector:
    app: nginx-deployment
    sessionAffinity: None
    type: NodePort

Malayamanas Panda:
@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.

spec:
ports:

  • nodePort: 32653 #This the port that all NODES accept traffic into TCP 32653
    port: 80 #What is this port 80 ?
    protocol: TCP
    targetPort: 8080#This is the port that pod/container listens

unnivkn:

Malayamanas Panda:
k explain service.spec.ports
KIND: Service
VERSION: v1

RESOURCE: ports <[]Object>

DESCRIPTION:
The list of ports that are exposed by this service. More info:
https://kubernetes.io/docs/concepts/services-networking/service/#virtual-ips-and-service-proxies

 ServicePort contains information on service's port.

FIELDS:
name <string>
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.

nodePort <integer>
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:
https://kubernetes.io/docs/concepts/services-networking/service/#type-nodeport

port <integer> -required-
The port that will be exposed by this service.

protocol <string>
The IP protocol for this port. Supports “TCP”, “UDP”, and “SCTP”. Default
is TCP.

targetPort <string>
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.
More info:
https://kubernetes.io/docs/concepts/services-networking/service/#defining-a-service

Malayamanas Panda:
in type: NodePort , a random 30000 - 32767 , is exposed, but what is this port: field ?

Malayamanas Panda:
https://kubernetes.io/docs/concepts/services-networking/service/#nodeport

Malayamanas Panda:

      # 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 ?

I Malla:
“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 ?

unnivkn:
@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.

Gurudutt Dongre:
‘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.

Gurudutt Dongre:
–port is mandatory when you use kubectl expose pod. Target port will default to --port if you don’t specify a value I believe.