Interacting with a Kubernetes Cluster

Containers

containers

Experimenting with MiniKube and AWS

This tool is excellent to quickly setup a local Kubernetes cluster for easy local experimentation

AWS, Google Cloud, and other cloud providers offer free credits and courses to get you started quickly

Kubectl

Listing objects: kubectl get pods|deployments|daemonsets|svc|nodes|namespaces

You can get each type of object with the same command.

Use -A to list all, or -n to scope by namespace:

kubectl get pods
kubectl get pods -A
kubectl get pods -n special-namespace
kubectl get namespaces  # not sure about -n, try listing all namespaces!

Many resources have synonyms and pluralized versions like pod and pods:

# SAME command:
kubectl get svc
kubectl get service
kubectl get services  # Lists active TCP ports as well

Custom resources (CRDs)

Remember people can write customer resources so you never know what you might be able kubectl get on your cluster, such as ArgoCD resources or Prometheus scraping configuration.

kubectl get apps            # argocd
kubectl get applicationset  # argocd
kubectl get stream          # nats
kubectl get servicemonitor  # prometheus

More information, alternate formats: kubectl get -o=wide, get -o=json

These commands show you additional information

You can pipe JSON output to jq to allow for more complicated CLI commands or scripts.

Show more information: describe pod|nodes|svc|deployments

Debugging

Showing logs: logs or stern

I recommend stern because you don’t need to write out the unique pod name to list all logs and to get logs from all pods:

kubectl logs podname
stern regex

Port forwarding: port-forward svc|pod NAME HOSTPORT:PORT

Useful but be careful, avoid in production environments.

Executing a command within a container: exec, exec -it POD -- COMMAND

Useful but be careful, ban this in production environments.

Resource consumption: top

Copying files to host: cp POD:PATH LOCALPATH

Ban this in production environments.

Manage multiple clusters with kubectx

https://github.com/ahmetb/kubectx

Alternative: KUBECONFIG environment bariable

Configure Starship.rs to show your current kubernetes cluster

https://starship.rs/ use it and configure it to show your kubernetes cluster so you’re always aware of where kubectl is pointed at.

K9S is an awesome CLI kubernetes navigator

I love it for monitoring new deployments because it refreshes live. You can easily view logs and port-forward as well as look up all resource types, like kubectl get and describe but more powerful.

Tab completion: Fish, ZSH, maybe BASH

Setup your shell to do this!

Fish and ZSH make this easy.

Bash toohttps://kubernetes.io/docs/reference/kubectl/generated/kubectl_completion/

Fish abbreviations

I use most of these daily:

Notice I totally replaced kubectl logs with stern.

# Essential
abbr k kubectl
abbr kx kubectx
# List and detail resources
abbr kg 'kubectl get'
abbr kgp 'kubectl get pods'
abbr kd 'kubectl describe'
# Debugging pods
abbr kl stern  ## Kubectl logs
abbr kex 'kubectl exec'
abbr kpf 'kubectl port-forward'
# Emergency/local modifications. Prefer devops. Ensure correct cluster is targeted.
abbr krr 'bash -c "correct-kubernetes-cluster.sh && kubectl rollout restart"'
    
Rarely used fyi:

abbr kcp ‘kubectl cp’ kubectl exec -it rfr-edge-redis-0 -n default -c redis – redis-cli kubectl get apps -A | grep -v Synced abbr kc ‘kubectl config’ abbr kcc ‘kubectl config current-context’ abbr whereami ‘kubectl config current-context’ abbr kcn ‘kubectl config set-context –current –namespace’ abbr kgs ‘kubectl get services’ abbr kgcm ‘kubectl get configmaps’ abbr kgi ‘kubectl get ingress’ abbr ktop ‘kubectl top’