Kubectl Cheatsheet

Kubectl cheatsheet Get, Describe, Delete, Apply, Rollouts, Expose and Port-forward # BASIC COMMANDS kubectl get pods # List all pods in the current namespace kubectl get nodes # List all nodes in the cluster kubectl get services # List all services in the current namespace kubectl get deployments # List all deployments in the current namespace kubectl describe pod <pod-name> # Show detailed information about a pod kubectl describe node <node-name> # Show detailed information about a node kubectl describe svc <service-name> # Show detailed information about a service kubectl delete pod <pod-name> # Delete a specific pod kubectl delete <resource> <resource-name> # Delete a specific resource (ingress, deployment) kubectl get all -n <namespace> # Show detailed information about all resources and workloads within the defined namespace kubectl get all -n <namespace> -o wide # Show ALL details for all workloads within the defined namespace # NAMESPACES kubectl get namespaces # List all namespaces kubectl get pods -n <namespace> # List pods in a specific namespace kubectl delete namespace <namespace> # Delete a specific namespace # FILTERING OUTPUT kubectl get pods -o wide # List pods with additional details kubectl get pods -n <namespace> # List pods in a specific namespace kubectl get pods --selector <key>=<value> # List pods using a label selector # CONFIGURATION AND MANAGEMENT kubectl apply -f <file.yaml> # Apply changes to a resource from a YAML file kubectl create namespace <namespace> # Create a new namespace kubectl config view # View Kubernetes cluster configuration kubectl config use-context <context> # Switch between contexts kubectl edit deployment <deployment> # Edit an existing deployment # LOGS AND DEBUGGING kubectl logs <pod-name> # View logs of a specific pod kubectl logs <pod-name> -c <container> # View logs of a specific container in a pod kubectl exec -it <pod-name> -- /bin/bash # Access a pod's shell kubectl top pods # Show resource usage for pods kubectl top nodes # Show resource usage for nodes # DEPLOYMENT MANAGEMENT kubectl scale deployment <deployment> --replicas=<count> # Scale a deployment kubectl rollout status deployment <deployment> # Check the status of a deployment kubectl rollout undo deployment <deployment> # Rollback a deployment kubectl rollout restart deployment <deployment-name> # Restart a deployment # PORT-FORWARDING AND ACCESS kubectl port-forward <pod-name> 8080:80 # Forward local port 8080 to pod port 80 kubectl expose pod <pod-name> --type=NodePort --port=80 # Expose a pod as a service Ingress and Configmap commands # INSPECTING AND CLEANUP kubectl get events # Show cluster events kubectl delete all --all # Delete all resources in the current namespace kubectl cluster-info # Display cluster information # INGRESS COMMANDS kubectl get ingress # List all Ingress resources in the current namespace kubectl get ingress -n <namespace> # List all Ingress resources in a specific namespace kubectl describe ingress <ingress-name> # Show details of a specific Ingress resource kubectl edit ingress <ingress-name> # Edit an existing Ingress resource kubectl delete ingress <ingress-name> # Delete a specific Ingress resource # INGRESS LOGS kubectl logs <ingress-controller-pod> # Check logs of the ingress controller # ADDITIONAL INGRESS CONTROLLER COMMANDS kubectl get pods -n <ingress-namespace> # Verify the Ingress controller is running kubectl get svc -n <ingress-namespace> # Get the service of the Ingress controller kubectl describe svc <ingress-controller-service> -n <namespace> # View details of the Ingress controller service # CONFIGMAP COMMANDS kubectl create configmap <configmap-name> --from-literal=<key>=<value> # Create a ConfigMap from literal values kubectl create configmap <configmap-name> --from-file=<file> # Create a ConfigMap from a file kubectl get configmaps # List all ConfigMaps in the current namespace kubectl get configmaps -n <namespace> # List all ConfigMaps in a specific namespace kubectl describe configmap <configmap-name> # Show details of a specific ConfigMap kubectl edit configmap <configmap-name> # Edit an existing ConfigMap kubectl delete configmap <configmap-name> # Delete a specific ConfigMap # Use ConfigMap in a Pod kubectl exec -it <pod-name> -- env | grep <configmap-key> # Verify the ConfigMap is mounted as environment variables Node taint, node drain commands, daemonset & statefulset commands # TAINTS COMMANDS kubectl taint nodes <node-name> <key>=<value>:<effect> # Add a taint to a node # Example: kubectl taint nodes node1 key=value:NoSchedule # Prevent pods without tolerations from scheduling on this node kubectl taint nodes <node-name> <key>- # Remove a taint from a node # Example: kubectl taint nodes node1 key- # Removes the taint with key "key" kubectl get nodes --show-labels # List nodes with their labels and taints kubectl describe node <node-name> # View details of a node, including taints # DRAIN COMMANDS kubectl drain <node-name> # Evict all pods from a node (prepare for maintenance) # Example: kubectl drain node1 --ignore-daemonsets --delete-emptydir-data # Options: # --ignore-daemonsets: Ignore daemonsets during drain # --delete-emptydir-data: Force eviction of pods with 'emptyDir' volumes kubectl cordon <node-name> # Mark a node as unschedulable (prevent new pods from being scheduled) # Example: kubectl cordon node1 kubectl uncordon <node-name> # Mark a node as schedulable (allow new pods to be scheduled) # Example: kubectl uncordon node1 # ADDITIONAL USEFUL COMMANDS kubectl get nodes -o wide # View node statuses and scheduling availability kubectl describe node <node-name> # Check node taints and conditions kubectl get pods -o wide # Verify where pods are running kubectl delete pod <pod-name> --grace-period=0 --force # Force delete a pod (if drain gets stuck) # DAEMONSET COMMANDS kubectl get daemonsets # List all DaemonSets in the current namespace kubectl get daemonsets -n <namespace> # List all DaemonSets in a specific namespace kubectl describe daemonset <daemonset-name> # Show details of a specific DaemonSet kubectl edit daemonset <daemonset-name> # Edit a DaemonSet definition kubectl delete daemonset <daemonset-name> # Delete a specific DaemonSet # STATEFULSET COMMANDS kubectl get statefulsets # List all StatefulSets in the current namespace kubectl get statefulsets -n <namespace> # List all StatefulSets in a specific namespace kubectl describe statefulset <statefulset-name> # Show details of a specific StatefulSet kubectl edit statefulset <statefulset-name> # Edit a StatefulSet definition kubectl delete statefulset <statefulset-name> # Delete a specific StatefulSet kubectl scale statefulset <statefulset-name> --replicas=<count> # Scale a StatefulSet Kubectl kubernetes RBAC commands # RBAC (ROLE-BASED ACCESS CONTROL) COMMANDS kubectl get roles # List all roles in the current namespace kubectl get clusterroles # List all cluster-wide roles kubectl get rolebindings # List all RoleBindings in the current namespace kubectl get clusterrolebindings # List all cluster-wide RoleBindings kubectl describe role <role-name> # View details of a specific Role kubectl describe clusterrole <clusterrole-name> # View details of a ClusterRole kubectl create role <role-name> --verb=<verb> --resource=<resource> --namespace=<namespace> # Example: kubectl create role pod-reader --verb=get,list --resource=pods --namespace=dev kubectl create rolebinding <binding-name> --role=<role-name> --user=<user> --namespace=<namespace> # Example: kubectl create rolebinding pod-reader-binding --role=pod-reader --user=dev-user --namespace=dev kubectl delete role <role-name> # Delete a specific Role kubectl delete rolebinding <binding-name> # Delete a specific RoleBinding kubectl auth can-i <verb> <resource> --namespace=<namespace> # Example: kubectl auth can-i create pods --namespace=dev # Check RBAC permissions # CRD (CUSTOM RESOURCE DEFINITION) COMMANDS kubectl get crds # List all CRDs in the cluster kubectl describe crd <crd-name> # View details of a specific CRD kubectl delete crd <crd-name> # Delete a specific CRD kubectl get <cr-kind> # List custom resources of the given kind kubectl get <cr-kind> -n <namespace> # List custom resources in a specific namespace kubectl describe <cr-kind> <cr-name> # Show details of a specific custom resource kubectl apply -f <cr.yaml> # Create or update a custom resource kubectl delete <cr-kind> <cr-name> # Delete a specific custom resource kubectl get crds -o wide # Show additional details about CRDs kubectl explain <cr-kind> # Show schema and details about the custom resource

