Tech-Notes

Kubernetes

  1. Kubernetes Overview
  2. Key Features
  3. Components of Kubernetes
  4. Key Kubernetes Concepts
  5. Kubernetes Installation Requirements
  6. Kubectl Commands
  7. YAML Configuration

Kubernetes Overview

open source container orchestration tool for manageing microservices or contanierized application

Key featues

  1. Resilient Infrastructure: Ensures availability through automatic scaling, health checks, failover.
  2. Zero-Downtime Deployment: smooth rollouts and automatic rollbacks during updates.
  3. Self-Healing: Automatically handles tasks like container placement, restarts, replication, and scaling based on metrics.

Components of k8s:

Master Components:

Component Description
kube-apiserver Gateway for all requests, responsible for authentication and communication.
etcd storage Key-value store that holds cluster state information.
kube-controller-manager Monitors the cluster state and ensures the desired state is maintained.
cloud-controller-manager Manages cloud-specific operations (like load balancing, storage) in cloud environments.
kube-scheduler Decides on which node a new pod should be scheduled based on resource availability.

Node Components:

Term Description
kubelet An agent that runs on each node, ensuring containers are running.
kube-proxy Handles network routing and forwarding requests between containers and services.

Additional Concepts:

Term Description
Minikube A single-node Kubernetes cluster used for local development and testing.
Kubectl The command-line interface (CLI) tool used to interact with Kubernetes clusters.

Key Kubernetes Concepts:

Component Description
Pod Basic unit with containers.
Service Exposes and balances traffic.
ConfigMap Stores configuration data for pods.
Secret Stores sensitive, encrypted data.
Deployment Manages pod scaling and updates.
ReplicaSet Ensures specified pod replicas are running.
StatefulSet Manages stateful applications with stable storage.
PersistentVolume (PV) Provides persistent storage.
PersistentVolumeClaim (PVC) Requests and binds to PV storage.
Ingress Manages external access and balancing.
Helm Kubernetes package manager.
Horizontal Pod Autoscaler (HPA) Auto-scales pods based on usage.
DaemonSet Runs a pod on every node.
Job Runs pods until completion.
CronJob Schedules jobs at intervals.
Role-Based Access Control (RBAC) Manages cluster permissions and roles.
Namespaces Divides cluster resources among teams.

Kubernetes Installation Requirements:

  1. Container Runtime: Like Docker, responsible for running containers.
  2. Kubelet: Communicates with the Kubernetes control plane, manages the container lifecycle on the node.
  3. Kube Proxy: Manages network routing and forwarding traffic between services.

Kubectl commands:

Command Description
kubectl get pod Lists all pods in the current namespace.
kubectl get nodes Lists all nodes in the Kubernetes cluster.
kubectl get deployment Lists all deployments in the current namespace.
kubectl create deployment nginx-depl --image=nginx Creates a new deployment with the specified image.
kubectl get replicaset Lists all ReplicaSets in the current namespace.
kubectl edit deployment [deploymentName] Opens the auto-generated YAML configuration file of the deployment for editing.
kubectl logs [podName] Fetches the logs of the specified pod.
kubectl describe pod [podName] Shows detailed information about a pod, including events and status changes.
kubectl exec -it [podName] -- /bin/bash Opens an interactive shell session inside the container running in the pod.
kubectl delete deployment [deploymentName] Deletes the specified deployment.
kubectl apply -f [yaml fileName] Applies a YAML configuration file to create/update resources.
kubectl delete -f [yaml fileName] Deletes resources defined in the YAML configuration file.

Yaml Configuration:

1. Pod Configuration:

apiVersion: v1
kind: Pod  
metadata:
  name: nginx  
  labels:
    name: nginx
spec:
  containers:
  - name: nginx
    image: nginx  
    ports:
    - containerPort: 80
Explanation:

Summary: This YAML file defines a Pod named nginx that runs an Nginx web server container, exposing port 80.

2. Service Configuration:

apiVersion: v1
kind: Service  
metadata:
  name: my-nginx  
  labels:
    run: my-nginx
spec:
  ports:
  - port: 80
    protocol: TCP  
  selector:
    run: my-nginx
Explanation:

Summary: This YAML file defines a Service that exposes port 80 using TCP and directs traffic to Pods with the label run: my-nginx. It acts as a load balancer or a gateway for external traffic to reach the Pods.

3. Deployment Configuration:

apiVersion: apps/v1
kind: Deployment  
metadata:
  name: nginx-deployment  
  labels:
    run: my-nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      run: my-nginx
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx
        ports:
        - containerPort: 80

Explanation:

Summary: This YAML file defines a Deployment that will ensure 2 replicas (copies) of a Pod running an Nginx web server are always up and running. It also allows for easier updates and scaling of Pods.