Kubectl Commands
General Information
kubectl
can be replaced withk
by using an alias.kubectl
makes calls to thekube-apiserver
behind the scenes.
Get or List Resources
- List all K8s objects:
123k get all
- List nodes:
123k get nodes
- List pods:
123k get pods
- List pods with additional info:
123k get pods -o wide
- List services:
123k get services123k get svc
- List deployments:
123k get deployments
- List replica sets:
123k get replicasets
- List replica sets with additional information:
123k get replicasets -o wide
- List deployments in another namespace:
123k get deployments --namespace=<namespace>
- List pods in all namespaces:
123k get pods --all-namespaces
- List service accounts:
123k get serviceaccount
- List pods with a label filter:
123k get pod --selector key=value
- Get pods with multiple label filters:
123k get pods --selector env=prod,bu=finance,tier=frontend
- List all jobs:
123k get jobs
- List all cron jobs:
123k get cronjobs
- List all network policies:
123k get networkpolicies
- List all ingress resources:
123k get ingress
- List persistent volumes:
123k get persistentvolume
- List persistent volume claims:
123k get persistentvolumeclaim
- List all DaemonSets:
123k get daemonsets
- List cluster events:
123k get events -o wide
- Get which node each pod is deployed on:
123k get pods -o wide
Describe Resources
- Describe a pod:
123k describe pod <pod-name>
- Describe a deployment:
123k describe deployment <deployment-name>
- Describe a service account:
123k describe serviceaccount <service-account-name>
- Describe an ingress:
123k describe ingress <ingress-name>
- Describe a DaemonSet:
123k describe daemonset <daemonset-name>
Create Resources
- Create a pod from an image:
123k run <pod-name> --image <image-name>
- Apply a K8s config.yaml file (declarative):
123k apply -f <filename>.yaml
- Create a namespace:
123k create namespace <namespace>
- Create a service account:
123k create serviceaccount <service-account-name>
Delete Resources
- Delete a pod:
123k delete pod <pod-name>
- Delete a replica set:
123k delete replicaset <replicaset-name>
- Delete a deployment:
123k delete deployment <deployment-name>
- Delete a K8s resource from a config file:
123k delete -f config.yaml
View Logs
- View logs of a pod:
123k logs <pod-name>
- View live logs of a pod:
123k logs -f <pod-name>
- View logs of a container in a multi-container pod:
123k logs <pod-name> <container-name>
- View logs of the previous container if the pod failed:
123k logs <pod-name> --previous
Rollouts and Updates
- View the status of rollout for a deployment:
123k rollout status deployment/<deployment-name>
- View the history of rollouts for a deployment:
123k rollout history deployment/<deployment-name>
- View the history of a specific rollout revision:
123k rollout history deployment/<deployment-name> --revision=1
- Rollback a deployment:
123k rollout undo deployment/<deployment-name>
- Rollback a deployment to a specific revision:
123k rollout undo deployment/<deployment-name> --to-revision=1
Node Commands
- Cordon a node (mark it unschedulable):
123k cordon <node-name>
- Un-cordon a node (mark it schedulable):
123k uncordon <node-name>
- Drain a node of all the pods:
123k drain <node-name>
- Drain a node of all the pods except DaemonSets:
123k drain <node-name> --ignore-daemonsets
Auth Commands
- View roles:
123k get roles
- View role bindings:
123k get rolebindings
- Check if you have access to perform an operation:
- Create deployments:
123k auth can-i create deployments
- Delete nodes in dev namespace:
123k auth can-i delete nodes -n dev
- Check if another user has access to perform an operation:
- Create deployments:
123k auth can-i create deployments --as dev-user
- Delete nodes in dev namespace:
123k auth can-i delete nodes --as dev-user -n dev
- Check if a service account has access to perform an operation:
123k auth can-i get pods --as=system:serviceaccount:default:<serviceaccount-name>
Config Commands
- List all clusters:
123k config get-clusters
- Set context for a cluster:
123k config use-context <cluster-name>
Certificates Commands
- List all CSRs:
123k get csr
- View a CSR in YAML format:
123k get csr <csr-name> -o yaml
- Delete a CSR:
123k delete csr <csr-name>
- Approve a CSR:
123k certificate approve <csr-name>
- Deny a CSR:
123k certificate deny <csr-name>
Output Formats
-
123-o json
-
123-o name
-
123-o wide
-
123-o yaml
Useful Imperative Commands
- Create a pod:
123k run <pod-name> --image=<image-name> --command -- <command-with-args>
- Create a deployment:
123k create deployment <deployment-name> --image <image-name> --replicas 3
- Create a service for a deployment:
123k expose deployment <deployment-name> --port 80 --name <service-name>
- Create a service for a pod:
123k expose pod <pod-name> --port 80 --name <service-name>
- Edit a deployment (in memory):
123k edit deployment <deployment-name>
- Scale a deployment:
123k scale deployment <deployment-name> --replicas 3
- Update the image in a deployment:
123k set image deployment <deployment-name> <old-image>=<new-image>
- Create resources from a config file:
123k create -f filename.yaml
- Update resources from a config file:
123k replace -f filename.yaml
- Delete and recreate resources from a config file:
123k replace --force -f filename.yaml
- Edit a deployment (doesn’t work for pods as they require recreation):
123k edit deployment <deployment-name>
Handy Commands
- Get the config YAML of a pod:
123k get pod <pod-name> -o yaml > pod.yaml
- Get the config YAML of a deployment:
123k get deployment <deployment-name> -o yaml > deployment.yaml
- Create a sample config YAML file for a pod:
123k run <pod-name> --image <image-name> --dry-run=client -o yaml > filename.yaml
- Create a sample config YAML file for a deployment:
123k create deployment <deployment-name> --image <image-name> --dry-run=client -o yaml > filename.yaml
- Create a sample config YAML file for a service:
123k expose deployment <deployment-name> --port 80 --dry-run=client -o yaml > filename.yaml
JSON Path in Kubectl
- Get images of all the pods:
123k get pods -o jsonpath='{.items[0].spec.containers[0].image}'
Metrics Commands
- Get top nodes by memory:
123k top nodes --sort-by memory
- Get top nodes by CPU for a context:
123k --context <context> top nodes --sort-by cpu