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
