Services
What are Services?
Services provide stable network endpoints for pods, enabling communication between different parts of an application.
Service Types
1. ClusterIP (Default)
- Internal communication only
- Accessible only within cluster
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP
2. NodePort
- Exposes service on each node's IP
- Accessible from outside cluster
apiVersion: v1
kind: Service
metadata:
name: my-nodeport-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 30080
type: NodePort
3. LoadBalancer
- Creates external load balancer
- Typically used in cloud environments
apiVersion: v1
kind: Service
metadata:
name: my-loadbalancer-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
4. ExternalName
- Maps service to external DNS name
apiVersion: v1
kind: Service
metadata:
name: external-service
spec:
type: ExternalName
externalName: example.com
Service Discovery
# List services
kubectl get services
# Describe service
kubectl describe service <service-name>
# Get service endpoints
kubectl get endpoints <service-name>
Next Steps
Learn about Deployments to manage multiple pod replicas and updates.