Post

kubectl command book

This is my digital command book for kubectl commands to interact with K8s clusters. This is a work in progress and will be updated with more detail + descriptions as I go.

kubectl command book

Basic commands for managing Kubernetes contexts and configurations.

CommandDescription
kubectl config get-contextsList all available contexts and show which one is currently active.
kubectl config use-context $context$Switch to a different context.

Operations

Common operations for managing Kubernetes resources.

CommandDescriptionExample
kubectl applyApply a configuration to a resource by file or stdin.kubectl apply -f ./my-manifest.yaml
kubectl createCreate a resource from a file or from stdin.kubectl create -f ./my-resource.yaml
kubectl runRun a particular image on the cluster.kubectl run my-nginx --image=nginx
kubectl explainGet documentation for a resource.kubectl explain pods
kubectl getList one or more resources.kubectl get pods
kubectl get podsList all pods in the namespace.kubectl get pods
kubectl describeShow detailed information about a resource.kubectl describe pods my-pod
kubectl execExecute a command in a container.kubectl exec -it my-pod -- /bin/bash
kubectl logsPrint the logs for a container in a pod.kubectl logs my-pod

Resource Aliases

Shortcuts for commonly used Kubernetes resources.

ResourceAlias
nodesno
podspo
servicessvc

Output Options

Options for formatting the output of kubectl commands.

CommandDescription
-o wideOutput additional information.
-o yamlOutput the resource in YAML format.
-o jsonOutput the resource in JSON format.
--dry-runSimulate the command without making any changes.

Options for adjusting verbosity in output

| Verbosity | Description | |———–|————————————————————————————————————————————————————————————————————-| | --v=0 | Generally useful for this to ALWAYS be visible to an operator. | | --v=1 | A reasonable default log level if you don’t want verbosity. | | --v=2 | Useful steady state information about the service and important log messages that may correlate to significant changes in the system. This is the recommended default log level for most systems. | | --v=3 | Extended information about changes. | | --v=4 | Debug level verbosity. | | --v=6 | Display requested resources. | | --v=7 | Display HTTP request headers, including Application-Type and User Agent. | | --v=8 | Display HTTP request contents. | | --v=9 | Display HTTP request contents without truncation of contents. |

Cluster Information

Commands for retrieving information about the Kubernetes cluster and its resources.

CommandDescription
kubectl cluster-infoDisplay cluster information.
kubectl get nodesList all nodes with their status, roles, age, and version.
kubectl get nodes -o wideList all nodes with additional information like IPs and kernel versions.
kubectl get podsList all pods in the default namespace.
kubectl get pods --namespace redisList all pods in the redis namespace.
kubectl get all --all-namespacesList all resources in all namespaces.
kubectl api-resourcesList all available resource types.
kubectl explain podShow detailed documentation for the pod resource.
kubectl explain pod.specShow detailed documentation for the pod spec.
kubectl explain pod.spec.containersShow detailed documentation for the pod containers spec.
kubectl explain pod --recursiveShow detailed documentation for the pod resource and all its fields.

Node Information

Commands for retrieving detailed information about nodes in the cluster.

CommandDescriptionExample
kubectl describe nodes <node>Show detailed information about a specific node.kubectl describe nodes aks-nodepool1-30702851-vmss000000

Help Commands

Commands for getting help and documentation for kubectl commands.

CommandDescriptionExample
kubectl -hShow help for kubectl.kubectl get -h
kubectl create -hShow help for the create command.kubectl create -h

Deployment (Imperative)

Commands for creating and managing deployments imperatively.

CommandDescription
kubectl create deployment nginx --image=nginxCreate a deployment named nginx with the nginx image.
kubectl run nginx --image=nginxStart a pod named nginx with the nginx image.

Deployment (Declarative)

Define the desired state in code using a manifest in YAML or JSON, then use kubectl apply -f deployment.yml.

CommandDescription
kubectl apply -f deployment.ymlApply the deployment configuration from a YAML file.

Deploying Resources Imperatively

Commands for creating and managing resources imperatively.

CommandDescription
kubectl create deployment hello-world --image=redis:alpineCreate a deployment named hello-world with the redis:alpine image.
kubectl run hello-world-pod --image=redis:alpineStart a pod named hello-world-pod with the redis:alpine image.
kubectl create deployment hello-world --image=$image --dry-run=client -o yaml > deployment.yamlGenerate a deployment YAML file without creating the deployment.
kubectl get deployment hello-worldList the hello-world deployment.
kubectl get replicasetList all ReplicaSets.
kubectl describe deployment hello-worldShow detailed information about the hello-world deployment.
kubectl describe replicaset hello-worldShow detailed information about the hello-world ReplicaSet.
kubectl describe pod hello-worldShow detailed information about the hello-world pod.
kubectl expose deployment hello-world --port=80 --target-port=8080Expose the hello-world deployment as a service.
kubectl get service hello-worldList the hello-world service.
kubectl describe service hello-worldShow detailed information about the hello-world service.
curl http://$SERVICEIP:PORTAccess the hello-world service inside the cluster.
kubectl get endpoints hello-worldList the endpoints for the hello-world service.
kubectl get deployment hello-world -o yamlOutput the hello-world deployment in YAML format.
kubectl get deployment hello-world -o jsonOutput the hello-world deployment in JSON format.

Deployment Declaratively

Commands for creating and managing deployments declaratively.

CommandDescription
kubectl create deployment hello-world --image=redis:alpine --dry-run=client -o yamlGenerate a deployment YAML file without creating the deployment.
more deployment.ymlView the contents of the deployment.yml file.
kubectl deployDeploy the resources defined in the YAML files.
kubectl expose > service.ymlGenerate a service YAML file using the expose command.
kubectl apply -f service.ymlApply the service YAML file to create the service.
kubectl get allList all resources including State, Deployment, ReplicaSet, Pod, and Service.
kubectl get service hello-worldList the hello-world service.
kubectl edit deployment hello-worldEdit the hello-world deployment on the fly.
kubectl scale deployment hello-world --replicas=40Scale the hello-world deployment to 40 replicas.
kubectl diff -f deployment-new.yamlDiff that with the current deployment file to narrow down where the changes are.

Logs

Commands for retrieving logs from containers and troubleshooting.

CommandDescriptionExample
kubectl logs $podname$Print the logs for a specific pod.kubectl logs hello-world-pod
1
2
3
4
5
# This is how you can remote into a container.
kubectl exec -it redis -- /bin/sh
hostname
ip addr
exit
This post is licensed under CC BY 4.0 by the author.