User Tools

Site Tools


k8s:core:commands

Kubernetes kubectl Commands Guide

This guide covers the most commonly used Kubernetes commands, explanations, and examples.

Cheat Sheet

Category Command Purpose
Cluster `kubectl cluster-info` Show cluster information
Cluster `kubectl version` Show client/server versions
Namespace `kubectl get ns` List namespaces
Namespace `kubectl create ns <name>` Create namespace
Namespace `kubectl delete ns <name>` Delete namespace
Resources `kubectl get pods` List pods
Resources `kubectl get deployments` List deployments
Resources `kubectl get svc` List services
Resources `kubectl get nodes` List nodes
Resources `kubectl get all` List common resources
Details `kubectl describe pod <pod>` Detailed pod information
Details `kubectl describe deployment <name>` Deployment details
Details `kubectl describe node <node>` Node details
Create `kubectl apply -f file.yaml` Create/update resources
Create `kubectl create deployment …` Create deployment quickly
Update `kubectl edit deployment <name>` Edit deployment
Update `kubectl scale deployment <name> –replicas=N` Scale deployment
Update `kubectl set image …` Update container image
Delete `kubectl delete pod <pod>` Delete pod
Delete `kubectl delete deployment <name>` Delete deployment
Delete `kubectl delete service <name>` Delete service
Logs `kubectl logs <pod>` Show logs
Logs `kubectl logs -f <pod>` Stream logs
Logs `kubectl logs –previous <pod>` Show previous container logs
Exec `kubectl exec -it <pod> – bash` Open shell inside container
Network `kubectl port-forward pod/<pod> 8080:80` Forward local port
Files `kubectl cp file.txt pod:/tmp/` Copy file to pod
Files `kubectl cp pod:/tmp/file.txt .` Copy file from pod
Rollout `kubectl rollout status deployment/<name>` Check rollout status
Rollout `kubectl rollout history deployment/<name>` Show rollout history
Rollout `kubectl rollout undo deployment/<name>` Rollback deployment
Config `kubectl config get-contexts` List contexts
Config `kubectl config use-context <ctx>` Switch context
Debug `kubectl top pod` Show pod CPU/memory
Debug `kubectl top node` Show node CPU/memory

Cluster Commands

Show Cluster Information

kubectl cluster-info

Displays API server and cluster services.

Example:

kubectl cluster-info

Show Kubernetes Version

kubectl version

Example:

kubectl version --short

Output:

Client Version: v1.34.0
Server Version: v1.34.0

Namespace Commands

List Namespaces

kubectl get namespaces

Short form:

kubectl get ns

Create Namespace

kubectl create namespace dev

Delete Namespace

kubectl delete namespace dev

Viewing Resources

List Pods

kubectl get pods

List Pods in All Namespaces

kubectl get pods -A

List Deployments

kubectl get deployments

List Services

kubectl get svc

List Nodes

kubectl get nodes

Show All Common Resources

kubectl get all

Displays:

  • Pods
  • Deployments
  • ReplicaSets
  • Services

Describe Resources

Describe Pod

kubectl describe pod nginx

Useful for:

  • Events
  • Container status
  • Volumes
  • Environment variables
  • Scheduling issues

Describe Deployment

kubectl describe deployment nginx

Describe Node

kubectl describe node worker-1

Shows:

  • CPU
  • Memory
  • Conditions
  • Allocated resources

Creating Resources

Apply YAML File

kubectl apply -f deployment.yaml

Most common Kubernetes command.

Example:

kubectl apply -f nginx-deployment.yaml

Apply Entire Directory

kubectl apply -f manifests/

Create Deployment Quickly

kubectl create deployment nginx --image=nginx

Updating Resources

Edit Resource

kubectl edit deployment nginx

Scale Deployment

kubectl scale deployment nginx --replicas=5

Update Container Image

kubectl set image deployment/nginx nginx=nginx:1.27

Deleting Resources

Delete Pod

kubectl delete pod nginx

Delete Deployment

kubectl delete deployment nginx

Delete Service

kubectl delete service nginx

Delete Resources from YAML

kubectl delete -f deployment.yaml

Logs

Show Logs

kubectl logs nginx

Stream Logs

kubectl logs -f nginx

Equivalent to:

tail -f

Logs from Specific Container

kubectl logs nginx -c app

Previous Container Logs

kubectl logs nginx --previous

Useful when troubleshooting:

  • CrashLoopBackOff
  • Container restart failures

Execute Commands Inside Container

Run One Command

kubectl exec nginx -- ls

Open Interactive Shell

kubectl exec -it nginx -- bash

or

kubectl exec -it nginx -- sh

Port Forwarding

Forward Pod Port

kubectl port-forward pod/nginx 8080:80

Access application:

http://localhost:8080

Forward Deployment Port

kubectl port-forward deployment/nginx 8080:80

Copy Files

Copy File To Pod

kubectl cp test.txt nginx:/tmp/test.txt

Copy File From Pod

kubectl cp nginx:/tmp/log.txt .

Rollout Management

Check Rollout Status

kubectl rollout status deployment/nginx

View Rollout History

kubectl rollout history deployment/nginx

Rollback Deployment

kubectl rollout undo deployment/nginx

Context Management

Show Contexts

kubectl config get-contexts

Show Current Context

kubectl config current-context

Switch Context

kubectl config use-context production

Resource Usage

Show Pod CPU and Memory

kubectl top pod

Show Node CPU and Memory

kubectl top node

Troubleshooting Workflow

When an application is not working:

kubectl get pods
kubectl describe pod <pod-name>
kubectl logs <pod-name>
kubectl exec -it <pod-name> -- sh
kubectl get events --sort-by=.metadata.creationTimestamp

Typical troubleshooting order:

  1. Check pod status
  2. Check events
  3. Check logs
  4. Enter container
  5. Verify network/service configuration
k8s/core/commands.txt · Last modified: by phong2018