๐๐ฎ๐๐ฏ๐ฌ ๐ง๐ฎ๐๐ธ: ๐๐๐ฏ๐ฒ๐ฟ๐ป๐ฒ๐๐ฒ๐ ๐๐ฟ๐ฐ๐ต๐ถ๐๐ฒ๐ฐ๐๐๐ฟ๐ฒ
What is Kubernetes? Write in your own words and why do we call it K8s?
Kubernetes is an open-source container management tool that automates the deployment, scaling, and load balancing of containers. It schedules, runs, and manages isolated containers that are running on virtual, physical, or cloud machines. Kubernetes supports almost all container platforms, and is not specific to Docker.
Kubernetes is also called K8s because, in the past, names were often shortened for convenience. The "K" is the first letter, and "S" is the last letter, with the number 8 representing the eight letters in between, making it easier to refer to.
What are the benefits of using K8s?
Kubernetes provides both horizontal and vertical scaling. It also offers strong fault tolerance; for example, if a pod fails, Kubernetes automatically creates a new one. Kubernetes includes health monitoring for containers, ensuring that if a container fails, a new one is created (although this may require a plugin). Additionally, Kubernetes supports different types of batch execution, such as one-time, sequential, and parallel execution, which can be done in the background.
Portability: Since Kubernetes works with containers, it provides an environment-agnostic way to run applications, making it easy to move applications between different environments (on-prem, cloud, hybrid).
Integration with CI/CD: Kubernetes integrates well with continuous integration and continuous deployment (CI/CD) tools, streamlining the software delivery pipeline.
High Availability: Kubernetes provides features like self-healing (restarting failed containers) and load balancing, which help ensure that applications remain available even in case of failure.
Efficient Resource Management: Kubernetes efficiently utilizes infrastructure resources by distributing containers across nodes based on available resources, helping reduce costs and improve performance.
kubectl is the command-line interface (CLI) tool for interacting with a Kubernetes cluster. It is the most common way to interact with Kubernetes for developers and administrators, allowing them to manage and control the cluster's resources directly from their terminal or command prompt.
The primary function of kubectl is to communicate with the Kubernetes API server to issue commands, retrieve information, and perform operations on the Kubernetes cluster. kubectl provides a variety of commands for performing common operations such as creating, updating, deleting, and scaling resources like pods, deployments, services, and more.
Key Features of kubectl
Cluster Interaction:
- kubectl enables communication with the Kubernetes API server, which serves as the control plane for the cluster. When you issue a command through kubectl, the tool sends an API request to the server, which then processes the request and returns the results.
Management of Kubernetes Resources:
kubectl allows you to manage and interact with many Kubernetes resources, including:
Pods: The smallest deployable units in Kubernetes, often containing one or more containers.
Deployments: A resource used to manage replica sets and handle scaling and rolling updates.
Services: Abstracts access to a set of pods, enabling networking and load balancing.
Namespaces: Logical partitions within a cluster that separate different environments (e.g., development, staging, production).
ConfigMaps & Secrets: Used to manage configuration data and sensitive information like passwords.
ReplicaSets: Ensures that a specified number of replicas of a pod are running at all times.
Declarative Configuration:
With kubectl, you can apply configuration files (written in YAML or JSON) to manage Kubernetes resources. This allows you to define the desired state of your resources and have kubectl send these configurations to the cluster. It can then create or update resources accordingly.
- Example:
kubectl apply -f deployment.yaml
- Example:
Resource Monitoring:
kubectl can be used to monitor the state of your cluster and its resources. You can view details such as the status of pods, services, and nodes, as well as retrieve logs for debugging.
Example:
kubectl get pods
(Shows the list of all pods in the current namespace).Example:
kubectl logs <pod-name>
(Displays logs from a specific pod).
Namespace Support:
Kubernetes supports namespaces to organize resources into virtual clusters within the same physical cluster. You can use kubectl to work within specific namespaces by using the
--namespace
flag or setting the default namespace for the context.- Example:
kubectl get pods --namespace=dev
- Example:
Context Switching:
kubectl allows you to manage multiple Kubernetes clusters by switching contexts. Contexts refer to the configuration settings, including the cluster, user, and namespace, that kubectl uses to interact with the cluster.
- Example:
kubectl config use-context <context-name>
- Example:
Common kubectl Commands
Get Resources:
Retrieve information about different resources in the cluster.
kubectl get pods
- Lists all the pods.kubectl get deployments
- Lists all deployments.kubectl get services
- Lists all services.kubectl get nodes
- Lists all nodes in the cluster.
Create Resources:
Create resources like pods, deployments, or services using a configuration file.
kubectl create -f <filename>.yaml
- Creates a resource defined in a YAML file.kubectl expose pod <pod-name>
- Exposes a pod to external traffic.
Apply Changes:
Apply changes defined in a configuration file to the cluster.
kubectl apply -f <filename>.yaml
- Creates or updates the resources as defined in the configuration file.
Delete Resources:
Delete a resource (pod, deployment, service, etc.) from the cluster.
kubectl delete pod <pod-name>
- Deletes a specific pod.kubectl delete -f <filename>.yaml
- Deletes resources defined in a configuration file.
View Resource Details:
Inspect more detailed information about a specific resource.
kubectl describe pod <pod-name>
- Describes a specific pod, showing details like events, container statuses, and more.kubectl describe deployment <deployment-name>
- Provides detailed information about a deployment.
Execute Commands Inside Containers:
kubectl allows you to run commands inside a container within a pod.
kubectl exec -it <pod-name> -- <command>
- Execute a command inside a running container in the specified pod.
Get Logs:
View the logs for a specific pod, container, or application.
kubectl logs <pod-name>
- Retrieves the logs of a pod's main container.kubectl logs <pod-name> -c <container-name>
- Retrieves logs from a specific container within a pod.
kubectl Configuration and Context
Configuration File:
kubectl uses a configuration file called kubeconfig, which stores cluster connection information (e.g., API server URLs, authentication tokens, etc.). This file allows kubectl to communicate with the correct cluster.
The kubeconfig file is typically stored in
~/.kube/config
by default, but this can be changed by setting theKUBECONFIG
environment variable.
Contexts:
A context in Kubernetes is a set of parameters used by kubectl to communicate with a specific cluster. It includes the cluster, the user, and the namespace settings. You can switch between multiple contexts when working with multiple clusters.
kubectl config use-context <context-name>
- Switches to a different context.
Authentication with kubectl
- kubectl uses various methods to authenticate users to the Kubernetes API server. This may include tokens, client certificates, or integrated authentication mechanisms like OAuth or OpenID Connect.
Troubleshooting and Debugging with kubectl
kubectl describe: Use the
describe
command to fetch detailed information about resources, which helps identify issues.- Example:
kubectl describe pod <pod-name>
gives information about the pod's state and events that have occurred.
- Example:
kubectl logs: Use this to view logs for troubleshooting container issues. This is helpful when a container is failing or behaving unexpectedly.
kubectl get events: Shows events in the cluster, which can provide valuable insights about problems such as scheduling failures, resource issues, or application crashes.
What is Control Plane?
The Control Plane is the brain of a Kubernetes cluster, responsible for the global management and decision-making of the cluster. It contains components that make global decisions about the cluster (such as scheduling and managing the lifecycle of containers). The control plane continuously monitors the cluster's state and makes adjustments as needed to ensure that the desired state of the application is maintained.
The control plane is typically composed of:
API Server
Scheduler
Controller Manager
etcd
The control plane's components can run on a single machine or be distributed across multiple nodes for high availability.
Difference Between kubectl and kubelet
kubectl:
kubectl is the command-line tool used by administrators and developers to interact with a Kubernetes cluster. It allows users to create, update, delete, and manage Kubernetes resources such as pods, deployments, services, and more. kubectl communicates with the Kubernetes API server to perform its actions.
Example commands:
kubectl get pods
,kubectl apply -f pod.yaml
kubelet:
kubelet is an agent running on each worker node in the Kubernetes cluster. It ensures that containers are running in their respective pods and reports the status of those containers to the control plane. The kubelet receives instructions from the control plane and ensures the containers on the node are operating as expected.
Example: Kubelet is responsible for starting and stopping containers on a worker node and ensuring that they match the desired state defined by the control plane.
Role of the API Server
The API Server is a critical component of the Kubernetes control plane. It acts as the entry point for all interactions with the cluster. It exposes the Kubernetes REST API, allowing users, administrators, and external services to query and manipulate the cluster's state. The API server serves as a bridge between the external world and the internal components of Kubernetes.
Key functions of the API Server include:
Handling HTTP requests from clients (e.g., kubectl commands).
Validating and processing API requests.
Exposing the Kubernetes API endpoints for managing the cluster.
Acting as the central communication hub, allowing components like the scheduler, controller manager, and etcd to communicate with each other.
Ensuring that the clusterโs state is continuously monitored and updated.