From fba77d8bf6b44a3d0899069e32f70149cc91ef2d Mon Sep 17 00:00:00 2001 From: root Date: Mon, 30 Jun 2025 13:21:52 +0330 Subject: [PATCH] kubernetes doc --- .../{4-BasicSetup.md => 3-BasicSetup.md} | 0 .../Kubernetes/{ => Commands}/3-Commands.md | 0 .../Kubernetes/{ => Commands}/label.md | 0 .../Kubernetes/{ => Commands}/log-debug.md | 0 .../Kubernetes/{ => Commands}/node-manage.md | 0 .../{components => workloads}/1-NameSpace.md | 0 .../workloads/10-Deployment-With-Services.md | 189 ++++++++++++++++ .../{components => workloads}/2-Pod.md | 0 .../{components => workloads}/3-Replicaset.md | 0 .../{components => workloads}/4-Deployment.md | 0 .../{components => workloads}/5-hpa.md | 0 .../{components => workloads}/6-DaemonSet.md | 0 .../{components => workloads}/7-Jobs.md | 0 .../{components => workloads}/8-Cronjobs.md | 0 .../Kubernetes/workloads/9-Services.md | 73 ++++++ .../Kubernetes/workloads/all.md | 208 ------------------ .../workloads/deployment-with-service.yaml | 23 -- .../workloads/{ => manifests}/cronjob.yaml | 0 .../manifests/deployment-with-service.yaml | 52 +++++ .../workloads/{ => manifests}/deployment.yaml | 0 .../workloads/{ => manifests}/ds.yml | 0 .../workloads/{ => manifests}/job.yaml | 0 .../workloads/{ => manifests}/namespace.yaml | 0 .../workloads/{ => manifests}/pod.yaml | 0 .../workloads/{ => manifests}/replicaset.yml | 0 .../workloads/{ => manifests}/service.yaml | 0 26 files changed, 314 insertions(+), 231 deletions(-) rename Containerization & Orchestration/Kubernetes/{4-BasicSetup.md => 3-BasicSetup.md} (100%) rename Containerization & Orchestration/Kubernetes/{ => Commands}/3-Commands.md (100%) rename Containerization & Orchestration/Kubernetes/{ => Commands}/label.md (100%) rename Containerization & Orchestration/Kubernetes/{ => Commands}/log-debug.md (100%) rename Containerization & Orchestration/Kubernetes/{ => Commands}/node-manage.md (100%) rename Containerization & Orchestration/Kubernetes/{components => workloads}/1-NameSpace.md (100%) create mode 100644 Containerization & Orchestration/Kubernetes/workloads/10-Deployment-With-Services.md rename Containerization & Orchestration/Kubernetes/{components => workloads}/2-Pod.md (100%) rename Containerization & Orchestration/Kubernetes/{components => workloads}/3-Replicaset.md (100%) rename Containerization & Orchestration/Kubernetes/{components => workloads}/4-Deployment.md (100%) rename Containerization & Orchestration/Kubernetes/{components => workloads}/5-hpa.md (100%) rename Containerization & Orchestration/Kubernetes/{components => workloads}/6-DaemonSet.md (100%) rename Containerization & Orchestration/Kubernetes/{components => workloads}/7-Jobs.md (100%) rename Containerization & Orchestration/Kubernetes/{components => workloads}/8-Cronjobs.md (100%) create mode 100644 Containerization & Orchestration/Kubernetes/workloads/9-Services.md delete mode 100755 Containerization & Orchestration/Kubernetes/workloads/all.md delete mode 100644 Containerization & Orchestration/Kubernetes/workloads/deployment-with-service.yaml rename Containerization & Orchestration/Kubernetes/workloads/{ => manifests}/cronjob.yaml (100%) create mode 100644 Containerization & Orchestration/Kubernetes/workloads/manifests/deployment-with-service.yaml rename Containerization & Orchestration/Kubernetes/workloads/{ => manifests}/deployment.yaml (100%) rename Containerization & Orchestration/Kubernetes/workloads/{ => manifests}/ds.yml (100%) rename Containerization & Orchestration/Kubernetes/workloads/{ => manifests}/job.yaml (100%) rename Containerization & Orchestration/Kubernetes/workloads/{ => manifests}/namespace.yaml (100%) rename Containerization & Orchestration/Kubernetes/workloads/{ => manifests}/pod.yaml (100%) rename Containerization & Orchestration/Kubernetes/workloads/{ => manifests}/replicaset.yml (100%) rename Containerization & Orchestration/Kubernetes/workloads/{ => manifests}/service.yaml (100%) diff --git a/Containerization & Orchestration/Kubernetes/4-BasicSetup.md b/Containerization & Orchestration/Kubernetes/3-BasicSetup.md similarity index 100% rename from Containerization & Orchestration/Kubernetes/4-BasicSetup.md rename to Containerization & Orchestration/Kubernetes/3-BasicSetup.md diff --git a/Containerization & Orchestration/Kubernetes/3-Commands.md b/Containerization & Orchestration/Kubernetes/Commands/3-Commands.md similarity index 100% rename from Containerization & Orchestration/Kubernetes/3-Commands.md rename to Containerization & Orchestration/Kubernetes/Commands/3-Commands.md diff --git a/Containerization & Orchestration/Kubernetes/label.md b/Containerization & Orchestration/Kubernetes/Commands/label.md similarity index 100% rename from Containerization & Orchestration/Kubernetes/label.md rename to Containerization & Orchestration/Kubernetes/Commands/label.md diff --git a/Containerization & Orchestration/Kubernetes/log-debug.md b/Containerization & Orchestration/Kubernetes/Commands/log-debug.md similarity index 100% rename from Containerization & Orchestration/Kubernetes/log-debug.md rename to Containerization & Orchestration/Kubernetes/Commands/log-debug.md diff --git a/Containerization & Orchestration/Kubernetes/node-manage.md b/Containerization & Orchestration/Kubernetes/Commands/node-manage.md similarity index 100% rename from Containerization & Orchestration/Kubernetes/node-manage.md rename to Containerization & Orchestration/Kubernetes/Commands/node-manage.md diff --git a/Containerization & Orchestration/Kubernetes/components/1-NameSpace.md b/Containerization & Orchestration/Kubernetes/workloads/1-NameSpace.md similarity index 100% rename from Containerization & Orchestration/Kubernetes/components/1-NameSpace.md rename to Containerization & Orchestration/Kubernetes/workloads/1-NameSpace.md diff --git a/Containerization & Orchestration/Kubernetes/workloads/10-Deployment-With-Services.md b/Containerization & Orchestration/Kubernetes/workloads/10-Deployment-With-Services.md new file mode 100644 index 0000000..91bbf6c --- /dev/null +++ b/Containerization & Orchestration/Kubernetes/workloads/10-Deployment-With-Services.md @@ -0,0 +1,189 @@ +### **1st Document: Namespace** + +```yaml +apiVersion: v1 +``` + +* Specifies the API version used. Here, it's version 1 of the core Kubernetes API. + +```yaml +kind: Namespace +``` + +* Declares the resource type. This is a **Namespace**, which logically isolates groups of resources. + +```yaml +metadata: + name: ns +``` + +* Metadata block. + + * `name: ns` sets the name of the namespace to `ns`. + +--- + +### **2nd Document: Service** + +```yaml +--- +``` + +* Separates multiple documents in the YAML file. + +```yaml +apiVersion: v1 +``` + +* Uses the core v1 API again. + +```yaml +kind: Service +``` + +* Declares a **Service** resource, which provides stable networking to access pods. + +```yaml +metadata: + name: nginx-service + namespace: ns + labels: + app: nginx +``` + +* Metadata block: + + * `name: nginx-service`: name of the Service. + * `namespace: ns`: places this service in the previously created `ns` namespace. + * `labels`: key-value pairs used for organizing and selecting resources. Here, `app: nginx`. + +```yaml +spec: + type: ClusterIP +``` + +* `spec` describes the behavior. + + * `type: ClusterIP`: exposes the service internally within the cluster using a virtual IP. + +```yaml + selector: + app: nginx +``` + +* This selects pods with the label `app: nginx` to receive traffic from this service. + +```yaml + ports: + - name: http + port: 80 + targetPort: 8080 +``` + +* Defines port configuration: + + * `name: http`: a name for the port (optional but useful for readability). + * `port: 80`: the port that the service exposes internally. + * `targetPort: 8080`: the port on the pod that receives the traffic. + +--- + +### **3rd Document: Deployment** + +```yaml +--- +``` + +* Separates from the previous document. + +```yaml +apiVersion: apps/v1 +``` + +* Uses the `apps/v1` API group, suitable for deployments and other controllers. + +```yaml +kind: Deployment +``` + +* Declares a **Deployment**, which ensures a specified number of pod replicas are running. + +```yaml +metadata: + name: nginx-deployment + namespace: ns + labels: + app: nginx +``` + +* Metadata block: + + * `name: nginx-deployment`: name of the deployment. + * `namespace: ns`: places this in the `ns` namespace. + * `labels: app: nginx`: used for matching with selectors. + +```yaml +spec: + replicas: 2 +``` + +* Desired number of pod replicas to run: 2. + +```yaml + selector: + matchLabels: + app: nginx +``` + +* Tells the deployment which pods to manage, by matching labels (`app: nginx`). + +```yaml + template: + metadata: + labels: + app: nginx +``` + +* Template for creating new pods: + + * Each pod created will have `app: nginx` label. + +```yaml + spec: + containers: + - name: nginx + image: nginx:latest +``` + +* Pod spec: + + * One container named `nginx`, using the latest official Nginx image. + +```yaml + ports: + - containerPort: 8080 +``` + +* The container exposes port 8080 (must match the `targetPort` in the Service). + +```yaml + resources: + requests: + cpu: "100m" + memory: "128Mi" + limits: + cpu: "250m" + memory: "256Mi" +``` + +* **Resource management**: + + * `requests`: the minimum guaranteed resources. + + * `cpu: 100m` = 0.1 CPU core. + * `memory: 128Mi` = 128 MiB RAM. + * `limits`: the maximum resources the container can use. + + * `cpu: 250m` = 0.25 CPU core. + * `memory: 256Mi` = 256 MiB RAM. + diff --git a/Containerization & Orchestration/Kubernetes/components/2-Pod.md b/Containerization & Orchestration/Kubernetes/workloads/2-Pod.md similarity index 100% rename from Containerization & Orchestration/Kubernetes/components/2-Pod.md rename to Containerization & Orchestration/Kubernetes/workloads/2-Pod.md diff --git a/Containerization & Orchestration/Kubernetes/components/3-Replicaset.md b/Containerization & Orchestration/Kubernetes/workloads/3-Replicaset.md similarity index 100% rename from Containerization & Orchestration/Kubernetes/components/3-Replicaset.md rename to Containerization & Orchestration/Kubernetes/workloads/3-Replicaset.md diff --git a/Containerization & Orchestration/Kubernetes/components/4-Deployment.md b/Containerization & Orchestration/Kubernetes/workloads/4-Deployment.md similarity index 100% rename from Containerization & Orchestration/Kubernetes/components/4-Deployment.md rename to Containerization & Orchestration/Kubernetes/workloads/4-Deployment.md diff --git a/Containerization & Orchestration/Kubernetes/components/5-hpa.md b/Containerization & Orchestration/Kubernetes/workloads/5-hpa.md similarity index 100% rename from Containerization & Orchestration/Kubernetes/components/5-hpa.md rename to Containerization & Orchestration/Kubernetes/workloads/5-hpa.md diff --git a/Containerization & Orchestration/Kubernetes/components/6-DaemonSet.md b/Containerization & Orchestration/Kubernetes/workloads/6-DaemonSet.md similarity index 100% rename from Containerization & Orchestration/Kubernetes/components/6-DaemonSet.md rename to Containerization & Orchestration/Kubernetes/workloads/6-DaemonSet.md diff --git a/Containerization & Orchestration/Kubernetes/components/7-Jobs.md b/Containerization & Orchestration/Kubernetes/workloads/7-Jobs.md similarity index 100% rename from Containerization & Orchestration/Kubernetes/components/7-Jobs.md rename to Containerization & Orchestration/Kubernetes/workloads/7-Jobs.md diff --git a/Containerization & Orchestration/Kubernetes/components/8-Cronjobs.md b/Containerization & Orchestration/Kubernetes/workloads/8-Cronjobs.md similarity index 100% rename from Containerization & Orchestration/Kubernetes/components/8-Cronjobs.md rename to Containerization & Orchestration/Kubernetes/workloads/8-Cronjobs.md diff --git a/Containerization & Orchestration/Kubernetes/workloads/9-Services.md b/Containerization & Orchestration/Kubernetes/workloads/9-Services.md new file mode 100644 index 0000000..4700b38 --- /dev/null +++ b/Containerization & Orchestration/Kubernetes/workloads/9-Services.md @@ -0,0 +1,73 @@ +# ๐Ÿ”— 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 +```bash +kubectl get ep -n +```` + +### ๐Ÿ“„ Get Services + +```bash +kubectl get svc -n +``` + +--- + +## ๐Ÿงพ Example Service Manifest + +```yaml +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 ` to troubleshoot or verify service-to-pod connectivity. +> ๐ŸŒ Services are resolved by DNS using the format: `..svc.cluster.local`. + diff --git a/Containerization & Orchestration/Kubernetes/workloads/all.md b/Containerization & Orchestration/Kubernetes/workloads/all.md deleted file mode 100755 index 3856572..0000000 --- a/Containerization & Orchestration/Kubernetes/workloads/all.md +++ /dev/null @@ -1,208 +0,0 @@ -# Kubernetes YAML Files - -This document provides explanations and details for various Kubernetes YAML configurations, describing how different Kubernetes objects such as Namespaces, Pods, and other specifications are defined and utilized. The examples cover creating namespaces, deploying pods, setting resource limits, and using node selectors. - -## Namespace Definition - -```yaml -apiVersion: v1 -kind: Namespace -metadata: - name: my-ns -``` - -- **apiVersion**: Specifies the version of the Kubernetes API. -- **kind**: Defines the type of Kubernetes object, here it's a `Namespace`. -- **metadata**: Contains data that helps uniquely identify the object, including a `name`. - -This YAML file creates a namespace named `my-ns` which isolates a group of resources within Kubernetes. - -## Pod Definitions - -### Nginx Pod - -```yaml -apiVersion: v1 -kind: Pod -metadata: - namespace: my-ns - name: nginx-pod - labels: - app: app1 - zone: staging - version: v1.0.1 - app.kubernetes.io/product: nginx-pod -spec: - containers: - - name: naginx-container - image: nginx:latest - ports: - - containerPort: 80 -``` - -- **metadata.namespace**: Specifies the namespace the pod belongs to (`my-ns`). -- **metadata.name**: The name of the pod (`nginx-pod`). -- **metadata.labels**: Key-value pairs for organizing and selecting resources. -- **spec.containers**: Specifies the containers within the pod. Each container has: - - **name**: Container name. - - **image**: The Docker image to run (`nginx:latest`). - - **ports**: List of ports to expose from the container (`containerPort: 80`). - -This file defines a pod named `nginx-pod` running the latest Nginx container in the `my-ns` namespace. - -### Test Pod 1 - -```yaml -apiVersion: v1 -kind: Pod -metadata: - namespace: my-ns - name: testpod1 -spec: - containers: - - name: c00 - image: ubuntu - command: ["/bin/bash", "-c", "while true; do echo Hello-Coder; sleep 5 ; done"] - - name: c01 - image: ubuntu - command: ["/bin/bash", "-c", "while true; do echo Hello-Programmer; sleep 5 ; done"] -``` - -- **spec.containers.command**: Overrides the default command for the container, in this case, running a looped bash script that prints a message every 5 seconds. - -This defines a pod named `testpod1` with two Ubuntu containers in the `my-ns` namespace, each running a different command. - -## Pod with Resource Requests and Limits - -```yaml -apiVersion: v1 -kind: Pod -metadata: - namespace: my-ns - name: testpod1 -spec: - containers: - - name: c00 - image: ubuntu - command: - - /bin/bash - - -c - - while true; do echo Hello-Coder; sleep 5 ; done - - name: c01 - image: ubuntu - command: - - /bin/bash - - -c - - while true; do echo Hello-Programmer; sleep 5 ; done - resources: - limits: - memory: "128Mi" - cpu: "500m" - requests: - memory: "64Mi" - cpu: "250m" -``` - -- **resources.limits**: Specifies the maximum amount of resources a container can use. -- **resources.requests**: Specifies the amount of resources a container is guaranteed. - -This pod configuration defines resource limits and requests for the containers to ensure they do not exceed specific memory and CPU usage. - -## Pod with NodeSelector - -```yaml -apiVersion: v1 -kind: Pod -metadata: - namespace: my-ns - name: testpod3 -spec: - containers: - - name: c00 - image: ubuntu - command: - - /bin/bash - - -c - - while true; do echo Hello-Coder; sleep 5 ; done - - name: c01 - image: ubuntu - command: - - /bin/bash - - -c - - while true; do echo Hello-Programmer; sleep 5 ; done - resources: - limits: - memory: "128Mi" - cpu: "500m" - requests: - memory: "64Mi" - cpu: "250m" - nodeSelector: - kubernetes.io/hostname: k8s2 - kubernetes.io/disk: ssd -``` - -- **nodeSelector**: Ensures the pod is scheduled on nodes with the specified labels (`kubernetes.io/hostname: k8s2` and `kubernetes.io/disk: ssd`). - -This configuration places the pod on specific nodes that match the given labels. - -## Simple Pod Templates - -### Basic Pod - -```yaml -apiVersion: v1 -kind: Pod -metadata: - name: myapp - labels: - name: myapp -spec: - containers: - - name: myapp - image: - resources: - limits: - memory: "128Mi" - cpu: "500m" - ports: - - containerPort: -``` - -This is a template for a basic pod named `myapp` with configurable image and port settings. - -### Nginx Pod - -```yaml -apiVersion: v1 -kind: Pod -metadata: - name: my-pod - labels: - app: MyApp -spec: - containers: - - name: my-container - image: nginx:latest - ports: - - containerPort: 80 -``` - -This defines a pod named `my-pod` running an Nginx container exposing port 80. - -## Useful Kubernetes Commands - -### View Pod Details - -```bash -kubectl get pod -n my-ns -o yaml -``` - -This command retrieves and displays the YAML configuration of the pod `testpod1` in the namespace `my-ns`. - -### Label a Node - -```bash -kubectl label node kubernetes.io/= -kubectl get nodes --show-labels -``` diff --git a/Containerization & Orchestration/Kubernetes/workloads/deployment-with-service.yaml b/Containerization & Orchestration/Kubernetes/workloads/deployment-with-service.yaml deleted file mode 100644 index 912d9a0..0000000 --- a/Containerization & Orchestration/Kubernetes/workloads/deployment-with-service.yaml +++ /dev/null @@ -1,23 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - name: app-deployment - namespace: ns-test - labels: - name: deployment-test - app: nginx -spec: - replicas: 2 - selector: - matchLabels: - name: app1 - app: nginx - template: - metadata: - labels: - name: app1 - app: nginx - spec: - containers: - - name: nginx-deployment - image: nginx:1.26 diff --git a/Containerization & Orchestration/Kubernetes/workloads/cronjob.yaml b/Containerization & Orchestration/Kubernetes/workloads/manifests/cronjob.yaml similarity index 100% rename from Containerization & Orchestration/Kubernetes/workloads/cronjob.yaml rename to Containerization & Orchestration/Kubernetes/workloads/manifests/cronjob.yaml diff --git a/Containerization & Orchestration/Kubernetes/workloads/manifests/deployment-with-service.yaml b/Containerization & Orchestration/Kubernetes/workloads/manifests/deployment-with-service.yaml new file mode 100644 index 0000000..b282f0b --- /dev/null +++ b/Containerization & Orchestration/Kubernetes/workloads/manifests/deployment-with-service.yaml @@ -0,0 +1,52 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: ns + +--- +apiVersion: v1 +kind: Service +metadata: + name: nginx-service + namespace: ns + labels: + app: nginx +spec: + type: ClusterIP + selector: + app: nginx + ports: + - name: http + port: 80 + targetPort: 8080 + +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-deployment + namespace: ns + labels: + app: nginx +spec: + replicas: 2 + selector: + matchLabels: + app: nginx + template: + metadata: + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx:latest + ports: + - containerPort: 8080 + resources: + requests: + cpu: "100m" + memory: "128Mi" + limits: + cpu: "250m" + memory: "256Mi" diff --git a/Containerization & Orchestration/Kubernetes/workloads/deployment.yaml b/Containerization & Orchestration/Kubernetes/workloads/manifests/deployment.yaml similarity index 100% rename from Containerization & Orchestration/Kubernetes/workloads/deployment.yaml rename to Containerization & Orchestration/Kubernetes/workloads/manifests/deployment.yaml diff --git a/Containerization & Orchestration/Kubernetes/workloads/ds.yml b/Containerization & Orchestration/Kubernetes/workloads/manifests/ds.yml similarity index 100% rename from Containerization & Orchestration/Kubernetes/workloads/ds.yml rename to Containerization & Orchestration/Kubernetes/workloads/manifests/ds.yml diff --git a/Containerization & Orchestration/Kubernetes/workloads/job.yaml b/Containerization & Orchestration/Kubernetes/workloads/manifests/job.yaml similarity index 100% rename from Containerization & Orchestration/Kubernetes/workloads/job.yaml rename to Containerization & Orchestration/Kubernetes/workloads/manifests/job.yaml diff --git a/Containerization & Orchestration/Kubernetes/workloads/namespace.yaml b/Containerization & Orchestration/Kubernetes/workloads/manifests/namespace.yaml similarity index 100% rename from Containerization & Orchestration/Kubernetes/workloads/namespace.yaml rename to Containerization & Orchestration/Kubernetes/workloads/manifests/namespace.yaml diff --git a/Containerization & Orchestration/Kubernetes/workloads/pod.yaml b/Containerization & Orchestration/Kubernetes/workloads/manifests/pod.yaml similarity index 100% rename from Containerization & Orchestration/Kubernetes/workloads/pod.yaml rename to Containerization & Orchestration/Kubernetes/workloads/manifests/pod.yaml diff --git a/Containerization & Orchestration/Kubernetes/workloads/replicaset.yml b/Containerization & Orchestration/Kubernetes/workloads/manifests/replicaset.yml similarity index 100% rename from Containerization & Orchestration/Kubernetes/workloads/replicaset.yml rename to Containerization & Orchestration/Kubernetes/workloads/manifests/replicaset.yml diff --git a/Containerization & Orchestration/Kubernetes/workloads/service.yaml b/Containerization & Orchestration/Kubernetes/workloads/manifests/service.yaml similarity index 100% rename from Containerization & Orchestration/Kubernetes/workloads/service.yaml rename to Containerization & Orchestration/Kubernetes/workloads/manifests/service.yaml