• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • Maven
  • All
  • About
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.

    See 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):
      $ sudo apt update
    • [Optional] Install packages to allow apt to use a repository over HTTPS (Ubuntu):
      $ sudo apt install \
          apt-transport-https \
          curl
    • Download kubectl:
      $ curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
    • Make kubectl executable:
      $ chmod +x ./kubectl
    • Move kubectl to /usr/local/bin/:
      $ sudo mv ./kubectl /usr/local/bin/kubectl
    • Verify kubectl:
      $ kubectl version --client
      Client Version: v.1.32.3

    You can use the help command to see details information about the commands supported by kubectl:
    $ kubectl help
    Use "kubectl <command> --help" for more information about a given command.
    $ kubectl describe --help
    Show details of a specific resource or group of resources.
    ...
    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):
    $ cp "$HOME/.kube/config" "$HOME/.kube/config.bak"
    If you have installed Kubernetes with Docker Desktop, you should see something similar to the following:
    $ cat $HOME/.kube/config
    apiVersion: v1
    clusters:
    - cluster:
        certificate-authority-data: DATA+OMITTED
        server: https://kubernetes.docker.internal:6443
      name: docker-desktop
    contexts:
    - context:
        cluster: docker-desktop
        user: docker-desktop
      name: docker-desktop
    current-context: docker-desktop
    kind: Config
    preferences: {}
    users:
    - name: docker-desktop
      user:
        client-certificate-data: DATA+OMITTED
        client-key-data: DATA+OMITTED
    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:
    $ kubectl create namespace my-namespace
    namespace/my-namespace created
    To list all namespaces:
    $ kubectl get namespaces
    NAME                   STATUS   AGE
    default                Active   16d
    kube-node-lease        Active   16d
    kube-public            Active   16d
    kube-system            Active   16d
    kubernetes-dashboard   Active   7d20h
    my-namespace           Active   3s
    To associate a namespace with a context:
    $ kubectl config set-context docker-desktop --namespace=my-namespace
    Context "docker-desktop" modified.
    To view the information about the context (the context "docker-desktop" was associated to the namespace "my-namespace"):
    $ kubectl config view --minify
    apiVersion: v1
    clusters:
    - cluster:
        certificate-authority-data: DATA+OMITTED
        server: https://kubernetes.docker.internal:6443
      name: docker-desktop
    contexts:
    - context:
        cluster: docker-desktop
        namespace: my-namespace
        user: docker-desktop
      name: docker-desktop
    current-context: docker-desktop
    kind: Config
    preferences: {}
    users:
    - name: docker-desktop
      user:
        client-certificate-data: DATA+OMITTED
        client-key-data: DATA+OMITTED
    You can create new contexts with the "kubectl config set-context" command:
    $ kubectl config set-context docker-desktop-new-context --namespace=my-namespace
    Context "docker-desktop-new-context" created.
    The "--cluster" and "--user" flags can be used to set a specific cluster and user to the context:
    $ kubectl config set-context docker-desktop-new-context --cluster=docker-desktop --user=docker-desktop
    Context "docker-desktop-new-context" modified.
    To make a context as the current context:
    $ kubectl config use-context docker-desktop-new-context
    Switched to context "docker-desktop-new-context".
  4. kubectl auto completion
    To use auto completion with kubectl you need first to install the bash-completion package (Ubuntu):
    $ sudo apt-get install bash-completion
    To enable auto completion for kubectl:
    $ source <(kubectl completion bash)
    To enable auto completion permanently for a user:
    $ echo "source <(kubectl completion bash)" >> ~/.bashrc
  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
    {
      "major": "1",
      "minor": "19",
      "gitVersion": "v1.19.7",
      "gitCommit": "1dd5338295409edcfff11505e7bb246f0d325d15",
      "gitTreeState": "clean",
      "buildDate": "2021-01-13T13:15:20Z",
      "goVersion": "go1.15.5",
      "compiler": "gc",
      "platform": "linux/amd64"
    }
    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:
    $ kubectl get Pods
    NAME       READY   STATUS    RESTARTS   AGE
    dnsutils   1/1     Running   0          12d
    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.
    $ kubectl get Pods -o wide --no-headers | awk {'print $1 " | " $6 " | " $7 '}
    dnsutils | 10.1.0.152 | docker-desktop
    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.
    $ kubectl get Pods -o json
    {
        "apiVersion": "v1",
        "items": [
            {
                "apiVersion": "v1",
                "kind": "Pod",
                ...
            }
            ...
        ],
        "kind": "List",
        "metadata": {
            "resourceVersion": "",
            "selfLink": ""
        }
    }
    You get a similar response by targeting directly the API server endpoint: http://localhost:8001/api/v1/namespaces/default/pods/
    {
      "kind": "PodList",
      "apiVersion": "v1",
      "metadata": {
        "selfLink": "/api/v1/namespaces/default/pods/",
        "resourceVersion": "2967265"
      },
      "items": [
        {
          "metadata": {
            "name": "dnsutils",
            "namespace": "default",
            ...
           }
           ...
         }
         ...
       ]
    }
    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/
    $ kubectl get pods -o jsonpath --template={.items..status.podIP}
    10.1.0.152
    Another way to get details about an object is to use the "kubectl describe" command: kubectl describe <resource-name> <object-name>
    $ kubectl describe Pods dnsutils
    Name:         dnsutils
    Namespace:    default
    Priority:     0
    Node:         docker-desktop/192.168.65.4
    ...
    You can get information about a specific Kubernetes resource: kubectl explain <resource-name>
    $ kubectl explain Pod
    KIND:       Pod
    VERSION:    v1
    ...
  6. Uninstall kubectl
    • Remove kubectl:
      $ sudo rm -f /usr/local/bin/kubectl
    • [Optional] Remove kubectl configuration folder:
      $ rm -rf ~/.kube/
© 2025  mtitek