DEV Community

Cover image for What is Kubernetes? 🧐
Roshdi Raed
Roshdi Raed

Posted on

What is Kubernetes? 🧐

Certainly! Here's a detailed explanation of Kubernetes, including key points, commands, and code examples:

1. What is Kubernetes?

  • Kubernetes is an open-source container orchestration platform designed to automate the deployment, scaling, and management of containerized applications.
  1. Key Concepts:
    a. Pod: The smallest unit of deployment in Kubernetes. It represents a single instance of a running process.
    b. Node: A physical or virtual machine in the Kubernetes cluster where containers are deployed.
    c. ReplicaSet: Ensures a specific number of pod replicas are running at all times.
    d. Deployment: A higher-level abstraction that manages ReplicaSets and provides rolling updates and rollbacks.
    e. Service: An abstract way to expose an application running on a set of pods.
    f. Namespace: A logical partitioning of the cluster used to separate resources and manage access.

  2. Architecture:

    • Master Node: Controls the cluster and manages scheduling, scaling, and overall cluster state.
    • Worker Nodes: Run the actual application workloads in the form of pods.
  3. Download and Installation:

    • Visit the official Kubernetes website or use a package manager like apt, yum, or Homebrew.
    • For example, to install using the package manager on Ubuntu, run the following command:
     sudo apt-get update && sudo apt-get install -y kubelet kubeadm kubectl
    
  4. Setting up a Cluster:

    • Initialize the cluster on the master node using kubeadm:
     sudo kubeadm init
    
  • Follow the instructions provided to set up the cluster and configure the networking.
  1. Interacting with the Cluster:

    • kubectl is the command-line interface to interact with the Kubernetes cluster.
    • To check the cluster status, run:
     kubectl cluster-info
    
  • To get information about nodes, pods, services, etc., use various kubectl commands such as:

     kubectl get nodes
     kubectl get pods
     kubectl get services
    
  1. Deploying an Application:

    • Create a deployment YAML file, e.g., app-deployment.yaml, defining the application's specifications.
     apiVersion: apps/v1
     kind: Deployment
     metadata:
       name: my-app
     spec:
       replicas: 3
       selector:
         matchLabels:
           app: my-app
       template:
         metadata:
           labels:
             app: my-app
         spec:
           containers:
           - name: my-app-container
             image: my-app-image:latest
             ports:
             - containerPort: 80
    
  • Apply the deployment using kubectl:

     kubectl apply -f app-deployment.yaml
    
  1. Exposing the Application:

    • Create a service YAML file, e.g., app-service.yaml, defining the service's specifications.
     apiVersion: v1
     kind: Service
     metadata:
       name: my-app-service
     spec:
       selector:
         app: my-app
       ports:
       - protocol: TCP
         port: 80
         targetPort: 80
       type: NodePort
    
  • Apply the service using kubectl:

     kubectl apply -f app-service.yaml
    
  1. Scaling the Application:

    • Scale the deployment to increase the number of replicas:
     kubectl scale deployment my-app --replicas=5
    
  2. Rolling Updates and Rollbacks:

    • Update the deployment with a new version of the container

image:

  ```
  kubectl set image deployment/my-app my-app-container=my-app-
  ```
Enter fullscreen mode Exit fullscreen mode

image:v2

- Monitor the rollout status:
Enter fullscreen mode Exit fullscreen mode
  ```
  kubectl rollout status deployment/my-app
  ```
Enter fullscreen mode Exit fullscreen mode
- Rollback to the previous version if needed:
Enter fullscreen mode Exit fullscreen mode
  ```
  kubectl rollout undo deployment/my-app
  ```
Enter fullscreen mode Exit fullscreen mode

This provides a general overview of Kubernetes, its architecture, and some essential commands for deploying and managing applications. Keep in mind that this is just the tip of the iceberg, and Kubernetes offers a wide range of features and capabilities for container orchestration.

Did you benefit from the explanation?

Top comments (0)