Skip to main content

kubectl - The Command-Line Tool

kubectl is the primary command-line tool for interacting with a Kubernetes cluster. It allows you to deploy applications, inspect and manage cluster resources, and view logs.

Common kubectl Commands

  • kubectl get: List resources.

    # Get all pods in the current namespace
    kubectl get pods

    # Get a specific pod
    kubectl get pod my-pod

    # Get all pods in all namespaces
    kubectl get pods --all-namespaces
  • kubectl describe: Show detailed information about a resource.

    # Get detailed information about a pod
    kubectl describe pod my-pod
  • kubectl apply: Apply a configuration to a resource by filename or stdin.

    # Apply the configuration in my-deployment.yaml
    kubectl apply -f my-deployment.yaml
  • kubectl delete: Delete resources.

    # Delete a pod named my-pod
    kubectl delete pod my-pod

    # Delete all resources defined in my-deployment.yaml
    kubectl delete -f my-deployment.yaml
  • kubectl logs: Print the logs for a container in a pod.

    # Get the logs for a pod
    kubectl logs my-pod

    # Follow the logs for a pod
    kubectl logs -f my-pod
  • kubectl exec: Execute a command in a container.

    # Get an interactive shell to a running container
    kubectl exec -it my-pod -- /bin/bash

Context and Configuration

kubectl uses a configuration file called kubeconfig to store information about clusters, users, namespaces, and authentication mechanisms. The default location for the kubeconfig file is ~/.kube/config.

  • View the current context:

    kubectl config current-context
  • List all contexts:

    kubectl config get-contexts
  • Switch to a different context:

    kubectl config use-context my-cluster-context