From 24df655dc0ad8ce8d290666bdd7e4bb4cfe5d676 Mon Sep 17 00:00:00 2001 From: RadinPirouz Date: Sat, 13 Jul 2024 23:37:18 +0330 Subject: [PATCH 01/16] Move Commands --- kubernetes/commands.md | 17 ++++++++++++++++- kubernetes/workloads/{all.md => pod.md} | 15 --------------- 2 files changed, 16 insertions(+), 16 deletions(-) rename kubernetes/workloads/{all.md => pod.md} (93%) diff --git a/kubernetes/commands.md b/kubernetes/commands.md index 9817309..d998d42 100644 --- a/kubernetes/commands.md +++ b/kubernetes/commands.md @@ -99,4 +99,19 @@ kubectl api-resources ### Apply Yaml File ```bash kubectl apply -f -n -``` \ No newline at end of file +``` +### 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/kubernetes/workloads/all.md b/kubernetes/workloads/pod.md similarity index 93% rename from kubernetes/workloads/all.md rename to kubernetes/workloads/pod.md index 3856572..9b92aff 100644 --- a/kubernetes/workloads/all.md +++ b/kubernetes/workloads/pod.md @@ -190,19 +190,4 @@ spec: 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 -``` From c5fc73e396a01295d11dc777e41b9a13b5d0fbb7 Mon Sep 17 00:00:00 2001 From: RadinPirouz Date: Mon, 15 Jul 2024 23:03:44 +0330 Subject: [PATCH 02/16] replicaset doc --- kubernetes/commands.md | 19 ++++++ kubernetes/workloads/pod.md | 4 -- kubernetes/workloads/replicaset.md | 94 ++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 kubernetes/workloads/replicaset.md diff --git a/kubernetes/commands.md b/kubernetes/commands.md index d998d42..2a8089d 100644 --- a/kubernetes/commands.md +++ b/kubernetes/commands.md @@ -114,4 +114,23 @@ This command retrieves and displays the YAML configuration of the pod `testpod1` kubectl label node kubernetes.io/= kubectl get nodes --show-labels ``` +### Retrieve the ReplicaSet +To retrieve information about the ReplicaSet in the `my-ns` namespace, use the following command: +```bash +kubectl get rs -n my-ns +``` + +### Delete All Pods in the Namespace +To delete all pods in the `my-ns` namespace, use the following command: +```bash +kubectl delete pod --all -n my-ns +``` + +### Change the Replica Count to 5 +To scale the ReplicaSet to 5 replicas, you can use one of the following methods: + +1. **Using the `kubectl scale` Command** +```bash +kubectl scale rs my-app --replicas=5 -n my-ns +``` diff --git a/kubernetes/workloads/pod.md b/kubernetes/workloads/pod.md index 9b92aff..e470bb4 100644 --- a/kubernetes/workloads/pod.md +++ b/kubernetes/workloads/pod.md @@ -1,7 +1,3 @@ -# 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 diff --git a/kubernetes/workloads/replicaset.md b/kubernetes/workloads/replicaset.md new file mode 100644 index 0000000..a51ff7d --- /dev/null +++ b/kubernetes/workloads/replicaset.md @@ -0,0 +1,94 @@ +## Document: Kubernetes ReplicaSet YAML Explanation + +### Overview +This document provides an explanation of a Kubernetes ReplicaSet YAML file and commands to manage the ReplicaSet. The YAML file defines the desired state for a ReplicaSet, which ensures a specified number of pod replicas are running at any given time. + +### YAML File Breakdown + +#### 1. Define the ReplicaSet +The YAML file begins with the `apiVersion`, `kind`, and `metadata` fields, which specify the API version, the type of Kubernetes object, and metadata about the object, respectively. + +```yaml +apiVersion: apps/v1 +kind: ReplicaSet +metadata: + name: my-app + namespace: my-ns + labels: + app.kubernetes.io/name: my-app + app.kubernetes.io/env: development +``` +- `apiVersion: apps/v1`: Specifies that this configuration uses the `apps/v1` API version. +- `kind: ReplicaSet`: Defines the object as a ReplicaSet. +- `metadata`: Provides metadata for the ReplicaSet, including: + - `name`: The name of the ReplicaSet (`my-app`). + - `namespace`: The namespace where the ReplicaSet will be created (`my-ns`). + - `labels`: Key-value pairs to categorize the ReplicaSet (`app.kubernetes.io/name: my-app` and `app.kubernetes.io/env: development`). + +#### 2. Define the Specification +The `spec` section describes the desired state of the ReplicaSet. + +```yaml +spec: + replicas: 3 + selector: + matchLabels: + app.kubernetes.io/name: my-app + template: + metadata: + labels: + app.kubernetes.io/name: my-app + spec: + containers: + - name: nginx + image: nginx +``` +- `replicas: 3`: Specifies that three replicas of the pod should be running. +- `selector`: Defines how to identify the pods managed by this ReplicaSet. + - `matchLabels`: Matches pods with the label `app.kubernetes.io/name: my-app`. +- `template`: Provides the pod template used by the ReplicaSet to create new pods. + - `metadata`: Metadata for the pod template. + - `labels`: Labels applied to the pods (`app.kubernetes.io/name: my-app`). + - `spec`: Describes the pod specification. + - `containers`: Defines the containers within the pod. + - `name`: The name of the container (`nginx`). + - `image`: The container image to use (`nginx`). + + +### Update the Container Image Version + +To update the container image version, edit the YAML manifest and change the `image` field: +```yaml +containers: +- name: nginx + image: nginx:1.25 +``` +Apply the updated manifest to update the pods with the new image version. + +### Complete YAML File with Image Version Update + +Here is the complete YAML file with the container image version updated to `nginx:1.25`: + +```yaml +apiVersion: apps/v1 +kind: ReplicaSet +metadata: + name: my-app + namespace: my-ns + labels: + app.kubernetes.io/name: my-app + app.kubernetes.io/env: development +spec: + replicas: 3 + selector: + matchLabels: + app.kubernetes.io/name: my-app + template: + metadata: + labels: + app.kubernetes.io/name: my-app + spec: + containers: + - name: nginx + image: nginx:1.25 +``` From fd6d4ed9440ba5c61490dc81b9d838e37f9f90bc Mon Sep 17 00:00:00 2001 From: RadinPirouz Date: Mon, 15 Jul 2024 23:08:26 +0330 Subject: [PATCH 03/16] deployment --- kubernetes/commands.md | 58 +++++++++++ kubernetes/workloads/deployment.md | 157 +++++++++++++++++++++++++++++ 2 files changed, 215 insertions(+) create mode 100644 kubernetes/workloads/deployment.md diff --git a/kubernetes/commands.md b/kubernetes/commands.md index 2a8089d..beef64a 100644 --- a/kubernetes/commands.md +++ b/kubernetes/commands.md @@ -134,3 +134,61 @@ To scale the ReplicaSet to 5 replicas, you can use one of the following methods: kubectl scale rs my-app --replicas=5 -n my-ns ``` +## Commands to Manage the Deployment + +### Scale the Deployment +To scale the Deployment to 6 replicas: +```bash +kubectl -n my-ns scale deployment myapp --replicas 6 +``` + +### Retrieve Deployment, ReplicaSets, and Pods +To retrieve information about the Deployment, ReplicaSets, and Pods: +```bash +kubectl get deployment,rs,po myapp -n my-ns +``` + +### Delete the Deployment +To delete the Deployment: +```bash +kubectl delete deployment myapp -n my-ns +``` + +### Retrieve ReplicaSets +To retrieve ReplicaSets: +```bash +kubectl get rs -n my-ns +``` + +### Rollback a Deployment +To undo the last rollout: +```bash +kubectl rollout undo deployment -n my-ns myapp +``` + +To view rollout history: +```bash +kubectl rollout history deployment -n my-ns +``` + +To view a specific revision: +```bash +kubectl rollout history deployment -n my-ns --revision 2 +``` + +To rollback to a specific revision: +```bash +kubectl rollout undo deployment -n my-ns myapp --to-revision 2 +``` + +### Annotate Deployment with Change Cause +To add a change cause annotation: +```bash +kubectl annotate deployment/myapp -n my-ns myapp "kubectl.kubernetes.io/change-cause=v14 released" +``` + +### Horizontal Pod Autoscaler +To create an autoscaler for the Deployment: +```bash +kubectl -n my-ns autoscale deployment nginx --cpu-percent=50 --min=4 --max=10 +``` diff --git a/kubernetes/workloads/deployment.md b/kubernetes/workloads/deployment.md new file mode 100644 index 0000000..d239278 --- /dev/null +++ b/kubernetes/workloads/deployment.md @@ -0,0 +1,157 @@ +#### 1. Deployment with Resource Limits and Horizontal Pod Autoscaler + +```yaml +apiVersion: apps/v1 +kind: Deployment +metadata: + name: myapp + namespace: my-ns + labels: + app.kubernetes.io/name: myapp + app.kubernetes.io/env: development +spec: + replicas: 5 + selector: + matchLabels: + app.kubernetes.io/name: myapp + app.kubernetes.io/env: development + template: + metadata: + labels: + app.kubernetes.io/name: myapp + app.kubernetes.io/env: development + spec: + containers: + - name: nginx + image: nginx # change image and apply again + resources: + limits: + memory: "128Mi" + cpu: "500m" + requests: + memory: "64Mi" + cpu: "250m" + ports: + - containerPort: 80 +``` +- `apiVersion: apps/v1`: Specifies the API version. +- `kind: Deployment`: Defines the object as a Deployment. +- `metadata`: Provides metadata for the Deployment. + - `name`: The name of the Deployment (`myapp`). + - `namespace`: The namespace where the Deployment will be created (`my-ns`). + - `labels`: Key-value pairs to categorize the Deployment. +- `spec`: Describes the desired state. + - `replicas`: Number of pod replicas (5). + - `selector`: Identifies the pods managed by this Deployment. + - `template`: The pod template used by the Deployment. + - `metadata`: Metadata for the pod template. + - `spec`: Describes the pod specification. + - `containers`: Defines the containers within the pod. + - `name`: The name of the container (`nginx`). + - `image`: The container image (`nginx`). + - `resources`: Resource limits and requests. + - `limits`: Maximum resources (`128Mi` memory, `500m` CPU). + - `requests`: Minimum resources (`64Mi` memory, `250m` CPU). + - `ports`: Container ports (80). + + +### 2. Deployment with Rolling Update Strategy + +```yaml +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + name: my-name + labels: + name: my-name +spec: + strategy: + rollingUpdate: + maxSurge: 1 + maxUnavailable: 1 + type: RollingUpdate + template: + metadata: + labels: + name: my-name + spec: + containers: + - image: ipedrazas/docmock + name: my-name + resources: + requests: + cpu: "20m" + memory: "55M" + livenessProbe: + httpGet: + path: /_status/healthz + port: 5000 + initialDelaySeconds: 90 + timeoutSeconds: 10 + readinessProbe: + httpGet: + path: /_status/healthz + port: 5000 + initialDelaySeconds: 30 + timeoutSeconds: 10 + env: + - name: ENVVARNAME + value: ENVVARVALUE + ports: + - containerPort: 5000 + name: my-name + volumeMounts: + - mountPath: /data + name: data + volumes: + - name: data + emptyDir: {} + restartPolicy: Always + imagePullPolicy: Always +``` +- `apiVersion: extensions/v1beta1`: Specifies the API version. +- `kind: Deployment`: Defines the object as a Deployment. +- `metadata`: Provides metadata for the Deployment. + - `name`: The name of the Deployment (`my-name`). + - `labels`: Key-value pairs to categorize the Deployment. +- `spec`: Describes the desired state. + - `strategy`: Rolling update strategy. + - `rollingUpdate`: Defines the update strategy. + - `maxSurge`: Maximum number of additional pods (1). + - `maxUnavailable`: Maximum number of unavailable pods (1). + - `type`: The type of update strategy (`RollingUpdate`). + - `template`: The pod template used by the Deployment. + - `metadata`: Metadata for the pod template. + - `spec`: Describes the pod specification. + - `containers`: Defines the containers within the pod. + - `name`: The name of the container (`my-name`). + - `image`: The container image (`ipedrazas/docmock`). + - `resources`: Resource requests. + - `requests`: Minimum resources (`20m` CPU, `55M` memory). + - `livenessProbe`: Health check for the container. + - `httpGet`: HTTP GET request for the probe. + - `path`: The path to check (`/_status/healthz`). + - `port`: The port to check (5000). + - `initialDelaySeconds`: Initial delay before the probe starts (90 seconds). + - `timeoutSeconds`: Timeout for the probe (10 seconds). + - `readinessProbe`: Readiness check for the container. + - `httpGet`: HTTP GET request for the probe. + - `path`: The path to check (`/_status/healthz`). + - `port`: The port to check (5000). + - `initialDelaySeconds`: Initial delay before the probe starts (30 seconds). + - `timeoutSeconds`: Timeout for the probe (10 seconds). + - `env`: Environment variables for the container. + - `name`: The name of the environment variable (`ENVVARNAME`). + - `value`: The value of the environment variable (`ENVVARVALUE`). + - `ports`: Container ports. + - `containerPort`: The container port (5000). + - `name`: The name of the port (`my-name`). + - `volumeMounts`: Mounting volumes to the container. + - `mountPath`: The path to mount the volume (`/data`). + - `name`: The name of the volume (`data`). + - `volumes`: Defines the volumes. + - `name`: The name of the volume (`data`). + - `emptyDir`: An empty directory volume. + - `restartPolicy`: Restart policy for the container (`Always`). + - `imagePullPolicy`: Image pull policy (`Always`). + From 3ec6a06e164714dd3a948ff1f64e9d60d173ea1f Mon Sep 17 00:00:00 2001 From: RadinPirouz Date: Tue, 16 Jul 2024 23:00:08 +0330 Subject: [PATCH 04/16] new docs --- kubernetes/workloads/daemonset.md | 93 +++++++++++++++ kubernetes/workloads/job.md | 74 ++++++++++++ kubernetes/workloads/stateful.md | 185 ++++++++++++++++++++++++++++++ 3 files changed, 352 insertions(+) create mode 100644 kubernetes/workloads/daemonset.md create mode 100644 kubernetes/workloads/job.md create mode 100644 kubernetes/workloads/stateful.md diff --git a/kubernetes/workloads/daemonset.md b/kubernetes/workloads/daemonset.md new file mode 100644 index 0000000..61266f3 --- /dev/null +++ b/kubernetes/workloads/daemonset.md @@ -0,0 +1,93 @@ +### YAML File Breakdown + +#### 1. Node Exporter DaemonSet + +This DaemonSet is configured to run a Node Exporter container on each node in the `my-ns` namespace. + +```yaml +apiVersion: apps/v1 +kind: DaemonSet +metadata: + name: node-exporter + namespace: my-ns + labels: + app: example +spec: + selector: + matchLabels: + app.kubernetes.io/name: node-exporter + app.kubernetes.io/env: development + app.kubernetes.io/part-of: monitoring + template: + metadata: + labels: + app.kubernetes.io/name: node-exporter + app.kubernetes.io/env: development + app.kubernetes.io/part-of: monitoring + spec: + containers: + - name: node-exporter + image: prom/node-exporter +``` +- `apiVersion: apps/v1`: Specifies the API version. +- `kind: DaemonSet`: Defines the object as a DaemonSet. +- `metadata`: Provides metadata for the DaemonSet. + - `name`: The name of the DaemonSet (`node-exporter`). + - `namespace`: The namespace where the DaemonSet will be created (`my-ns`). + - `labels`: Key-value pairs to categorize the DaemonSet (`app: example`). +- `spec`: Describes the desired state. + - `selector`: Identifies the pods managed by this DaemonSet. + - `matchLabels`: Matches pods with specified labels. + - `app.kubernetes.io/name`: `node-exporter` + - `app.kubernetes.io/env`: `development` + - `app.kubernetes.io/part-of`: `monitoring` + - `template`: The pod template used by the DaemonSet. + - `metadata`: Metadata for the pod template. + - `labels`: Labels applied to the pods. + - `app.kubernetes.io/name`: `node-exporter` + - `app.kubernetes.io/env`: `development` + - `app.kubernetes.io/part-of`: `monitoring` + - `spec`: Describes the pod specification. + - `containers`: Defines the containers within the pod. + - `name`: The name of the container (`node-exporter`). + - `image`: The container image (`prom/node-exporter`). + +#### 2. Example DaemonSet + +This DaemonSet is configured to run an example container on each node. + +```yaml +apiVersion: apps/v1 +kind: DaemonSet +metadata: + name: example-daemonset + labels: + app: example +spec: + selector: + matchLabels: + app: example + template: + metadata: + labels: + app: example + spec: + containers: + - name: example-container + image: nginx +``` +- `apiVersion: apps/v1`: Specifies the API version. +- `kind: DaemonSet`: Defines the object as a DaemonSet. +- `metadata`: Provides metadata for the DaemonSet. + - `name`: The name of the DaemonSet (`example-daemonset`). + - `labels`: Key-value pairs to categorize the DaemonSet (`app: example`). +- `spec`: Describes the desired state. + - `selector`: Identifies the pods managed by this DaemonSet. + - `matchLabels`: Matches pods with the label `app: example`. + - `template`: The pod template used by the DaemonSet. + - `metadata`: Metadata for the pod template. + - `labels`: Labels applied to the pods (`app: example`). + - `spec`: Describes the pod specification. + - `containers`: Defines the containers within the pod. + - `name`: The name of the container (`example-container`). + - `image`: The container image (`nginx`). diff --git a/kubernetes/workloads/job.md b/kubernetes/workloads/job.md new file mode 100644 index 0000000..36e27fd --- /dev/null +++ b/kubernetes/workloads/job.md @@ -0,0 +1,74 @@ + +#### 1. Simple Job + +This Job is configured to run a single container that prints "hello world" to the console. + +```yaml +apiVersion: batch/v1 +kind: Job +metadata: + name: myjob + namespace: my-ns +spec: + template: + spec: + containers: + - name: myjob + image: alpine + command: + - echo + - "hello world" + restartPolicy: Never +``` +- `apiVersion: batch/v1`: Specifies the API version. +- `kind: Job`: Defines the object as a Job. +- `metadata`: Provides metadata for the Job. + - `name`: The name of the Job (`myjob`). + - `namespace`: The namespace where the Job will be created (`my-ns`). +- `spec`: Describes the desired state. + - `template`: The pod template used by the Job. + - `spec`: Describes the pod specification. + - `containers`: Defines the containers within the pod. + - `name`: The name of the container (`myjob`). + - `image`: The container image (`alpine`). + - `command`: The command to run in the container (`echo "hello world"`). + - `restartPolicy`: Specifies the restart policy for the pod (`Never`). + +#### 2. Job with Error and Retries + +This Job attempts to list a non-existent directory (`/chert`) and will try to complete the task up to 6 times due to the error. + +```yaml +apiVersion: batch/v1 +kind: Job +metadata: + name: myjob + namespace: my-ns +spec: + backoffLimit: 6 + template: + spec: + containers: + - name: myjob + image: alpine + command: + - ls + - "/chert" + restartPolicy: Never +``` +- `apiVersion: batch/v1`: Specifies the API version. +- `kind: Job`: Defines the object as a Job. +- `metadata`: Provides metadata for the Job. + - `name`: The name of the Job (`myjob`). + - `namespace`: The namespace where the Job will be created (`my-ns`). +- `spec`: Describes the desired state. + - `backoffLimit`: The number of retries before the Job is considered failed (6). + - `template`: The pod template used by the Job. + - `spec`: Describes the pod specification. + - `containers`: Defines the containers within the pod. + - `name`: The name of the container (`myjob`). + - `image`: The container image (`alpine`). + - `command`: The command to run in the container (`ls /chert`). + - `restartPolicy`: Specifies the restart policy for the pod (`Never`). + +This guide provides a detailed explanation of Kubernetes Job YAML files. Jobs are designed to run a task to completion, and they can retry in case of failures. Each Job configuration includes specifications for containers, commands, and restart policies, with the option to set a retry limit for handling errors. \ No newline at end of file diff --git a/kubernetes/workloads/stateful.md b/kubernetes/workloads/stateful.md new file mode 100644 index 0000000..5cbad06 --- /dev/null +++ b/kubernetes/workloads/stateful.md @@ -0,0 +1,185 @@ + +#### 1. Basic Redis StatefulSet + +This StatefulSet is configured to run Redis instances in the `my-ns` namespace with 3 replicas. + +```yaml +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: redis + namespace: my-ns + labels: + app.kubernetes.io/name: redis +spec: + replicas: 3 + selector: + matchLabels: + app.kubernetes.io/name: redis + template: + metadata: + labels: + app.kubernetes.io/name: redis + spec: + containers: + - name: redis + image: redis +``` +- `apiVersion: apps/v1`: Specifies the API version. +- `kind: StatefulSet`: Defines the object as a StatefulSet. +- `metadata`: Provides metadata for the StatefulSet. + - `name`: The name of the StatefulSet (`redis`). + - `namespace`: The namespace where the StatefulSet will be created (`my-ns`). + - `labels`: Key-value pairs to categorize the StatefulSet. +- `spec`: Describes the desired state. + - `replicas`: Number of pod replicas (3). + - `selector`: Identifies the pods managed by this StatefulSet. + - `matchLabels`: Matches pods with the label `app.kubernetes.io/name: redis`. + - `template`: The pod template used by the StatefulSet. + - `metadata`: Metadata for the pod template. + - `labels`: Labels applied to the pods (`app.kubernetes.io/name: redis`). + - `spec`: Describes the pod specification. + - `containers`: Defines the containers within the pod. + - `name`: The name of the container (`redis`). + - `image`: The container image (`redis`). + +#### 2. Redis StatefulSet with Volume + +This StatefulSet is similar to the first one but includes persistent volume claims (PVCs) to ensure data persistence. + +```yaml +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: redis + namespace: my-ns + labels: + app.kubernetes.io/name: redis +spec: + replicas: 3 + selector: + matchLabels: + app.kubernetes.io/name: redis + template: + metadata: + labels: + app.kubernetes.io/name: redis + spec: + containers: + - name: redis + image: redis + volumeMounts: + - name: redis-data + mountPath: /var/lib/redis + volumeClaimTemplates: + - metadata: + name: redis-data + spec: + accessModes: + - "ReadWriteOnce" + resources: + requests: + storage: 1Gi +``` +- `apiVersion: apps/v1`: Specifies the API version. +- `kind: StatefulSet`: Defines the object as a StatefulSet. +- `metadata`: Provides metadata for the StatefulSet. + - `name`: The name of the StatefulSet (`redis`). + - `namespace`: The namespace where the StatefulSet will be created (`my-ns`). + - `labels`: Key-value pairs to categorize the StatefulSet. +- `spec`: Describes the desired state. + - `replicas`: Number of pod replicas (3). + - `selector`: Identifies the pods managed by this StatefulSet. + - `matchLabels`: Matches pods with the label `app.kubernetes.io/name: redis`. + - `template`: The pod template used by the StatefulSet. + - `metadata`: Metadata for the pod template. + - `labels`: Labels applied to the pods (`app.kubernetes.io/name: redis`). + - `spec`: Describes the pod specification. + - `containers`: Defines the containers within the pod. + - `name`: The name of the container (`redis`). + - `image`: The container image (`redis`). + - `volumeMounts`: Mounts the specified volume to `/var/lib/redis`. + - `name`: The name of the volume (`redis-data`). + - `mountPath`: The path to mount the volume (`/var/lib/redis`). + - `volumeClaimTemplates`: Defines the PVCs for the StatefulSet. + - `metadata`: Metadata for the PVC. + - `name`: The name of the PVC (`redis-data`). + - `spec`: Describes the PVC specification. + - `accessModes`: Access mode for the PVC (`ReadWriteOnce`). + - `resources`: Resource requests for the PVC. + - `requests`: Storage request (1Gi). + +#### 3. Web StatefulSet with Volume + +This StatefulSet runs NGINX instances with persistent storage. + +```yaml +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: web +spec: + selector: + matchLabels: + app: nginx # has to match .spec.template.metadata.labels + serviceName: "nginx" + replicas: 3 # by default is 1 + minReadySeconds: 10 # by default is 0 + template: + metadata: + labels: + app: nginx # has to match .spec.selector.matchLabels + spec: + terminationGracePeriodSeconds: 10 + containers: + - name: nginx + image: registry.k8s.io/nginx-slim:0.8 + ports: + - containerPort: 80 + name: web + volumeMounts: + - name: www + mountPath: /usr/share/nginx/html + volumeClaimTemplates: + - metadata: + name: www + spec: + accessModes: [ "ReadWriteOnce" ] + storageClassName: "my-storage-class" + resources: + requests: + storage: 1Gi +``` +- `apiVersion: apps/v1`: Specifies the API version. +- `kind: StatefulSet`: Defines the object as a StatefulSet. +- `metadata`: Provides metadata for the StatefulSet. + - `name`: The name of the StatefulSet (`web`). +- `spec`: Describes the desired state. + - `selector`: Identifies the pods managed by this StatefulSet. + - `matchLabels`: Matches pods with the label `app: nginx`. + - `serviceName`: The name of the service that governs this StatefulSet (`nginx`). + - `replicas`: Number of pod replicas (3). + - `minReadySeconds`: Minimum time for pods to be ready (10 seconds). + - `template`: The pod template used by the StatefulSet. + - `metadata`: Metadata for the pod template. + - `labels`: Labels applied to the pods (`app: nginx`). + - `spec`: Describes the pod specification. + - `terminationGracePeriodSeconds`: Time for the pod to terminate gracefully (10 seconds). + - `containers`: Defines the containers within the pod. + - `name`: The name of the container (`nginx`). + - `image`: The container image (`registry.k8s.io/nginx-slim:0.8`). + - `ports`: Container ports. + - `containerPort`: The container port (80). + - `name`: The name of the port (`web`). + - `volumeMounts`: Mounts the specified volume to `/usr/share/nginx/html`. + - `name`: The name of the volume (`www`). + - `mountPath`: The path to mount the volume (`/usr/share/nginx/html`). + - `volumeClaimTemplates`: Defines the PVCs for the StatefulSet. + - `metadata`: Metadata for the PVC. + - `name`: The name of the PVC (`www`). + - `spec`: Describes the PVC specification. + - `accessModes`: Access mode for the PVC (`ReadWriteOnce`). + - `storageClassName`: The storage class name (`my-storage-class`). + - `resources`: Resource requests for the PVC. + - `requests`: Storage request (1Gi). + From bc1ba4cb46da0b990fedcb555b3d3ba27e5e754b Mon Sep 17 00:00:00 2001 From: radinpirouz Date: Thu, 31 Oct 2024 09:08:11 +0330 Subject: [PATCH 05/16] Remove Old Kubernetes Commands File --- kubernetes/commands.md | 194 ----------------------------------------- 1 file changed, 194 deletions(-) delete mode 100644 kubernetes/commands.md diff --git a/kubernetes/commands.md b/kubernetes/commands.md deleted file mode 100644 index beef64a..0000000 --- a/kubernetes/commands.md +++ /dev/null @@ -1,194 +0,0 @@ -# Kubernetes - -## `kubectl` Command Reference - -### Get State of API Resources -```bash -kubectl api-resources -``` - -### Node Management -- **Show all nodes:** - ```bash - kubectl get node - ``` - -### Namespace Management -- **List all namespaces:** - ```bash - kubectl get namespaces - ``` - ```bash - kubectl get ns - ``` -- **Create a custom namespace:** - ```bash - kubectl create ns - ``` - -### Pod Management -- **Get the list of pods in the default namespace:** - ```bash - kubectl get pod - ``` -- **Get the list of pods in the default namespace with full information:** - ```bash - kubectl get pod -o wide - ``` -- **Get the list of pods in a custom namespace with full information:** - ```bash - kubectl get pod -o wide -n - ``` - -### Running a Pod -- **Run a new pod:** - ```bash - kubectl run { - --image=, # Specifies the container image to use - --port=, # Specifies the port that the container exposes - -n , # Specifies the namespace - --env="KEY=VALUE", # Sets environment variables in the container - --command, # Treats the rest of the arguments as the command to run in the container - --replicas=, # Specifies the number of replicas for the deployment - --labels="key=value,key2=value2", # Adds labels to the pod(s) - --dry-run=client, # Prints the object that would be sent, without creating it - --restart=, # Determines the restart policy for the pod - --overrides='', # Provides a JSON override for the generated object - --image-pull-policy=, # Specifies the image pull policy (Always, IfNotPresent, Never) - --limits=cpu=,memory=, # Specifies resource limits for the container - --requests=cpu=,memory= # Specifies resource requests for the container - } - ``` - - *Example:* - ```bash - kubectl run mypod --image=nginx --port=80 -n mynamespace \ - --env="ENV_VAR_NAME=VALUE" --command -- nginx -g "daemon off;" \ - --replicas=3 --labels="app=myapp,env=prod" --dry-run=client \ - --restart=Always --overrides='{"spec": {"containers": [{"name": "nginx", "image": "nginx"}]}}' \ - --image-pull-policy=IfNotPresent --limits=cpu=100m,memory=256Mi \ - --requests=cpu=50m,memory=128Mi - ``` - - -### Deleting a Pod -- **Delete a pod in a custom namespace:** - ```bash - kubectl delete pod -n - ``` - -### API Resource Documentation -- **Get documentation of an API resource:** - ```bash - kubectl explain - ``` - - *Example:* - ```bash - kubectl explain pod - ``` - -### Logging and Pod Information -- **Get and follow logs of a pod (pod must be created and running):** - ```bash - kubectl logs -f -n - ``` -- **Get logs and state information of a pod (works at any time):** - ```bash - kubectl describe pod -n - ``` - -### Apply Yaml File -```bash -kubectl apply -f -n -``` -### 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 -``` -### Retrieve the ReplicaSet -To retrieve information about the ReplicaSet in the `my-ns` namespace, use the following command: -```bash -kubectl get rs -n my-ns -``` - -### Delete All Pods in the Namespace -To delete all pods in the `my-ns` namespace, use the following command: -```bash -kubectl delete pod --all -n my-ns -``` - -### Change the Replica Count to 5 -To scale the ReplicaSet to 5 replicas, you can use one of the following methods: - -1. **Using the `kubectl scale` Command** -```bash -kubectl scale rs my-app --replicas=5 -n my-ns -``` - -## Commands to Manage the Deployment - -### Scale the Deployment -To scale the Deployment to 6 replicas: -```bash -kubectl -n my-ns scale deployment myapp --replicas 6 -``` - -### Retrieve Deployment, ReplicaSets, and Pods -To retrieve information about the Deployment, ReplicaSets, and Pods: -```bash -kubectl get deployment,rs,po myapp -n my-ns -``` - -### Delete the Deployment -To delete the Deployment: -```bash -kubectl delete deployment myapp -n my-ns -``` - -### Retrieve ReplicaSets -To retrieve ReplicaSets: -```bash -kubectl get rs -n my-ns -``` - -### Rollback a Deployment -To undo the last rollout: -```bash -kubectl rollout undo deployment -n my-ns myapp -``` - -To view rollout history: -```bash -kubectl rollout history deployment -n my-ns -``` - -To view a specific revision: -```bash -kubectl rollout history deployment -n my-ns --revision 2 -``` - -To rollback to a specific revision: -```bash -kubectl rollout undo deployment -n my-ns myapp --to-revision 2 -``` - -### Annotate Deployment with Change Cause -To add a change cause annotation: -```bash -kubectl annotate deployment/myapp -n my-ns myapp "kubectl.kubernetes.io/change-cause=v14 released" -``` - -### Horizontal Pod Autoscaler -To create an autoscaler for the Deployment: -```bash -kubectl -n my-ns autoscale deployment nginx --cpu-percent=50 --min=4 --max=10 -``` From ce88b657fe42c359c3c18f7b423a6676343604c7 Mon Sep 17 00:00:00 2001 From: radinpirouz Date: Thu, 31 Oct 2024 10:29:22 +0330 Subject: [PATCH 06/16] Monitoring: Add Stress-ng Doc --- Monitoring & Logging/Tools/stress-ng.md | 164 ++++++++++++++++++++ kubernetes/workloads/daemonset.md | 93 ------------ kubernetes/workloads/deployment.md | 157 -------------------- kubernetes/workloads/job.md | 74 ---------- kubernetes/workloads/pod.md | 189 ------------------------ kubernetes/workloads/replicaset.md | 94 ------------ kubernetes/workloads/stateful.md | 185 ----------------------- 7 files changed, 164 insertions(+), 792 deletions(-) create mode 100644 Monitoring & Logging/Tools/stress-ng.md delete mode 100644 kubernetes/workloads/daemonset.md delete mode 100644 kubernetes/workloads/deployment.md delete mode 100644 kubernetes/workloads/job.md delete mode 100644 kubernetes/workloads/pod.md delete mode 100644 kubernetes/workloads/replicaset.md delete mode 100644 kubernetes/workloads/stateful.md diff --git a/Monitoring & Logging/Tools/stress-ng.md b/Monitoring & Logging/Tools/stress-ng.md new file mode 100644 index 0000000..51e289b --- /dev/null +++ b/Monitoring & Logging/Tools/stress-ng.md @@ -0,0 +1,164 @@ +# Stress-ng: Hardware Stress Testing Tool + +`stress-ng` is a powerful tool for performing various stress tests on your hardware components, including CPU, memory, and I/O. This utility helps in assessing hardware stability under heavy loads, making it useful for benchmarking or diagnosing hardware issues. + +## Table of Contents +- [Installation](#installation) +- [CPU Stress Testing](#cpu-stress-testing) +- [Memory Stress Testing](#memory-stress-testing) +- [I/O Stress Testing](#io-stress-testing) +- [Full System Stress Testing](#full-system-stress-testing) + +### Installation + +To install `stress-ng` on Ubuntu or other Debian-based systems, run: +```bash +sudo apt install stress-ng +``` + +--- + +## CPU Stress Testing + +Use `stress-ng` to test CPU performance under different configurations: + +### 1. Run a CPU Test with a Specified Number of Threads + +You can specify the number of threads to use during a CPU stress test. Using `0` as the thread number utilizes all available CPU cores, maximizing CPU usage. + +```bash +stress-ng --cpu +``` + +**Example:** +```bash +stress-ng --cpu 4 +``` +This command uses 4 CPU threads to run the stress test. + +### 2. Run a CPU Test for a Specified Duration + +Specify both the number of CPU threads and a time limit for the test. + +```bash +stress-ng --cpu --timeout +``` + +**Example:** +```bash +stress-ng --cpu 2 --timeout 60s +``` +This command uses 2 CPU threads and runs the test for 60 seconds. + +### 3. Run a CPU Load Test at a Specific Percentage + +You can control the CPU load by specifying a percentage, which is helpful for testing different levels of CPU usage. + +```bash +stress-ng --cpu-load +``` + +**Example:** +```bash +stress-ng --cpu-load 50 +``` +This command keeps the CPU load at approximately 50%. + +--- + +## Memory Stress Testing + +Stress test the system's memory by allocating and releasing blocks of memory. This can help evaluate memory stability and performance. + +### 1. Basic Memory Stress Test + +Run a memory test with a specified number of workers (processes) that continuously allocate and deallocate memory. + +```bash +stress-ng --vm +``` + +**Example:** +```bash +stress-ng --vm 2 +``` +This command uses 2 workers to perform memory stress testing. + +### 2. Run a Timed Memory Stress Test + +Add a timeout option to run a memory test for a specific duration. + +```bash +stress-ng --vm --timeout +``` + +**Example:** +```bash +stress-ng --vm 2 --timeout 60s +``` +This command uses 2 memory workers and runs the test for 60 seconds. + +### 3. Allocate a Specific Amount of Memory + +Specify the amount of memory to allocate per worker. + +```bash +stress-ng --vm --vm-bytes +``` + +**Example:** +```bash +stress-ng --vm 1 --vm-bytes 512M +``` +This command allocates 512 MB of memory for one worker. + +--- + +## I/O Stress Testing + +I/O testing evaluates the performance of your system’s storage by reading and writing files repeatedly. This is useful for identifying storage bottlenecks and stress-testing the I/O subsystem. + +### 1. Basic I/O Stress Test + +Run an I/O test with a specified number of workers performing file operations. + +```bash +stress-ng --io +``` + +**Example:** +```bash +stress-ng --io 4 +``` +This command runs 4 I/O workers, continuously reading and writing data to test disk performance. + +### 2. Timed I/O Stress Test + +Specify a timeout to limit the duration of the I/O stress test. + +```bash +stress-ng --io --timeout +``` + +**Example:** +```bash +stress-ng --io 4 --timeout 60s +``` +This command runs the I/O stress test for 60 seconds with 4 workers. + +--- + +## Full System Stress Testing + +For a comprehensive stress test, `stress-ng` allows you to stress multiple components at once, such as CPU, memory, and I/O. This puts a combined load on the system to simulate heavy usage conditions. + +```bash +stress-ng --all --timeout +``` + +**Example:** +```bash +stress-ng --all --timeout 10s +``` +This command runs a 10-second full system stress test, targeting all components that `stress-ng` supports. + diff --git a/kubernetes/workloads/daemonset.md b/kubernetes/workloads/daemonset.md deleted file mode 100644 index 61266f3..0000000 --- a/kubernetes/workloads/daemonset.md +++ /dev/null @@ -1,93 +0,0 @@ -### YAML File Breakdown - -#### 1. Node Exporter DaemonSet - -This DaemonSet is configured to run a Node Exporter container on each node in the `my-ns` namespace. - -```yaml -apiVersion: apps/v1 -kind: DaemonSet -metadata: - name: node-exporter - namespace: my-ns - labels: - app: example -spec: - selector: - matchLabels: - app.kubernetes.io/name: node-exporter - app.kubernetes.io/env: development - app.kubernetes.io/part-of: monitoring - template: - metadata: - labels: - app.kubernetes.io/name: node-exporter - app.kubernetes.io/env: development - app.kubernetes.io/part-of: monitoring - spec: - containers: - - name: node-exporter - image: prom/node-exporter -``` -- `apiVersion: apps/v1`: Specifies the API version. -- `kind: DaemonSet`: Defines the object as a DaemonSet. -- `metadata`: Provides metadata for the DaemonSet. - - `name`: The name of the DaemonSet (`node-exporter`). - - `namespace`: The namespace where the DaemonSet will be created (`my-ns`). - - `labels`: Key-value pairs to categorize the DaemonSet (`app: example`). -- `spec`: Describes the desired state. - - `selector`: Identifies the pods managed by this DaemonSet. - - `matchLabels`: Matches pods with specified labels. - - `app.kubernetes.io/name`: `node-exporter` - - `app.kubernetes.io/env`: `development` - - `app.kubernetes.io/part-of`: `monitoring` - - `template`: The pod template used by the DaemonSet. - - `metadata`: Metadata for the pod template. - - `labels`: Labels applied to the pods. - - `app.kubernetes.io/name`: `node-exporter` - - `app.kubernetes.io/env`: `development` - - `app.kubernetes.io/part-of`: `monitoring` - - `spec`: Describes the pod specification. - - `containers`: Defines the containers within the pod. - - `name`: The name of the container (`node-exporter`). - - `image`: The container image (`prom/node-exporter`). - -#### 2. Example DaemonSet - -This DaemonSet is configured to run an example container on each node. - -```yaml -apiVersion: apps/v1 -kind: DaemonSet -metadata: - name: example-daemonset - labels: - app: example -spec: - selector: - matchLabels: - app: example - template: - metadata: - labels: - app: example - spec: - containers: - - name: example-container - image: nginx -``` -- `apiVersion: apps/v1`: Specifies the API version. -- `kind: DaemonSet`: Defines the object as a DaemonSet. -- `metadata`: Provides metadata for the DaemonSet. - - `name`: The name of the DaemonSet (`example-daemonset`). - - `labels`: Key-value pairs to categorize the DaemonSet (`app: example`). -- `spec`: Describes the desired state. - - `selector`: Identifies the pods managed by this DaemonSet. - - `matchLabels`: Matches pods with the label `app: example`. - - `template`: The pod template used by the DaemonSet. - - `metadata`: Metadata for the pod template. - - `labels`: Labels applied to the pods (`app: example`). - - `spec`: Describes the pod specification. - - `containers`: Defines the containers within the pod. - - `name`: The name of the container (`example-container`). - - `image`: The container image (`nginx`). diff --git a/kubernetes/workloads/deployment.md b/kubernetes/workloads/deployment.md deleted file mode 100644 index d239278..0000000 --- a/kubernetes/workloads/deployment.md +++ /dev/null @@ -1,157 +0,0 @@ -#### 1. Deployment with Resource Limits and Horizontal Pod Autoscaler - -```yaml -apiVersion: apps/v1 -kind: Deployment -metadata: - name: myapp - namespace: my-ns - labels: - app.kubernetes.io/name: myapp - app.kubernetes.io/env: development -spec: - replicas: 5 - selector: - matchLabels: - app.kubernetes.io/name: myapp - app.kubernetes.io/env: development - template: - metadata: - labels: - app.kubernetes.io/name: myapp - app.kubernetes.io/env: development - spec: - containers: - - name: nginx - image: nginx # change image and apply again - resources: - limits: - memory: "128Mi" - cpu: "500m" - requests: - memory: "64Mi" - cpu: "250m" - ports: - - containerPort: 80 -``` -- `apiVersion: apps/v1`: Specifies the API version. -- `kind: Deployment`: Defines the object as a Deployment. -- `metadata`: Provides metadata for the Deployment. - - `name`: The name of the Deployment (`myapp`). - - `namespace`: The namespace where the Deployment will be created (`my-ns`). - - `labels`: Key-value pairs to categorize the Deployment. -- `spec`: Describes the desired state. - - `replicas`: Number of pod replicas (5). - - `selector`: Identifies the pods managed by this Deployment. - - `template`: The pod template used by the Deployment. - - `metadata`: Metadata for the pod template. - - `spec`: Describes the pod specification. - - `containers`: Defines the containers within the pod. - - `name`: The name of the container (`nginx`). - - `image`: The container image (`nginx`). - - `resources`: Resource limits and requests. - - `limits`: Maximum resources (`128Mi` memory, `500m` CPU). - - `requests`: Minimum resources (`64Mi` memory, `250m` CPU). - - `ports`: Container ports (80). - - -### 2. Deployment with Rolling Update Strategy - -```yaml -apiVersion: extensions/v1beta1 -kind: Deployment -metadata: - name: my-name - labels: - name: my-name -spec: - strategy: - rollingUpdate: - maxSurge: 1 - maxUnavailable: 1 - type: RollingUpdate - template: - metadata: - labels: - name: my-name - spec: - containers: - - image: ipedrazas/docmock - name: my-name - resources: - requests: - cpu: "20m" - memory: "55M" - livenessProbe: - httpGet: - path: /_status/healthz - port: 5000 - initialDelaySeconds: 90 - timeoutSeconds: 10 - readinessProbe: - httpGet: - path: /_status/healthz - port: 5000 - initialDelaySeconds: 30 - timeoutSeconds: 10 - env: - - name: ENVVARNAME - value: ENVVARVALUE - ports: - - containerPort: 5000 - name: my-name - volumeMounts: - - mountPath: /data - name: data - volumes: - - name: data - emptyDir: {} - restartPolicy: Always - imagePullPolicy: Always -``` -- `apiVersion: extensions/v1beta1`: Specifies the API version. -- `kind: Deployment`: Defines the object as a Deployment. -- `metadata`: Provides metadata for the Deployment. - - `name`: The name of the Deployment (`my-name`). - - `labels`: Key-value pairs to categorize the Deployment. -- `spec`: Describes the desired state. - - `strategy`: Rolling update strategy. - - `rollingUpdate`: Defines the update strategy. - - `maxSurge`: Maximum number of additional pods (1). - - `maxUnavailable`: Maximum number of unavailable pods (1). - - `type`: The type of update strategy (`RollingUpdate`). - - `template`: The pod template used by the Deployment. - - `metadata`: Metadata for the pod template. - - `spec`: Describes the pod specification. - - `containers`: Defines the containers within the pod. - - `name`: The name of the container (`my-name`). - - `image`: The container image (`ipedrazas/docmock`). - - `resources`: Resource requests. - - `requests`: Minimum resources (`20m` CPU, `55M` memory). - - `livenessProbe`: Health check for the container. - - `httpGet`: HTTP GET request for the probe. - - `path`: The path to check (`/_status/healthz`). - - `port`: The port to check (5000). - - `initialDelaySeconds`: Initial delay before the probe starts (90 seconds). - - `timeoutSeconds`: Timeout for the probe (10 seconds). - - `readinessProbe`: Readiness check for the container. - - `httpGet`: HTTP GET request for the probe. - - `path`: The path to check (`/_status/healthz`). - - `port`: The port to check (5000). - - `initialDelaySeconds`: Initial delay before the probe starts (30 seconds). - - `timeoutSeconds`: Timeout for the probe (10 seconds). - - `env`: Environment variables for the container. - - `name`: The name of the environment variable (`ENVVARNAME`). - - `value`: The value of the environment variable (`ENVVARVALUE`). - - `ports`: Container ports. - - `containerPort`: The container port (5000). - - `name`: The name of the port (`my-name`). - - `volumeMounts`: Mounting volumes to the container. - - `mountPath`: The path to mount the volume (`/data`). - - `name`: The name of the volume (`data`). - - `volumes`: Defines the volumes. - - `name`: The name of the volume (`data`). - - `emptyDir`: An empty directory volume. - - `restartPolicy`: Restart policy for the container (`Always`). - - `imagePullPolicy`: Image pull policy (`Always`). - diff --git a/kubernetes/workloads/job.md b/kubernetes/workloads/job.md deleted file mode 100644 index 36e27fd..0000000 --- a/kubernetes/workloads/job.md +++ /dev/null @@ -1,74 +0,0 @@ - -#### 1. Simple Job - -This Job is configured to run a single container that prints "hello world" to the console. - -```yaml -apiVersion: batch/v1 -kind: Job -metadata: - name: myjob - namespace: my-ns -spec: - template: - spec: - containers: - - name: myjob - image: alpine - command: - - echo - - "hello world" - restartPolicy: Never -``` -- `apiVersion: batch/v1`: Specifies the API version. -- `kind: Job`: Defines the object as a Job. -- `metadata`: Provides metadata for the Job. - - `name`: The name of the Job (`myjob`). - - `namespace`: The namespace where the Job will be created (`my-ns`). -- `spec`: Describes the desired state. - - `template`: The pod template used by the Job. - - `spec`: Describes the pod specification. - - `containers`: Defines the containers within the pod. - - `name`: The name of the container (`myjob`). - - `image`: The container image (`alpine`). - - `command`: The command to run in the container (`echo "hello world"`). - - `restartPolicy`: Specifies the restart policy for the pod (`Never`). - -#### 2. Job with Error and Retries - -This Job attempts to list a non-existent directory (`/chert`) and will try to complete the task up to 6 times due to the error. - -```yaml -apiVersion: batch/v1 -kind: Job -metadata: - name: myjob - namespace: my-ns -spec: - backoffLimit: 6 - template: - spec: - containers: - - name: myjob - image: alpine - command: - - ls - - "/chert" - restartPolicy: Never -``` -- `apiVersion: batch/v1`: Specifies the API version. -- `kind: Job`: Defines the object as a Job. -- `metadata`: Provides metadata for the Job. - - `name`: The name of the Job (`myjob`). - - `namespace`: The namespace where the Job will be created (`my-ns`). -- `spec`: Describes the desired state. - - `backoffLimit`: The number of retries before the Job is considered failed (6). - - `template`: The pod template used by the Job. - - `spec`: Describes the pod specification. - - `containers`: Defines the containers within the pod. - - `name`: The name of the container (`myjob`). - - `image`: The container image (`alpine`). - - `command`: The command to run in the container (`ls /chert`). - - `restartPolicy`: Specifies the restart policy for the pod (`Never`). - -This guide provides a detailed explanation of Kubernetes Job YAML files. Jobs are designed to run a task to completion, and they can retry in case of failures. Each Job configuration includes specifications for containers, commands, and restart policies, with the option to set a retry limit for handling errors. \ No newline at end of file diff --git a/kubernetes/workloads/pod.md b/kubernetes/workloads/pod.md deleted file mode 100644 index e470bb4..0000000 --- a/kubernetes/workloads/pod.md +++ /dev/null @@ -1,189 +0,0 @@ -## 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. - - diff --git a/kubernetes/workloads/replicaset.md b/kubernetes/workloads/replicaset.md deleted file mode 100644 index a51ff7d..0000000 --- a/kubernetes/workloads/replicaset.md +++ /dev/null @@ -1,94 +0,0 @@ -## Document: Kubernetes ReplicaSet YAML Explanation - -### Overview -This document provides an explanation of a Kubernetes ReplicaSet YAML file and commands to manage the ReplicaSet. The YAML file defines the desired state for a ReplicaSet, which ensures a specified number of pod replicas are running at any given time. - -### YAML File Breakdown - -#### 1. Define the ReplicaSet -The YAML file begins with the `apiVersion`, `kind`, and `metadata` fields, which specify the API version, the type of Kubernetes object, and metadata about the object, respectively. - -```yaml -apiVersion: apps/v1 -kind: ReplicaSet -metadata: - name: my-app - namespace: my-ns - labels: - app.kubernetes.io/name: my-app - app.kubernetes.io/env: development -``` -- `apiVersion: apps/v1`: Specifies that this configuration uses the `apps/v1` API version. -- `kind: ReplicaSet`: Defines the object as a ReplicaSet. -- `metadata`: Provides metadata for the ReplicaSet, including: - - `name`: The name of the ReplicaSet (`my-app`). - - `namespace`: The namespace where the ReplicaSet will be created (`my-ns`). - - `labels`: Key-value pairs to categorize the ReplicaSet (`app.kubernetes.io/name: my-app` and `app.kubernetes.io/env: development`). - -#### 2. Define the Specification -The `spec` section describes the desired state of the ReplicaSet. - -```yaml -spec: - replicas: 3 - selector: - matchLabels: - app.kubernetes.io/name: my-app - template: - metadata: - labels: - app.kubernetes.io/name: my-app - spec: - containers: - - name: nginx - image: nginx -``` -- `replicas: 3`: Specifies that three replicas of the pod should be running. -- `selector`: Defines how to identify the pods managed by this ReplicaSet. - - `matchLabels`: Matches pods with the label `app.kubernetes.io/name: my-app`. -- `template`: Provides the pod template used by the ReplicaSet to create new pods. - - `metadata`: Metadata for the pod template. - - `labels`: Labels applied to the pods (`app.kubernetes.io/name: my-app`). - - `spec`: Describes the pod specification. - - `containers`: Defines the containers within the pod. - - `name`: The name of the container (`nginx`). - - `image`: The container image to use (`nginx`). - - -### Update the Container Image Version - -To update the container image version, edit the YAML manifest and change the `image` field: -```yaml -containers: -- name: nginx - image: nginx:1.25 -``` -Apply the updated manifest to update the pods with the new image version. - -### Complete YAML File with Image Version Update - -Here is the complete YAML file with the container image version updated to `nginx:1.25`: - -```yaml -apiVersion: apps/v1 -kind: ReplicaSet -metadata: - name: my-app - namespace: my-ns - labels: - app.kubernetes.io/name: my-app - app.kubernetes.io/env: development -spec: - replicas: 3 - selector: - matchLabels: - app.kubernetes.io/name: my-app - template: - metadata: - labels: - app.kubernetes.io/name: my-app - spec: - containers: - - name: nginx - image: nginx:1.25 -``` diff --git a/kubernetes/workloads/stateful.md b/kubernetes/workloads/stateful.md deleted file mode 100644 index 5cbad06..0000000 --- a/kubernetes/workloads/stateful.md +++ /dev/null @@ -1,185 +0,0 @@ - -#### 1. Basic Redis StatefulSet - -This StatefulSet is configured to run Redis instances in the `my-ns` namespace with 3 replicas. - -```yaml -apiVersion: apps/v1 -kind: StatefulSet -metadata: - name: redis - namespace: my-ns - labels: - app.kubernetes.io/name: redis -spec: - replicas: 3 - selector: - matchLabels: - app.kubernetes.io/name: redis - template: - metadata: - labels: - app.kubernetes.io/name: redis - spec: - containers: - - name: redis - image: redis -``` -- `apiVersion: apps/v1`: Specifies the API version. -- `kind: StatefulSet`: Defines the object as a StatefulSet. -- `metadata`: Provides metadata for the StatefulSet. - - `name`: The name of the StatefulSet (`redis`). - - `namespace`: The namespace where the StatefulSet will be created (`my-ns`). - - `labels`: Key-value pairs to categorize the StatefulSet. -- `spec`: Describes the desired state. - - `replicas`: Number of pod replicas (3). - - `selector`: Identifies the pods managed by this StatefulSet. - - `matchLabels`: Matches pods with the label `app.kubernetes.io/name: redis`. - - `template`: The pod template used by the StatefulSet. - - `metadata`: Metadata for the pod template. - - `labels`: Labels applied to the pods (`app.kubernetes.io/name: redis`). - - `spec`: Describes the pod specification. - - `containers`: Defines the containers within the pod. - - `name`: The name of the container (`redis`). - - `image`: The container image (`redis`). - -#### 2. Redis StatefulSet with Volume - -This StatefulSet is similar to the first one but includes persistent volume claims (PVCs) to ensure data persistence. - -```yaml -apiVersion: apps/v1 -kind: StatefulSet -metadata: - name: redis - namespace: my-ns - labels: - app.kubernetes.io/name: redis -spec: - replicas: 3 - selector: - matchLabels: - app.kubernetes.io/name: redis - template: - metadata: - labels: - app.kubernetes.io/name: redis - spec: - containers: - - name: redis - image: redis - volumeMounts: - - name: redis-data - mountPath: /var/lib/redis - volumeClaimTemplates: - - metadata: - name: redis-data - spec: - accessModes: - - "ReadWriteOnce" - resources: - requests: - storage: 1Gi -``` -- `apiVersion: apps/v1`: Specifies the API version. -- `kind: StatefulSet`: Defines the object as a StatefulSet. -- `metadata`: Provides metadata for the StatefulSet. - - `name`: The name of the StatefulSet (`redis`). - - `namespace`: The namespace where the StatefulSet will be created (`my-ns`). - - `labels`: Key-value pairs to categorize the StatefulSet. -- `spec`: Describes the desired state. - - `replicas`: Number of pod replicas (3). - - `selector`: Identifies the pods managed by this StatefulSet. - - `matchLabels`: Matches pods with the label `app.kubernetes.io/name: redis`. - - `template`: The pod template used by the StatefulSet. - - `metadata`: Metadata for the pod template. - - `labels`: Labels applied to the pods (`app.kubernetes.io/name: redis`). - - `spec`: Describes the pod specification. - - `containers`: Defines the containers within the pod. - - `name`: The name of the container (`redis`). - - `image`: The container image (`redis`). - - `volumeMounts`: Mounts the specified volume to `/var/lib/redis`. - - `name`: The name of the volume (`redis-data`). - - `mountPath`: The path to mount the volume (`/var/lib/redis`). - - `volumeClaimTemplates`: Defines the PVCs for the StatefulSet. - - `metadata`: Metadata for the PVC. - - `name`: The name of the PVC (`redis-data`). - - `spec`: Describes the PVC specification. - - `accessModes`: Access mode for the PVC (`ReadWriteOnce`). - - `resources`: Resource requests for the PVC. - - `requests`: Storage request (1Gi). - -#### 3. Web StatefulSet with Volume - -This StatefulSet runs NGINX instances with persistent storage. - -```yaml -apiVersion: apps/v1 -kind: StatefulSet -metadata: - name: web -spec: - selector: - matchLabels: - app: nginx # has to match .spec.template.metadata.labels - serviceName: "nginx" - replicas: 3 # by default is 1 - minReadySeconds: 10 # by default is 0 - template: - metadata: - labels: - app: nginx # has to match .spec.selector.matchLabels - spec: - terminationGracePeriodSeconds: 10 - containers: - - name: nginx - image: registry.k8s.io/nginx-slim:0.8 - ports: - - containerPort: 80 - name: web - volumeMounts: - - name: www - mountPath: /usr/share/nginx/html - volumeClaimTemplates: - - metadata: - name: www - spec: - accessModes: [ "ReadWriteOnce" ] - storageClassName: "my-storage-class" - resources: - requests: - storage: 1Gi -``` -- `apiVersion: apps/v1`: Specifies the API version. -- `kind: StatefulSet`: Defines the object as a StatefulSet. -- `metadata`: Provides metadata for the StatefulSet. - - `name`: The name of the StatefulSet (`web`). -- `spec`: Describes the desired state. - - `selector`: Identifies the pods managed by this StatefulSet. - - `matchLabels`: Matches pods with the label `app: nginx`. - - `serviceName`: The name of the service that governs this StatefulSet (`nginx`). - - `replicas`: Number of pod replicas (3). - - `minReadySeconds`: Minimum time for pods to be ready (10 seconds). - - `template`: The pod template used by the StatefulSet. - - `metadata`: Metadata for the pod template. - - `labels`: Labels applied to the pods (`app: nginx`). - - `spec`: Describes the pod specification. - - `terminationGracePeriodSeconds`: Time for the pod to terminate gracefully (10 seconds). - - `containers`: Defines the containers within the pod. - - `name`: The name of the container (`nginx`). - - `image`: The container image (`registry.k8s.io/nginx-slim:0.8`). - - `ports`: Container ports. - - `containerPort`: The container port (80). - - `name`: The name of the port (`web`). - - `volumeMounts`: Mounts the specified volume to `/usr/share/nginx/html`. - - `name`: The name of the volume (`www`). - - `mountPath`: The path to mount the volume (`/usr/share/nginx/html`). - - `volumeClaimTemplates`: Defines the PVCs for the StatefulSet. - - `metadata`: Metadata for the PVC. - - `name`: The name of the PVC (`www`). - - `spec`: Describes the PVC specification. - - `accessModes`: Access mode for the PVC (`ReadWriteOnce`). - - `storageClassName`: The storage class name (`my-storage-class`). - - `resources`: Resource requests for the PVC. - - `requests`: Storage request (1Gi). - From bb2d98ff12fb52d5dd822a3d19fa639675d6980e Mon Sep 17 00:00:00 2001 From: radinpirouz Date: Thu, 31 Oct 2024 16:24:08 +0330 Subject: [PATCH 07/16] Docker : Add Docker System Commands --- .../Docker/2-Commands.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Containerization & Orchestration/Docker/2-Commands.md b/Containerization & Orchestration/Docker/2-Commands.md index 0a1d3da..9db0793 100755 --- a/Containerization & Orchestration/Docker/2-Commands.md +++ b/Containerization & Orchestration/Docker/2-Commands.md @@ -267,4 +267,19 @@ Docker networks allow communication between containers. - **Disconnect a container from a network:** ```bash docker network disconnect - ``` \ No newline at end of file + ``` + + --- + +## **6. System Commands** + +- **Show Docker Disk usage:** + ```bash + docker system df + ``` +- **Remove Unuse Cache,Container And More** +- ```bash +- docker system prune +- ``` +- +- From b9659ada40738ac36d24402a7a8eb59375512111 Mon Sep 17 00:00:00 2001 From: radinpirouz Date: Thu, 7 Nov 2024 13:37:10 +0330 Subject: [PATCH 08/16] Git Doc: Add Clone With Custom SSh Key --- Code Management/Git/main.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Code Management/Git/main.md b/Code Management/Git/main.md index f3fe02d..bcd3f12 100644 --- a/Code Management/Git/main.md +++ b/Code Management/Git/main.md @@ -31,6 +31,12 @@ If you need to use a specific SSH key for your Git operations, you can configure git config --add --local core.sshCommand 'ssh -i ' ``` +For Clone With Custom SSH Key Use: +```bash +git -c core.sshCommand="ssh -i " clone host:repo +``` + + *Replace `` with the actual path to your SSH key file.* ## Creating and Managing a Local Git Repository From 353237a01b55e737c06d0746fd40a428e5277be4 Mon Sep 17 00:00:00 2001 From: radinpirouz Date: Thu, 7 Nov 2024 15:37:24 +0330 Subject: [PATCH 09/16] Nginx : Add Basic Doc --- Nginx/1-Information.md | 63 +++++++++++++++++++++++ Nginx/2-Installtion.md | 114 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 177 insertions(+) create mode 100644 Nginx/1-Information.md create mode 100644 Nginx/2-Installtion.md diff --git a/Nginx/1-Information.md b/Nginx/1-Information.md new file mode 100644 index 0000000..5b503d7 --- /dev/null +++ b/Nginx/1-Information.md @@ -0,0 +1,63 @@ +# Nginx Documentation + +## What Is Nginx? + +**Nginx** (pronounced "engine-x") is a popular open-source web server and reverse proxy software. Known for its high performance, stability, rich feature set, simple configuration, and low resource consumption, Nginx has become one of the most widely used server applications worldwide. + +Originally developed by Igor Sysoev, Nginx was designed to address the **C10k problem**—the challenge of handling 10,000 concurrent client connections. Nginx overcomes this limitation through an **event-driven, asynchronous architecture** that enables it to manage a vast number of simultaneous connections efficiently and with minimal resource usage. + +--- + +## Key Features of Nginx + +1. **High Performance**: Nginx is optimized to handle high-traffic websites and can serve static content faster than many other web servers. + +2. **Stability**: Its design enables stable operation under heavy load, making it reliable for production environments. + +3. **Low Resource Consumption**: The asynchronous architecture minimizes memory and CPU usage, making it suitable for high-concurrency environments. + +4. **Flexibility**: Nginx can be easily configured to function as a web server, reverse proxy, load balancer, and more. + +5. **Security**: Nginx supports SSL/TLS and can be configured for secure HTTPS connections, with built-in features to prevent DoS and DDoS attacks. + +6. **Extensibility**: Through a range of modules, Nginx can be extended to support various functions such as caching, load balancing, access control, and more. + +--- + +## Common Uses of Nginx + +Nginx’s versatility makes it a powerful tool for a wide range of applications. Below are some of its most common uses: + +### 1. Web Server + Nginx can serve as a **standalone web server** to deliver static content like HTML files, images, videos, and more. Due to its efficiency, it’s commonly used to serve content directly or in front of other server applications for added performance and caching benefits. + +### 2. Reverse Proxy + Acting as a **reverse proxy**, Nginx can forward client requests to another server, often used to route traffic to applications hosted on multiple servers. This approach helps manage and distribute incoming traffic, improving performance and security by hiding the backend server details from clients. + +### 3. Load Balancer + Nginx’s **load balancing** capabilities help distribute traffic across multiple servers. Load balancing not only increases fault tolerance by rerouting traffic in case of server failure but also enhances performance by preventing any single server from becoming overloaded. + + Common load balancing methods in Nginx: + - **Round Robin**: Distributes requests sequentially across servers. + - **Least Connections**: Routes traffic to the server with the fewest active connections. + - **IP Hash**: Ensures clients are consistently routed to the same server based on their IP address. + +### 4. Caching + Nginx can act as a **caching server** to store copies of frequently requested content. By serving cached content, Nginx can significantly reduce load times for users and lessen the workload on backend servers. This is especially beneficial for high-traffic websites with dynamic content. + +--- + +## Additional Nginx Features + +- **SSL/TLS Termination**: Nginx can terminate SSL/TLS connections, handling the encryption and decryption process to reduce the burden on backend servers. + +- **URL Rewriting and Redirection**: With URL rewriting rules, Nginx can redirect requests to different URLs, enabling efficient handling of routing and user-friendly URLs. + +- **Access Control**: Provides robust tools for managing access controls, including IP-based access restrictions, user authentication, and authorization. + +- **HTTP/2 and HTTP/3 Support**: Supports newer HTTP protocols for faster and more secure connections. + +- **Customizable Modules**: Nginx’s modular architecture allows for custom modules, enabling functionality for a wide range of applications and configurations. + +--- + diff --git a/Nginx/2-Installtion.md b/Nginx/2-Installtion.md new file mode 100644 index 0000000..6a2e07e --- /dev/null +++ b/Nginx/2-Installtion.md @@ -0,0 +1,114 @@ +# Installing Nginx + +## Prerequisites +Before installing Nginx, ensure that you have root or sudo privileges on your system to carry out installation and configuration commands. + +## Step-by-Step Installation + +### For Debian-Based Systems (e.g., Ubuntu) + +1. **Update Package Repositories** + It’s a good practice to update your package repositories before installing new software to ensure you’re downloading the latest version available. + ```bash + sudo apt update + ``` + +2. **Install Nginx** + Install Nginx from the package repository. + ```bash + sudo apt install nginx + ``` + +3. **Start Nginx Service** + Once installed, start the Nginx service. + ```bash + sudo systemctl start nginx + ``` + +4. **Enable Nginx to Start on Boot** + This command configures Nginx to start automatically whenever the server reboots. + ```bash + sudo systemctl enable nginx + ``` + +5. **Check Status (Optional)** + Verify that Nginx is running correctly. + ```bash + sudo systemctl status nginx + ``` + +### For Red Hat-Based Systems (e.g., CentOS, Fedora) + +1. **Update Package Repositories** + As with Debian-based systems, it’s recommended to update repositories first. + ```bash + sudo yum update + ``` + +2. **Install Nginx** + On Red Hat-based systems, install Nginx with `yum`. + ```bash + sudo yum install nginx + ``` + +3. **Start Nginx Service** + Start Nginx after installation. + ```bash + sudo systemctl start nginx + ``` + +4. **Enable Nginx to Start on Boot** + Configure Nginx to launch automatically on system startup. + ```bash + sudo systemctl enable nginx + ``` + +5. **Check Status (Optional)** + Confirm that Nginx is running and functioning properly. + ```bash + sudo systemctl status nginx + ``` + +--- + +## Post-Installation Steps + +1. **Allow Nginx Through the Firewall** + If your server has a firewall enabled, you may need to allow HTTP (port 80) and HTTPS (port 443) traffic. + + ### Debian-Based Systems: + ```bash + sudo ufw allow 'Nginx Full' + ``` + + ### Red Hat-Based Systems: + ```bash + sudo firewall-cmd --permanent --add-service=http + sudo firewall-cmd --permanent --add-service=https + sudo firewall-cmd --reload + ``` + +2. **Verify Installation** + Open a web browser and navigate to your server’s IP address or domain name: + ``` + http:// + ``` + You should see the default Nginx welcome page, which confirms that the installation is successful. + +--- + +## Troubleshooting Common Installation Issues + +- **Error: Package Not Found** + If you encounter an error stating that the Nginx package was not found, you may need to install the **EPEL repository** (Extra Packages for Enterprise Linux) on Red Hat-based systems: + ```bash + sudo yum install epel-release + sudo yum install nginx + ``` + +- **Permission Denied Errors** + Ensure you’re using `sudo` to run commands that require root privileges. + +- **Firewall Blocking Access** + If you can’t access Nginx via a browser, ensure that firewall rules are configured to allow HTTP/HTTPS traffic. + From 983cc5c88706f907083f4f022d44411395f1e8fa Mon Sep 17 00:00:00 2001 From: radinpirouz Date: Thu, 7 Nov 2024 15:40:59 +0330 Subject: [PATCH 10/16] Nginx : Added Web Server Doc --- Nginx/3-Setup-Web-Server.md | 155 ++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 Nginx/3-Setup-Web-Server.md diff --git a/Nginx/3-Setup-Web-Server.md b/Nginx/3-Setup-Web-Server.md new file mode 100644 index 0000000..8644ddc --- /dev/null +++ b/Nginx/3-Setup-Web-Server.md @@ -0,0 +1,155 @@ +# Setting Up a Web Server on Nginx + +This guide covers the steps to configure Nginx as a basic web server to serve static HTML files and handle HTTP requests. We'll set up a sample web server on a Debian-based system, but the steps are similar for other Linux distributions. + +--- + +## Prerequisites + +- A server with **Nginx installed**. If you haven’t installed Nginx yet, follow the installation instructions in the [Nginx Installation Guide](#) (or use the provided installation commands). +- **Root or sudo privileges** to edit configuration files and restart Nginx services. + +--- + +## Step 1: Set Up the Web Directory + +1. **Create a Directory for Your Website** + Nginx typically serves content from `/var/www/`. Create a new directory for your website content. + ```bash + sudo mkdir -p /var/www/example.com/html + ``` + +2. **Set Permissions** + Ensure that the Nginx user (usually `www-data`) has permission to read files in this directory. + ```bash + sudo chown -R $USER:$USER /var/www/example.com/html + sudo chmod -R 755 /var/www/example.com + ``` + +3. **Add a Sample HTML File** + Create a simple HTML file to confirm the setup. + ```bash + echo " + + Welcome to Example.com! + + +

