expose

Expose a resource as a new Kubernetes service.

Looks up a deployment, service, replica set, replication controller or pod by name and uses the selector for that resource as the selector for a new service on the specified port. A deployment or replica set will be exposed as a service only if its selector is convertible to a selector that service supports, i.e. when the selector contains only the matchLabels component. Note that if no port is specified via –port and the exposed resource has multiple ports, all will be re-used by the new service. Also if no labels are specified, the new service will re-use the labels from the resource it exposes.

Resources

Possible resources include (case insensitive):

  • pod (po)
  • service (svc)
  • replicationcontroller (rc)
  • deployment (deploy)
  • replicaset (rs)

Command

  1. $ kubectl run NAME --image=image [--env="key=value"] [--port=port] [--dry-run=server|client] $ kubectl expose (-f FILENAME | TYPE NAME) [--port=port] [--protocol=TCP|UDP|SCTP] [--target-port=number-or-name] [--name=name] [--external-ip=external-ip-of-service] [--type=type]

Example

Current state

  1. $ kubectl get pods
  2. NAME READY STATUS RESTARTS AGE
  3. nginx-6db489d4b7-9wgn9 1/1 Running 0 28s
  4. $ kubectl get deployment
  5. NAME READY UP-TO-DATE AVAILABLE AGE
  6. nginx 1/1 1 1 44s

Command

  1. $ kubectl expose po nginx-6db489d4b7-9wgn9 --port=80 --target-port=8000

Output

  1. $ kubectl get services
  2. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  3. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 8m47s
  4. nginx-6db489d4b7-9wgn9 ClusterIP 10.106.133.77 <none> 80/TCP 2m56s