liveBook · Manning

Port number

As explained in Kubernetes in Action, Second Edition

A container port is a network port on which an application in a container accepts connections.

A container port is the port number on which an application in a container accepts connections. To send it a request, a client needs both an address and a port: for example, a pod IP address and port 8080. section 5.3.1

Listing 5.1 A basic pod manifest file
apiVersion: v1      #1
kind: Pod      #2
metadata:     
  name: kiada     #3
spec: 
  containers:
  - name: kiada      #4
    image: luksa/kiada:0.1     #5
    ports: 
    - containerPort: 8080      #6

This pod manifest records the application's port as `containerPort: 8080`, making the intended port visible to someone inspecting the pod. section 5.2.1

Declaring a container port is informative, not a requirement for connectivity. Leaving it out does not stop clients connecting to an application that accepts connections on the pod's IP address; declaring the wrong number does not change the port the application uses. Include it anyway so others can find the intended port. You can also name it for use by a Service. section 5.3.1

Keep these port numbers distinct:

  • The application's port is where the container accepts connections. `kubectl get pod kiada -o wide` shows the pod IP; with the example application's port, a client able to reach that IP can request `10.244.2.4:8080`. section 5.3.1
  • A Service's `port` is where the Service accepts connections; its `targetPort` identifies the port on a selected pod to which it forwards them. `targetPort` can use a number or a name declared in the container's port list. section 11.1.2
  • For access from your computer when the pod IP is not reachable there, `kubectl port-forward kiada 8080` forwards a local port to the pod; you can then request `localhost:8080`. section 5.3.1
Listing 11.1 YAML manifest for the quote Service object
apiVersion: v1    #1
kind: Service    #1
metadata:
  name: quote     #2
spec:
  type: ClusterIP      #3
  selector:          #4
    app: quote       #4
  ports:                 #5
  - name: http           #5
    port: 80             #5
    targetPort: 80       #5
    protocol: TCP        #5

In this ClusterIP Service manifest, `port: 80` accepts Service connections and `targetPort: 80` forwards them to a matching pod. The equal numbers are separate settings. section 11.1.2

How Kubernetes in Action, Second Edition covers it

How deep it goes
Distinguishes an informative pod port declaration from actual connectivity, then explains Service port and targetPort configuration.
Examples
Uses a pod manifest declaring `containerPort: 8080`, requests to a pod IP, and a Service manifest with `port` and `targetPort`.
What it assumes
Introduces the port through a basic pod manifest and explains how to find the pod IP before making a request.
What you can do after
Operate/configure it by recording a container port, finding a pod IP, and choosing a Service targetPort or local port-forward.
How it connects
Connects container ports to pod IP addresses, named ports, Services, and port forwarding.

Also explained in

Compare how 7 books explain port number →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes in Action, Second Edition. The section links point to the parts of the book it draws on.