Files
my-docs/Containerization & Orchestration/Kubernetes/workloads/9-Services.md
2025-06-30 13:21:52 +03:30

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

  1. ClusterIP (default)

    • Accessible only within the cluster.
  2. NodePort

    • Exposes the service on a static port on each node.
  3. 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 selector must match pod labels for the service to route traffic correctly. 🧠 Tip: Use kubectl 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.