• Home
  • Docker
  • Kubernetes
  • Java
  • Ubuntu
  • Maven
  • Big Data
  • CI
  • Install
  • Samples
  • Archived
Kubernetes | kubectl
  1. Notes
  2. Install kubectl
  3. kubectl configuration file
  4. kubectl auto completion
  5. View Kubernetes resources
  6. Uninstall kubectl

  1. Notes
    kubectl is the Kubernetes CLI (Command-Line Interface) tool that can be used to interact with the Kubernetes API server.

    It can be used to manage the Kubernetes objects (Pods, ...) and the Kubernetes cluster components.

    Please visit this page for more details on how to install kubectl: https://kubernetes.io/docs/tasks/tools/

    Cloud providers also provide their own CLIs:
    • AWS Command Line Interface ("aws"): https://aws.amazon.com/cli/

    • Azure Command-Line Interface ("az"): https://learn.microsoft.com/en-us/cli/azure/

    • Google Cloud Command Line Interface ("gcloud"): https://cloud.google.com/cli
  2. Install kubectl
    • Check if any update (Ubuntu):

    • [Optional] Install packages to allow apt to use a repository over HTTPS (Ubuntu):

    • Download kubectl:

    • Make kubectl executable:

    • Move kubectl to /usr/local/bin/:

    • Verify kubectl:

    You can use the help command to see details information about the commands supported by kubectl:

    Use "kubectl <command> --help" for more information about a given command.

    Use "kubectl options" for a list of global command-line options (applies to all commands).
  3. kubectl configuration file
    The kubectl configuration file is located at "$HOME/.kube/config".

    You might want to backup the kubectl configuration file before modifying it (either manually or through kubectl):

    If you have installed Kubernetes with Docker Desktop, you should see something similar to the following:

    The configuration file might have multiple contexts.
    A context regroup information about a Kubernetes cluster, the user credentials to authenticate against the cluster, and a namespace.

    Kubernetes uses namespaces to regroup some of the Kubernetes objects.
    By default the "default" namespace is created and the kubectl command-line tool uses it to interacts with the Kubernetes API server.

    To create a new namespace:

    To list all namespaces:

    To associate a namespace with a context:

    To view the information about the context (the context "docker-desktop" was associated to the namespace "my-namespace"):

    You can create new contexts with the "kubectl config set-context" command:

    The "--cluster" and "--user" flags can be used to set a specific cluster and user to the context:

    To make a context as the current context:
  4. kubectl auto completion
    To use auto completion with kubectl you need first to install the bash-completion package (Ubuntu):

    To enable auto completion for kubectl:

    To enable auto completion permanently for a user:
  5. View Kubernetes resources
    kubernetes API server provides RESTful endpoints that allow kubectl to make HTTP requests to view and manage Kubernetes resources and their objects.

    The URI of the endpoints can be constructed using the name of a resource (this will return the info of its objects), and can be narrowed by using the name of an object of the resource. The URI can be further detailed if the object regroup other resources and hence the URI can be expanded by adding the name of the resource and any of its objects.

    For example, the following endpoint returns information about all Pods in the default namespace:
    http://localhost:8001/api/v1/namespaces/default/pods/

    If you access http://localhost:8001/ you should see all the endpoints accessible at the ROOT level.

    To see the version of the API server, oen http://localhost:8001/version

    You can use the "kubectl get <resource-name>" command to view information about the objects of a kubernetes resource.
    You can specify an object name "kubectl get <resource-name> <object-name>" to view information about that object.

    By default you can view resources of the current namespace configured for the kubectl config (the "default" namespace is used if none is configured).

    To confugure another namespace for the current context you can use the commande: "kubectl config set-context my-context --namespace=my-namespace"

    You can also override the namespace by using the "--namespace" flag ("-n"): "kubectl get <resource-name> -n another-namespace"

    You can also use the "--all-namespaces" flag to view information about resources from all the namespaces of the cluster: "kubectl get <resource-name> --all-namespaces"

    To see information about the Pods resource:

    You can use the "-o wide" flag to get more information about the resource.
    You can also remove the headers from the output by adding the "--no-headers" flag. This can be useful if you want to do custom actions on the output.

    By default, kubectl doesn't show the full response of the API server.
    You can print the full response in either YAML or JSON formats by using the "-o yaml" or "-o json" flags.

    You get a similar response by targeting directly the API server endpoint: http://localhost:8001/api/v1/namespaces/default/pods/

    You can get detail of an abject by specifying its name after the resource.
    kubectl: "kubectl get Pods dnsutils -o json"
    API Server endpoint: http://localhost:8001/api/v1/namespaces/default/pods/dnsutils

    You can use the "-o jsonpath" flag that allows you to use the JSONPath expressions to filter on specific fields in the JSON object https://kubernetes.io/docs/reference/kubectl/jsonpath/

    Another way to get details about an object is to use the "kubectl describe" command: kubectl describe <resource-name> <object-name>

    You can get information about a specific Kubernetes resource: kubectl explain <resource-name>
  6. Uninstall kubectl
    • Remove kubectl:

    • [Optional] Remove kubectl configuration folder:
© 2025  mtitek