liveBook · Manning

Kubectl expose deployment

As explained in Kubernetes in Action, Second Edition

kubectl expose is a Kubernetes command that creates a Service for an existing workload or pod.

kubectl expose deployment creates a Kubernetes Service for an existing Deployment. The Service selects the Deployment’s pods and gives clients a stable address instead of requiring them to contact individual pods directly. section 3.4 section 11.1.2

A typical application workflow uses the command to connect workload deployment with client access:

  • Create or identify a Deployment that manages the application’s pods. section 3.4
  • Run `kubectl expose deployment <deployment-name>` to create a Service using the Deployment’s selector. section 3.4 section 11.1.2
  • Use `kubectl get services` or `kubectl get svc -o wide` to inspect the Service type, internal IP, ports, and selector. section 11.1.2

Expose a Deployment inside the cluster

Run `kubectl expose deployment <deployment-name>`. Kubernetes creates a Service that forwards connections to matching pods. The default Service type is ClusterIP, so workloads inside the cluster can use its internal IP. A LoadBalancer Service can additionally request a publicly accessible address from the cloud infrastructure. section 3.4 section 11.1.2

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

Use the Service manifest to inspect the declarative object created by exposure: its type, selector, and forwarding ports. section 11.1.2

How Kubernetes in Action, Second Edition covers it

How deep it goes
Explains selector-based Service creation and inspection, while deferring detailed Service types and endpoint management.
Examples
Demonstrates exposing a Deployment and inspecting Services with kubectl get services or kubectl get svc -o wide.
What it assumes
Assumes a Kubernetes Deployment exists and relates it to the pods, labels, and Service objects involved in exposure.
What you can do after
Operate/configure Kubernetes application access by creating and inspecting a Service with kubectl.
How it connects
Connects kubectl expose deployment with Deployments, Services, Pods, selectors, ports, and load balancing.

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.