Rework Dirs

This commit is contained in:
2024-09-01 18:34:55 +03:30
parent 84196c3034
commit 8c9be226a5
51 changed files with 0 additions and 92 deletions

View File

@@ -0,0 +1,137 @@
# Docker Swarm Documentation
## Manager Count
To ensure the cluster remains functional, the number of manager nodes must be more than 50%. If it falls below 51%, the cluster will become non-operational.
## Example: Setting Up Nginx
**Workflow:** API → Allocator → Dispatcher → Scheduler
## Commands
### Cluster Initialization and Management
- **Create Cluster**
```bash
docker swarm init
```
Initializes a new Docker Swarm cluster.
- **Join Cluster**
```bash
docker swarm join
```
Joins a node to an existing Docker Swarm cluster.
- **Create Cluster with Specific Interface**
```bash
docker swarm init --advertise-addr <ip or interface name>
```
Initializes a new Docker Swarm cluster, specifying the IP or interface name to advertise.
- **Leave Cluster**
```bash
docker swarm leave
```
Removes a node from the Docker Swarm cluster.
- **Unlock Locked Manager**
```bash
docker swarm unlock
```
Unlocks a locked manager node in the Docker Swarm cluster.
### Node Management
- **List Nodes**
```bash
docker node ls
```
Displays information about the nodes in the cluster.
**Example Output:**
```bash
docker node ls
```
```
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
8yw8jrjeqczaci0qkuy060g09 * docker-1 Ready Active Leader 24.0.5
v4gvf7xenw0izmxgvhr6hb2rj docker-2 Ready Active 24.0.5
kd3ujmt1ey3pw6v9189fouxfa docker-3 Ready Active Reachable 24.0.5
tm1msy58ztcltt36rs1lb76p7 docker-3 Down Active 24.0.5
```
- **Remove Node**
```bash
docker node rm <node-id>
```
Removes a node from the cluster.
**Example:**
```bash
docker node rm tm1msy58ztcltt36rs1lb76p7
```
- **Promote Node to Manager**
```bash
docker node promote <hostname or ID>
```
Promotes a worker node to a manager node.
**Example:**
```bash
root@docker-1:~# docker node promote v4gvf7xenw0izmxgvhr6hb2rj
Node v4gvf7xenw0izmxgvhr6hb2rj promoted to a manager in the swarm.
```
### Service Management
- **Show Task Status on Cluster**
```bash
docker node ps
```
Displays the status of tasks running on nodes in the cluster.
- **List Services**
```bash
docker service ls
```
Lists all services running in the cluster.
- **Create Service**
```bash
docker service create --name <service-name> <image-name>
```
Creates a new service with the specified name and image.
- **Scale Service**
```bash
docker service scale <service-name>=<replica-count>
```
Scales the number of replicas for a service.
**Example:**
```bash
docker service scale nginx=5
```
Scales the `nginx` service to 5 replicas.
- **Inspect Service**
```bash
docker service inspect <service-name>
```
Displays detailed information about a service.
- **Create Service with Replicas and Environment Variables**
```bash
docker service create --name <service-name> --replicas <replica-count> --env <env-variable> <image-name>
```
Creates a new service with the specified name, number of replicas, and environment variables.
**Example:**
```bash
docker service create --name nginx --replicas 3 --env MY_ENV_VAR=value nginx
```
Creates an `nginx` service with 3 replicas and an environment variable `MY_ENV_VAR` set to `value`.

View File

