DK

Kubernetes Explained

What is Kubernetes?

  • Kubernetes is an open-source container orchestration platform designed to automate deploying, scaling, and managing containerized applications. It ensures that applications run where and when you want them to, with the resources they require.

  • Kubernetes works by orchestrating containers across multiple machines, optimizing resource utilization, and providing self-healing capabilities.

  • Kubernetes originated from Google's internal container orchestration system, Borg, which managed both long-running services and batch jobs.

  • In 2014, Google open-sourced a version of Borg, naming it Kubernetes and donating it to the Cloud Native Computing Foundation (CNCF), fostering a vendor-neutral ecosystem.

Key Features:
  • Automated Deployments and Rollouts: Kubernetes automates the deployment process, allowing you to easily deploy new versions of your applications.

  • Service Discovery and Load Balancing: Kubernetes can expose containers using the DNS name or IP address. If traffic to a container is high, Kubernetes can load balance and distribute the network traffic so that the deployment is stable.

  • Storage Orchestration: Kubernetes allows you to automatically mount the storage system of your choice, such as local storage, public cloud providers, and more.

  • Self-Healing: Kubernetes restarts containers that fail, replaces containers, kills containers that don’t respond to your user-defined health check, and doesn’t advertise them to clients until they are ready to serve.

  • Secret and Configuration Management: Kubernetes lets you store and manage sensitive information, such as passwords, OAuth tokens, and SSH keys. You can deploy and update secrets and application configuration without rebuilding your container images and without exposing secrets in your stack configuration.

Why is it called K8s?

  • K8s is an abbreviation method used for long words to simplify communication and documentation.

  • The number 8 represents the eight letters between the first 'K' and the last 's' in 'Kubernetes,' making it a convenient shorthand.

  • Examples:

    • I18n for internationalization, commonly used in software development.

    • L10n for localization, indicating the adaptation of software for different regions or languages.

Kubernetes Cluster

  • A Kubernetes cluster is a set of machines, known as nodes, that work together to run containerized applications. These nodes can be physical or virtual machines.

  • Two core components:

    • Control Plane: Manages the state of the cluster, making decisions about scheduling and resource allocation. In production, it runs on multiple nodes across data center zones to ensure high availability and fault tolerance.

    • Worker Nodes: Run the containerized application workloads, executing the tasks assigned by the control plane.

Cluster Architecture:
  • Master Node(s): Host the control plane components. They manage the overall cluster state and ensure the desired state is maintained.

  • Nodes: Worker machines that run the applications. Each node runs a Kubelet agent and a container runtime.

Pods

  • Containerized applications run inside pods, which are the smallest deployable units in Kubernetes. Each pod can contain one or more containers.

  • Pods are the basic building blocks of Kubernetes applications, representing a single instance of a running process in the cluster.

  • A pod hosts one or more containers, sharing storage, network resources, and specifications on how to run the containers. This allows closely coupled applications to operate as a single unit.

  • Pods are created and managed by the Kubernetes control plane, which ensures that the desired number of pods are running and healthy.

Key Aspects:
  • Shared Context: Containers within a pod share the same network namespace, IPC namespace, and storage volumes.

  • Lifecycle: Pods have a defined lifecycle, from creation to termination, with various phases such as Pending, Running, Succeeded, and Failed.

Control Plane Components

  • API Server: The primary interface between the control plane and the rest of the cluster. It exposes a RESTful API, allowing users and other components to interact with the cluster.

  • etcd: A distributed key-value store that stores the cluster’s persistent state. It provides a reliable and consistent way to store configuration data, ensuring that the cluster can recover from failures.

  • Scheduler: Responsible for scheduling pods onto worker nodes, considering resource requirements, availability, and other constraints. It optimizes resource utilization and ensures that pods are placed on the most suitable nodes.

  • Controller Manager: Runs controllers that manage the state of the cluster. These controllers continuously monitor the cluster's state and take corrective actions to maintain the desired state.

    • Examples:

      • Replication controller: Ensures the desired number of pod replicas are running, automatically creating or deleting pods as needed.

      • Deployment controller: Manages rolling updates and rollbacks of deployments, allowing you to update applications without downtime.

Worker Node Components

  • Kubelet: A daemon that runs on each worker node, communicating with the control plane. It receives instructions about which pods to run and maintains the desired state of the pods.

  • Container Runtime: Runs the containers on the worker nodes. It handles pulling container images, starting/stopping containers, and managing container resources. Common container runtimes include Docker, containerd, and CRI-O.

  • Kube Proxy: A networking proxy that runs on each worker node, routing traffic to the correct pods and providing load balancing. It ensures that network traffic is directed to the appropriate containers, regardless of which node they are running on.

Node Communication:
  • Node Registration: Nodes register themselves with the control plane, providing information about their resources and capabilities.

  • Heartbeats: Nodes send regular heartbeats to the control plane to indicate their health and availability.

Why Use Kubernetes?

  • Scalable and Highly Available:

    • Provides self-healing, automatically restarting failed containers, replacing unhealthy nodes, and performing automatic rollbacks to previous deployments.

    • Scales applications up and down as needed, dynamically adjusting resource allocation to meet demand.

  • Portable:

    • Deploys and manages applications consistently across different infrastructures, ensuring that applications behave the same way regardless of the environment.

    • Runs on-premise, in public clouds, or in hybrid environments, offering flexibility and choice.

    • Offers a uniform way to package, deploy, and manage applications, simplifying the development and operations lifecycle.

Benefits:
  • Resource Optimization: Efficiently utilizes hardware resources by bin-packing containers onto nodes.

  • Simplified Management: Automates many of the manual tasks associated with deploying and managing applications.

Downsides of Kubernetes

  • Complexity:

    • Complex to set up and operate, requiring a deep understanding of its architecture and components.

    • High upfront cost, especially for organizations new to container orchestration, due to the learning curve and initial setup effort.

    • Requires significant expertise and resources for production environments, including dedicated teams for managing and maintaining the cluster.

  • Cost:

    • Requires a minimum level of resources to run, which can be overkill for smaller organizations with limited workloads.

Challenges:
  • Configuration Management: Managing complex application configurations can be challenging.

  • Monitoring and Logging: Setting up comprehensive monitoring and logging solutions requires additional effort.

Managed Kubernetes Services

  • Offload control plane management to cloud providers, reducing the operational burden on organizations.

  • Examples: Amazon EKS (Elastic Kubernetes Service), GKE (Google Kubernetes Engine) on Google Cloud, AKS (Azure Kubernetes Service) on Azure.

  • Allows organizations to run Kubernetes applications without managing the underlying infrastructure, focusing on application development and deployment.

  • Handles tasks like setting up the control plane, scaling the cluster, providing maintenance and support, and ensuring high availability.

  • Suitable for mid-sized organizations to test Kubernetes and for larger organizations to simplify cluster management.

Advantages:
  • Reduced Operational Overhead: Cloud providers handle the complexities of managing the control plane.

  • Simplified Scaling: Easily scale clusters up or down based on demand.

Recommendation

  • For small organizations, YAGNI (You Ain't Gonna Need It) is recommended, suggesting that they should avoid implementing Kubernetes unless there is a clear and immediate need. Simple solutions may be more appropriate for smaller projects.