Must-know Kubernetes commands for power users.
Cluster Management
kubectl cluster-info
# Displays information about the clusterkubectl get nodes
# Lists nodes in the clusterkubectl describe node [node-name]
# Shows details of a nodekubectl drain [node-name]
# Drains a node of pods before maintenancekubectl cordon [node-name]
# Marks a node as unschedulablekubectl uncordon [node-name]
# Marks a node as schedulablekubectl top nodes
# Shows resource usage statistics for nodes
Pod Management
kubectl get pods
# Lists all pods in the current namespacekubectl get pods --all-namespaces
# Lists all pods across all namespaceskubectl describe pod [pod-name]
# Shows details of a podkubectl logs [pod-name]
# Displays the logs of a podkubectl exec -it [pod-name] -- /bin/sh
# Runs a command inside a podkubectl delete pod [pod-name]
# Deletes a podkubectl run [pod-name] --image=[image]
# Creates a pod with a specific imagekubectl port-forward [pod-name] [local-port]:[pod-port]
# Forwards ports from the pod to your machinekubectl top pod [pod-name]
# Displays resource usage of a pod
Deployments Management
kubectl get deployments
# Lists all deploymentskubectl describe deployment [deployment-name]
# Shows details of a deploymentkubectl apply -f [deployment-file.yaml]
# Applies changes from a YAML deployment filekubectl delete deployment [deployment-name]
# Deletes a deploymentkubectl rollout status deployment [deployment-name]
# Checks the status of a deploymentkubectl rollout undo deployment [deployment-name]
# Reverts the last deployment updatekubectl scale deployment [deployment-name] --replicas=[number]
# Scales a deploymentkubectl set image deployment/[deployment-name] [container]=[new-image]
# Updates the image of a container in a deployment
Services Management
kubectl get services
# Lists all serviceskubectl describe service [service-name]
# Shows details of a servicekubectl expose deployment [deployment-name] --type=[type] --port=[port]
# Exposes a deployment as a servicekubectl delete service [service-name]
# Deletes a servicekubectl port-forward service/[service-name] [local-port]:[service-port]
# Forwards ports from the service
Namespace Management
kubectl get namespaces
# Lists all namespaceskubectl create namespace [namespace-name]
# Creates a new namespacekubectl delete namespace [namespace-name]
# Deletes a namespacekubectl config set-context --current --namespace=[namespace-name]
# Sets the current namespace
ConfigMaps and Secrets Management
kubectl get configmaps
# Lists all ConfigMapskubectl describe configmap [configmap-name]
# Shows details of a ConfigMapkubectl create configmap [configmap-name] --from-file=[file]
# Creates a ConfigMap from a filekubectl delete configmap [configmap-name]
# Deletes a ConfigMapkubectl get secrets
# Lists all secretskubectl describe secret [secret-name]
# Shows details of a secretkubectl create secret generic [secret-name] --from-literal=[key]=[value]
# Creates a secret from literal valueskubectl delete secret [secret-name]
# Deletes a secret
Volumes and Storage Management
kubectl get pv
# Lists all persistent volumes (PVs)kubectl describe pv [pv-name]
# Shows details of a PVkubectl get pvc
# Lists all persistent volume claims (PVCs)kubectl describe pvc [pvc-name]
# Shows details of a PVCkubectl delete pvc [pvc-name]
# Deletes a PVC
Roles and Access Management (RBAC)
kubectl get roles
# Lists all roleskubectl get rolebindings
# Lists all role bindingskubectl describe role [role-name]
# Shows details of a rolekubectl create role [role-name] --verb=[action] --resource=[resource]
# Creates a rolekubectl delete role [role-name]
# Deletes a rolekubectl get clusterroles
# Lists all cluster-level roleskubectl describe clusterrole [clusterrole-name]
# Shows details of a cluster rolekubectl create clusterrole [clusterrole-name] --verb=[action] --resource=[resource]
# Creates a cluster rolekubectl delete clusterrole [clusterrole-name]
# Deletes a cluster role
Jobs and CronJobs Management
kubectl get jobs
# Lists all jobskubectl describe job [job-name]
# Shows details of a jobkubectl create job [job-name] --image=[image]
# Creates a jobkubectl delete job [job-name]
# Deletes a jobkubectl get cronjobs
# Lists all cronjobskubectl describe cronjob [cronjob-name]
# Shows details of a cronjobkubectl create cronjob [cronjob-name] --schedule="*/5 * * * *" --image=[image]
# Creates a cronjob with a schedulekubectl delete cronjob [cronjob-name]
# Deletes a cronjob
Verification and Debugging
kubectl get events
# Lists all events in the clusterkubectl describe [resource] [resource-name]
# Shows details of any resourcekubectl get all
# Lists all resources in the current namespacekubectl explain [resource]
# Displays the documentation of a resourcekubectl diff -f [file.yaml]
# Compares a YAML file with the current statekubectl api-resources
# Lists all available API resourceskubectl api-versions
# Lists all API versionskubectl version
# Displays the version of kubectl and the server
Other Useful Commands
kubectl edit [resource] [resource-name]
# Edits a resource livekubectl patch [resource] [resource-name] --patch '{...}'
# Applies a patch to a resourcekubectl label [resource] [resource-name] [label]=[value]
# Adds a label to a resourcekubectl annotate [resource] [resource-name] [annotation]=[value]
# Adds an annotation to a resourcekubectl rollout restart deployment [deployment-name]
# Restarts a deploymentkubectl taint nodes [node-name] [key]=[value]:[effect]
# Adds a taint to a nodekubectl wait --for=condition=[condition] pod/[pod-name]
# Waits for a resource to reach a condition
Comments
Post a Comment