For containers, if i add containerPort, do i still need to expose it via service . . .

merina ghoghwani:
for containers, if i add containerPort, do i still need to expose it via service or it means that this port is open for the container and anyone can access inside cluster? is the containerPort same as targetPort?

Vinay Reddy:
yes

merina ghoghwani:
yes for second question, right?

Madhan Kumar:
By specifying container port explicitly in the yaml is equivalent to opening that port on the pod which hosts that container . If the app running inside the container is configured to use the port then yes it will work , else its just a dummy open port on the container which is open but does nothing .

Madhan Kumar:
Containerport defined in the pod.yaml is same as the target port defined in the service.yaml

merina ghoghwani:
@Madhan Kumar ok, so if have added a port, i do not need clusterIP service for my pod?

Madhan Kumar:
by exposing a port on a pod you can directly access in the pods-ip:port . Services are used mainly if you need advanced routing or load balancing or dns resolution for example if you want to access your applicaiton using a name then you need a service, as services can be accessed using service-name:port or its clusterip:port and it will automatically route the traffic to one of the pods:port that it supports . use kubectl get ep to see the relation between service-name and the podip:port it is mapped to .

Madhan Kumar:
you can directly run your application either by using pod-ip:port or route it through a service using servicename:port or service-clusterip:port , all 3 will work .

merina ghoghwani:
@Madhan Kumar so i will add a containerport if my application needs traffic on that particular port only and service to support dynamic IP change.
i created a pod without opening a port, and then exposed using kubectl expose:

service:
nginx ClusterIP 10.107.213.79 <none> 80/TCP 3m53s run=nginx run=nginx

why can’t i see it in the pod?
Port: <none>
Host Port: <none>
State: Running

Madhan Kumar:
when creating the pod if we don’t specify the port in the yaml or in the run command then when you describe the pod after creating the port will be blank . even though port is blank when you describe the pod , the container can by default expose ports if it wants , in this case the port 80 will get exposed by default even though we do not expose it manually the ngnix container when it starts running it will expose the port 80 . hope it makes sense :slightly_smiling_face:

Madhan Kumar:
after pod creation when you expose it using the expose command the expose command just creates a service to route the traffic to the pod on port 80 .

Madhan Kumar:
user -> serviceclusterip:80 -> podip:80

merina ghoghwani:
suppose it’s not 80…

merina ghoghwani:
why is this so confusing for me

merina ghoghwani:
the ngnix container when it starts running it will expose the port 80 . >> this is coz the app does it… but when my app does accept traffic on a port, i will need to open a port on my container…

the question is why do i need service if i need to talk to this pod only from other pods in the cluster or do i need containerPort if i just expose a service to the app port?

Madhan Kumar:
its actually simple … if you have access to udemy you can go through the udemy services section which explains it clearly . may be I am not able to articulate it …

Madhan Kumar:
to put it simple if you just want to access your pod using its ip and port you can do so and service is no needed at all . only thing we need in this case is that the port should be open on the pod . containers running inside the pod can by default open a port which it wants or you can manually open a port on the container by specifying the port field .