This commit is contained in:
2024-07-16 23:00:08 +03:30
parent fd6d4ed944
commit 3ec6a06e16
3 changed files with 352 additions and 0 deletions

View File

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

View File

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

View File

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