Apply a Manifest File
kubectl apply -f ./my-manifest.yamlCreates or updates resources from a manifest file.
Everyday kubectl commands for pods, deployments, nodes and clusters. Every item below is clearly marked as a shortcut, command, or syntax reference.
kubectl apply -f ./my-manifest.yamlCreates or updates resources from a manifest file.
kubectl get podsLists all pods in the namespace.
kubectl logs my-podDumps a pod's logs to your terminal.
kubectl logs -f my-podStreams pod logs as they are written.
kubectl exec --stdin --tty my-pod -- /bin/shOpens an interactive shell into a running pod.
kubectl config use-context <cluster-name>Sets the default context to the named cluster.
kubectl get pods --all-namespacesLists all pods in every namespace.
kubectl set image deployment/frontend www=image:v2Performs a rolling update of a container's image.
Creates or updates resources from a manifest file.
kubectl apply -f ./my-manifest.yamlLists all pods in the namespace.
kubectl get podsDumps a pod's logs to your terminal.
kubectl logs my-podStreams pod logs as they are written.
kubectl logs -f my-podOpens an interactive shell into a running pod.
kubectl exec --stdin --tty my-pod -- /bin/shSets the default context to the named cluster.
kubectl config use-context <cluster-name>Lists all pods in every namespace.
kubectl get pods --all-namespacesPerforms a rolling update of a container's image.
kubectl set image deployment/frontend www=image:v2Rolls back to the previous deployment revision.
kubectl rollout undo deployment/frontendListens on local port 5000 and forwards to port 6000 on the pod.
kubectl port-forward my-pod 5000:6000Runs a command in an existing pod.
kubectl exec my-pod -- ls /Prints the name of the context you are working in.
kubectl config current-contextStarts a single instance of nginx as a deployment.
kubectl create deployment nginx --image=nginxLists all services in the namespace.
kubectl get servicesShows details of a pod, including events and conditions.
kubectl describe pods my-podPerforms a rolling restart of a deployment.
kubectl rollout restart deployment/frontendScales a replicaset to a given number of replicas.
kubectl scale --replicas=3 rs/fooDeletes a pod using the type and name in pod.json.
kubectl delete -f ./pod.jsonShows the merged settings from your kubeconfig files.
kubectl config viewPermanently saves the namespace for all subsequent kubectl commands in that context.
kubectl config set-context --current --namespace=<namespace>Creates resources from all manifest files in a directory.
kubectl apply -f ./dirLists all pods in the namespace with extra details such as node and IP.
kubectl get pods -o widePrints the full YAML of one pod.
kubectl get pod my-pod -o yamlLists a particular deployment.
kubectl get deployment my-depLists pods carrying a matching label.
kubectl get pods --selector=app=cassandraWatches the rolling update until it completes.
kubectl rollout status -w deployment/frontendOpens the resource in your default editor.
kubectl edit svc/docker-registryRuns a single instance of nginx pod in a namespace.
kubectl run nginx --image=nginx -n mynamespaceShows CPU and memory metrics for all pods in the namespace.
kubectl top podDisplays the addresses of the control plane and services.
kubectl cluster-infoPrints the client and server version.
kubectl versionLists every context configured in your kubeconfig.
kubectl config get-contextsCreates resources from several manifest files at once.
kubectl apply -f ./my1.yaml -f ./my2.yamlShows the documentation for a resource's fields.
kubectl explain podsShows details of a node.
kubectl describe nodes my-nodeDiffs the cluster against the manifest without applying anything.
kubectl diff -f ./my-manifest.yamlShows the history of revisions for a deployment.
kubectl rollout history deployment/frontendAdds a label to a pod.
kubectl label pods my-pod new-label=awesomeApplies a partial update to a resource in place.
kubectl patch node k8s-node-1 -p '{"spec":{"unschedulable":true}}'Deletes pods and services carrying a matching label.
kubectl delete pods,services -l name=myLabelDeletes every pod and service in a namespace.
kubectl -n my-ns delete pod,svc --allDumps the logs of the previous container instantiation.
kubectl logs my-pod --previousCopies a local file into a remote pod.
kubectl cp /tmp/foo my-pod:/tmp/barCreates an interactive debugging session inside an existing pod.
kubectl debug my-pod -it --image=busybox:1.28Shows metrics for all nodes.
kubectl top nodePrepares a node for maintenance by evicting its pods.
kubectl drain my-nodeLists every resource type the API supports.
kubectl api-resourcesCreates a job that runs a command in a container.
kubectl create job hello --image=busybox:1.28 -- echo "Hello World"Creates a cron job with a schedule that runs a command.
kubectl create cronjob hello --image=busybox:1.28 --schedule="*/1 * * * *" -- echo "Hello World"Lists only warning events.
kubectl events --types=WarningLists pods sorted by how often their container restarted.
kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'Lists only pods whose status phase is Running.
kubectl get pods --field-selector=status.phase=RunningAdds an annotation to a pod.
kubectl annotate pods my-pod icon-url=http://example.comCreates an autoscaler that scales between the given limits.
kubectl autoscale deployment foo --min=2 --max=10Attaches to the process running in a container.
kubectl attach my-pod -iCordons a node so new pods are not scheduled on it.
kubectl cordon my-nodeLists pods together with their labels.
kubectl get pods --show-labelsReplaces an existing label value.
kubectl label pods my-pod new-label=new-value --overwriteUncordons a node so new pods can be scheduled on it again.
kubectl uncordon my-nodeAdds a taint to a node so only matching pods are scheduled there.
kubectl taint nodes foo dedicated=special-user:NoScheduleCommon Kubernetes commands include Apply a Manifest File (kubectl apply -f ./my-manifest.yaml), List Pods (kubectl get pods), Dump Pod Logs (kubectl logs my-pod), Stream Pod Logs (kubectl logs -f my-pod), Open a Shell in a Pod (kubectl exec --stdin --tty my-pod -- /bin/sh).
Type each command in the terminal or command-line interface used by Kubernetes.
Read its reference page and the linked documentation for prerequisites and placeholders. Check the current directory and target before running commands that change or delete data.