Success! Nginx is serving your website.

+ + " | sudo tee /var/www/example.com/html/index.html + ``` + +--- + +## Step 2: Configure Nginx to Serve the Website + +1. **Create a Server Block Configuration File** + Nginx server blocks (similar to Apache virtual hosts) allow you to host multiple sites on the same server. Create a new configuration file for your site. + ```bash + sudo nano /etc/nginx/sites-available/example.com + ``` + +2. **Add Server Block Configuration** + Paste the following configuration into the file, replacing `example.com` with your domain or IP address: + + ```nginx + server { + listen 80; + server_name example.com www.example.com; + + root /var/www/example.com/html; + index index.html; + + location / { + try_files $uri $uri/ =404; + } + } + ``` + +3. **Enable the Server Block** + Link the configuration file to `sites-enabled` to enable it in Nginx: + ```bash + sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ + ``` + +4. **Test the Nginx Configuration** + Run the following command to check for any syntax errors in the configuration: + ```bash + sudo nginx -t + ``` + +5. **Reload Nginx to Apply Changes** + If the syntax test passes, reload Nginx to apply the new configuration. + ```bash + sudo systemctl reload nginx + ``` + +--- + +## Step 3: Configure DNS (Optional) + +If you have a domain name, point it to your server’s IP address in your DNS provider’s settings. Create an A record for `example.com` and, if desired, `www.example.com` to direct traffic to your server’s IP address. + +--- + +## Step 4: Access Your Website + +In a web browser, navigate to `http://example.com` (replace `example.com` with your domain or IP address). You should see the sample HTML page you created, confirming that Nginx is serving your web content. + +--- + +## Optional: Enabling HTTPS with SSL/TLS + +For added security, you can configure HTTPS on your Nginx web server. One free and easy way to do this is by using **Let’s Encrypt**. + +1. **Install Certbot and the Nginx Plugin** + ```bash + sudo apt install certbot python3-certbot-nginx + ``` + +2. **Obtain and Install a Certificate** + Run the following Certbot command to automatically obtain and configure an SSL certificate for your website: + ```bash + sudo certbot --nginx -d example.com -d www.example.com + ``` + +3. **Verify Renewal Process** + Certificates from Let’s Encrypt expire every 90 days. To automatically renew the certificates, add a cron job or use Certbot’s built-in renewal service: + ```bash + sudo certbot renew --dry-run + ``` + +Now your website will be accessible securely at `https://example.com`. + +--- + +## Nginx Configuration Summary + +Here's a quick reference for the key commands and file paths: + +- **Site root directory**: `/var/www/example.com/html` +- **Nginx configuration files**: + - Site-specific: `/etc/nginx/sites-available/example.com` + - Enabled sites: `/etc/nginx/sites-enabled/` +- **Commands**: + - Check configuration syntax: `sudo nginx -t` + - Reload Nginx: `sudo systemctl reload nginx` + +--- + +## Troubleshooting Common Issues + +1. **Error: 403 Forbidden** + - Check that Nginx has the necessary permissions to access files in the root directory (`/var/www/example.com/html`). Use `chmod 755` and `chown` commands as shown above. + +2. **Error: 404 Not Found** + - Ensure the `index.html` file exists in the specified directory and that `try_files` directive is correctly pointing to it. + +3. **Configuration Errors** + - Always run `sudo nginx -t` to check configuration changes before reloading Nginx. + +4. **SSL Issues** + - If HTTPS fails, make sure Certbot successfully installed the certificate and that the DNS settings correctly point to your server’s IP address. + +--- + From 4f45c37014388322c37826950924236acbc268b3 Mon Sep 17 00:00:00 2001 From: RadinPirouz <75082987+RadinPirouz@users.noreply.github.com> Date: Thu, 7 Nov 2024 15:42:23 +0330 Subject: [PATCH 11/16] Update 3-Setup-Web-Server.md --- Nginx/3-Setup-Web-Server.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Nginx/3-Setup-Web-Server.md b/Nginx/3-Setup-Web-Server.md index 8644ddc..595a10a 100644 --- a/Nginx/3-Setup-Web-Server.md +++ b/Nginx/3-Setup-Web-Server.md @@ -6,7 +6,7 @@ This guide covers the steps to configure Nginx as a basic web server to serve st ## Prerequisites -- A server with **Nginx installed**. If you haven’t installed Nginx yet, follow the installation instructions in the [Nginx Installation Guide](#) (or use the provided installation commands). +- A server with **Nginx installed**. If you haven’t installed Nginx yet, follow the installation instructions in the [Nginx Installation Guide](2-Installtion.md) (or use the provided installation commands). - **Root or sudo privileges** to edit configuration files and restart Nginx services. --- From 405f140324ac3bc1ca7f4f0b8bc5da260684ad37 Mon Sep 17 00:00:00 2001 From: radinpirouz Date: Thu, 7 Nov 2024 15:43:11 +0330 Subject: [PATCH 12/16] Nginx : Update Web Server Doc --- Nginx/3-Setup-Web-Server.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Nginx/3-Setup-Web-Server.md b/Nginx/3-Setup-Web-Server.md index 595a10a..5dc927d 100644 --- a/Nginx/3-Setup-Web-Server.md +++ b/Nginx/3-Setup-Web-Server.md @@ -46,7 +46,7 @@ This guide covers the steps to configure Nginx as a basic web server to serve st 1. **Create a Server Block Configuration File** Nginx server blocks (similar to Apache virtual hosts) allow you to host multiple sites on the same server. Create a new configuration file for your site. ```bash - sudo nano /etc/nginx/sites-available/example.com + sudo vim /etc/nginx/sites-available/example.com ``` 2. **Add Server Block Configuration** From 4b6e71d2be86dff3d23bfd7e53e1226d2a6f6d06 Mon Sep 17 00:00:00 2001 From: radinpirouz Date: Thu, 7 Nov 2024 15:53:45 +0330 Subject: [PATCH 13/16] Nginx Add Reverse Proxy Doc --- Nginx/4-Reverse-Proxy.md | 108 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 Nginx/4-Reverse-Proxy.md diff --git a/Nginx/4-Reverse-Proxy.md b/Nginx/4-Reverse-Proxy.md new file mode 100644 index 0000000..39e20b5 --- /dev/null +++ b/Nginx/4-Reverse-Proxy.md @@ -0,0 +1,108 @@ +# Setting Up a Reverse Proxy with Nginx + +A reverse proxy can forward client requests to multiple backend servers, helping manage traffic, load balance, and secure the backend infrastructure. This guide provides a step-by-step approach to setting up a basic reverse proxy configuration in Nginx. + +--- + +## Prerequisites + +- **Nginx Installed**: Ensure that Nginx is installed and running on your server. +- **Root or sudo privileges** to edit configuration files and restart Nginx. +- **Backend Servers**: At least two backend services or applications you want to proxy, such as `http://web1.com` and `http://web2.com`. + +--- + +## Step 1: Create the Reverse Proxy Configuration File + +1. **Open a new configuration file** for your reverse proxy in Nginx's `sites-available` directory: + ```bash + sudo vim /etc/nginx/sites-available/reverse-proxy.conf + ``` + +2. **Define the Reverse Proxy Configuration** + Copy the following configuration into the file. Adjust the backend server names (`web1.com` and `web2.com`) to match your actual server addresses. + + ```nginx + server { + listen 80; + server_name _; # Use "_" to accept any hostname, or specify a domain name + + # Proxy for the first backend application + location /web1 { + proxy_pass http://web1.com; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + + # Proxy for the second backend application + location /web2 { + proxy_pass http://web2.com; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + + # Log settings + access_log /var/log/nginx/reverse-proxy-access.log; + error_log /var/log/nginx/reverse-proxy-error.log; + } + ``` + + ### Explanation of Key Directives + - **listen**: Specifies the port Nginx will listen on (80 for HTTP). + - **server_name**: The domain name or IP address for this reverse proxy. Using `_` allows it to accept any hostname. + - **location**: Defines the URL path (`/web1`, `/web2`) to route to different backend servers. + - **proxy_pass**: Specifies the backend server URL to which traffic should be forwarded. + - **proxy_set_header**: Sets headers that pass client information to the backend, preserving the original IP and protocol. + - **access_log**: Logs access requests. + - **error_log**: Logs error messages for easier troubleshooting. + +--- + +## Step 2: Enable the Reverse Proxy Configuration + +1. **Create a symbolic link** from `sites-available` to `sites-enabled` to enable the reverse proxy configuration in Nginx: + ```bash + sudo ln -s /etc/nginx/sites-available/reverse-proxy.conf /etc/nginx/sites-enabled/reverse-proxy.conf + ``` + +2. **Verify Nginx Configuration** + Run a configuration test to ensure there are no syntax errors: + ```bash + sudo nginx -t + ``` + +3. **Reload Nginx** to apply the changes: + ```bash + sudo systemctl reload nginx + ``` + +--- + +## Step 3: Access Your Reverse Proxy + +With the reverse proxy set up, you can now access your backend services using the following URLs: + +- **http://your-server-ip/web1**: For requests proxied to `http://web1.com` +- **http://your-server-ip/web2**: For requests proxied to `http://web2.com` + +Replace `your-server-ip` with the actual IP address or domain name of your Nginx server. + +--- + +## Troubleshooting Common Issues + +- **Error: 502 Bad Gateway** + - This error usually occurs if the backend server is down or unreachable. Verify that the backend server addresses (`http://web1.com`, `http://web2.com`) are correct and accessible. + +- **Permission Denied for Log Files** + - Make sure the log file paths are writable by Nginx. Use `sudo chown www-data:www-data /var/log/nginx/reverse-proxy-access.log` if necessary. + +- **Configuration Errors** + - Always test configuration changes using `sudo nginx -t` before reloading or restarting Nginx. + +--- + From 761bdc8904c731635c2285f0a5c834f6c99ab16c Mon Sep 17 00:00:00 2001 From: radinpirouz Date: Thu, 7 Nov 2024 15:56:05 +0330 Subject: [PATCH 14/16] Nginx: Update Reverse Proxy --- Nginx/4-Reverse-Proxy.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Nginx/4-Reverse-Proxy.md b/Nginx/4-Reverse-Proxy.md index 39e20b5..440c72a 100644 --- a/Nginx/4-Reverse-Proxy.md +++ b/Nginx/4-Reverse-Proxy.md @@ -77,7 +77,7 @@ A reverse proxy can forward client requests to multiple backend servers, helping 3. **Reload Nginx** to apply the changes: ```bash - sudo systemctl reload nginx + nginx -s reload ``` --- From f0b240989a98561a0a0e7531875b96c913bb6084 Mon Sep 17 00:00:00 2001 From: radinpirouz Date: Thu, 7 Nov 2024 16:03:57 +0330 Subject: [PATCH 15/16] Nginx : Added load Balancer Doc --- Nginx/5-Load-Balancer.md | 128 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 Nginx/5-Load-Balancer.md diff --git a/Nginx/5-Load-Balancer.md b/Nginx/5-Load-Balancer.md new file mode 100644 index 0000000..e3e9fbb --- /dev/null +++ b/Nginx/5-Load-Balancer.md @@ -0,0 +1,128 @@ +# Setting Up Load Balancing with Nginx + +Load balancing with Nginx helps distribute incoming traffic across multiple backend servers, improving the performance, reliability, and availability of your applications. This guide provides a step-by-step process to configure a basic round-robin load balancer using Nginx. + +--- + +## Prerequisites + +- **Nginx Installed**: Ensure Nginx is installed on your server. +- **Root or sudo privileges** to edit configuration files and restart Nginx. +- **Multiple Backend Servers**: Two or more backend servers with applications running. In this example, we use `10.10.10.1` and `10.10.10.2`. + +--- + +## Step 1: Create the Load Balancer Configuration File + +1. **Open a new configuration file** for the load balancer in Nginx’s `sites-available` directory: + ```bash + sudo vim /etc/nginx/sites-available/load_balancer.conf + ``` + +2. **Define the Load Balancer Configuration** + Copy the following configuration into the file. Replace the IP addresses (`10.10.10.1` and `10.10.10.2`) with the actual IP addresses of your backend servers. + + ```nginx + # Define the upstream group of backend servers + upstream backend_servers { + server 10.10.10.1; + server 10.10.10.2; + } + + server { + listen 80; + server_name _; # Accept any hostname or specify a domain name if needed + + location / { + proxy_pass http://backend_servers; # Forward requests to the backend servers group + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + + # Log files + access_log /var/log/nginx/load_balancer_access.log; + error_log /var/log/nginx/load_balancer_error.log; + } + ``` + + ### Explanation of Key Directives + - **upstream**: Defines a pool of backend servers to which Nginx will forward traffic. By default, Nginx uses a round-robin algorithm, sending requests to each server in turn. + - **server_name**: Accepts any hostname (`_`) or a specific domain name. + - **proxy_pass**: Specifies the backend server group defined by `upstream`. + - **proxy_set_header**: Passes client information such as the original IP and protocol to the backend servers. + - **access_log** and **error_log**: Directs logs to specified files for easier monitoring and troubleshooting. + +--- + +## Step 2: Enable the Load Balancer Configuration + +1. **Create a symbolic link** to `sites-enabled` to activate the load balancer configuration in Nginx: + ```bash + sudo ln -s /etc/nginx/sites-available/load_balancer.conf /etc/nginx/sites-enabled/load_balancer.conf + ``` + +2. **Verify Nginx Configuration** + Test the Nginx configuration for syntax errors: + ```bash + sudo nginx -t + ``` + +3. **Reload Nginx** to apply the new configuration: + ```bash + sudo systemctl reload nginx + ``` + +--- + +## Step 3: Test the Load Balancer + +To ensure the load balancer is distributing traffic correctly, you can access the Nginx server’s IP address or domain name in your web browser: +``` +http://your-server-ip/ +``` + +You should see responses from the backend servers. Testing multiple times should show responses alternating between `10.10.10.1` and `10.10.10.2`, as Nginx forwards requests in a round-robin fashion. + +--- + +## Optional: Configure Additional Load Balancing Methods + +Nginx supports multiple load balancing algorithms, which you can specify within the `upstream` block: + +- **Round Robin (default)**: Distributes requests evenly across all servers. +- **Least Connections**: Directs traffic to the server with the fewest active connections. + ```nginx + upstream backend_servers { + least_conn; + server 10.10.10.1; + server 10.10.10.2; + } + ``` + +- **IP Hash**: Directs requests from the same client IP to the same backend server, which can help with session persistence. + ```nginx + upstream backend_servers { + ip_hash; + server 10.10.10.1; + server 10.10.10.2; + } + ``` + +--- + +## Troubleshooting Common Issues + +- **Error: 502 Bad Gateway** + - This error often means that the backend server is unreachable or down. Verify the IP addresses and ensure each backend server is running and accessible. + +- **Permission Denied for Log Files** + - Ensure the log file paths are writable by Nginx. Adjust permissions as needed: + ```bash + sudo chown www-data:www-data /var/log/nginx/load_balancer_access.log + ``` + +- **Configuration Errors** + - Always test configuration changes with `sudo nginx -t` before reloading or restarting Nginx. + From 35314b827f65dafd4611da431baabf0b3280e5f6 Mon Sep 17 00:00:00 2001 From: radinpirouz Date: Thu, 7 Nov 2024 16:06:11 +0330 Subject: [PATCH 16/16] Nginx : The Doc Is Ready --- .../nginx/django.md | 0 .../nginx/dotnet.md | 0 {Nginx => Web Servers & Reverse Proxies/Nginx}/1-Information.md | 0 {Nginx => Web Servers & Reverse Proxies/Nginx}/2-Installtion.md | 0 .../Nginx}/3-Setup-Web-Server.md | 0 .../Nginx}/4-Reverse-Proxy.md | 0 .../Nginx}/5-Load-Balancer.md | 2 +- 7 files changed, 1 insertion(+), 1 deletion(-) rename {Web Servers & Reverse Proxies => Web Development & Frameworks}/nginx/django.md (100%) rename {Web Servers & Reverse Proxies => Web Development & Frameworks}/nginx/dotnet.md (100%) rename {Nginx => Web Servers & Reverse Proxies/Nginx}/1-Information.md (100%) rename {Nginx => Web Servers & Reverse Proxies/Nginx}/2-Installtion.md (100%) rename {Nginx => Web Servers & Reverse Proxies/Nginx}/3-Setup-Web-Server.md (100%) rename {Nginx => Web Servers & Reverse Proxies/Nginx}/4-Reverse-Proxy.md (100%) rename {Nginx => Web Servers & Reverse Proxies/Nginx}/5-Load-Balancer.md (99%) diff --git a/Web Servers & Reverse Proxies/nginx/django.md b/Web Development & Frameworks/nginx/django.md similarity index 100% rename from Web Servers & Reverse Proxies/nginx/django.md rename to Web Development & Frameworks/nginx/django.md diff --git a/Web Servers & Reverse Proxies/nginx/dotnet.md b/Web Development & Frameworks/nginx/dotnet.md similarity index 100% rename from Web Servers & Reverse Proxies/nginx/dotnet.md rename to Web Development & Frameworks/nginx/dotnet.md diff --git a/Nginx/1-Information.md b/Web Servers & Reverse Proxies/Nginx/1-Information.md similarity index 100% rename from Nginx/1-Information.md rename to Web Servers & Reverse Proxies/Nginx/1-Information.md diff --git a/Nginx/2-Installtion.md b/Web Servers & Reverse Proxies/Nginx/2-Installtion.md similarity index 100% rename from Nginx/2-Installtion.md rename to Web Servers & Reverse Proxies/Nginx/2-Installtion.md diff --git a/Nginx/3-Setup-Web-Server.md b/Web Servers & Reverse Proxies/Nginx/3-Setup-Web-Server.md similarity index 100% rename from Nginx/3-Setup-Web-Server.md rename to Web Servers & Reverse Proxies/Nginx/3-Setup-Web-Server.md diff --git a/Nginx/4-Reverse-Proxy.md b/Web Servers & Reverse Proxies/Nginx/4-Reverse-Proxy.md similarity index 100% rename from Nginx/4-Reverse-Proxy.md rename to Web Servers & Reverse Proxies/Nginx/4-Reverse-Proxy.md diff --git a/Nginx/5-Load-Balancer.md b/Web Servers & Reverse Proxies/Nginx/5-Load-Balancer.md similarity index 99% rename from Nginx/5-Load-Balancer.md rename to Web Servers & Reverse Proxies/Nginx/5-Load-Balancer.md index e3e9fbb..0f67851 100644 --- a/Nginx/5-Load-Balancer.md +++ b/Web Servers & Reverse Proxies/Nginx/5-Load-Balancer.md @@ -71,7 +71,7 @@ Load balancing with Nginx helps distribute incoming traffic across multiple back 3. **Reload Nginx** to apply the new configuration: ```bash - sudo systemctl reload nginx + sudo nginx -s reload ``` ---