From 148698fcc0aab617cde7011cca42a04626aae33d Mon Sep 17 00:00:00 2001 From: root Date: Sat, 5 Jul 2025 21:26:54 +0330 Subject: [PATCH] updated kuber: svc doc --- .../Kubernetes/Commands/3-Commands.md | 13 --- .../Kubernetes/workloads/09-Services.md | 82 +++++++++++++------ 2 files changed, 55 insertions(+), 40 deletions(-) diff --git a/Containerization & Orchestration/Kubernetes/Commands/3-Commands.md b/Containerization & Orchestration/Kubernetes/Commands/3-Commands.md index a0f3ca5..02f0118 100755 --- a/Containerization & Orchestration/Kubernetes/Commands/3-Commands.md +++ b/Containerization & Orchestration/Kubernetes/Commands/3-Commands.md @@ -106,19 +106,6 @@ This guide provides a concise reference for common `kubectl` commands used to ma --- -## Port Forwarding - -Sometimes you need to access a service or pod directly from your local machine. Use the following command to forward a port: - -```bash -kubectl port-forward -n svc/ : -``` - -> **Example:** Forward local port 8080 to port 80 of the service named `my-service` in the `mynamespace` namespace: -> -> ```bash -> kubectl port-forward -n mynamespace svc/my-service 8080:80 -> ``` --- diff --git a/Containerization & Orchestration/Kubernetes/workloads/09-Services.md b/Containerization & Orchestration/Kubernetes/workloads/09-Services.md index 4700b38..aa5f782 100644 --- a/Containerization & Orchestration/Kubernetes/workloads/09-Services.md +++ b/Containerization & Orchestration/Kubernetes/workloads/09-Services.md @@ -1,43 +1,46 @@ -# ๐Ÿ”— Services in Kubernetes (SVC) +# ๐Ÿ”— **Kubernetes Services (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. +A **Service** in Kubernetes provides a stable network endpoint to access a set of Pods. It abstracts access through selectors and DNS names, enabling loose coupling between client applications and Pods. --- -## ๐ŸŒ Service Basics +## ๐ŸŒ **Service Basics** -### ๐Ÿ“Œ Service Flow +### ๐Ÿ“Œ **Service Flow** + +``` +Service โžก๏ธ Endpoint โžก๏ธ Pods ``` -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. +* Services **group Pods** behind a single access point. +* They get a **cluster-wide DNS name** automatically. +* Use **label selectors** to forward traffic to matching Pods. --- -## ๐Ÿงญ Service Types +## ๐Ÿงญ **Types of Services** -1. **ClusterIP** (default) - - Accessible only within the cluster. +1. **ClusterIP** (default) -2. **NodePort** - - Exposes the service on a static port on each node. + * Only reachable within the cluster. -3. **LoadBalancer** - - Provisions an external IP via a cloud provider to expose the service. +2. **NodePort** + + * Exposes the service via a static port on each node. + +3. **LoadBalancer** + + * Creates an external IP address using a cloud provider. --- -## ๐Ÿงช Useful Commands +## ๐Ÿงช **Useful Commands** ### ๐Ÿ” Get Endpoints + ```bash kubectl get ep -n -```` +``` ### ๐Ÿ“„ Get Services @@ -47,7 +50,33 @@ kubectl get svc -n --- -## ๐Ÿงพ Example Service Manifest +## ๐Ÿ” **Port Forwarding** + +To access a service from your local machine, forward a local port to the service port: + +```bash +kubectl port-forward -n svc/ : +``` + +> **Example:** +> Forward local port `8080` to port `80` of `my-service` in the `mynamespace` namespace: +> +> ```bash +> kubectl port-forward -n mynamespace svc/my-service 8080:80 +> ``` + +You can also bind to all network interfaces: + +```bash +kubectl port-forward -n svc/nginx 80:80 --address 0.0.0.0 +``` + +> ๐ŸŒ **DNS format:** +> `..svc.cluster.local` + +--- + +## ๐Ÿงพ **Example Service Manifest** ```yaml apiVersion: v1 @@ -62,12 +91,11 @@ spec: selector: app: nginx ports: - - name: http # Port name is optional but useful - port: 80 # Service port - targetPort: 8080 # Container port + - name: http + port: 80 + targetPort: 8080 ``` -> ๐Ÿ” **Note:** The `selector` must match pod labels for the service to route traffic correctly. -> ๐Ÿง  **Tip:** Use `kubectl describe svc ` to troubleshoot or verify service-to-pod connectivity. -> ๐ŸŒ Services are resolved by DNS using the format: `..svc.cluster.local`. +> ๐Ÿ” **Note:** The `selector` must match the labels of the target Pods. +> ๐Ÿง  **Tip:** Use `kubectl describe svc ` to inspect the service and verify connectivity.