Kubernetes

To read more on kubectl, check out the Kubectl Cheat Sheet.

Usage

kubectx + kubens

kubectx is a tool to switch between contexts (clusters) on kubectl faster. kubens is a tool to switch between Kubernetes namespaces (and configure them for kubectl) easily.

# switch to another cluster that's in kubeconfig
kubectx rancher-desktop

# switch back to previous cluster
kubectx -

# change the active namespace on kubectl
kubens kube-system

# go back to the previous namespace
kubens -

k9s

Kubernetes CLI To Manage Your Clusters In Style!

Kubectl commands

commonly used Kubectl commands

you can pratice kubectl commands at katacoda playground

Namespaces and Context

Execute the kubectl Command for Creating Namespaces

Assign a Context to Each Namespace

Switch to the Appropriate Context

see cluster-info

nested kubectl commands

kubectl proxy creates proxy server between your machine and Kubernetes API server. By default it is only accessible locally (from the machine that started it).

Accessing logs

Execute commands in running Pods

CI/CD

Redeploy newly build image to existing k8s deployment

Rolling back deployments

Once you run kubectl apply -f manifest.yml

Tips and Tricks

troubleshoot headless services

Alias

you can use busybox for debuging inside cluster

after SSH to a container, you can use this command to check connectivity to external host

Container Security

for better security add following securityContext settings to manifest

Debug k8s

For many steps here you will want to see what a Pod running in the k8s cluster sees. The simplest way to do this is to run an interactive busybox Pod:

Debugging with an ephemeral debug container

Ephemeral containers are useful for interactive troubleshooting when kubectl exec is insufficient because a container has crashed or a container image doesn't include debugging utilities, such as with distroless images.

This allows a user to inspect a running pod without restarting it and without having to enter the container itself to, for example, check the filesystem, execute additional debugging utilities, or initial network requests from the pod network namespace. Part of the motivation for this enhancement is to also eliminate most uses of SSH for node debugging and maintenance

Generateing k8s YAML from local files using --dry-run

verify

Ghostty tips

in Ghostty

  1. split screen horizontally

  2. go to the bottom screen and split it vertically

I was using top screen for the work with yaml files and kubectl.

Left bottom screen was running:

watch kubectl get pods

Right bottom screen was running:

watch "kubectl get events --sort-by='{.lastTimestamp}' | tail -6"

With such setup it was easy to observe in real time how my pods are being created.

Last updated

Was this helpful?