liveBook · Manning

Port forwarding

As explained in Road to Kubernetes

Port forwarding is a networking technique that maps a local port to a port on a remote resource.

Port forwarding maps a local port to a port on a remote Kubernetes resource, letting localhost connect to a cluster resource without exposing the database publicly. section 8.4.2

Use kubectl to forward traffic to different Kubernetes targets:

  • Pod: kubectl port-forward postgres-0 3312:5432 section 8.4.2
  • StatefulSet: kubectl port-forward statefulsets/postgres 3312:5432; this selects the first Pod. section 8.4.2
  • Service: kubectl port-forward service/postgres 3312:5432. section 8.4.2

Connecting to PostgreSQL locally

After forwarding local port 3312 to PostgreSQL port 5432, connect with psql -h localhost -U postgres -d postgres-db -p 3312. Port forwarding verifies access from the local machine, not container-to-container communication inside the cluster. section 8.4.2

How Road to Kubernetes covers it

How deep it goes
Explains Pod, StatefulSet, and Service commands with port mappings, but stops short of in-cluster communication.
Examples
Uses postgres-0, a postgres StatefulSet, a postgres Service, port 3312, port 5432, and the psql client.
What it assumes
Assumes a running Kubernetes PostgreSQL resource and optionally a locally installed psql client.
What you can do after
Operate/configure kubectl port forwarding and verify database access with a local PostgreSQL client.
How it connects
Connects Kubernetes, kubectl, Pods, StatefulSets, Services, ClusterIP, and PostgreSQL.

Also explained in

Compare how 5 books explain port forwarding →

Related concepts

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