Nginx Pod not taking specified port

9/22/2021

I am learning K8s and I have created a nginx pod using below yaml

apiVersion: v1
kind: Pod
metadata:
  name: apache-manual
spec:
  containers:
  - image: ewoutp/docker-nginx-curl
    name: apache-manual
    ports:
    - containerPort: 8080
      protocol: TCP

I run this pod using the command microk8s.kubectl create -f k8s-apache.yaml now if I describe my pod it looks like

Name:         apache-manual
Namespace:    default
Priority:     0
Node:         itsupport-thinkpad-l490/192.168.0.190
Start Time:   Wed, 22 Sep 2021 15:30:15 +0530
Labels:       <none>
Annotations:  <none>
Status:       Running
IP:           10.1.7.69
IPs:
  IP:  10.1.7.69
Containers:
  apache-manual:
    Container ID:   containerd://b7f1e7c2779076b786c001de2743d53f8c44214a1f3f98a21a77321f036138bf
    Image:          ewoutp/docker-nginx-curl
    Image ID:       sha256:806865143d5c6177c9fad6f146cc2e01085f367043f78aa16f226da5c09217b2
    // #####  PORT it's taking is 8080 here but when I am hitting curl on 8080 it's failing but working on port 80  #########
    Port:           8080/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Wed, 22 Sep 2021 15:30:20 +0530
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    // other stuff

Now if I am executing microk8s.kubectl exec -it apache-manual -- curl http://localhost:8080 it's giving me connection refused while microk8s.kubectl exec -it apache-manual -- curl http://localhost:80 is working fine, I am not able to understand why nginx is running on default port i.e 80 but not on the port I have specified in the above yaml i.e - containerPort: 8080

Am I missing something?

-- Kumar-Sandeep
kubernetes
nginx

1 Answer

9/22/2021

NGINX Docker image uses port 80 to listen for HTTP connections by default. containerPort is the port which you expose your application to external traffic.

-- coldbreathe
Source: StackOverflow