@@ -0,0 +1,78 @@
# Docker Commands and Concepts
## Docker Concepts
- **Stateless**: Do not save your data (like Nginx).
- **Stateful**: Save your data.
## Docker Data Directory
- `/var/lib/docker/`: Docker data directory.
- `/var/lib/docker/containers/`: Container configuration and file directory.
- `/var/lib/docker/volumes`: Directory where docker volumes are saved.
## Docker Installtion (Ubuntu)
```bash
apt update && apt install ca-certificates curl && install -m 0755 -d /etc/apt/keyrings && curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc && chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
apt update && apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
```
## Docker CLI Commands
### Authentication
- `docker login`: Login to Docker Hub with CLI.
### Image Management
- `docker pull <repo-addr>`: Pull a Docker image.
- `docker images`: Show pulled images.
- `docker rmi -f <image-id>`: Remove an image.
- `docker save -o <file-location-and-name> <image-name>`: Save image as an external file.
- `docker load -i <file-location>`: Import a Docker image.
### Container Management
- `docker run <options> <img-name>`: Run a Docker container.
- `docker run`: Run Docker (after run exited).
- `docker run -it`: Run and give bash to me.
- `docker run -dit`: Run and give bash to me and run in the background.
- `docker exec -it <container-name>`: Go to container shell.
- `docker rm <container-name>`: Remove Docker container.
- `docker stop <container-name>`: Stop Docker container.
- `docker rm -f <container-name>`: Stop and remove Docker container.
- `docker ps -aq`: Give all Docker container IDs.
- `docker ps -aq -f status=exited`: Give all Docker container IDs with exited status.
- `docker container prune`: Remove all stopped containers.
- `docker commit <container-name> <new-name>`: Make a custom Docker image.
- `docker inspect <container-name>`: Get all data about container information.
- `docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container-name>`: Get IP of the container.
- `docker cp <file_on_local> <container-name>:/<location>`: Copy from local to container.
- `docker cp <container-name>:/<location> <local-location>`: Copy from container to local.
- `docker stats`: Monitor Docker stats.
- `docker run -dit --name server --restart=always ubuntu`: Run container again after restarting the service or main server.
- `docker build -t <appname>:<appver> <location-of-docker-file>`: Build Docker image from a Dockerfile.
### Volume Management
- `docker volume ls`: List all volumes.
- `docker volume create <name-of-volume>`: Create a volume for Docker.
- `docker volume inspect <vol-name>`: Give information about the volume.
- `docker run -dit --name <container-name> -v <volume-name>:<location-in-container> <img-name>`: Run Docker image and save target location data in volume.
### Network Management
- `docker network ls`: List all Docker networks.
- `docker network create <network-name>`: Create a Docker network.
- `docker network create --subnet <ip>/<subnet> --gateway <gateway-ip> --driver=<network-type> <net-name>`: Create network with custom settings.
- `docker run -dit --name <container-name> --network <network-name> <img-name>`: Run a container and connect it to a custom network.
- `docker network connect <network-name> <container-name>`: Connect a container to a custom network.
- `docker network disconnect <network-name> <container-name>`: Disconnect a network from a container.

View File

