Kubernetes: common pod operations

In this article i will show you how to create pods from the command line in kubernetes, to avoid extensive complexity and focus only to the…

Kubernetes: common pod operations
Photo by Growtika on Unsplash

In this article i will show you how to create pods from the command line in kubernetes, to avoid extensive complexity and focus only to the commands i will not use files that describe a pod but i will use simple commands to run as pods like the ping command using a minimal image.

How to create a pod

To create a pod with a single container is super easy

$ kubectl run ping --image=alpine --restart=Never -- ping 8.8.8.8 
pod/ping created

This command will start a pod with the image alpine, it will not restart in case of failure and the command that will be executed is ping towards 8.8.8.8

How to get a list of running pods

$ kubectl get pods 
NAME   READY   STATUS    RESTARTS   AGE 
ping   1/1     Running   0          4m18s

The output of the command gives the names of the running pods, in our case is ping, the status, number of restarts and how long it runs.

How to view logs of a running pod

When we say pod logs we mean the stdout/stderr of a running pod, to view the last 5 lines of output we give the following command

$ kubectl logs ping --tail=5 
64 bytes from 8.8.8.8: seq=447 ttl=54 time=49.601 ms 
64 bytes from 8.8.8.8: seq=448 ttl=54 time=49.463 ms 
64 bytes from 8.8.8.8: seq=449 ttl=54 time=49.530 ms 
64 bytes from 8.8.8.8: seq=450 ttl=54 time=49.547 ms 
64 bytes from 8.8.8.8: seq=451 ttl=54 time=49.595 ms

We can even add timestamps on each log line with the --timestamps parameter

$ kubectl logs ping --tail=5 --timestamps 
2023-02-28T15:12:40.258188552+02:00 64 bytes from 8.8.8.8: seq=669 ttl=54 time=49.607 ms 
2023-02-28T15:12:41.258237567+02:00 64 bytes from 8.8.8.8: seq=670 ttl=54 time=49.517 ms 
2023-02-28T15:12:42.258456090+02:00 64 bytes from 8.8.8.8: seq=671 ttl=54 time=49.546 ms 
2023-02-28T15:12:43.258481029+02:00 64 bytes from 8.8.8.8: seq=672 ttl=54 time=49.481 ms 
2023-02-28T15:12:44.258586900+02:00 64 bytes from 8.8.8.8: seq=673 ttl=54 time=49.467 ms

To print logs generated the last 5 seconds use the --since option

$ kubectl logs ping --timestamps --since=5s 
2023-02-28T15:17:41.301096117+02:00 64 bytes from 8.8.8.8: seq=970 ttl=54 time=49.585 ms 
2023-02-28T15:17:42.300997163+02:00 64 bytes from 8.8.8.8: seq=971 ttl=54 time=49.465 ms 
2023-02-28T15:17:43.301291057+02:00 64 bytes from 8.8.8.8: seq=972 ttl=54 time=49.654 ms 
2023-02-28T15:17:44.301250995+02:00 64 bytes from 8.8.8.8: seq=973 ttl=54 time=49.501 ms 
2023-02-28T15:17:45.301328263+02:00 64 bytes from 8.8.8.8: seq=974 ttl=54 time=49.467 ms

To print logs generated since a specific timestamp use the --since-time option

kubectl logs ping --timestamps --since-time=2023-02-28T15:30:29+02:00 
2023-02-28T15:30:29.410318394+02:00 64 bytes from 8.8.8.8: seq=1738 ttl=54 time=49.467 ms 
2023-02-28T15:30:30.410454471+02:00 64 bytes from 8.8.8.8: seq=1739 ttl=54 time=49.470 ms 
2023-02-28T15:30:31.410505950+02:00 64 bytes from 8.8.8.8: seq=1740 ttl=54 time=49.408 ms 
2023-02-28T15:30:32.410591914+02:00 64 bytes from 8.8.8.8: seq=1741 ttl=54 time=49.421 ms

If we want to emulate the Linux tail command we provide the --follow parameter

$ kubectl logs ping --follow

If we want to view the full logs just give

$ kubectl logs ping

How to attach to a pod

Attaching to the shell of a pod allows to perform actions on this pod

$ kubectl attach ping

or use the exec command with the appropriate shell

kubectl exec ping -- /bin/sh

How to delete a pod

To delete a pod will cause the pod to stop working

$ kubectl delete pod ping 
pod "ping" deleted

Conclusion

Learning kubernetes is like learning a new operating system, there are multiple ways to do things and most likely there is no wrong or right way to perform operations, but its important to learn with simple examples that later can help you build more “complex” examples, i hope you found this article useful and helped you understand Kubernetes and Pods operations.

Join Medium with my referral link - Konstantinos Patronas
As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…