Kubernetes is an open-source system for automating the deployment, scaling, and management of containerized applications. Think of it as an orchestrator for your containers, making sure they run where they're supposed to, handle traffic efficiently, and recover from failures automatically.
Kubernetes is built for stability. It's designed to manage critical aspects of your application, ensuring continuous operation even when individual parts fail.
The typical workflow with Kubernetes involves taking your application code, packaging it into Docker images, and then instructing Kubernetes on how to run and manage these images as containers across a cluster of machines.
Concept: A Kubernetes Service provides a stable network endpoint for a set of Pods.
Purpose:
- Abstraction from dynamic IPs: Pods are ephemeral; they can be created and destroyed, and their IP addresses change. A Service provides a fixed, easy-to-remember name and IP address that other Pods or external clients can use to communicate with your application, regardless of which specific Pod is running it.
- Load Balancing: If you have multiple replicas of a Pod (managed by a Deployment), a Service automatically distributes incoming traffic across these healthy Pods.
- Simplifies inter-service communication: Instead of needing to know the IP address and port of individual Pods, other microservices can simply use the Service's stable name (e.g.,
post-service) to communicate with it.
Important Note: The term Service in Kubernetes refers specifically to this networking abstraction, not to your application's microservice program itself. This distinction is crucial to avoid confusion.
Concept: Kubernetes configuration files (often called manifest files) are human-readable files that describe the desired state of your Kubernetes objects (Pods, Deployments, Services, etc.).
Purpose: They tell Kubernetes what you want to create and how you want it configured within your cluster.
- Format: These config files are written entirely in YAML (YAML Ain't Markup Language) syntax.
- Simplicity: YAML is straightforward, especially if you're familiar with JSON. It relies heavily on indentation to define structure, similar to Python.
- Crucial Detail: Correct indentation (typically two spaces per level) is absolutely critical. Any typo or incorrect spacing will lead to errors when Kubernetes tries to process the file.
-
Source Control: Always store your config files with your project's source code (e.g., in Git).
-
Documentation: These files serve as the best documentation for what your Kubernetes cluster is running.
-
Avoid Direct CLI Creation: While it's possible to create objects directly via
kubectlcommands, this is discouraged for production.- Reason: Direct commands don't leave a persistent record of your desired state. If the object is deleted or the cluster restarts, you lose that configuration.
-
Best Practice: Always use config files.
kubectl apply -f <file-name.yaml>is the preferred method.
apiVersion: v1
kind: Pod
metadata:
name: posts
spec:
containers:
- name: posts
image: <your-docker-id>/posts:0.0.1-
apiVersion: v1
- Specifies the Kubernetes API version.
v1refers to the core API group.
- Specifies the Kubernetes API version.
-
kind: Pod
- Defines the type of Kubernetes object to create.
-
metadata:
- Provides descriptive information. Here, the pod is named
posts.
- Provides descriptive information. Here, the pod is named
-
spec:
- The blueprint of the object.
- containers: Defines which containers to run inside the Pod.
- image:
<your-docker-id>/posts:0.0.1specifies the Docker image. - Version Tag: Always use explicit tags (e.g.,
:0.0.1) instead of:latestto avoid unexpected behavior.
Once you've defined your desired state in config files, kubectl is your primary tool for interacting with the cluster.
kubectl apply -f <config-file.yaml>- Purpose: Creates or updates objects defined in a YAML config file.
- How it works: Reads your config file and tells Kubernetes to match the desired state. If the object already exists, it updates it.
kubectl get pods- Lists all running Pods (or other objects like deployments, services, nodes).
kubectl describe pod <pod-name>- Shows detailed information about a Pod, including Events useful for debugging.
kubectl exec -it <pod-name> sh- Executes a command inside a running container within a Pod.
-it: Interactive + TTY for shell access.sh: Starts a shell inside the container.
kubectl logs <pod-name>- Retrieves logs from a container inside a Pod (similar to
docker logs).
kubectl delete pod <pod-name>- Deletes a specific Pod or object.
- Useful for forcing a restart (the Deployment will recreate it).
Shortcut: Some use k as an alias for kubectl (e.g., k get pods).