Monitoring: Add Stress-ng Doc

This commit is contained in:
2024-10-31 10:29:22 +03:30
parent eb9ed35417
commit ce88b657fe
7 changed files with 164 additions and 792 deletions

View File

@@ -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 <thread-number>
```
**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 <thread-number> --timeout <seconds>
```
**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 <percentage>
```
**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 <workers>
```
**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 <workers> --timeout <seconds>
```
**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 <workers> --vm-bytes <size>
```
**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 systems 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 <workers>
```
**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 <workers> --timeout <seconds>
```
**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 <seconds>
```
**Example:**
```bash
stress-ng --all --timeout 10s
```
This command runs a 10-second full system stress test, targeting all components that `stress-ng` supports.

View File

@@ -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`).

View File

@@ -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`).

View File

@@ -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.

View File

@@ -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: <Image>
resources:
limits:
memory: "128Mi"
cpu: "500m"
ports:
- containerPort: <Port>
```
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.

View File

@@ -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
```

View File

@@ -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).