Kubernetes is a powerful container orchestration platform, and there are many useful commands you can use to interact with a Kubernetes cluster.
These commands are typically executed using the kubectl
(Kube Control) command-line tool.
Some essential Kubernetes commands:
Cluster Info and Status:
kubectl cluster-info
: Displays information about the Kubernetes cluster.kubectl get nodes
: Lists all the nodes in the cluster and their status.kubectl get pods -n <namespace>
: Lists pods in the specified namespace.Creating and Managing Resources:
kubectl create -f <filename.yaml>
: Create a resource from a YAML file.kubectl apply -f <filename.yaml>
: Apply changes to a resource from a YAML file.kubectl delete -f <filename.yaml>
: Delete a resource defined in a YAML file.Scaling:
kubectl scale deployment <deployment-name> --replicas=<desired-replicas>
: Scale a deployment to a specific number of replicas.Managing Deployments:
kubectl get deployments
: List all deployments.kubectl rollout status deployment/<deployment-name>
: Check the rollout status of a deployment.kubectl rollout history deployment/<deployment-name>
: View the rollout history of a deployment.kubectl rollout undo deployment/<deployment-name>
: Rollback a deployment to a previous revision.Managing Services:
kubectl get services
: List all services.kubectl expose deployment <deployment-name> --port=<port> --target-port=<target-port> --type=<service-type>
: Expose a deployment as a service.Logs and Debugging:
kubectl logs <pod-name>
: View logs of a specific pod.kubectl describe pod <pod-name>
: Get detailed information about a pod.kubectl exec -it <pod-name> -- /bin/sh
: Start an interactive shell in a pod for debugging.Configuring Context:
kubectl config get-contexts
: List available contexts.kubectl config use-context <context-name>
: Switch to a specific context.Namespace Management:
kubectl create namespace <namespace-name>
: Create a new namespace.kubectl delete namespace <namespace-name>
: Delete a namespace and its resources.kubectl get namespaces
: List all namespaces.Secrets and ConfigMaps:
kubectl create secret generic <secret-name> --from-literal=<key>=<value>
: Create a secret from literal values.kubectl create configmap <configmap-name> --from-literal=<key>=<value>
: Create a ConfigMap from literal values.Label and Annotation:
kubectl label <resource-type> <resource-name> <key>=<value>
: Add labels to a resource.kubectl annotate <resource-type> <resource-name> <key>=<value>
: Add annotations to a resource.These are some of the most commonly used kubectl
commands, but Kubernetes offers a vast array of functionality, so be sure to consult the official documentation and other resources for more advanced use cases and commands.
Some useful Kubernetes commands for managing your cluster:
General purpose commands:
kubectl get pods
will list all pods in the current namespace.kubectl create deployment my-deployment --replicas=3
will create a deployment named my-deployment
with 3 replicas.kubectl delete pod my-pod
will delete the pod named my-pod
.kubectl describe deployment my-deployment
will provide detailed information about the deployment named my-deployment
.kubectl edit deployment my-deployment
will open the deployment named my-deployment
for editing in your default text editor.Pod-related commands:
Deployment-related commands:
Service-related commands:
my-service
.Node-related commands:
These are just a few of the many useful Kubernetes commands.
For more information, please see the kubectl documentation:
https://kubernetes.io/docs/reference/kubectl/kubectl/