Kubernetes
Intro
An open-source container orchestration platform designed to automate the deployment, scaling, and management of containerized applications. It provides a robust framework for running distributed systems resiliently, ensuring application uptime and ease of operation in a cloud-native environment. Kubernetes, often abbreviated as K8s, was inspired by Google’s internal system Borg, which managed and scheduled the company’s massive workloads across data centers worldwide. Created by Google engineers in 2014, Kubernetes was developed as an open-source project to enable developers to automate deployment, scaling, and management of containerized applications. With its robust orchestration capabilities, Kubernetes simplifies the complexities of managing containerized applications by automating processes like load balancing, storage management, and fault tolerance, making it a staple in cloud-native environments. Today, backed by the Cloud Native Computing Foundation (CNCF), Kubernetes continues to evolve with contributions from global tech communities, aiming to expand its reach in edge computing, hybrid cloud, and AI-driven orchestration.
Definitions
Pod
Kubernetes pods are the smallest and simplest unit in the Kubernetes object model, representing a single instance of a running process in a cluster. Each pod can contain one or more containers that share resources like networking and storage, enabling applications to run as isolated but cooperative components.
Node
A node is a physical or virtual machine in the Kubernetes cluster that runs pods. It contains the necessary services to manage networking between containers, monitor running containers, and communicate with the control plane.
Cluster
A Kubernetes cluster is a set of nodes grouped together to work as a single system. It includes a control plane and multiple nodes to run containerized applications efficiently.
Control Plane
The control plane manages the Kubernetes cluster. It is responsible for making global decisions about the cluster (e.g., scheduling), and it includes components such as the API server, scheduler, and etcd for cluster state storage.
Namespace
A namespace is a virtual cluster within a Kubernetes cluster. It is used to divide cluster resources among multiple users or teams, providing isolation and organization for resources.
Deployment
A deployment is a Kubernetes resource that defines the desired state of your application, such as the number of replicas to run. Kubernetes uses deployments to ensure your application is running as specified.
Service
A service in Kubernetes is an abstraction that defines a logical set of pods and a policy by which to access them. It enables stable networking for pods, even as they are replaced or restarted.
Ingress
Ingress is a Kubernetes object that manages external HTTP/S access to services within a cluster. It provides routing rules to map external traffic to internal services.
ReplicaSet
A ReplicaSet ensures a specified number of pod replicas are running at any given time. It replaces pods that are deleted or terminated to maintain the desired state.
ConfigMap
A ConfigMap is a Kubernetes object used to store configuration data in key-value pairs. It decouples application code from configuration, enabling easier updates.
Secret
A Secret is a Kubernetes object designed to store sensitive information like passwords, tokens, or keys. Secrets are base64-encoded for obfuscation and are mounted into pods securely.
PersistentVolume (PV)
A PersistentVolume is a piece of storage provisioned in a Kubernetes cluster, abstracting the underlying storage details. It is used for persisting data beyond the lifecycle of a pod.
PersistentVolumeClaim (PVC)
A PersistentVolumeClaim is a request for storage by a user. It enables pods to consume persistent storage resources provisioned as PersistentVolumes.
DaemonSet
A DaemonSet ensures that all (or some) nodes run a copy of a specified pod. It is commonly used for log collection, monitoring, or networking applications.
StatefulSet
A StatefulSet is a Kubernetes object designed for applications that require persistent identity and storage, such as databases. It ensures that pods have stable network identifiers and persistent storage.
Job
A job is a Kubernetes resource that runs a single task until completion. It is used for batch processing and ensures that the task completes successfully.
CronJob
A CronJob schedules jobs to run at specified intervals, similar to a cron scheduler in Unix/Linux systems.
Horizontal Pod Autoscaler (HPA)
The Horizontal Pod Autoscaler automatically adjusts the number of pods in a deployment, replica set, or stateful set based on observed CPU utilization or other custom metrics.
Custom Resource Definition (CRD)
A CRD allows users to define custom objects and resources to extend Kubernetes functionality beyond the default resources.
Volume
A volume in Kubernetes provides storage to a pod. Unlike container storage, volumes persist across container restarts and can share data among containers in the same pod.
Kubectl
kubectl
is the command-line tool used to interact with a Kubernetes cluster, allowing users to deploy applications, inspect resources, and manage cluster components. It provides powerful commands to streamline Kubernetes operations, from creating pods to troubleshooting workloads.
Common Commands
List all namespaces
kubectl get namespaces
Get all pods in the current namespace
kubectl get pods
Get all resources in all namespaces
kubectl get all --all-namespaces
Describe a specific pod
kubectl describe pod <pod-name>
Create a resource from a YAML or JSON file
kubectl apply -f <file.yaml>
Delete a resource
kubectl delete <resource-type> <resource-name>
View pod logs
kubectl logs <pod-name>
Execute a command in a running pod
kubectl exec -it <pod-name> -- <command>
Scale a deployment
kubectl scale deployment <deployment-name> --replicas=<num-replicas>
Expose a deployment as a service
kubectl expose deployment <deployment-name> --type=<service-type> --port=<port>
View the status of nodes
kubectl get nodes
Get detailed information on services
kubectl get services
Set a context for a specific cluster
kubectl config set-context <context-name>
Switch to a different context
kubectl config use-context <context-name>
Port-forward a pod to your local machine
kubectl port-forward <pod-name> <local-port>:<remote-port>
View events int the cluster
kubectl get events
Apply changes interactively
kubectl edit <resource-type> <resource-name>
Roll back to a previous deployment version
kubectl rollout undo deployment/<deployment-name>
Get detailed information about a deployment
kubectl describe deployment <deployment-name>
Check the current context
kubectl config current-context