Kubernetes for DevOps Engineers

kubernetes commands

50 KUBERNETES COMMANDS

  1. kubectl version

Display the Kubernetes client and server version.

kubectl version

  1. kubectl cluster-info

Display cluster information, including the master and services.

kubectl cluster-info

  1. kubectl get nodes

List all nodes in the cluster.

kubectl get nodes

  1. kubectl get pods

List all pods in the default namespace.

kubectl get pods

  1. kubectl get deployments

List all deployments in the default namespace.

kubectl get deployments

  1. kubectl describe pod podname

Display detailed information about a specific pod.

kubectl describe pod mypod

  1. kubectl logs pod_name

Display the logs of a specific pod.

kubectl logs mypod

  1. kubectl exec -it podname -- /bin/sh

Start an interactive shell in a specific pod.

kubectl exec -it mypod -- /bin/sh

  1. kubectl create deployment name --image=imagename

Create a deployment with a specified container image.

kubectl create deployment myapp --image=myimage:tag

  1. kubectl expose deployment name --port=port --type=LoadBalancer

Expose a deployment as a service.

kubectl expose deployment myapp --port=80 --type=LoadBalancer

  1. kubectl scale deployment name --replicas=replicacount

Scale the number of replicas for a deployment.

kubectl scale deployment myapp --replicas=3

  1. kubectl get svc

List all services in the default namespace.

kubectl get svc

  1. kubectl delete pod podname

Delete a specific pod.

kubectl delete pod mypod

  1. kubectl delete deployment name

Delete a deployment and its associated pods.

kubectl delete deployment myapp

  1. kubectl apply -f file

Apply a configuration file to the cluster.

kubectl apply -f myconfig.yaml

  1. kubectl get configmaps

List all ConfigMaps in the default namespace.

kubectl get configmaps

  1. kubectl describe service servicename

Display detailed information about a specific service.

kubectl describe service myservice

  1. kubectl get namespaces

List all namespaces in the cluster.

kubectl get namespaces

  1. kubectl create namespace namespacename

Create a new namespace.

kubectl create namespace mynamespace

  1. kubectl get pods -n namespace

List all pods in a specific namespace

kubectl get pods -n mynamespace

  1. kubectl delete namespace namespacename

Delete a namespace and all its resources.

kubectl delete namespace mynamespace

  1. kubectl get services --sort-by=.metadata.name

List services and sort them by name.

kubectl get services --sort-by=.metadata.name

  1. kubectl rollout status deployment deploymentname

Check the status of a deployment rollout.

kubectl rollout status deployment myapp

  1. kubectl get pods --field-selector=status.phase=Running

List pods that are in the Running phase.

kubectl get pods --field-selector=status.phase=Running

  1. kubectl get events --sort-by=.metadata.creationTimestamp

List events sorted by creation timestamp.

kubectl get events --sort-by=.metadata.creationTimestamp

  1. kubectl create secret generic secretname --from-literal=key=value

Create a generic secret from literal values.

kubectl create secret generic mysecret --from-literal=username=admin -- from-literal=password=pass123

  1. kubectl describe secret secretname

Display detailed information about a specific secret.

kubectl describe secret mysecret

  1. kubectl edit deployment deploymentname

Edit the YAML of a deployment interactively.

kubectl edit deployment myapp

  1. kubectl get pods -o wide

List pods with additional details like node information.

kubectl get pods -o wide

  1. kubectl get nodes -o custom- columns=NODE:.metadata.name,IP:.status.addresses[0].address

    List nodes with custom output columns.

    kubectl get nodes -o custom- columns=NODE:.metadata.name,IP:.status.addresses[0].address

  2. kubectl top pods

Display resource usage (CPU and memory) of pods.

kubectl top pods

  1. kubectl apply -f https://url-to-yaml-file

Apply a configuration file directly from a URL.

kubectl apply -f https://raw.githubusercontent.com/example/myconfig.yaml

  1. kubectl get pods --selector=labelkey=labelvalue

List pods with a specific label.

kubectl get pods --selector=app=myapp

  1. kubectl get pods --field-selector=status.phase!=Running

List pods that are not in the Running phase.

kubectl get pods --field-selector=status.phase!=Running

  1. kubectl rollout undo deployment deploymentname

Rollback a deployment to the previous version.

kubectl rollout undo deployment myapp

  1. kubectl label pod podname labelkey=labelvalue

Add a label to a specific pod.

kubectl label pod mypod environment=production

  1. kubectl get componentstatuses

List the health of different cluster components.

kubectl get componentstatuses

  1. kubectl describe node nodename

Display detailed information about a specific node.

kubectl describe node mynode

  1. kubectl rollout history deployment deploymentname

View the rollout history of a deployment.

kubectl rollout history deployment myapp

  1. kubectl delete pod --selector=labelkey=labelvalue

Delete pods with a specific label.

kubectl delete pod --selector=app=myapp

  1. kubectl top nodes

Display resource usage (CPU and memory) of nodes.

kubectl top nodes

  1. kubectl get pods --watch

Watch for changes to pods in real-time.

kubectl get pods –watch

  1. kubectl rollout pause deployment deploymentname

Pause a deployment to prevent further rollouts.

kubectl rollout pause deployment myapp

  1. kubectl rollout resume deployment deploymentname

Resume a paused deployment.

kubectl rollout resume deployment myapp

  1. kubectl explain resource

Get information about a Kubernetes resource.

kubectl explain pod

  1. kubectl get pods -o jsonpath="{.items[*].metadata.name}"

Extract specific information using JSONPath.

kubectl get pods -o jsonpath="{.items[*].metadata.name}"
  1. kubectl apply --dry-run=client -f file

Dry run to validate a configuration file without applying it.

kubectl apply --dry-run=client -f myconfig.yaml

  1. kubectl exec -it podname -- /bin/sh -c 'command'

Execute a command in a specific pod.

kubectl exec -it mypod -- /bin/sh -c 'ls /app'

  1. kubectl get events --sort-by=.metadata.creationTimestamp -n namespace

List events sorted by creation timestamp in a specific namespace.

kubectl get events --sort-by=.metadata.creationTimestamp -n mynamespace

  1. kubectl get secrets

List all secrets in the default namespace.

kubectl get secrets