ripgrep in context of Kubernetes
23 July, 2023 - Tags: kubernetes, ripgrep
Ripgrep
ripgrep is a command line tool that helps you in searching either code or a pattern in a file or a directory. It's awesome and once you start using this, you'll start liking the default behaviour of the ripgrep. In this blog post, I'm going to show you how you can use ripgrep in context of kubernetes. This is an ever growing list and I'll keep adding to this list, if I find something new and cool things that can be solved using ripgrep.
Listing all the pods that are not running
Many a times, you want to list all the pods that are not running in the cluster. This can be achieved using kubectl but it's hard that way. Let's use ripgrep for the purpose of printing out the pods that are not running.
kubectl get pods | rg -v 'Running'
The above command will list all the pods that are not running and you can use this information to debug things further.
Listing storageclasses
This is more of a pattern that you can use against other k8s resources too.
Say you want to list all the storageclass with volumeBindingMode of Immediate
or WaitForFirstConsumer
then you can use the following.
kubectl get sc | rg -i 'waitforfirstconsumer|immediate'
premium-rwo pd.csi.storage.gke.io Delete WaitForFirstConsumer true 29m
standard kubernetes.io/gce-pd Delete Immediate true 29m
standard-rwo (default) pd.csi.storage.gke.io Delete WaitForFirstConsumer true 29m
Listing Events
kubectl get ev | rg 'Failed|BackOff'
The above will select all the lines that will contain the word Failed
or BackOff
I use to to get context around events that are failing in the cluster as of now.
23m Warning Failed pod/pod Failed to pull image "agentx": rpc error: code = Unknown desc = failed to pull and unpack image "docker.io/library/agentx:latest": failed to resolve reference "docker.io/library/agentx:latest": failed to do request: Head "https://registry-1.docker.io/v2/library/agentx/manifests/latest": dial tcp: lookup registry-1.docker.io on [2001:4860:4860::8844]:53: read udp [fc00:f853:ccd:e793::3]:48144->[2001:4860:4860::8844]:53: i/o timeout
21m Warning Failed pod/pod Error: ErrImagePull
4m8s Normal BackOff pod/pod Back-off pulling image "agentx"
21m Warning Failed pod/pod Error: ImagePullBackOff
23m Warning Failed pod/pod Failed to pull image "agentx": rpc error: code = Unknown desc = failed to pull and unpack image "docker.io/library/agentx:latest": failed to resolve reference "docker.io/library/agentx:latest": failed to do request: Head "https://registry-1.docker.io/v2/library/agentx/manifests/latest": dial tcp: lookup registry-1.docker.io on [2001:4860:4860::8844]:53: read udp [fc00:f853:ccd:e793::3]:48339->[2001:4860:4860::8844]:53: i/o timeout
22m Warning Failed pod/pod Failed to pull image "agentx": rpc error: code = Unknown desc = failed to pull and unpack image "docker.io/library/agentx:latest": failed to resolve reference "docker.io/library/agentx:latest": failed to do request: Head "https://registry-1.docker.io/v2/library/agentx/manifests/latest": dial tcp: lookup registry-1.docker.io on [2001:4860:4860::8844]:53: read udp [fc00:f853:ccd:e793::3]:50627->[2001:4860:4860::8844]:53: i/o timeout
21m Warning Failed pod/pod Failed to pull image "agentx": rpc error: code = Unknown desc = failed to pull and unpack image "docker.io/library/agentx:latest": failed to resolve reference "docker.io/library/agentx:latest": failed to do request: Head "https://registry-1.docker.io/v2/library/agentx/manifests/latest": dial tcp: lookup registry-1.docker.io on [2001:4860:4860::8844]:53: read udp [fc00:f853:ccd:e793::3]:60303->[2001:4860:4860::8844]:53: i/o timeout
Sometimes, it's also good to use the above command with -i
flag to avoid strict typing. kubectl get ev | rg -i 'failed|backoff'
Future considerations
- Listing all the pods where some of the containers are crashing by filtering through the pattern of
2/3
or1/2
. I don't know if it's required but something that's coming in my head as of writing this. - Listing down all the pods that are frequently starting.
Please note that some of this can be simplified using awk
combined to the mathematical operators that ships with awk
.
With the examples, I showed above, I hope you got some understanding of how to use ripgrep in context of kubernetes. If you've some more cool ideas where one can use this then please let me know via my socials and I would be grateful to note this down on my blog. Thanks for reading.