@@ -0,0 +1,27 @@
# Docker Information
## What is Docker?
[Docker](https://www.docker.com/) is a platform for developing, shipping, and running applications in containers. Containers are lightweight, portable, and self-sufficient environments that contain everything needed to run an application, including the code, runtime, libraries, and dependencies. Docker provides a way to package and distribute applications as containers, allowing them to run consistently across different environments.
## Key Concepts
### Images
An image is a read-only template used to create containers. It contains the application code, runtime, libraries, and dependencies needed to run the application. Images are built using a Dockerfile, which specifies the instructions for creating the image.
### Containers
A container is a runnable instance of an image. It encapsulates the application and its dependencies, providing isolation from the host system and other containers. Containers are lightweight, portable, and can be easily moved between different environments.
### Dockerfile
A Dockerfile is a text file that contains the instructions for building a Docker image. It specifies the base image, environment variables, dependencies, and commands needed to set up the application environment. Dockerfiles allow developers to automate the process of building images and ensure consistency across environments.
### Docker Hub
[Docker Hub](https://hub.docker.com/) is a cloud-based registry service that hosts Docker images. It provides a centralized repository for sharing, storing, and managing Docker images. Docker Hub allows developers to pull pre-built images from public repositories or publish their own images for others to use.
## Conclusion
Docker simplifies the process of developing, shipping, and running applications by providing a consistent environment across different platforms. It enables developers to package applications as containers, making them portable, scalable, and easy to deploy. With its rich ecosystem of tools and services, Docker has become a key technology in modern software development workflows.

View File

@@ -0,0 +1,102 @@
# 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 <namespace-name>
```
### 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 <name-space>
```
### Running a Pod
- **Run a new pod:**
```bash
kubectl run <pod-name> <switch> {
--image=<image-name>, # Specifies the container image to use
--port=<portnumber>, # Specifies the port that the container exposes
-n <namespace-name>, # 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=<number>, # 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=<Always|OnFailure|Never>, # Determines the restart policy for the pod
--overrides='<json>', # Provides a JSON override for the generated object
--image-pull-policy=<policy>, # Specifies the image pull policy (Always, IfNotPresent, Never)
--limits=cpu=<cpu>,memory=<memory>, # Specifies resource limits for the container
--requests=cpu=<cpu>,memory=<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 <namespace-name> <pod-name>
```
### API Resource Documentation
- **Get documentation of an API resource:**
```bash
kubectl explain <api-resource-name>
```
- *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 <namespace-name> <podname>
```
- **Get logs and state information of a pod (works at any time):**
```bash
kubectl describe pod -n <namespace-name> <podname>
```
### Apply Yaml File
```bash
kubectl apply -f <yaml-file> -n <namespace-name>
```

View File

@@ -0,0 +1,25 @@
# Kubernetes (Kuber) Documentation
## Control Plane (CP)
- **CP (Control Plane)**: The central management entity of the Kubernetes cluster.
- By default, the manager in Kubernetes does not directly handle any containers.
## Kubernetes Manager Components
- **Control Manager**
- **Scheduler**
- **API Server**
- **etcd (Database)**
- **Kubelet**
## Kubernetes Worker Components
- **Kube Proxy**
- **Kubelet**
## Data Flow
- **Kube Proxy** and **Kubelet** communicate with the Kubernetes Manager for data handling.
## Administration Tools
- **kubeadm**: Used for administration commands.
- **kubectl**: Used to manage nodes and services.
---

View File

@@ -0,0 +1,67 @@
---
# Containerd and Kubernetes Installation Guide
## 1. Disable Swap
Turn off swap and disable it permanently.
```bash
swapoff -a
sed -i '/swap/d' /etc/fstab
```
## 2. Enable Required Kernel Modules
Create a configuration file to load necessary kernel modules and load them temporarily.
```bash
echo -e "overlay\nbr_netfilter" | sudo tee /etc/modules-load.d/containerd.conf
sudo modprobe overlay
sudo modprobe br_netfilter
```
## 3. Enable IPv4 Forwarding
Enable IPv4 forwarding in the sysctl configuration and apply the changes.
```bash
sed -i 's/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/' /etc/sysctl.conf
sysctl -p
```
## 4. Configure Containerd
Generate the default configuration for Containerd and modify it to use systemd as the cgroup driver.
```bash
containerd config default | sudo tee /etc/containerd/config.toml
sudo sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml
```
## 5. Install Kubernetes
Add the Kubernetes package repository and install the required packages.
```bash
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.30/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.30/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
```
## 6. Enable kubelet
Enable and start the kubelet service.
```bash
sudo systemctl enable --now kubelet
```
## 7. Initialize the Kubernetes Cluster
Initialize the Kubernetes control plane with the specified parameters.
```bash
sudo kubeadm init --control-plane-endpoint 192.168.2.100 --apiserver-advertise-address 192.168.2.100 --pod-network-cidr 10.244.0.0/16 | tee kuber-install.log
```
## 8. Create Control Plane Join Command
Create the control plane join command and save it for later use.
```bash
sudo kubeadm init phase upload-certs --upload-certs
Copy the output certificate key and run the following command, replacing <CERTIFICATE_KEY> with the copied key.
sudo kubeadm token create --certificate-key <CERTIFICATE_KEY> --print-join-command | tee cp-command.txt
```
## 9. Join Control Plane and Worker Nodes
Use the command from cp-command.txt on your control plane nodes to join them. Additionally, get the join command for worker nodes from kuber-install.log and run it on each worker node.
---
This revised guide provides clear, step-by-step instructions, making it easier to follow and ensuring all necessary actions are covered.

View File

@@ -0,0 +1,208 @@
# Kubernetes YAML Files
This document provides explanations and details for various Kubernetes YAML configurations, describing how different Kubernetes objects such as Namespaces, Pods, and other specifications are defined and utilized. The examples cover creating namespaces, deploying pods, setting resource limits, and using node selectors.
## Namespace Definition
```yaml
apiVersion: v1
kind: Namespace
metadata:
name: my-ns
```
- **apiVersion**: Specifies the version of the Kubernetes API.
- **kind**: Defines the type of Kubernetes object, here it's a `Namespace`.
- **metadata**: Contains data that helps uniquely identify the object, including a `name`.
This YAML file creates a namespace named `my-ns` which isolates a group of resources within Kubernetes.
## Pod Definitions
### Nginx Pod
```yaml
apiVersion: v1
kind: Pod
metadata:
namespace: my-ns
name: nginx-pod
labels:
app: app1
zone: staging
version: v1.0.1
app.kubernetes.io/product: nginx-pod
spec:
containers:
- name: naginx-container
image: nginx:latest
ports:
- containerPort: 80
```
- **metadata.namespace**: Specifies the namespace the pod belongs to (`my-ns`).
- **metadata.name**: The name of the pod (`nginx-pod`).
- **metadata.labels**: Key-value pairs for organizing and selecting resources.
- **spec.containers**: Specifies the containers within the pod. Each container has:
- **name**: Container name.
- **image**: The Docker image to run (`nginx:latest`).
- **ports**: List of ports to expose from the container (`containerPort: 80`).
This file defines a pod named `nginx-pod` running the latest Nginx container in the `my-ns` namespace.
### Test Pod 1
```yaml
apiVersion: v1
kind: Pod
metadata:
namespace: my-ns
name: testpod1
spec:
containers:
- name: c00
image: ubuntu
command: ["/bin/bash", "-c", "while true; do echo Hello-Coder; sleep 5 ; done"]
- name: c01
image: ubuntu
command: ["/bin/bash", "-c", "while true; do echo Hello-Programmer; sleep 5 ; done"]
```
- **spec.containers.command**: Overrides the default command for the container, in this case, running a looped bash script that prints a message every 5 seconds.
This defines a pod named `testpod1` with two Ubuntu containers in the `my-ns` namespace, each running a different command.
## Pod with Resource Requests and Limits
```yaml
apiVersion: v1
kind: Pod
metadata:
namespace: my-ns
name: testpod1
spec:
containers:
- name: c00
image: ubuntu
command:
- /bin/bash
- -c
- while true; do echo Hello-Coder; sleep 5 ; done
- name: c01
image: ubuntu
command:
- /bin/bash
- -c
- while true; do echo Hello-Programmer; sleep 5 ; done
resources:
limits:
memory: "128Mi"
cpu: "500m"
requests:
memory: "64Mi"
cpu: "250m"
```
- **resources.limits**: Specifies the maximum amount of resources a container can use.
- **resources.requests**: Specifies the amount of resources a container is guaranteed.
This pod configuration defines resource limits and requests for the containers to ensure they do not exceed specific memory and CPU usage.
## Pod with NodeSelector
```yaml
apiVersion: v1
kind: Pod
metadata:
namespace: my-ns
name: testpod3
spec:
containers:
- name: c00
image: ubuntu
command:
- /bin/bash
- -c
- while true; do echo Hello-Coder; sleep 5 ; done
- name: c01
image: ubuntu
command:
- /bin/bash
- -c
- while true; do echo Hello-Programmer; sleep 5 ; done
resources:
limits:
memory: "128Mi"
cpu: "500m"
requests:
memory: "64Mi"
cpu: "250m"
nodeSelector:
kubernetes.io/hostname: k8s2
kubernetes.io/disk: ssd
```
- **nodeSelector**: Ensures the pod is scheduled on nodes with the specified labels (`kubernetes.io/hostname: k8s2` and `kubernetes.io/disk: ssd`).
This configuration places the pod on specific nodes that match the given labels.
## Simple Pod Templates
### Basic Pod
```yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp
labels:
name: myapp
spec:
containers:
- name: myapp
image: <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.
## Useful Kubernetes Commands
### View Pod Details
```bash
kubectl get pod -n my-ns <pod-name> -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 <node-name> kubernetes.io/<var-name>=<var-value>
kubectl get nodes --show-labels
```