Published January 20, 2025 · Updated January 21, 2025 · 6 min · 1247 words · Marko Nisic

How to Install FluxCD on a Kubernetes Cluster

How to Install FluxCD on a Kubernetes Cluster (Step-by-Step Guide) Overview Hello “home-labers”, devops people and kubernetes adventurers. This post is a continuation of my new home-lab series and here I’ll be talking about the steps I took in order to install FluxCD on a Kubernetes cluster. In my case, I run a HA k3s cluster on my home-lab(link to the step by step guide on high availability k3s cluster setup - LINK) and I decided to go full GitOps and try to integrate all the best practices to deploy apps, manage infrastructure and provision resources. The first step for that was to choose a GitOps tool and for me it was FluxCD LINK. ...

Published January 15, 2025 · Updated January 20, 2025 · 7 min · 1482 words · Marko Nisic

How to setup High Availability K3s Cluster

How to setup High Availability K3s Cluster Overview Hi there Kubernetes explorers and ‘home labers’. Here i want to document the steps how to setup high availability k3s cluster with kube-vip. This setup will enable us to have an HA kubernetes cluster without the need of adding an external load balancer. I want to avoid of wasting as much of resources as possible and allocate most of the resources to the cluster, this is the way - for me at least :). This setup will be created on VMs, running on top of Proxmox cluster. ...

Published January 5, 2025 · Updated January 20, 2025 · 10 min · 1962 words · Marko Nisic

How to create symbolic links in Linux step by step

Overview Hi there fellow Linux learners! Hope you’re ready to expand your knowledge about Linux. Today’s topic is how to create symlinks in Linux, which is short for symbolic links. There are two different kinds of links: soft links hard links In this article we’ll focus on soft symbolic links. Hard links will be on menu some other time. Don’t be intimidated by its name, you don’t have to write symbols, neither remember or decipher ones. Soft symbolic links is just the fancy name for shortcuts. As simple as that. To create symlink you’ll have to use ln command (short for link – makes sense, eh?). ...

