Skip to content

This tutorial repository provides Kubernetes configuration files for deploying a MongoDB database and a sample web application on a Minikube cluster. It also covers fundamental Kubernetes concepts, including worker nodes, the master node, kubectl commands, and more.

Notifications You must be signed in to change notification settings

EmmyAnieDev/kubernetes-tutorial-setup

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kubernetes Overview

Kubernetes is an open-source system that automates the management, deployment, and scaling of containerized applications. It's used by organizations to run distributed applications and services at scale.

Kubernetes is an open-source container orchestration platform designed to manage containers, such as those created with Docker or other containerization technologies. It simplifies the deployment, scaling, and management of containerized applications across various environments, including virtual machines, physical machines, cloud platforms, or hybrid deployment setups.


Problems Kubernetes Solves

Kubernetes streamlines the management of microservices by efficiently handling the deployment, scaling, and coordination of multiple service containers.


Key Features of Kubernetes

  1. High Availability:

    • Ensures applications remain accessible without downtime, providing users with uninterrupted access.
  2. Scalability:

    • Automatically adjusts application performance based on demand.
    • Scales up/out during high load periods and scales down during low load periods, ensuring efficient resource utilization.
  3. Disaster Recovery:

    • Supports robust backup and restore mechanisms.
    • Enables recovery to the most recent state in the event of issues such as data loss, server failures, or infrastructure problems.
    • Ensures data integrity and resumption of operation from the latest state after recovery.

Kubernetes Architecture

A Kubernetes cluster consists of at least one master node (physical or virtual machine) functioning as the control plane, and multiple worker nodes connected to it. Each worker node runs a Kubernetes process known as the kubelet and executes tasks such as running application processes.

Key Components of Kubernetes Architecture

Worker Nodes

  • Kubelet:

    • A Kubernetes process that facilitates communication between the nodes and the master node.
    • Ensures tasks are executed and manages containerized applications.
  • Containers:

    • Each worker node runs containers for different applications.
    • The number of containers depends on the workload distribution across the nodes.
  • Functionality:

    • Worker nodes are where all the actual work happens, hosting and running the applications.

Master Node

The master node orchestrates the cluster and runs critical Kubernetes processes required to manage the cluster. Key components include:

  1. API Server:

    • Acts as the entry point to the Kubernetes cluster.
    • Handles interactions with Kubernetes clients (e.g., kubectl, scripts, automation tools, or the Kubernetes dashboard).
    • Facilitates communication between the components of the cluster.
    • All the configurationn usually goes through here to the kubernetes cluster.
  2. Controller Manager:

    • Monitors the state of the cluster.
    • Ensures necessary repairs, such as restarting a failed container.
  3. Scheduler:

    • Allocates containers to worker nodes based on workload and available resources.
    • Optimizes the placement of containers to balance load and maximize resource utilization.
  4. etcd:

  • The cluster brain. It holds the current status of any Kubernetes components.
  • A distributed key-value store that maintains the cluster's current state.
  • Stores configuration and status data for nodes and containers.
  • Enables backup and recovery of the cluster state using snapshots.

Virtual Network

  • This creates a unified system by connecting all nodes in the cluster into a single powerful machine with combined resources.

  • Enables seamless communication between master nodes and worker nodes, ensuring efficient coordination and operation within the cluster.


Additional Notes on Kubernetes Architecture

Worker Nodes

  • Worker nodes are typically more resource-intensive because they handle most of the workload by running the applications.
  • Often host hundreds of containers, making their resource requirements significantly higher than those of the master node.

Master Node

  • Runs a limited number of critical master processes (e.g., API server, Scheduler, Controller Manager, etcd).
  • Critical Role:
    • The master node is vital for the cluster's operation.
    • Losing access to the master node means the entire cluster becomes inaccessible.
    • Regular backups of the master node are essential.

High Availability in Production

  • In production environments, it is common to have at least two master nodes to ensure high availability.
  • Often, multiple master nodes are used, so if one master node fails, the cluster continues to function without downtime.

Main Components of Kubernetes

