Kubernetes Useful Commands


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:

  1. 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.
  2. 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.
  3. Scaling:

    • kubectl scale deployment <deployment-name> --replicas=<desired-replicas>: Scale a deployment to a specific number of replicas.
  4. 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.
  5. 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.
  6. 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.
  7. Configuring Context:

    • kubectl config get-contexts: List available contexts.
    • kubectl config use-context <context-name>: Switch to a specific context.
  8. 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.
  9. 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.
  10. 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 <resource type>: List all resources of that particular type in the current namespace. For example, kubectl get pods will list all pods in the current namespace.
  • kubectl create <resource type> <name> <other properties>: Creates the "X" resource in the current namespace with the specified name. For example, kubectl create deployment my-deployment --replicas=3 will create a deployment named my-deployment with 3 replicas.
  • kubectl delete <resource type> <name>: Deletes the "X" resource with the given name in the current namespace. For example, kubectl delete pod my-pod will delete the pod named my-pod.
  • kubectl describe <resource type> <name>: Provides detailed information about the specified resource. For example, kubectl describe deployment my-deployment will provide detailed information about the deployment named my-deployment.
  • kubectl edit <resource type> <name>: Opens the specified resource for editing in a text editor. For example, kubectl edit deployment my-deployment will open the deployment named my-deployment for editing in your default text editor.

Pod-related commands:

  • kubectl get pods: List all pods in the current namespace.
  • kubectl describe pod <pod name>: Provides detailed information about the specified pod.
  • kubectl logs <pod name>: Gets the logs for the specified pod.
  • kubectl exec <pod name> -- <command>: Executes the specified command in the specified pod.
  • kubectl cp <local file or directory> <pod name>:<container name>:<destination path>: Copies a file or directory from your local machine to the specified pod and container.
  • kubectl cp <pod name>:<container name>:<source path> <local file or directory>: Copies a file or directory from the specified pod and container to your local machine.

Deployment-related commands:

  • kubectl get deployments: List all deployments in the current namespace.
  • kubectl describe deployment <deployment name>: Provides detailed information about the specified deployment.
  • kubectl rollout status deployment <deployment name>: Displays the rollout status of the specified deployment.
  • kubectl rollout pause deployment <deployment name>: Pauses the rollout of the specified deployment.
  • kubectl rollout resume deployment <deployment name>: Resumes the rollout of the specified deployment.
  • kubectl rollout latest deployment <deployment name>: Rolls out the latest version of the specified deployment.
  • kubectl scale deployment <deployment name> --replicas=<number of replicas>: Scales the specified deployment to the specified number of replicas.

Service-related commands:

  • kubectl get services: List all services in the current namespace.
  • kubectl describe service <service name>: Provides detailed information about the specified service.
  • kubectl expose deployment <deployment name> --port=80 --name=my-service: Exposes the specified deployment as a service on port 80 with the name my-service.
  • kubectl delete service <service name>: Deletes the specified service.

Node-related commands:

  • kubectl get nodes: List all nodes in the cluster.
  • kubectl describe node <node name>: Provides detailed information about the specified node.
  • kubectl cordon node <node name>: Cordon a node, meaning that no new pods will be scheduled on it.
  • kubectl uncordon node <node name>: Uncordon a node, meaning that new pods can be scheduled on it.
  • kubectl drain node <node name>: Drain a node, meaning that all pods running on it are evicted.
  • kubectl delete node <node name>: Delete a node from the cluster.

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/

 

 

Kubernetes Useful Commands


Enroll Now

  • DevOps
  • Kubernetes