kubernetes doc
This commit is contained in:
@@ -0,0 +1,189 @@
|
||||
### **1st Document: Namespace**
|
||||
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
```
|
||||
|
||||
* Specifies the API version used. Here, it's version 1 of the core Kubernetes API.
|
||||
|
||||
```yaml
|
||||
kind: Namespace
|
||||
```
|
||||
|
||||
* Declares the resource type. This is a **Namespace**, which logically isolates groups of resources.
|
||||
|
||||
```yaml
|
||||
metadata:
|
||||
name: ns
|
||||
```
|
||||
|
||||
* Metadata block.
|
||||
|
||||
* `name: ns` sets the name of the namespace to `ns`.
|
||||
|
||||
---
|
||||
|
||||
### **2nd Document: Service**
|
||||
|
||||
```yaml
|
||||
---
|
||||
```
|
||||
|
||||
* Separates multiple documents in the YAML file.
|
||||
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
```
|
||||
|
||||
* Uses the core v1 API again.
|
||||
|
||||
```yaml
|
||||
kind: Service
|
||||
```
|
||||
|
||||
* Declares a **Service** resource, which provides stable networking to access pods.
|
||||
|
||||
```yaml
|
||||
metadata:
|
||||
name: nginx-service
|
||||
namespace: ns
|
||||
labels:
|
||||
app: nginx
|
||||
```
|
||||
|
||||
* Metadata block:
|
||||
|
||||
* `name: nginx-service`: name of the Service.
|
||||
* `namespace: ns`: places this service in the previously created `ns` namespace.
|
||||
* `labels`: key-value pairs used for organizing and selecting resources. Here, `app: nginx`.
|
||||
|
||||
```yaml
|
||||
spec:
|
||||
type: ClusterIP
|
||||
```
|
||||
|
||||
* `spec` describes the behavior.
|
||||
|
||||
* `type: ClusterIP`: exposes the service internally within the cluster using a virtual IP.
|
||||
|
||||
```yaml
|
||||
selector:
|
||||
app: nginx
|
||||
```
|
||||
|
||||
* This selects pods with the label `app: nginx` to receive traffic from this service.
|
||||
|
||||
```yaml
|
||||
ports:
|
||||
- name: http
|
||||
port: 80
|
||||
targetPort: 8080
|
||||
```
|
||||
|
||||
* Defines port configuration:
|
||||
|
||||
* `name: http`: a name for the port (optional but useful for readability).
|
||||
* `port: 80`: the port that the service exposes internally.
|
||||
* `targetPort: 8080`: the port on the pod that receives the traffic.
|
||||
|
||||
---
|
||||
|
||||
### **3rd Document: Deployment**
|
||||
|
||||
```yaml
|
||||
---
|
||||
```
|
||||
|
||||
* Separates from the previous document.
|
||||
|
||||
```yaml
|
||||
apiVersion: apps/v1
|
||||
```
|
||||
|
||||
* Uses the `apps/v1` API group, suitable for deployments and other controllers.
|
||||
|
||||
```yaml
|
||||
kind: Deployment
|
||||
```
|
||||
|
||||
* Declares a **Deployment**, which ensures a specified number of pod replicas are running.
|
||||
|
||||
```yaml
|
||||
metadata:
|
||||
name: nginx-deployment
|
||||
namespace: ns
|
||||
labels:
|
||||
app: nginx
|
||||
```
|
||||
|
||||
* Metadata block:
|
||||
|
||||
* `name: nginx-deployment`: name of the deployment.
|
||||
* `namespace: ns`: places this in the `ns` namespace.
|
||||
* `labels: app: nginx`: used for matching with selectors.
|
||||
|
||||
```yaml
|
||||
spec:
|
||||
replicas: 2
|
||||
```
|
||||
|
||||
* Desired number of pod replicas to run: 2.
|
||||
|
||||
```yaml
|
||||
selector:
|
||||
matchLabels:
|
||||
app: nginx
|
||||
```
|
||||
|
||||
* Tells the deployment which pods to manage, by matching labels (`app: nginx`).
|
||||
|
||||
```yaml
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx
|
||||
```
|
||||
|
||||
* Template for creating new pods:
|
||||
|
||||
* Each pod created will have `app: nginx` label.
|
||||
|
||||
```yaml
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx:latest
|
||||
```
|
||||
|
||||
* Pod spec:
|
||||
|
||||
* One container named `nginx`, using the latest official Nginx image.
|
||||
|
||||
```yaml
|
||||
ports:
|
||||
- containerPort: 8080
|
||||
```
|
||||
|
||||
* The container exposes port 8080 (must match the `targetPort` in the Service).
|
||||
|
||||
```yaml
|
||||
resources:
|
||||
requests:
|
||||
cpu: "100m"
|
||||
memory: "128Mi"
|
||||
limits:
|
||||
cpu: "250m"
|
||||
memory: "256Mi"
|
||||
```
|
||||
|
||||
* **Resource management**:
|
||||
|
||||
* `requests`: the minimum guaranteed resources.
|
||||
|
||||
* `cpu: 100m` = 0.1 CPU core.
|
||||
* `memory: 128Mi` = 128 MiB RAM.
|
||||
* `limits`: the maximum resources the container can use.
|
||||
|
||||
* `cpu: 250m` = 0.25 CPU core.
|
||||
* `memory: 256Mi` = 256 MiB RAM.
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
# 🔗 Services in Kubernetes (SVC)
|
||||
|
||||
A **Service** in Kubernetes provides a stable networking interface to access a set of pods. It allows for decoupling between client applications and the underlying pods by using DNS names and selectors.
|
||||
|
||||
---
|
||||
|
||||
## 🌐 Service Basics
|
||||
|
||||
### 📌 Service Flow
|
||||
```
|
||||
|
||||
Service ➡️ Endpoint ➡️ Pods
|
||||
|
||||
````
|
||||
|
||||
- Services abstract access to a group of pods.
|
||||
- Services automatically get a DNS name in the cluster.
|
||||
- They use selectors to route traffic to matching pods.
|
||||
|
||||
---
|
||||
|
||||
## 🧭 Service Types
|
||||
|
||||
1. **ClusterIP** (default)
|
||||
- Accessible only within the cluster.
|
||||
|
||||
2. **NodePort**
|
||||
- Exposes the service on a static port on each node.
|
||||
|
||||
3. **LoadBalancer**
|
||||
- Provisions an external IP via a cloud provider to expose the service.
|
||||
|
||||
---
|
||||
|
||||
## 🧪 Useful Commands
|
||||
|
||||
### 🔍 Get Endpoints
|
||||
```bash
|
||||
kubectl get ep -n <namespace>
|
||||
````
|
||||
|
||||
### 📄 Get Services
|
||||
|
||||
```bash
|
||||
kubectl get svc -n <namespace>
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🧾 Example Service Manifest
|
||||
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: nginx
|
||||
namespace: ns
|
||||
labels:
|
||||
app: web-server
|
||||
spec:
|
||||
type: ClusterIP # Options: ClusterIP, NodePort, LoadBalancer
|
||||
selector:
|
||||
app: nginx
|
||||
ports:
|
||||
- name: http # Port name is optional but useful
|
||||
port: 80 # Service port
|
||||
targetPort: 8080 # Container port
|
||||
```
|
||||
|
||||
> 🔍 **Note:** The `selector` must match pod labels for the service to route traffic correctly.
|
||||
> 🧠 **Tip:** Use `kubectl describe svc <svc-name>` to troubleshoot or verify service-to-pod connectivity.
|
||||
> 🌐 Services are resolved by DNS using the format: `<service-name>.<namespace>.svc.cluster.local`.
|
||||
|
||||
@@ -1,208 +0,0 @@
|
||||
# Kubernetes YAML Files
|
||||
|
||||
This document provides explanations and details for various Kubernetes YAML configurations, describing how different Kubernetes objects such as Namespaces, Pods, and other specifications are defined and utilized. The examples cover creating namespaces, deploying pods, setting resource limits, and using node selectors.
|
||||
|
||||
## Namespace Definition
|
||||
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
kind: Namespace
|
||||
metadata:
|
||||
name: my-ns
|
||||
```
|
||||
|
||||
- **apiVersion**: Specifies the version of the Kubernetes API.
|
||||
- **kind**: Defines the type of Kubernetes object, here it's a `Namespace`.
|
||||
- **metadata**: Contains data that helps uniquely identify the object, including a `name`.
|
||||
|
||||
This YAML file creates a namespace named `my-ns` which isolates a group of resources within Kubernetes.
|
||||
|
||||
## Pod Definitions
|
||||
|
||||
### Nginx Pod
|
||||
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
namespace: my-ns
|
||||
name: nginx-pod
|
||||
labels:
|
||||
app: app1
|
||||
zone: staging
|
||||
version: v1.0.1
|
||||
app.kubernetes.io/product: nginx-pod
|
||||
spec:
|
||||
containers:
|
||||
- name: naginx-container
|
||||
image: nginx:latest
|
||||
ports:
|
||||
- containerPort: 80
|
||||
```
|
||||
|
||||
- **metadata.namespace**: Specifies the namespace the pod belongs to (`my-ns`).
|
||||
- **metadata.name**: The name of the pod (`nginx-pod`).
|
||||
- **metadata.labels**: Key-value pairs for organizing and selecting resources.
|
||||
- **spec.containers**: Specifies the containers within the pod. Each container has:
|
||||
- **name**: Container name.
|
||||
- **image**: The Docker image to run (`nginx:latest`).
|
||||
- **ports**: List of ports to expose from the container (`containerPort: 80`).
|
||||
|
||||
This file defines a pod named `nginx-pod` running the latest Nginx container in the `my-ns` namespace.
|
||||
|
||||
### Test Pod 1
|
||||
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
namespace: my-ns
|
||||
name: testpod1
|
||||
spec:
|
||||
containers:
|
||||
- name: c00
|
||||
image: ubuntu
|
||||
command: ["/bin/bash", "-c", "while true; do echo Hello-Coder; sleep 5 ; done"]
|
||||
- name: c01
|
||||
image: ubuntu
|
||||
command: ["/bin/bash", "-c", "while true; do echo Hello-Programmer; sleep 5 ; done"]
|
||||
```
|
||||
|
||||
- **spec.containers.command**: Overrides the default command for the container, in this case, running a looped bash script that prints a message every 5 seconds.
|
||||
|
||||
This defines a pod named `testpod1` with two Ubuntu containers in the `my-ns` namespace, each running a different command.
|
||||
|
||||
## Pod with Resource Requests and Limits
|
||||
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
namespace: my-ns
|
||||
name: testpod1
|
||||
spec:
|
||||
containers:
|
||||
- name: c00
|
||||
image: ubuntu
|
||||
command:
|
||||
- /bin/bash
|
||||
- -c
|
||||
- while true; do echo Hello-Coder; sleep 5 ; done
|
||||
- name: c01
|
||||
image: ubuntu
|
||||
command:
|
||||
- /bin/bash
|
||||
- -c
|
||||
- while true; do echo Hello-Programmer; sleep 5 ; done
|
||||
resources:
|
||||
limits:
|
||||
memory: "128Mi"
|
||||
cpu: "500m"
|
||||
requests:
|
||||
memory: "64Mi"
|
||||
cpu: "250m"
|
||||
```
|
||||
|
||||
- **resources.limits**: Specifies the maximum amount of resources a container can use.
|
||||
- **resources.requests**: Specifies the amount of resources a container is guaranteed.
|
||||
|
||||
This pod configuration defines resource limits and requests for the containers to ensure they do not exceed specific memory and CPU usage.
|
||||
|
||||
## Pod with NodeSelector
|
||||
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
namespace: my-ns
|
||||
name: testpod3
|
||||
spec:
|
||||
containers:
|
||||
- name: c00
|
||||
image: ubuntu
|
||||
command:
|
||||
- /bin/bash
|
||||
- -c
|
||||
- while true; do echo Hello-Coder; sleep 5 ; done
|
||||
- name: c01
|
||||
image: ubuntu
|
||||
command:
|
||||
- /bin/bash
|
||||
- -c
|
||||
- while true; do echo Hello-Programmer; sleep 5 ; done
|
||||
resources:
|
||||
limits:
|
||||
memory: "128Mi"
|
||||
cpu: "500m"
|
||||
requests:
|
||||
memory: "64Mi"
|
||||
cpu: "250m"
|
||||
nodeSelector:
|
||||
kubernetes.io/hostname: k8s2
|
||||
kubernetes.io/disk: ssd
|
||||
```
|
||||
|
||||
- **nodeSelector**: Ensures the pod is scheduled on nodes with the specified labels (`kubernetes.io/hostname: k8s2` and `kubernetes.io/disk: ssd`).
|
||||
|
||||
This configuration places the pod on specific nodes that match the given labels.
|
||||
|
||||
## Simple Pod Templates
|
||||
|
||||
### Basic Pod
|
||||
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: myapp
|
||||
labels:
|
||||
name: myapp
|
||||
spec:
|
||||
containers:
|
||||
- name: myapp
|
||||
image: <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
|
||||
```
|
||||
@@ -1,23 +0,0 @@
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: app-deployment
|
||||
namespace: ns-test
|
||||
labels:
|
||||
name: deployment-test
|
||||
app: nginx
|
||||
spec:
|
||||
replicas: 2
|
||||
selector:
|
||||
matchLabels:
|
||||
name: app1
|
||||
app: nginx
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
name: app1
|
||||
app: nginx
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx-deployment
|
||||
image: nginx:1.26
|
||||
@@ -0,0 +1,52 @@
|
||||
apiVersion: v1
|
||||
kind: Namespace
|
||||
metadata:
|
||||
name: ns
|
||||
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: nginx-service
|
||||
namespace: ns
|
||||
labels:
|
||||
app: nginx
|
||||
spec:
|
||||
type: ClusterIP
|
||||
selector:
|
||||
app: nginx
|
||||
ports:
|
||||
- name: http
|
||||
port: 80
|
||||
targetPort: 8080
|
||||
|
||||
---
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: nginx-deployment
|
||||
namespace: ns
|
||||
labels:
|
||||
app: nginx
|
||||
spec:
|
||||
replicas: 2
|
||||
selector:
|
||||
matchLabels:
|
||||
app: nginx
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx:latest
|
||||
ports:
|
||||
- containerPort: 8080
|
||||
resources:
|
||||
requests:
|
||||
cpu: "100m"
|
||||
memory: "128Mi"
|
||||
limits:
|
||||
cpu: "250m"
|
||||
memory: "256Mi"
|
||||
Reference in New Issue
Block a user