Published Apr 18, 2024
This post covers the general commands for kubernetes. Also installation steps for MacOS.
Run below command to install via Brew.
brew install kubectl
or
bres install kubernetes-cli
Test the installation with the version check below.
kubectl version --client
List all namespaces
kubectl get namespaces
All the kubectl
commands accept --namespace
or -n
argument. Alternatively you can use --all-namespaces
flag to get results from all the namespaces.
Example:
To get list of all the resources from the default namespace
kubectl get all
To get list of all the resources
kubectl get all -n <namespace>
To get list of all the resources from all the namespaces
kubectl get all --all-namespaces
Cluster and Configuration:
kubectl version
: Check kubectl versionkubectl cluster-info
: Get information about the clusterkubectl config current-context
: View current contextkubectl config get-contexts
: List available contextskubectl config use-context <name>
: Set the active contextResource Management:
kubectl get <resource>
: List resources (e.g. get pods
, get deployments
)kubectl describe <resource>
: Show details about a resourcekubectl create -f <file>
: Create resources from a YAML or JSON filekubectl apply -f <file>
: Create or update resources from a YAML or JSON filekubectl delete <resource> <name>
: Delete a resource by namePod Management:
kubectl logs <pod>
: View logs of a podkubectl exec -it <pod> <command>
: Run a command in a pod's containerkubectl cp <source> <dest>
: Copy files between local machine and podsNamespace Management:
kubectl get namespaces
: List namespaceskubectl create namespace <name>
: Create a namespaceService Management:
kubectl get services
: List serviceskubectl describe service <name>
: View details of a serviceRollouts and Scaling:
kubectl rollout history deployment/<deployment_name>
: View deployment historykubectl scale deployment/<deployment_name> --replicas=<number>
: Scale a deploymentAdvanced Commands:
kubectl cordon <node>
: Mark a node unschedulablekubectl drain <node>
: Drain a node in preparation for maintenancekubectl port-forward <pod> <port>:<local_port>
: Forward a pod port to your local machineResources:
#kubernetes #k8s #kube #kubectl #devops #sysadmin #containerorchestration #troubleshooting #debugging #sysops #cloudnative