1.6 KiB
1.6 KiB
🔗 Services in Kubernetes (SVC)
A Service in Kubernetes provides a stable networking interface to access a set of pods. It allows for decoupling between client applications and the underlying pods by using DNS names and selectors.
🌐 Service Basics
📌 Service Flow
Service ➡️ Endpoint ➡️ Pods
- Services abstract access to a group of pods.
- Services automatically get a DNS name in the cluster.
- They use selectors to route traffic to matching pods.
🧭 Service Types
-
ClusterIP (default)
- Accessible only within the cluster.
-
NodePort
- Exposes the service on a static port on each node.
-
LoadBalancer
- Provisions an external IP via a cloud provider to expose the service.
🧪 Useful Commands
🔍 Get Endpoints
kubectl get ep -n <namespace>
📄 Get Services
kubectl get svc -n <namespace>
🧾 Example Service Manifest
apiVersion: v1
kind: Service
metadata:
name: nginx
namespace: ns
labels:
app: web-server
spec:
type: ClusterIP # Options: ClusterIP, NodePort, LoadBalancer
selector:
app: nginx
ports:
- name: http # Port name is optional but useful
port: 80 # Service port
targetPort: 8080 # Container port
🔍 Note: The
selectormust match pod labels for the service to route traffic correctly. 🧠 Tip: Usekubectl describe svc <svc-name>to troubleshoot or verify service-to-pod connectivity. 🌐 Services are resolved by DNS using the format:<service-name>.<namespace>.svc.cluster.local.