Kubernetes Cheat Sheet: Essential Commands for Beginners

WHAT TO KNOW - Sep 7 - - Dev Community

<!DOCTYPE html>





Kubernetes Cheat Sheet: Essential Commands for Beginners

<br> body {<br> font-family: sans-serif;<br> margin: 0;<br> padding: 20px;<br> }</p> <div class="highlight"><pre class="highlight plaintext"><code> h1, h2, h3 { color: #333; } pre { background-color: #f0f0f0; padding: 10px; border-radius: 4px; font-family: monospace; } code { font-family: monospace; } img { max-width: 100%; height: auto; display: block; margin: 20px auto; } </code></pre></div> <p>



Kubernetes Cheat Sheet: Essential Commands for Beginners



Kubernetes, often referred to as K8s, is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It's a powerful tool for building and deploying microservices-based applications, ensuring high availability, and simplifying the management of complex infrastructure.



This cheat sheet provides a collection of essential Kubernetes commands for beginners to get started with K8s. We'll cover the basics, including creating deployments, services, pods, and namespaces, along with some useful utilities for troubleshooting and managing your applications.



Getting Started


  1. Install Kubernetes

You can install Kubernetes on your local machine, in a cloud environment, or using a managed Kubernetes service. Here are some popular options:

  • Access Your Kubernetes Cluster

    After installing, you need to configure your local machine to access the Kubernetes cluster. This involves setting up kubectl, the command-line tool for interacting with your cluster.

    Using kubectl:

    kubectl version
    

    This command will display the version of kubectl and the server version of your Kubernetes cluster.

    Kubernetes Architecture

    Core Kubernetes Concepts

    Understanding these core concepts is crucial for effective Kubernetes management:

  • Pods

    The smallest deployable unit in Kubernetes. A Pod is a group of one or more containers, sharing the same resources and network namespace.

  • Deployments

    A Kubernetes Deployment is used to manage and update your applications. It defines the desired state of your application, including the number of replicas, the image to use, and the container resources.

  • Services

    Services provide a consistent way to access your Pods. They abstract away the specific Pods running your application, providing a stable network endpoint. Services can be accessed using DNS names or IP addresses.

  • Namespaces

    Namespaces provide a way to logically organize your Kubernetes resources. They are useful for separating resources for different teams, applications, or environments.

  • Controllers

    Controllers are responsible for managing the desired state of your application. They ensure that the desired number of Pods are running and that they are healthy. Common controllers include Deployments, StatefulSets, and DaemonSets.

  • Ingress

    Ingress provides a way to route external traffic to your services. It acts as a reverse proxy, handling incoming HTTP requests and directing them to the appropriate service.

    Essential Kubernetes Commands

  • Creating and Managing Deployments

    Deployments are used to define the desired state of your application and manage updates.

    # Create a Deployment named 'nginx-deployment' with 3 replicas running the 'nginx' image
    kubectl create deployment nginx-deployment --image=nginx --replicas=3
  • Check the status of the Deployment

    kubectl get deployment nginx-deployment

    Scale the Deployment to 5 replicas

    kubectl scale deployment nginx-deployment --replicas=5

    Update the Deployment with a new image

    kubectl set image deployment nginx-deployment nginx=nginx:latest

    1. Creating and Managing Services

    Services provide a way to access your Pods and abstract away the specific Pods running your application.

    Create a Service named 'nginx-service' of type 'LoadBalancer' that exposes port 80 on the Pod to port 80 on the service

    kubectl create service LoadBalancer nginx-service --port=80 --target-port=80 --selector=app=nginx

    Get the external IP address of the service

    kubectl get service nginx-service -o wide

    Delete the service

    kubectl delete service nginx-service


    1. Listing Resources

    Use these commands to get an overview of your Kubernetes resources.

    List all Pods

    kubectl get pods

    List all Deployments

    kubectl get deployments

    List all Services

    kubectl get services

    List all Namespaces

    kubectl get namespaces

    List all nodes

    kubectl get nodes

    Get the status of a specific pod (replace 'nginx-pod' with the actual pod name)

    kubectl get pod nginx-pod -o wide


    1. Managing Pods

    These commands allow you to work directly with Pods.

    Get logs from a specific pod

    kubectl logs nginx-pod

    Describe the details of a specific pod

    kubectl describe pod nginx-pod

    Delete a specific pod

    kubectl delete pod nginx-pod

    Execute a command within a pod

    kubectl exec -it nginx-pod -- bash

    Copy files into a pod

    kubectl cp /path/to/local/file nginx-pod:/path/in/pod


    1. Working with Namespaces

    Use namespaces to logically organize your Kubernetes resources.

    Create a new namespace

    kubectl create namespace my-namespace

    List namespaces

    kubectl get namespaces

    Set the current namespace

    kubectl config set-context --current --namespace=my-namespace

    Delete a namespace (this will delete all resources within the namespace)

    kubectl delete namespace my-namespace


    1. Advanced Operations

    # Apply a configuration file (yaml or json) to your cluster
    kubectl apply -f deployment.yaml
    
    

    Rollback a Deployment to a previous revision

    kubectl rollout undo deployment nginx-deployment

    View events for a specific resource

    kubectl get events --field-selector involvedObject.kind=Deployment,involvedObject.name=nginx-deployment

    Get resource usage for a specific pod

    kubectl top pod nginx-pod



    Troubleshooting



    When things go wrong, you'll need to diagnose and resolve issues. Here are some helpful commands:


    Get logs from a specific pod

    kubectl logs nginx-pod

    Describe the details of a specific pod

    kubectl describe pod nginx-pod

    Check for events related to a resource

    kubectl get events --field-selector involvedObject.kind=Deployment,involvedObject.name=nginx-deployment

    View the status of the kubelet process on a node

    kubectl get nodes --field-selector spec.nodeName=your-node-name

    Get the status of the Kubernetes control plane

    kubectl get componentstatuses






    Best Practices





    Here are some best practices for working with Kubernetes:





    • Use Namespaces:

      Organize your resources for better management and security.


    • Define Resource Limits:

      Limit resource usage for each container to prevent resource starvation.


    • Use Health Checks:

      Ensure your application is healthy and restarts automatically if it fails.


    • Use Persistent Volumes:

      Store data persistently for your applications.


    • Automate Deployments:

      Use tools like Helm or Kustomize to manage and automate deployments.


    • Monitor and Log:

      Implement monitoring and logging solutions to gain insights into your applications and infrastructure.





    Conclusion





    This cheat sheet provided a foundation of essential Kubernetes commands and concepts for beginners. By familiarizing yourself with these commands and understanding the underlying concepts, you can start deploying and managing containerized applications in Kubernetes. Remember to practice, explore documentation, and utilize community resources to further your Kubernetes journey.




    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
    Terabox Video Player