Reading time: 3 minutes

On this blog post, I’m gonna write some additional commands that I use while working with K8s. Some you may already know, others uncommon, or rarely used tricks that may save you in a pinch.

(I’ll be displaying the said commands in the default namespace, of course you’ll need to add ‘-n namespace’ if you have it different)

How to check for YAML differences before applying?

Checking for differences between already present manifest with new, but changed, can be done with:

$ kubectl diff -f new-file.yaml

Although it’s near the same syntax with the most common ‘apply’ one, I haven’t heard many people using it. Good thing we have VCS, say no to ad-hoc changes!

I’ve applied the YAML file, and then modified it to start with two replicas instead of one. Here is the truncated output:

- generation: 6
+ generation: 7
name: api-gateway
namespace: default
resourceVersion: "11021062"
@@ -13,7 +13,7 @@
uid: 00183f87-a91d-42d3-a6bc-04e1914047dc
podManagementPolicy: OrderedReady
- replicas: 1
+ replicas: 2
revisionHistoryLimit: 10

How to sort persistent volume by storage capacity?

This command will sort them from lowest to highest capacity in order.

$ kubectl get pv


NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                            STORAGECLASS   REASON   AGE
pvc-de66d7b8-13e4-49d9-b75c-d12af266a5b6   1Gi        RWO            Delete           Bound    default/test-pv                  cloud-ssd               35s
pvc-d6483edb-f137-4a5d-9ee3-cf5d13800df3   2Gi        RWO            Delete           Bound    default/test-pv2                 cloud-ssd               35s
pvc-99618867-038c-4d1b-8d9d-1e363e76c4bb   7Gi        RWO            Delete           Bound    default/mongo-pvc                cloud-ssd               25h
pvc-7995d0af-5b79-417b-81e2-17679932d2d5   19Gi       RWO            Delete           Bound    lens-metrics/data-prometheus-0   standard                16m

Command to check for cluster events?

Use this. I find it very useful, it displays a ton of info without me manually going ‘describe’ on every pod. The sort-by filter will list the output with the latest changes first.

$ kubectl get events --sort-by=.metadata.creationTimestamp

Output picture since it prints too much data that won’t fit in a paragraph.

How to get logs from the container with specified time or date?

On the logs command, you can also specify since when do you want the logs. Either in format for example 1h, or by date.

$ kubectl logs pod-name --since=1h

variation with date using ISO 8601 format:

$ kubectl logs pod-name --since-time=2020-08-10T18:00:00Z

Sample output:

2020-08-10 18:22:50.246 INFO 1 --- [MessageBroker-1] o.s.w.s.c.WebSocketMessageBrokerStats : WebSocketSession[0 current WS(0)-HttpStream(0)-HttpPoll(0), 0 total, 0 closed abnormally (0 connect failure, 0 send limit, 0 transport error)], stompSubProtocol[processed CONNECT(0)-CONNECTED(0)-DISCONNECT(0)], stompBrokerRelay[null], inboundChannel[pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0], outboundChannelpool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0], sockJsScheduler[pool size = 2, active threads = 1, queued tasks = 1, completed tasks = 18472]

How to sum all CPU or RAM pod usage?

A bit of awk should help us with that. If you run:

$ kubectl top pods
NAME                                CPU(cores)  MEMORY(bytes)
api-gateway-0                          18m         480Mi
mongodb-7dc4596644-m9gq6               4m          129Mi
position-simulator-7d555dc46-g446w     21m         398Mi
position-tracker-5b6685986d-d8vwt      18m         484Mi
queue-577c5fccf4-zcpnp                 30m         211Mi
webapp-77984bc8b4-29g7g                0m          5Mi

And in the output, we can see that the CPU is column two, and RAM in column three. We can get those with $2 or $3 respectively. *I’ve added /1000 to divide the output by a thousand.

$ kubectl top pods | awk '{sum += $2} END {print sum/1000}' 

Output CPU in cores: 0.089

$ kubectl top pods | awk '{sum += $3} END {print sum/1000}'

Output RAM in GB: 1.708

That’s all for now, more will be added in the next part.