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

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

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

Setup Kubernetes cluster with Rancher

Setup Kubernetes cluster with Rancher Overview In this post we will go through the process how to setup Kubernetes cluster with Rancher. Kubernetes is an amazing technology and one of the best way to orchestrate your containers. But sometimes, if you have a lot of containers or planning to host a lot of applications, it can be a bit easier to manage all those with a GUI dashboard, like Kubernetes dashboard. In this article we will cover the Rancher software, how to install it on Debian 10 Buster and how to setup Kubernetes cluster. This method can be done on Debian VM or on a bare metal machine and it also works on Ubuntu. ...

Published May 16, 2021 · Updated December 2, 2023 · 4 min · 826 words · Marko Nisic

Categories

Updated December 14, 2024 · 0 min · 0 words · Marko Nisic