Logo

dev-resources.site

for different kinds of informations.

Kubernetes Cheat Sheet: Essential Commands for Beginners

Published at
9/7/2024
Categories
kubernetes
containers
cloud
cheatsheet
Author
idsulik
Author
7 person written this
idsulik
open
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!

cheatsheet Article's
30 articles in total
Favicon
Vim cheat sheet
Favicon
The Ultimate Cheat Sheet: CLI Man Pages, tldr, and cheat.sh
Favicon
From FZF file preview to a browser for cht.sh to discovering the ideal solution
Favicon
Seaborn Cheat Sheet
Favicon
SQL Quick Reference: Simplifying Database Management
Favicon
Terraform Commands Cheat Sheet
Favicon
JavaScript Interview Cheat Sheet - Part 1
Favicon
JavaScript Interview Cheat Sheet - Part 2
Favicon
Arch Linux Pacman: A Detailed Guide with Commands and Examples 🎩🐧
Favicon
The Art of AI Conversation: 6 Essential Tips for Chat LLM Success
Favicon
Master CSS Selectors
Favicon
End-to-End Flexbox vs. Grid vs. Traditional.
Favicon
Linux Commands Cheat Sheet :)
Favicon
sql joins: moving in together
Favicon
A Yocto Cheatsheet
Favicon
Typescript quick concept refresher and reference
Favicon
cheat sheet for go mod package management
Favicon
Git para Iniciantes: Tudo o que você precisa saber para começar a usar
Favicon
Git Commands You Need for Hacktoberfest 2024 - Git cheat sheet
Favicon
Git Cheatsheet that will make you a master in Git
Favicon
How to learn HTML: 46 great sites, courses and books (all free)
Favicon
Top 5 Cheat sheets for Developers
Favicon
CSS: List of Properties for Text
Favicon
What's in a name?
Favicon
The HTML History and Optimization Cheat Sheet
Favicon
Kubernetes Cheat Sheet: Essential Commands for Beginners
Favicon
🦊 GitLab Cheatsheet - 16 - CICD Catalog
Favicon
📝 SQL Cheat Sheet for Developers
Favicon
The Ultimate SQL JOIN Cheat Sheet
Favicon
JavaScript Cheat Sheets

Featured ones: