Kubernetes: Expose Your App
Exposing an app means giving it an address that traffic from outside the cluster can reach. The Service type you pick decides how that address works.
Expose with NodePort
NodePort opens one port on every node. Traffic to that port reaches the Service. It is the simplest way to reach an app from outside the cluster.
apiVersion: v1
kind: Service
metadata:
name: web
spec:
type: NodePort
selector:
app: web
ports:
- port: 80
targetPort: 80
nodePort: 30080Open it on minikube
On a local minikube cluster, the minikube service command opens the Service in your browser. It handles the node address and port for you.
minikube service webLoadBalancer stays pending locally
A local cluster has no cloud to hand out a public address. The Service waits with no external address. That waiting state is normal, not a bug.
kubectl get service web-lbExample
kubectl apply -f web-nodeport.yaml
kubectl get service webKey points
- ClusterIP: reachable only inside the cluster, so use it for internal apps
- NodePort: opens the same port on every node for traffic from outside
- LoadBalancer: asks the cloud for a public address for the Service
- Use minikube service to open a NodePort Service on a local cluster
- Use minikube tunnel to give a LoadBalancer Service a real address locally
In this example, the first command creates a NodePort Service from the file. The second command reads it back. The Service listens on port 30080 on every node, so traffic to that port reaches the web pods.
Note
a LoadBalancer Service stays pending on a local cluster until minikube tunnel or minikube service runs
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
A LoadBalancer Service on a local minikube cluster shows <pending> as its external address. What does that mean?