DEV Community

Suleiman Dibirov
Suleiman Dibirov

Posted on

Kubernetes Cheat Sheet: Essential Commands for Beginners

Kubernetes (K8s) is an open-source platform designed to automate the deployment, scaling, and operation of containerized applications. While it offers immense power and flexibility, it can be a bit overwhelming at first. This cheat sheet provides essential Kubernetes commands and concepts to help you get started.


Basic Terminology

Before diving into the commands, let’s cover some fundamental Kubernetes terms:

  • Pod: The smallest deployable unit in Kubernetes, usually containing one or more containers.
  • Node: A worker machine (virtual or physical) where Kubernetes runs your Pods.
  • Cluster: A set of Nodes managed by Kubernetes.
  • Service: An abstraction that defines a logical set of Pods and a policy to access them.
  • Namespace: A virtual cluster within Kubernetes that provides a scope for objects.
  • Deployment: A controller that manages rolling updates for Pods.

kubectl Basics

kubectl is the command-line tool used to interact with Kubernetes clusters. Here are some of the most common commands:

Cluster Operations

# Check the status of the cluster
kubectl cluster-info

# Get the current context (current cluster)
kubectl config current-context

# View all contexts (all clusters you're connected to)
kubectl config get-contexts

# Switch context (change cluster)
kubectl config use-context <context-name>
Enter fullscreen mode Exit fullscreen mode

Working with Nodes

# List all nodes
kubectl get nodes

# Get detailed info about a node
kubectl describe node <node-name>

# Drain a node (move workloads to another node)
kubectl drain <node-name> --ignore-daemonsets
Enter fullscreen mode Exit fullscreen mode

Working with Pods

# List all pods in the current namespace
kubectl get pods

# List all pods in a specific namespace
kubectl get pods -n <namespace>

# Get detailed information about a pod
kubectl describe pod <pod-name>

# View logs of a pod
kubectl logs <pod-name>

# Execute a command inside a running pod
kubectl exec -it <pod-name> -- /bin/bash
Enter fullscreen mode Exit fullscreen mode

Managing Deployments

# List all deployments
kubectl get deployments

# Get details of a specific deployment
kubectl describe deployment <deployment-name>

# Create a deployment
kubectl create deployment <deployment-name> --image=<image-name>

# Scale a deployment
kubectl scale deployment <deployment-name> --replicas=<number>

# Update an image in a deployment
kubectl set image deployment/<deployment-name> <container-name>=<image-name>

# Rollback a deployment
kubectl rollout undo deployment/<deployment-name>

# Delete a deployment
kubectl delete deployment <deployment-name>
Enter fullscreen mode Exit fullscreen mode

Working with Services

# List all services
kubectl get services

# Get detailed information about a service
kubectl describe service <service-name>

# Expose a deployment as a service
kubectl expose deployment <deployment-name> --type=LoadBalancer --port=<port>

# Delete a service
kubectl delete service <service-name>
Enter fullscreen mode Exit fullscreen mode

ConfigMaps and Secrets

ConfigMaps and Secrets store configuration data that Pods can consume.

ConfigMaps

# Create a ConfigMap from a file
kubectl create configmap <configmap-name> --from-file=<path>

# Get all ConfigMaps
kubectl get configmaps

# Get details of a specific ConfigMap
kubectl describe configmap <configmap-name>
Enter fullscreen mode Exit fullscreen mode

Secrets

# Create a secret from literal values
kubectl create secret generic <secret-name> --from-literal=<key>=<value>

# Get all secrets
kubectl get secrets

# Describe a secret
kubectl describe secret <secret-name>
Enter fullscreen mode Exit fullscreen mode

Namespaces

Namespaces help organize and isolate resources.

# List all namespaces
kubectl get namespaces

# Create a new namespace
kubectl create namespace <namespace-name>

# Delete a namespace
kubectl delete namespace <namespace-name>
Enter fullscreen mode Exit fullscreen mode

Useful Shortcuts

# List resources with short names (po=pods, svc=services, etc.)
kubectl get po,svc

# Apply changes from a file
kubectl apply -f <file.yaml>

# Delete resources defined in a file
kubectl delete -f <file.yaml>

# Watch for changes to resources
kubectl get pods --watch
Enter fullscreen mode Exit fullscreen mode

Wrapping Up

These are just some of the basic commands that will help you work with Kubernetes more effectively. Kubernetes can seem intimidating at first, but once you get familiar with the kubectl tool, you’ll be deploying, scaling, and managing applications with ease. Keep this cheat sheet handy as you explore and master Kubernetes!

Top comments (0)