Ninja Docs Help

kubectl

Revision

Date

Description

1.0

26.07.2024

Init document

Introduction

kubectl is the command-line tool used to interact with Kubernetes clusters. It provides commands for deploying applications, inspecting and managing cluster resources, and viewing logs. kubectl is essential for developers, operators, and administrators working with Kubernetes as it allows them to control and monitor their clusters.

Installation

Installing kubectl involves downloading the binary and setting it up on your system. Here are the steps to install kubectl on various operating systems:

  1. Download the latest release:


    curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
  2. Make the kubectl binary executable:


    chmod +x kubectl
  3. Move the binary to a directory in your PATH:


    sudo mv kubectl /usr/local/bin/
  4. Test to ensure the version you installed is up-to-date:


    kubectl version --client
  • Using apt on Debian/Ubuntu:


    sudo apt-get update sudo apt-get install -y apt-transport-https gnupg2 curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list sudo apt-get update sudo apt-get install -y kubectl
  • Using yum on CentOS/RHEL:


    cat << EOF > /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64 enabled=1 gpgcheck=1 repo_gpgcheck=1 gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg EOF sudo yum install -y kubectl
  1. Download the latest release:


    curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/darwin/amd64/kubectl"
  2. Make the kubectl binary executable:


    chmod +x kubectl
  3. Move the binary to a directory in your PATH:


    sudo mv kubectl /usr/local/bin/
  4. Test to ensure the version you installed is up-to-date:


    kubectl version --client
  1. Install Homebrew (if not already installed):


    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  2. Install kubectl with Homebrew:


    brew install kubectl
  3. Verify installation:


    kubectl version --client
  1. Download the latest release:


    curl -LO "https://dl.k8s.io/release/$(curl -s https://dl.k8s.io/release/stable.txt)/bin/windows/amd64/kubectl.exe"
  2. Add the binary to your PATH:


    • Move the kubectl.exe binary to a directory in your PATH.

    • You can do this by right-clicking on "This PC" > "Properties" > "Advanced system settings" > "Environment Variables", and then editing the PATH variable.

  3. Test to ensure the version you installed is up-to-date:


    kubectl version --client

Verifying the Installation

After installing kubectl, verify that it is correctly installed and that the version is up-to-date by running:

kubectl version --client

This should output the version information of the kubectl client, confirming that it is correctly installed and configured.

Key Features of kubectl

  1. Cluster Management:

    • Configuration: Use kubectl config to set cluster credentials, context, and configurations.

    • Namespace Management: Create, list, and switch between namespaces using commands like kubectl create namespace and kubectl get namespaces.

  2. Resource Management:

    • Create Resources: Deploy applications and services using kubectl apply -f <file> or kubectl create <resource>.

    • Inspect Resources: View details of resources such as pods, services, and deployments with kubectl get <resource> and kubectl describe <resource>.

    • Update Resources: Modify resources using kubectl edit <resource> or kubectl patch <resource>.

    • Delete Resources: Remove resources with kubectl delete <resource>.

  3. Application Management:

    • Logs: View logs of a specific pod using kubectl logs <pod-name>.

    • Exec: Execute commands in a container within a pod using kubectl exec -it <pod-name> -- <command>.

    • Port Forwarding: Forward one or more local ports to a pod using kubectl port-forward <pod-name> <local-port>:<pod-port>.

  4. Debugging and Troubleshooting:

    • Describe Resources: Use kubectl describe <resource> <name> to get detailed information about a resource.

    • Events: List events with kubectl get events to see what has happened in the cluster.

    • Debug: Use kubectl debug to create debugging sessions and troubleshoot issues.

  5. Resource Visualization:

    • Top Command: Monitor resource usage of nodes and pods with kubectl top nodes and kubectl top pods.

    • Apply Dry Run: Use kubectl apply --dry-run=client to validate configurations without applying them.

Common kubectl Commands

  • Getting Started:

    kubectl config set-cluster <cluster-name> kubectl config set-context <context-name> --cluster=<cluster-name> kubectl config use-context <context-name>
  • Basic Operations:

    kubectl get nodes kubectl get pods kubectl get services
  • Resource Management:

    kubectl apply -f deployment.yaml kubectl delete pod <pod-name> kubectl create namespace <namespace-name> kubectl describe pod <pod-name>
  • Logging and Debugging:

    kubectl logs <pod-name> kubectl exec -it <pod-name> -- /bin/bash kubectl port-forward <pod-name> 8080:80 kubectl get events
  • Monitoring:

    kubectl top nodes kubectl top pods

Benefits of Using kubectl

  • Comprehensive Control: Offers extensive control over all aspects of a Kubernetes cluster.

  • Automation and Scripting: Easily scriptable for automation tasks and CI/CD pipelines.

  • Flexibility: Works across various Kubernetes environments, including on-premises clusters and cloud-based services.

  • Community and Documentation: Well-supported by the Kubernetes community with extensive documentation and resources available.

kubectl is an indispensable tool for anyone working with Kubernetes, providing powerful and flexible commands to manage, monitor, and troubleshoot Kubernetes clusters and applications.

Last modified: 17 February 2025