updated kuber: svc doc

This commit is contained in:
2025-07-05 21:26:54 +03:30
parent 18d704e2d5
commit 148698fcc0
2 changed files with 55 additions and 40 deletions

View File

@@ -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 <namespace-name> svc/<service-name> <local-port>:<target-port>
```
> **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
> ```
--- ---

View File

@@ -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 **group Pods** behind a single access point.
* They get a **cluster-wide DNS name** automatically.
```` * Use **label selectors** to forward traffic to matching 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 ## 🧭 **Types of Services**
1. **ClusterIP** (default) 1. **ClusterIP** (default)
- Accessible only within the cluster.
* Only reachable within the cluster.
2. **NodePort** 2. **NodePort**
- Exposes the service on a static port on each node.
* Exposes the service via a static port on each node.
3. **LoadBalancer** 3. **LoadBalancer**
- Provisions an external IP via a cloud provider to expose the service.
* Creates an external IP address using a cloud provider.
--- ---
## 🧪 Useful Commands ## 🧪 **Useful Commands**
### 🔍 Get Endpoints ### 🔍 Get Endpoints
```bash ```bash
kubectl get ep -n <namespace> kubectl get ep -n <namespace>
```` ```
### 📄 Get Services ### 📄 Get Services
@@ -47,7 +50,33 @@ kubectl get svc -n <namespace>
--- ---
## 🧾 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 <namespace> svc/<service-name> <local-port>:<target-port>
```
> **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 <namespace> svc/nginx 80:80 --address 0.0.0.0
```
> 🌐 **DNS format:**
> `<service-name>.<namespace>.svc.cluster.local`
---
## 🧾 **Example Service Manifest**
```yaml ```yaml
apiVersion: v1 apiVersion: v1
@@ -62,12 +91,11 @@ spec:
selector: selector:
app: nginx app: nginx
ports: ports:
- name: http # Port name is optional but useful - name: http
port: 80 # Service port port: 80
targetPort: 8080 # Container port targetPort: 8080
``` ```
> 🔍 **Note:** The `selector` must match pod labels for the service to route traffic correctly. > 🔍 **Note:** The `selector` must match the labels of the target Pods.
> 🧠 **Tip:** Use `kubectl describe svc <svc-name>` to troubleshoot or verify service-to-pod connectivity. > 🧠 **Tip:** Use `kubectl describe svc <service-name>` to inspect the service and verify connectivity.
> 🌐 Services are resolved by DNS using the format: `<service-name>.<namespace>.svc.cluster.local`.