Kubernetes Commands

Kubernetes Commands
kubectl get nodes

Displays the status of the nodes in the Kubernetes cluster.

kubectl get pods

Lists all the pods running in the default namespace.

kubectl get pods -A

Lists all the pods across all namespaces.

kubectl get pods -n <namespace>

Lists pods in a specific namespace.

kubectl create deployment <deployment-name> –image=<image-name>

Creates a new deployment with the specified name and image.

kubectl get deployments

Lists all the deployments in the default namespace.

kubectl describe pod <pod-name>

Shows detailed information about a specific pod.

kubectl describe deployment <deployment-name>

Shows detailed information about a specific deployment.

kubectl apply -f <filename.yaml>

Applies a configuration to a resource from a YAML file.

kubectl delete -f <filename.yaml>

Deletes resources defined in a YAML file.

kubectl delete pod <pod-name>

Deletes a specific pod.

kubectl expose deployment <deployment-name> –port=<port> –target-port=<target-port> –type=LoadBalancer –name=<service-name>

Exposes a deployment as a new Service.

kubectl get services

Lists all the services in the default namespace.

kubectl get service <service-name> -o yaml

Displays the YAML definition of a specific service.

kubectl logs <pod-name>

Shows the logs of a specific pod.

kubectl logs -f <pod-name>

Follows the logs of a specific pod in real-time.

kubectl exec -it <pod-name> — /bin/bash

Executes a command inside a running pod (interactive shell).

kubectl scale deployment <deployment-name> –replicas=<count>

Scales the number of replicas for a deployment.

kubectl create namespace <namespace-name>

Creates a new namespace.

kubectl get namespaces

Lists all the namespaces in the cluster.

kubectl delete namespace <namespace-name>

Deletes a specific namespace.

kubectl get events

Displays events in the cluster.

kubectl get configmaps

Lists all the ConfigMaps in the default namespace.

kubectl create configmap <configmap-name> –from-file=<path/to/file>

Creates a ConfigMap from a file.

kubectl get secrets

Lists all the secrets in the default namespace.

kubectl create secret generic <secret-name> –from-literal=username=<username> –from-literal=password=<password>

Creates a generic secret from literal values.

kubectl get pvc

Lists all the PersistentVolumeClaims in the default namespace.

kubectl create pvc <pvc-name> –access-modes=ReadWriteOnce –size=1Gi

Creates a PersistentVolumeClaim.

kubectl get pv

Lists all the PersistentVolumes in the cluster.

kubectl create -f <filename.yaml>

Creates resources defined in a YAML file.

Advanced Commands

kubectl rollout restart deployment <deployment-name>

Restarts a deployment, triggering a rolling update.

kubectl rollout status deployment <deployment-name>

Shows the status of a deployment rollout.

kubectl rollout history deployment <deployment-name>

Shows the rollout history of a deployment.

kubectl rollout undo deployment <deployment-name> –to-revision=<revision-number>

Rolls back a deployment to a previous revision.

kubectl autoscale deployment <deployment-name> –min=<min-replicas> –max=<max-replicas> –cpu-percent=<percentage>

Enables autoscaling for a deployment based on CPU utilization.

kubectl top nodes

Displays resource usage (CPU/Memory) of nodes.

kubectl top pods

Displays resource usage (CPU/Memory) of pods in the default namespace.

kubectl top pods -n <namespace>

Displays resource usage of pods in a specific namespace.

kubectl cordon <node-name>

Marks a node as unschedulable.

kubectl uncordon <node-name>

Marks a node as schedulable.

kubectl drain <node-name> –ignore-daemonsets

Evicts all pods from a node in preparation for maintenance.