Kubernetes cheat sheet — to get up and running.

Joshua Callis
2 min readJan 9, 2021

Below is a list of the commands I most commonly use in Kubernetes.

Accessing a pod

#!/bin/bash
kubectl exec -ti <pod name> -n <namespace> bash

example:

#!/bin/bash
kubectl exec -ti app-php-59d59ccc98-98z6w -n app bash

This will allow you to interact with the pod. For example, if I wanted to list the directory structure.

#!/bin/bash
ls -la

View cron tasks

To see all the active cronjobs run:

#!/bin/bash
kubectl get jobs -n <namespace>

example:

#!/bin/bash
kubectl get jobs -n app

To watch all the jobs in real time

#!/bin/bash
kubectl get jobs -n app --watch

To get the state of a cron job run:

#!/bin/bash
kubectl get cronjob <name> -n <namespace>

example:

#!/bin/bash
kubectl get cronjob scheduler -n app

Get all pods

Get all the pods within the cluster

#!/bin/bash
kubectl get pods -A

To get all the pods within a namespace run:

#!/bin/bash
kubectl get pods -n <namespace>

E.G

#!/bin/bash
kubectl get pods -n app

Retrieve the logs from a pod

When something goes wrong with a pod. The best course of action is to check the logs or the description of a pod. Often, this will tell you exactly what is wrong.

To do this run both of these commands.

Describe pod:

#!/bin/bash
kubectl describe pod <pod> -n <namespace>

example:

#!/bin/bash
kubectl describe pod app-php-59d59ccc98-98z6w -n app

Check logs:

#!/bin/bash
kubectl describe pod <pod> -n <namespace>

example:

#!/bin/bash
kubectl logs app-php-59d59ccc98-98z6w -n app

If you wanted to see the logs in realtime. I.E have the logs keep updating and not having to run the command each time use -f

For instance, if we wanted to see all the traffic coming in from nginx we could use -f to see the logs in realtime and keep refreshing the domain http://example.com/ and see the requests instantly.

#!/bin/bash
kubectl logs app-nginx-79f99c558f-55gl5 -n app -f

Manage users

Check all the active users on the cluster

#!/bin/bash
eksctl get iamidentitymapping --cluster app-cluster --region eu-west-2

Add new user to the cluster

#!/bin/bash
ksctl create iamidentitymapping --cluster app-cluster --arn arn:aws:iam::754717905083:user/<username> --group system:masters --username <username> --region eu-west-2

Remove user

#!/bin/bash
eksctl delete iamidentitymapping --cluster <cluster> --arn arn:aws:iam::754711234567:user/<username> --region eu-west-2

Check what your userID, accountID and ARN is

#!/bin/bash
aws sts get-caller-identity

Change clusters

#!/bin/bash
aws eks update-kubeconfig --region eu-west-2 --name cluster-two

Get general information on TLS certificates

#!/bin/bash
kubectl get Issuers,ClusterIssuers,Certificates,CertificateRequests,Orders,Challenges -A

Get all events

This will list all events happening within kubernetes. This command is very useful, I often use it to check the past state of the cron-jobs running and checking the state of certificates or helm package installs.

#!/bin/bash
kubectl get events -A

--

--

Joshua Callis

Converted DevOps Engineer at oso.sh, Previously a Senior Software Engineer.