Nodes and Pods

  1. Node:

    • A node is a server (physical or virtual machine) in Kubernetes, commonly referred to as a worker node. It hosts and runs application workloads.
  2. Pod:

    • A pod is an abstraction over a container and provides the runtime environment for containers.

    • Typically, a pod is designed to run a single application container. Pods are the smallest deployable units in Kubernetes.

    • Each pod has its own unique IP address, enabling communication between pods using these addresses. For example, a web application container can communicate with a database container using the database pod's IP.

    • Pods are ephemeral and may crash or be replaced. When a new pod is created, it is assigned a new IP address, which can disrupt direct pod-to-pod communication.

Service and Ingress

  1. Service:

    • A service provides a stable, static IP address and DNS name for a set of pods.

    • Each pod type (e.g., web app or database) has its own service, ensuring consistent connectivity even when pods are replaced.

    • Services are independent of the lifecycle of pods, so even if a pod dies, the service and its IP remain active.

    • To expose an application to the internet, an External Service is created, allowing communication from external sources.

    • For internal components, such as a database, an Internal Service is used, ensuring that it is not publicly accessible.

    • Service also act as a Load balancer,it catches the request annd sends to whichever pod is less busy

  2. Ingress:

    • Ingress acts as a gateway, receiving external requests and routing them to the appropriate service within the cluster.

    • It allows advanced routing configurations, such as path-based or subdomain-based traffic distribution.

ConfigMap and Secret

  1. ConfigMap:

    • Used to store external configuration for applications.

    • Typically contains non-sensitive data such as database URLs or service endpoints.

    • ConfigMaps are linked to Pods, enabling the Pods to access the configuration data.

    • Designed for storing non-confidential information only.

  2. Secret:

    • Similar to ConfigMap but specifically used for storing sensitive data, such as credentials (e.g., passwords, certificates).

    • Secrets are stored in base64-encoded format and can be further encrypted using tools like bcrypt or third-party security solutions for enhanced protection.

    • Like ConfigMaps, Secrets are connected to Pods, allowing the Pods to access and use the sensitive data.

Volumes

  • Volumes in Kubernetes provide a way to persist and share data between containers in a Pod. Unlike container storage, which is ephemeral and tied to the container's lifecycle, volumes are more durable and allow data to persist beyond the lifecycle of a single container.

  • Volumes attaches physical storage to a Pod, which can either reside on a local machine (the same server node where the Pod is running) or on remote storage located outside the Kubernetes cluster. This remote storage could be cloud-based or an on-premises primary storage solution, providing an external reference to the data.

  • This is essential because Kubernetes does not inherently persist data.

Key Features of Kubernetes Volumes

  • Data Persistence: Volumes enable data to persist even if the container in a Pod is terminated and restarted.

  • Shared Storage: Volumes allow multiple containers within the same Pod to share data by mounting the same volume.

  • Flexibility: Kubernetes supports different types of volumes to accommodate various storage requirements, ranging from ephemeral storage to durable storage.

Deployments and StatefulSet

  1. Deployments

    • A Deployment in Kubernetes is a template or blueprint for creating pods.

    • A Deployment in Kubernetes is a higher-level abstraction that defines the desired state of an application, including the number of replicas, container images, and update strategies.

    • It manages the creation, scaling, and rolling updates of Pods, ensuring the application is consistently available and can recover automatically from failures or disruptions. Deployments simplify application management by handling updates, rollbacks, and scaling with declarative configurations.

    • This is an abstraction on top of Pods

    • We can't replicate database using deployment because database has state.

  2. StatefulSet

    • A StatefulSet is a Kubernetes resource designed specifically for managing stateful applications, such as databases (e.g., MySQL, MongoDB, Elasticsearch) or any application that requires stable, unique network identifiers and persistent storage.

    • Unlike Deployments, which are ideal for stateless applications, StatefulSets ensure that each Pod is uniquely identifiable and maintains its state across reschedules.

    • StatefulSets are essential for applications where the underlying data and state need to be preserved and accessible at all times, even during failures or updates.

