kubectl: command not found

The Kubernetes command-line tool, kubectl is used to run commands against Kubernetes clusters. You’ll use kubectl to inspect and manage your service’s cluster resources and view logs. Some commonly used commands for retrieving information about a Kubernetes cluster are as follows:

  • kubectl get shows information about the specified API object.
  • kubectl describe gives more detail about the specified API object.
  • kubectl logs display log output from containers.

If you doubt how to use the kubectl tool, the kubectl help and kubectl –help commands are always available and provide very useful information on how to use the kubectl tool.

If you encounter the below error while running the kubectl command:

kubectl: command not found

you may try installing the below package as per your choice of distribution.

Distribution Command
OS X brew install kubernetes-cli
Debian apt-get install kubernetes-client
CentOS yum install kubernetes-client
Fedora dnf install kubernetes-client

kubectl Command Examples

1. List information about a resource with more details:

$ kubectl get pod|service|deployment|ingress|... -o wide

2. Update specified pod with the label ‘unhealthy’ and the value ‘true’:

$ kubectl label pods name unhealthy=true

3. List all resources with different types:

$ kubectl get all

4. Display resource (CPU/Memory/Storage) usage of nodes or pods:

$ kubectl top pod|node

5. Print the address of the master and cluster services:

$ kubectl cluster-info

6. Display an explanation of a specific field:

$ kubectl explain pods.spec.containers

7. Print the logs for a container in a pod or specified resource:

$ kubectl logs pod_name

8. Run command in an existing pod:

$ kubectl exec pod_name -- ls /

9. You can also use the exec command to execute a command in a running container:

$ kubectl exec -it pod_name -- bash

10. If you don’t have bash or some other terminal available within your container, you can always attach to the running process:

$ kubectl attach -it pod_name

11. You can also copy files to and from a container using the cp command:

$ kubectl cp [pod-name]:[/path/to/remote/file] [/path/to/local/file]

12. If you want to access your Pod via the network, you can use the port-forward command to forward network traffic from the local machine to the Pod.

$ kubectl port-forward [pod-name] 8080:80

13. If you want to view Kubernetes events, you can use the kubectl get events command to see a list of the latest 10 events on all objects in a given namespace:

$ kubectl get events

14. If you are interested in how your cluster uses resources, you can use the top command to see the list of resources in use by either nodes or Pods. This command:

$ kubectl top nodes

15. Let’s assume that you have a simple object stored in obj.yaml. You can use kubectl to create this object in Kubernetes by running:

$ kubectl apply -f obj.yaml

16. Similarly, after you make changes to the object, you can use the apply command again to update the object:

$ kubectl apply -f obj.yaml

17. When you want to delete an object, you can simply run:

$ kubectl delete -f obj.yaml

18. Labels and annotations are tags for your objects. For example, to add the color=red label to a Pod named bar, you can run:

$ kubectl label pods bar color=red

19. If you would like to see a list of supported fields for each supported type of Kubernetes object, you can use the explain command:

$ kubectl explain pods

20. If you are interested in more detailed information about a particular object, use the describe command:

$ kubectl describe [resource-name] [obj-name]
Related Post