Kubernetes is a powerful tool for managing containerized applications. It provides a consistent and declarative way to deploy, manage, and scale complex distributed systems. If you are a software developer with less than 3 months of Linux administration experience, this blog post has provided you with a basic introduction to Kubernetes and some essential commands to get you started.
Kubernetes, often abbreviated as K8s, is an open-source container orchestration platform that automates the deployment, management, and scaling of containerized applications. It provides a consistent and declarative way to manage complex distributed systems, making it easier for developers to build, test, and deploy their applications.
Kubernetes is widely used in cloud computing environments, such as Amazon Elastic Kubernetes Service (EKS), Google Kubernetes Engine (GKE), and Microsoft Azure Kubernetes Service (AKS). It can also be deployed on-premises or in hybrid environments.
- Container Orchestration: Kubernetes manages the lifecycle of containers, including deployment, scaling, networking, and load balancing.
- Declarative Management: Users define desired state in a declarative manner, and Kubernetes automatically reconciles the actual state with the desired state.
- Self-Healing: Kubernetes automatically monitors and repairs containerized applications, ensuring high availability and resilience.
- Horizontal Pod Autoscaling: Kubernetes can automatically scale applications based on metrics such as CPU and memory utilization.
- Service Discovery and Load Balancing: Kubernetes provides built-in service discovery and load balancing mechanisms, making it easy to connect and route traffic between applications.
To get started with Kubernetes, you will need to install the following software:
- Kubernetes CLI: A command-line interface for interacting with Kubernetes clusters.
- Minikube: A tool for running Kubernetes locally on a single node.
- kubectl: A command-line tool for managing Kubernetes clusters.
On Ubuntu 22.04, you can install these software packages using the following commands:
sudo apt update
sudo apt install kubectl minikube
If you face any issues with kubectl installation via apt, you can also use snap to do the same The command is:
sudo snap install kubectl --classic
Container Runtime: You will also need a container runtime such as Docker or containerd. Docker is the most widely used option.
Cloud Provider: If you plan to deploy Kubernetes on a cloud platform like AWS or Azure, you may need to install additional components specific to that platform. E.g., eksctl (a simple CLI tool for creating clusters on EKS).
Helm: Helm is a package manager for Kubernetes. It simplifies the installation and management of complex applications.
Monitoring Tools: Monitoring tools such as Prometheus and Grafana are essential for observing and troubleshooting Kubernetes clusters.
CI/CD Pipeline: A continuous integration and continuous delivery (CI/CD) pipeline is recommended for automating the deployment and management of Kubernetes applications.
Kubernetes namespaces are a way to divide cluster resources between multiple users or teams. They provide a mechanism for isolating resources and managing them in a more organized manner. By using namespaces, you can ensure that resources like pods, services, and deployments do not collide or interfere with each other across different namespaces.
Example: Imagine you have a Kubernetes cluster with multiple teams working on different projects. Each team can be assigned its own namespace, such as dev for development and prod for production. Resources created within these namespaces are isolated from each other, making it easier to manage and control them.
Commands
1. Create a namespace
kubectl create namespace
To create a namespace called dev, we will use the following command:
kubectl create namespace dev
2. Switch to a new namespace
kubectl config set-context --current --namespace=
We can switch 'dev' namespace using the following command.
kubectl config set-context --current --namespace=dev
3. Delete a namespace
kubectl delete namespace
We can delete 'dev' namespace using the following command.
kubectl delete namespace dev
Kubernetes pods are the smallest and simplest units in Kubernetes. They represent a single instance of a running process in your cluster and can contain one or more containers. Containers within a pod share the same network namespace, meaning they can communicate with each other using localhost. Pods also share storage volumes, allowing data to persist across container restarts.
Simple Example
Imagine you have a web application that requires a backend service and a frontend service. You can deploy both services in the same pod if they need to be tightly coupled. For instance, you might run a web server and a logging agent in the same pod to handle incoming web requests and log data concurrently.
Commands
1. Create a Pod
To create a pod, you use a YAML configuration file. Here’s an example of a command that applies a pod configuration:
kubectl apply -f pod.yaml
-- Where pod.yaml contains the definition of the pod.
2. List Pods
To list all pods in the default context (namespace), we use the following command:
kubectl get pods
To list pods from a particular namespace, e.g., dev, we use the following command:
kubectl get pods -n dev
-- Where -n dev defines the namespace dev.
3. Delete Pods
To list all pods in the default context (namespace), we use the following command:
kubectl get pods
To list pods from a particular namespace, e.g., dev, we use the following command:
kubectl get pods -n dev
-- Where -n dev defines the namespace dev.
Kubernetes nodes are the fundamental building blocks of a Kubernetes cluster. They are virtual or physical machines that provide the runtime environment for containerized applications. Each node runs essential services including the container runtime (such as Docker), kubelet (which ensures containers are running as expected), and kube-proxy (which manages network communication).
Imagine you have a Kubernetes cluster running an online bookstore application. The cluster is composed of multiple nodes, each hosting different parts of the application. For instance, one node might run the frontend of the bookstore, while another runs the backend and database services. Together, these nodes work to ensure the application is distributed and scalable.
Commands
1. Creating a Node
Nodes are typically managed by your cloud provider or virtual machine environment. You don't create nodes directly with a command but rather provision VMs or instances that are then registered with your Kubernetes cluster. For example, using cloud provider tools or specific Kubernetes setup scripts.
2. Listing all nodes
kubectl get nodes
3. Deleting a node
Before deleting a node, it is recommended to drain a node. Draining a node will safely evict all pods from the node, ensuring they are rescheduled on other nodes.
We can use the following command to drain a node:
kubectl drain
Where:
--ignore-daemonsets>: Ignore DaemonSet-managed pods.
--delete-emptydir-data: Deletes any pods with emptyDir volumes (data stored in them will be lost).
We can now delete node using the following command:
kubectl delete node
Kubernetes Deployments are a higher-level abstraction that manages the deployment and scaling of applications. They ensure that a specified number of pod replicas are running at any given time, and they facilitate rolling updates and rollbacks. Deployments help in maintaining the desired state of your application by automatically handling updates and scaling.
Imagine you have a simple web application running in Docker containers. You want to deploy this application to Kubernetes and ensure that it is always running with three replicas. You can create a Deployment that specifies the desired state (e.g., three replicas of your web application) and Kubernetes will handle creating and managing the pods accordingly.
Commands
1. Creating a Deployment
This following command creates a Deployment named my-web-app using the specified Docker image.
kubectl create deployment my-web-app --image=my-web-app:latest
2. Listing all Deployments
This following command list all of deployments in the current namespace.
kubectl get deployments
To list deployments in other namespaces, we can use -n switch:
kubectl get deployments -n my-namespace
3. Delete the Deployment
This following command delete the namespace in the current namespace.
kubectl delete deployment my-deployment
To delete deployment in other namespaces, we can use -n switch:
kubectl delete deployment my-deployment -n my-namespace
Where: my-deployment is the name of the deployment to be deleted
my-namespace is the name of the namespace where my-deployment is present.
A Kubernetes Service is an abstraction that defines a logical set of Pods and a policy by which to access them. Services enable communication between different parts of an application, providing a stable IP address and DNS name for accessing a set of Pods. They ensure that the application remains reachable, even as Pods are added or removed.
Consider a scenario where you have a set of Pods running a web application. Without a Service, clients would need to know the IP addresses of each Pod to access the application. Instead, you can create a Service to expose the Pods, and clients can then connect to the Service's IP address or DNS name, which routes traffic to the appropriate Pods.
Commands
1. Creating a Service
To create a Service, you can use the following command, assuming you have a configuration file (service.yaml) defined:
kubectl apply -f service.yaml
Where service.yaml may look like:
2. Listing all services
This following command list all services in the current namespace.
kubectl get services
or
kubectl get svc
To list deployments in other namespaces, we can use -n switch:
kubectl get services -n my-namespace
my-namespace is the name of the namespace where services are present.
3. Deleting a service
This following command delete a service called my-service in the current namespace.
kubectl delete service my-service
To delete services in another namespace (e.g., my-namespace), we can use the following command:
kubectl delete service my-service -n my-namespace
my-namespace is the name of the namespace where services are present.
Once you have installed the necessary software, you can start using Kubernetes commands to manage your clusters. Here are some basic commands to get you started:
1. Creating/Starting a cluster
To start a cluster using minikube, we can use the following command:
minikube start
The minikube start command initializes and starts a local Kubernetes cluster using Minikube. It sets up a single-node Kubernetes environment on your local machine, allowing you to run and manage Kubernetes applications locally. This is useful for development, testing, and learning Kubernetes without requiring a full-scale cloud setup.
2. Enabling a deployment for internet access
The command `kubectl expose deployment` creates a service that exposes the `my-app` deployment to external traffic.
kubectl expose deployment my-app --type=LoadBalancer
- --type=LoadBalancer: This option specifies that the service should be of type `LoadBalancer`, which is used to expose the service externally. It provides an external IP address (if supported by the underlying cloud provider) and routes external traffic to the pods managed by the `my-app` deployment.
This setup is commonly used when you want to make an application accessible from outside the cluster, such as from the internet.
3. Get the application URL:
To get the details about your application (among) other things, we can use the following command:
kubectl get service my-app
This command retrieves and displays detailed information about the service named my-app in your Kubernetes cluster. It provides key details including the service's name, type (such as ClusterIP, NodePort, or LoadBalancer), cluster IP address, external IP address (if available), ports, and the service's age. This command is valuable for checking the status and configuration of a specific service, helping you monitor and troubleshoot service-related issues.
4. Viewing Pod's Logs:
We can retrieve logs for the pod using the following command:
kubectl logs pod my-pod
The command `kubectl logs pod my-pod` retrieves and displays the logs from the `my-pod` pod in the Kubernetes cluster. This output typically includes the standard output and error streams from the containers running within that pod. It's a useful command for debugging and monitoring the behavior of your applications, as it allows you to view the log output directly from the pod.
At FAMRO LLC, we specialize in cloud and DevOps services, helping businesses seamlessly transition to modern infrastructure. Whether you're starting from scratch or need expert advice on optimizing your existing setup, our experienced team is here to guide you every step of the way. Discover how we can help you implement and manage Kubernetes for robust, scalable applications by visiting our services page.
Please don't hesitate to Contact us for free initial consultation.
Our services enable clients to grow their business by providing customized technical solutions that improve infrastructure, streamline software development, and enhance project management.
Our technical consultancy and project management services ensure successful project outcomes by reviewing project requirements, gathering business requirements, designing solutions, and managing project plans with resource augmentation for business analyst and project management roles.