Expose Pods with ClusterIP, NodePort, and LoadBalancer services. Set up an Ingress for HTTP routing.
Pods get new IP addresses every time they restart. Services provide a stable IP and DNS name that routes to the current healthy Pods. Services are how Pods find each other.
# ClusterIP: internal-only, for pod-to-pod communication
apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
type: ClusterIP # or NodePort or LoadBalancer
selector:
app: web-app # routes to pods with this label
ports:
- port: 80 # port the service listens on
targetPort: 80 # port the pod listens on
---
# NodePort: accessible from outside the cluster on a high port
# LoadBalancer: provisions a cloud load balancer (AWS, GCP, Azure)
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: 30080 # 30000-32767 range# ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /api
pathType: Prefix
backend:
service:
name: api-service
port:
number: 80
- path: /
pathType: Prefix
backend:
service:
name: web-service
port:
number: 80minikube addons enable ingress.kubectl get svc and kubectl describe svc are your debugging tools for networking.