Configuration File

  • All the configurationn usually goes through the API server to the kubernetes cluster.

  • Kubernetes clients, such as a UI (Dashboard), an API (e.g., through scripts or CURL commands), or a command-line tool like kubectl, all interact with the API Server. The API Server serves as the single entry point to the cluster.

  • Clients send their requests to the API Server in either YAML or JSON format, specifying the desired state of the cluster or querying its current state. The API Server processes these requests and coordinates the necessary actions within the cluster.

Every Kubernetes configuration file typically consists of three main parts:

Metadata:

  • This section provides information about the resource, such as its name, namespace, labels, and annotations.

Spec (Specification):

  • This defines the desired state of the resource, describing what you want Kubernetes to do.

  • It includes details specific to the resource type, such as the container image for a Pod or the number of replicas for a Deployment.

Status:

  • The Status field in Kubernetes is automatically generated and managed by Kubernetes itself.

  • It reflects the current state of a resource in the cluster, such as a Pod, Deployment, or StatefulSet, and includes information about conditions, events, and runtime details.

  • Kubernetes continuously compares the desired state (defined in the spec) with the actual state (reflected in the status). If there is a mismatch, Kubernetes takes corrective actions to reconcile the states.

  • This self-monitoring mechanism forms the foundation of Kubernetes' self-healing capability, ensuring that resources are automatically adjusted to meet the desired state whenever possible.

  • Kubernnete get the satus data from the etcd

Minikube

Minikube is an open-source tool that allows you to run a single-node Kubernetes cluster locally.

  • In Minikube, both the master and worker nodes are combined into a single node, making it ideal for learning, development, and testing purposes.

  • It provides a lightweight Kubernetes environment, allowing developers to experiment with Kubernetes features without needing a full multi-node cluster.

  • The node will have docker container run time pre installed.

Kubectl

kubectl is a command-line tool used to manage and interact with Kubernetes clusters by communicating with the API Server on the Master node.

  • It is the most powerful and versatile Kubernetes client, enabling users to perform a wide range of tasks such as deploying applications, inspecting resources, and troubleshooting issues.

  • kubectl works seamlessly with any type of Kubernetes cluster, whether it's local (e.g., Minikube), on-premises, or cloud-based.

  • With kubectl, you can manage cluster resources through commands that are translated into API requests sent to the API Server.

INSTALATION OF MINIKUBE and Kubectli

  • bash brew install minikube

  • bash minikube start --driver docker (You must have either a container or Virtual Machine installed in your Computer).

  • bash minikube status

  • bash kubectl get node

  • bash minikube delete

Minikube is just for the start up and deleting the cluster while Kubectl is meant for configuring minikube cluster

Applying External Configurations and Deployments

  • Step-by-Step Approach: Create and apply the necessary external configurations (ConfigMaps and Secrets) first, followed by the MongoDB and web app deployments to ensure everything is correctly set up before the applications start running.

  • Commands

    bash kubectl apply -f mongo-config.yaml

    bash kubectl apply -f mongo-secret.yaml

    bash kubectl apply -f mongo.yaml

    bash kubectl apply -f webapp.yaml

  • Reasoning: This ensures that all required configurations and secrets are in place and accessible to the applications before they begin their deployment process.

Interacting with Kubernetes Cluster

  • Commands

    bash kubectl get all

    bash kubectl get configmap

    bash kubectl get secret

    bash kubectl get pod

    bash kubectl get service (svc)

    bash kubectl get --help

    bash kubectl describe service mongo-service

    bash kubectl describe pod webapp-deployment-c57674fc7-cx9m

    bash kubectl logs webapp-deployment-c57674fc7-cx9m7

    Access WebApp in the Browser

    • We can get the IP by either of the commands below.

    bash minikue ip

    bash kubectl get node -o wide

    bash minikube service webapp-service

About

This tutorial repository provides Kubernetes configuration files for deploying a MongoDB database and a sample web application on a Minikube cluster. It also covers fundamental Kubernetes concepts, including worker nodes, the master node, kubectl commands, and more.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published