Interesting Things About Kubernetes And Ports

After decades of Java, I am now reduced to a rank rookie as I work on Kubernetes-related tasks.  It’s fun being the Dumb New Guy again.

It was in that spirit that I started asking some questions on the #kubernetes-users Slack channel.

I learned that the Dockerfile of an image referred to by a Kubernetes Deployment is basically advisory.  Where ports are concerned, specifically, any EXPOSE directive is basically ignored or superfluous. That makes a certain amount of sense.

Instead I was advised that containerPorts are where you state definitively what ports to open on your Container.

As it turns out, this isn’t strictly speaking correct. I found from some careful reading of the Container documentation that the containerPorts directive is also advisory (well, “primarily informational”, whatever that means).

So that all means that you could, conceivably, have a Dockerfile in the mix that never uses EXPOSE, and a Deployment that never uses containerPorts, and you could write a program running on the Docker container in question on whatever port it wants (let’s say 9000), and assuming that you defined a Kubernetes Service with a targetPort of 9000, connections would go through just fine.