Welcome to the Linux Foundation Forum!

Doubts reg port while creating pod

In Lab 2.3. Create a Basic Pod

We have created basic pod using:

tkundu@master-node:~/LFD259/SOLUTIONS/s_02$ cat basic.yaml
apiVersion: v1
kind: Pod
metadata:
name: basicpod
spec:
containers:
- name: webcont
image: nginx
tkundu@master-node:~/LFD259/SOLUTIONS/s_02$

Now, we tried to add
ports: #<--Add this and following

  • containerPort: 80

But i was able to curl to the pod Ip and get nginx page before and after. So, would like to know the difference.

Even without adding containerPort, i am able to curl:

tkundu@master-node:~/LFD259/SOLUTIONS/s_02$ curl http://192.168.166.131
<!DOCTYPE html>


Welcome to nginx!

body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; }


Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.



tkundu@master-node

I am new into kubernetes, would be helpful is someone can explain.

Comments

  • FlamentBruno
    FlamentBruno Posts: 9
    edited July 2020

    Hello,
    It's normal, check container spec:

    https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#container-v1-core

    ports:
    List of ports to expose from the container. Exposing a port here gives the system additional information about the network connections a container uses, but is primarily informational. Not specifying a port here DOES NOT prevent that port from being exposed. Any port which is listening on the default "0.0.0.0" address inside a container will be accessible from the network. Cannot be updated.

    Cord
    Bruno

  • chrispokorni
    chrispokorni Posts: 2,155

    Hi @tapakund,

    The container running inside the Pod is already listening on the port(s) it was designed to listen for traffic.

    In the case of an nginx webserver, the container is listening on port 80. Therefore, you are able to access the webserver via curl to port 80.
    The containerPort property of the Pod spec is for informational purposes only in the context of the Kubernetes cluster. When exposing an application with Kubernetes, the cluster needs to know where to forward traffic requests, and specifying the containerPort is one method to inform the cluster where to forward such traffic.

    Regards,
    -Chris

  • tapakund
    tapakund Posts: 8

    Thanks @chrispokorni @FlamentBruno. This helps.

Categories

Upcoming Training