Published December 24, 2024 · Updated December 24, 2024 · 5 min · 993 words · Marko Nisic

Linux commands to copy files and folders

Overview Hello fellow Linux enthusiasts. Hope you’re all well and ready to broaden your knowledge. Today’s topic will be about the various linux commands to copy files and folders. And no, it’s with “right-click and copy file”. We do this the cool way, with the terminal. So, without much further delay let’s get our hands dirty. We’ll be covering several different methods and tools we can utilize that will allow us to perfom the copy actions for various scenarios and use cases. ...

Published December 23, 2024 · Updated December 23, 2024 · 8 min · 1554 words · Marko Nisic

Convert Virtualbox VMs to QEMU/KVM[VDI to QCOW2]

Overview In the following article, we’ll go through the steps how to convert Virtualbox VMs to QEMU/KVM hypervisor, or in another words, how to convert VM files from VDI to QCOW2 extenstion. This process is useful especially when you need to migrate from virtualbox to KVM or just want to try out the KVM hypervisor without the need to create new machines from the start. What is VDI? VDI(Virtual desktop infrastructure) is an file extension of the HDD disc image file for an Virtual machines that are commonly used by hypervisor solutions such as Virtualbox. ...

Published December 16, 2024 · Updated December 17, 2024 · 4 min · 651 words · Marko Nisic

How to setup NFS server on Alma Linux(NFS share)

This article will act as a guide for a procedure - how to setup NFS server on Alma Linux or in another words and how to create an NFS share on Alma Linux. NFS stands for Network File System is a network file sharing protocol that allows you to share files and directories over the network. You can mount the file systems over a network and use them as your local drive. NFS server is a client-server architecture where multiple clients can mount the shared drive from the NFS server and share resources between Linux systems. By using NFS, you can save space and the cost of storage, especially when you are using cloud instances. ...

Published December 16, 2024 · Updated December 17, 2024 · 4 min · 766 words · Marko Nisic

6 Open Source Kubernetes Desktop Tools for local development

Exploring Open Source Kubernetes Desktop tools for local development Overview Hello there Kubernetes adventurers. Kubernetes is the go-to solution for container orchestration in modern software development. But for many developers, working with Kubernetes locally can be challenging. Fortunately, there are variety of desktop Kubernetes tools to simplify the process, allowing you to experiment, learn, and develop applications right on your machine. In this post, we’ll explore some of the best open source Kubernetes desktop tools available, highlighting their pros and cons to help you choose the one that suits your needs. So, let’s get started. ...

Published December 16, 2024 · Updated December 17, 2024 · 3 min · 493 words · Marko Nisic

Sudo vs su command differences

Sudo vs su command differences Overview Hello there fellow Linuxers. Hopefully you’re all being practicing your skills or at least learning new skills. On your journey learning or trying to master Linux you must have came across certain commands that you saw others use and you just copy-paste it? For example, I’ve noticed that new users have troubles understanding the difference in (not holy) trinity of commands sudo, su and su- so they don’t use them properly. Ergo, on today’s menu is a special about sudo vs su command differences -. Let’s get started. ...

Published December 14, 2024 · Updated December 15, 2024 · 4 min · 764 words · Marko Nisic

Use MinIO as a Backend for Terraform Remote State

Overview Hello there DevOps and IaC enthusiast. In this article we will introduce MinIO, an interesting piece of software that can be self-hosted and help us solve a problem managing the Terraform remote state on the on-premises environemnts(Bare metal servers and data centers). And that’s our goal essentially - to use MinIO as a backend for Terraform remote state. My best use case for this setup is to utilizite it for hypervisor softwares(VMware, Proxmox), on prem kubernetes, docker too and so on. ...

Published November 9, 2024 · Updated December 13, 2024 · 5 min · 921 words · Marko Nisic

Deploy Docker container using Ansible

Overview Hello there fellow Linux adventurers. I hope you’re ready for today’s adventure in the land of automation. Through this article you’ll be shown steps how to deploy Docker container using Ansible on a remote Linux machine that runs Docker engine. By the end of article you should have enough understanding how to automate process of Docker container deployment on a multiple Linux inventory, with preset of environment variables and configs for needed Docker container. ...

Published November 7, 2024 · Updated November 7, 2024 · 6 min · 1104 words · Marko Nisic

Kubernetes overcommitment best practices

Overview Greetings knowledge seekers and kubernetes explorers. In the following post we will take a look at some of kubernetes overcommitment best practices and how to manage pods resources and how to manage pods request and limits. When working with Kubernetes and handling pods resources, this topic doesn’t get much attention than it needs or gets overlooked(guilty as charged). But understanding this and correctly managing these are crucial if you want to retain healhty and effecient cluster and avoiding a plethora of problems in the long run. ...

Published November 6, 2024 · Updated November 6, 2024 · 5 min · 1024 words · Marko Nisic