kubernetes commands
50 KUBERNETES COMMANDS
- kubectl version
Display the Kubernetes client and server version.
kubectl version
- kubectl cluster-info
Display cluster information, including the master and services.
kubectl cluster-info
- kubectl get nodes
List all nodes in the cluster.
kubectl get nodes
- kubectl get pods
List all pods in the default namespace.
kubectl get pods
- kubectl get deployments
List all deployments in the default namespace.
kubectl get deployments
- kubectl describe pod podname
Display detailed information about a specific pod.
kubectl describe pod mypod
- kubectl logs pod_name
Display the logs of a specific pod.
kubectl logs mypod
- kubectl exec -it podname -- /bin/sh
Start an interactive shell in a specific pod.
kubectl exec -it mypod -- /bin/sh
- kubectl create deployment name --image=imagename
Create a deployment with a specified container image.
kubectl create deployment myapp --image=myimage:tag
- kubectl expose deployment name --port=port --type=LoadBalancer
Expose a deployment as a service.
kubectl expose deployment myapp --port=80 --type=LoadBalancer
- kubectl scale deployment name --replicas=replicacount
Scale the number of replicas for a deployment.
kubectl scale deployment myapp --replicas=3
- kubectl get svc
List all services in the default namespace.
kubectl get svc
- kubectl delete pod podname
Delete a specific pod.
kubectl delete pod mypod
- kubectl delete deployment name
Delete a deployment and its associated pods.
kubectl delete deployment myapp
- kubectl apply -f file
Apply a configuration file to the cluster.
kubectl apply -f myconfig.yaml
- kubectl get configmaps
List all ConfigMaps in the default namespace.
kubectl get configmaps
- kubectl describe service servicename
Display detailed information about a specific service.
kubectl describe service myservice
- kubectl get namespaces
List all namespaces in the cluster.
kubectl get namespaces
- kubectl create namespace namespacename
Create a new namespace.
kubectl create namespace mynamespace
- kubectl get pods -n namespace
List all pods in a specific namespace
kubectl get pods -n mynamespace
- kubectl delete namespace namespacename
Delete a namespace and all its resources.
kubectl delete namespace mynamespace
- kubectl get services --sort-by=.metadata.name
List services and sort them by name.
kubectl get services --sort-by=.metadata.name
- kubectl rollout status deployment deploymentname
Check the status of a deployment rollout.
kubectl rollout status deployment myapp
- kubectl get pods --field-selector=status.phase=Running
List pods that are in the Running phase.
kubectl get pods --field-selector=status.phase=Running
- kubectl get events --sort-by=.metadata.creationTimestamp
List events sorted by creation timestamp.
kubectl get events --sort-by=.metadata.creationTimestamp
- 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
- kubectl describe secret secretname
Display detailed information about a specific secret.
kubectl describe secret mysecret
- kubectl edit deployment deploymentname
Edit the YAML of a deployment interactively.
kubectl edit deployment myapp
- kubectl get pods -o wide
List pods with additional details like node information.
kubectl get pods -o wide
-
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
-
kubectl top pods
Display resource usage (CPU and memory) of pods.
kubectl top pods
- 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
- kubectl get pods --selector=labelkey=labelvalue
List pods with a specific label.
kubectl get pods --selector=app=myapp
- 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
- kubectl rollout undo deployment deploymentname
Rollback a deployment to the previous version.
kubectl rollout undo deployment myapp
- kubectl label pod podname labelkey=labelvalue
Add a label to a specific pod.
kubectl label pod mypod environment=production
- kubectl get componentstatuses
List the health of different cluster components.
kubectl get componentstatuses
- kubectl describe node nodename
Display detailed information about a specific node.
kubectl describe node mynode
- kubectl rollout history deployment deploymentname
View the rollout history of a deployment.
kubectl rollout history deployment myapp
- kubectl delete pod --selector=labelkey=labelvalue
Delete pods with a specific label.
kubectl delete pod --selector=app=myapp
- kubectl top nodes
Display resource usage (CPU and memory) of nodes.
kubectl top nodes
- kubectl get pods --watch
Watch for changes to pods in real-time.
kubectl get pods –watch
- kubectl rollout pause deployment deploymentname
Pause a deployment to prevent further rollouts.
kubectl rollout pause deployment myapp
- kubectl rollout resume deployment deploymentname
Resume a paused deployment.
kubectl rollout resume deployment myapp
- kubectl explain resource
Get information about a Kubernetes resource.
kubectl explain pod
kubectl get pods -o jsonpath="{.items[*].metadata.name}"
Extract specific information using JSONPath.
kubectl get pods -o jsonpath="{.items[*].metadata.name}"
- 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
- kubectl exec -it podname -- /bin/sh -c 'command'
Execute a command in a specific pod.
kubectl exec -it mypod -- /bin/sh -c 'ls /app'
- 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
- kubectl get secrets
List all secrets in the default namespace.
kubectl get secrets