How to use the command `kube-fzf` (with examples)
Kube-fzf is a command-line tool used for fuzzy searching of Kubernetes pods. It provides a convenient way to quickly find pod details, describe pods, tail pod logs, exec into pods’ containers, and port-forward pods. This article will illustrate each of these use cases with examples.
Use case 1: Get pod details (from current namespace)
Code:
findpod
Motivation:
You may want to get the details of a specific pod in your current namespace. Using the findpod
command, you can easily search for and retrieve information about the desired pod.
Explanation:
The findpod
command without any arguments will search for pods in the current namespace and display their details. It performs a fuzzy-search based on the input provided after executing the command.
Example output:
POD NAME READY STATUS RESTARTS AGE IP NODE
nginx-78c5b8ffd 1/1 Running 0 2m 192.168.1.10 worker-node-1
redis-5df468925 1/1 Running 0 5m 192.168.1.20 worker-node-2
Use case 2: Get pod details (from all namespaces)
Code:
findpod -a
Motivation:
You may need to get details about pods from all namespaces in your Kubernetes cluster. The -a
flag allows you to search for pods across all namespaces.
Explanation:
By using the -a
flag with the findpod
command, it will search for pods in all namespaces and display their details.
Example output:
NAMESPACE POD NAME READY STATUS RESTARTS AGE IP NODE
default nginx-78c5b8ffd 1/1 Running 0 2m 192.168.1.10 worker-node-1
redis redis-5df468925 1/1 Running 0 5m 192.168.1.20 worker-node-2
kube-system kube-proxy-dsf 1/1 Running 0 5m 192.168.1.30 worker-node-3
Use case 3: Describe a pod
Code:
describepod
Motivation:
You may want to get a detailed description of a specific pod. The describepod
command provides you with a convenient way to fetch and display the pod’s description.
Explanation:
By executing the describepod
command, it will prompt you to enter the name of the pod. After providing the name, the command will describe the pod in detail, including information such as its status, events, containers, and conditions.
Example output:
Name: nginx-78c5b8ffd
Namespace: default
...
Use case 4: Tail pod logs
Code:
tailpod
Motivation:
You may need to monitor real-time logs of a specific pod. The tailpod
command allows you to tail the logs of a pod and continuously display new log entries as they are generated.
Explanation:
Executing the tailpod
command will prompt you to enter the name of the pod for which you want to tail the logs. After providing the pod name, the command will continuously display the new log entries as they appear.
Example output:
2022-01-01 12:34:00 [INFO] Log entry 1
2022-01-01 12:34:01 [INFO] Log entry 2
2022-01-01 12:34:02 [INFO] Log entry 3
...
Use case 5: Exec into a pod’s container
Code:
execpod shell_command
Motivation:
You may want to execute a shell command inside a specific container within a pod. The execpod
command allows you to run arbitrary shell commands inside a pod’s container.
Explanation:
The execpod
command is used to execute a shell command inside a pod’s container. After the command, you need to provide the name of the pod and the shell command you want to execute.
Example output:
$ execpod my-pod echo "Hello, Kubernetes!"
Hello, Kubernetes!
Use case 6: Port-forward a pod
Code:
pfpod port_number
Motivation:
You may need to access a specific port of a pod directly from your local machine. The pfpod
command allows you to easily set up port forwarding to a pod, enabling you to access the pod’s services.
Explanation:
Executing the pfpod
command followed by the desired port number will establish a port-forwarding configuration to the pod. With the port-forwarding set up, you can access the pod’s services by browsing to localhost:port_number
in your web browser or using other tools that require direct access to the pod.
Example output:
Port-forwarding pod my-pod on port 8080...
Conclusion:
Using the kube-fzf
command-line tool, you can perform various actions related to Kubernetes pods with ease. Whether it’s searching for pod details, describing pods, tailing logs, executing commands inside containers, or port-forwarding, the kube-fzf
tool provides a convenient and efficient way to interact with your Kubernetes cluster.