What You Will Learn
Beginner
- What a Service is and why Pods need it
- The three Service types
- How to create Services
Four service types: ClusterIP (internal), NodePort (node), LoadBalancer (cloud), Ingress (HTTP)
A Service provides a stable IP and DNS name for a set of Pods. Pods come and go (new IPs), but the Service IP stays the same. Clients talk to the Service, which routes to healthy Pods.
service.yamlyaml
apiVersion: v1
kind: Service
metadata:
name: my-app
spec:
type: ClusterIP # Default: internal only
selector:
app: my-app # Routes to Pods with this label
ports:
- port: 80 # Service port
targetPort: 8080 # Container portService Types
| Type | Access | Use case |
|---|---|---|
| ClusterIP | Internal only (default) | Internal services (API to DB) |
| NodePort | NodeIP:30000+ | Dev/testing |
| LoadBalancer | Cloud LB + external IP | Production (web apps) |
| Ingress | HTTP routing by domain | Multiple web apps on one IP |
Terminalbash
# Create a Service
kubectl apply -f service.yaml
# Check Services
kubectl get services
# Access ClusterIP internally
kubectl exec -it another-pod -- curl http://my-app
# NodePort access
minikube service my-app --urlPractical Exercise
Create a Deployment with label app=web
Create a ClusterIP Service matching that label
Check: kubectl get svc
Access: kubectl exec -it busybox -- curl http://my-app
Change to NodePort and access via minikube
Key Takeaways
- Service = stable IP + DNS for a set of Pods.
- ClusterIP: internal. NodePort: node port. LoadBalancer: cloud LB.
- Selector matches Pod labels to route traffic.
- Pods die and get new IPs - Service IP is stable.
- DNS: my-service.namespace.svc.cluster.local
Comments
Comments
Post a Comment