Pods and ports

I created postgres deployment with containerPort as 55432 and same for service. My understanding was when pod is deployed I could access postgres at 55432 but somehow it is not working. I could access postgres on 5432 instead. What am I missing here?

Also does all the pods that gets deployed with k8s have their own set of ports?

ref: https://stackoverflow.com/questions/54993699/how-to-test-if-container-is-running-postgres

From your stack overflow post: you only “told” Kubernetes at what port something is listening inside the container, you didn’t specify to which port you’d like to map that.

I personally, would put a service in front of it as documented here:
https://kubernetes.io/docs/concepts/services-networking/service/

kind: Service
apiVersion: v1
metadata:
name: my-postgres-service
spec:
  selector:
    app: my-postgres-app
  ports:
  - protocol: TCP
    port: 80 #That's the port you'll use to connect
    targetPort: 55432 # that's the port at which your app (your postgres) is listening

So I cant connect to postgres running on the pod if I am trying to access it on IP and port of the deployment pod?

I tried on IP from kubectl describe deployment postgres postgres deployment and port as containerport from deployment. dint work. But it worked on same IP and port as 5432. I am guessing somehow container is using containerPort option and just configuring 5432 for it.

The ports you specify in the deployment are “declarative”. You may want to use it to declare which ports the application uses.

But it won’t change in any way the ports the application uses. You can see them, for example, with netcat -putan in the container.

Is it more clear now?

spec:
  containers:
    - name: postgres
      image: postgres:10.4
      imagePullPolicy: "IfNotPresent"
      ports:
        - containerPort: 55432 # what does this port desc do?

This section of the documentation is probably the best to explain what “ContainerPort” actually does.
https://kubernetes.io/docs/concepts/services-networking/connect-applications-service/#exposing-pods-to-the-cluster

I went through it before. From my understanding it exposes port specified on the pod container is running. I guess for now I will go with 5432 and revisit it once I encounter a problem. Thanks Rira you have been great help.