# Kubernetes (K8s) Basics

Kubernetes (often abbreviated as K8s) is an open-source system for automating deployment, scaling, and management of containerized applications. It groups containers that make up an application into logical units for easy management and discovery.

## 1. Why Kubernetes?

If Docker is a single musician playing an instrument, Kubernetes is the conductor of the orchestra.
*   **Scalability:** Automatically scale your application up or down based on demand.
*   **High Availability:** If a container crashes, K8s restarts it. If a server dies, K8s moves the containers to another server.
*   **Service Discovery:** Automatically exposes containers to the network.

## 2. Core Architecture

### Cluster
A Kubernetes cluster consists of a set of worker machines, called **Nodes**, that run containerized applications. Every cluster has at least one worker node.

### Control Plane (Master Node)
The brain of the cluster. It makes global decisions (e.g., scheduling) and detects/responds to cluster events.
*   **API Server:** The front end of the control plane.
*   **etcd:** Consistent and highly-available key value store for all cluster data.
*   **Scheduler:** Watches for newly created Pods with no assigned node, and selects a node for them to run on.

### Worker Node
A physical or virtual machine that runs applications.
*   **Kubelet:** An agent that runs on each node. It ensures that containers are running in a Pod.
*   **Kube-proxy:** Maintains network rules on nodes.

## 3. Key Objects (Resources)

### Pod
The smallest deployable unit in Kubernetes. A Pod represents a single instance of a running process in your cluster.
*   Usually contains one container (e.g., your Node.js app).
*   Can contain multiple tightly-coupled containers (e.g., a sidecar logger).
*   Pods are ephemeral (they die and are replaced).

### Deployment
A Deployment provides declarative updates for Pods and ReplicaSets.
*   You describe a *desired state* in a Deployment, and the Deployment Controller changes the actual state to the desired state at a controlled rate.
*   **Use Case:** "I want 3 replicas of my `nginx` app running."

### Service
An abstract way to expose an application running on a set of Pods as a network service.
*   Since Pods die and get new IPs, a Service provides a stable IP address and DNS name.
*   **Types:** ClusterIP (Internal only), NodePort (Expose on static port), LoadBalancer (Expose via Cloud LB).

### Ingress
An API object that manages external access to the services in a cluster, typically HTTP. It can provide load balancing, SSL termination and name-based virtual hosting.

## 4. Configuration

*   **ConfigMap:** Stores non-confidential data in key-value pairs.
*   **Secret:** Stores confidential data (passwords, OAuth tokens, SSH keys).

## 5. Basic Commands (`kubectl`)

`kubectl` is the command-line tool for communicating with the cluster API.

*   `kubectl get pods`: List all pods.
*   `kubectl get services`: List all services.
*   `kubectl apply -f deployment.yaml`: Create or update resources defined in a YAML file.
*   `kubectl logs <pod-name>`: Print the logs for a container in a pod.
*   `kubectl exec -it <pod-name> -- /bin/bash`: Get a shell to a running container.

## 6. Helm Charts

Helm is the package manager for Kubernetes. It helps you define, install, and upgrade even the most complex Kubernetes applications.

*   **Chart:** A collection of files that describe a related set of Kubernetes resources. Think of it like a `deb` or `rpm` file, or an `npm` package.
*   **Templating:** Helm uses templates to generate Kubernetes YAML files. This allows you to parameterize your deployment (e.g., change the image tag or replica count) without editing the raw YAML.
*   **Release:** An instance of a chart running in a Kubernetes cluster.
*   **Repository:** A place where charts can be collected and shared.

### Basic Commands
*   `helm install <release-name> <chart>`: Install a chart.
*   `helm upgrade <release-name> <chart>`: Upgrade a release.
*   `helm uninstall <release-name>`: Uninstall a release.

[[programming/docker-and-containers]]
[[programming/microservices-architecture]]
[[programming/cloud-computing-basics]]