kubectl
is a command-line tool for interacting with your Kubernetes cluster. It lets you manage Kubernetes resources like pods, deployments, services, and more.
1. kubectl
Basics
1.1 Help and Version Information
kubectl version
Shows thekubectl
client and server versions.kubectl version --client
kubectl help
Provides help information forkubectl
.kubectl help
2. Context and Configuration
2.1 Context Management
kubectl config get-contexts
Lists all available contexts.kubectl config get-contexts
kubectl config use-context
Switches to a different context.kubectl config use-context my-cluster
kubectl config current-context
Displays the current context.kubectl config current-context
2.2 Config View and Edit
kubectl config view
Displays merged kubeconfig settings.kubectl config view
kubectl config set-context
Modifies a context entry in the kubeconfig file.kubectl config set-context my-context --namespace=dev
3. Managing Kubernetes Resources
3.1 Pods
kubectl get pods
Lists all pods in the default namespace.kubectl get pods
kubectl get pods -n my-namespace
Lists all pods in a specific namespace.kubectl get pods -n my-namespace
kubectl describe pod my-pod
Provides detailed information about a specific pod.kubectl describe pod my-pod
kubectl logs my-pod
Retrieves logs from a specific pod.kubectl logs my-pod
kubectl exec -it my-pod -- /bin/bash
Executes a command inside a running pod.kubectl exec -it my-pod -- /bin/bash
3.2 Deployments
kubectl get deployments
Lists all deployments in the default namespace.kubectl get deployments
kubectl create deployment nginx-deployment --image=nginx
Creates a new deployment.kubectl create deployment nginx-deployment --image=nginx
kubectl scale deployment nginx-deployment --replicas=4
Scales a deployment to a specified number of replicas.kubectl scale deployment nginx-deployment --replicas=4
kubectl rollout restart deployment/nginx-deployment
Restarts the pods managed by a deployment.kubectl rollout restart deployment/nginx-deployment
3.3 Services
kubectl get services
Lists all services in the default namespace.kubectl get services
kubectl expose deployment nginx-deployment --type=NodePort --port=80
Exposes a deployment as a service.kubectl expose deployment nginx-deployment --type=NodePort --port=80
kubectl delete service nginx-service
Deletes a service by name.kubectl delete service nginx-service
3.4 Nodes
kubectl get nodes
Lists all nodes in the cluster.kubectl get nodes
kubectl describe node my-node
Provides detailed information about a specific node.kubectl describe node my-node
3.5 ConfigMaps
kubectl create configmap my-config --from-literal=key1=value1
Creates a ConfigMap from a literal value.kubectl create configmap my-config --from-literal=key1=value1
kubectl get configmaps
Lists all ConfigMaps.kubectl get configmaps
kubectl describe configmap my-config
Provides detailed information about a specific ConfigMap.kubectl describe configmap my-config
3.6 Secrets
kubectl create secret generic my-secret --from-literal=password=myPassword
Creates a secret from a literal value.kubectl create secret generic my-secret --from-literal=password=myPassword
kubectl get secrets
Lists all secrets.kubectl get secrets
kubectl describe secret my-secret
Provides detailed information about a specific secret.kubectl describe secret my-secret
4. Advanced Resource Management
4.1 Apply Changes
kubectl apply -f my-deployment.yaml
Applies changes from a YAML or JSON file.kubectl apply -f my-deployment.yaml
4.2 Delete Resources
kubectl delete pod my-pod
Deletes a pod by name.kubectl delete pod my-pod
kubectl delete -f my-resource.yaml
Deletes resources defined in a YAML or JSON file.kubectl delete -f my-resource.yaml
4.3 Patching Resources
kubectl patch deployment my-deployment -p '{"spec":{"replicas":5}}'
Updates part of a resource specification.kubectl patch deployment my-deployment -p '{"spec":{"replicas":5}}'
5. Monitoring and Debugging
5.1 Events
kubectl get events
Lists all events in the cluster.kubectl get events
5.2 Resource Usage
kubectl top nodes
Displays CPU and memory usage for nodes.kubectl top nodes
kubectl top pods
Displays CPU and memory usage for pods.kubectl top pods
5.3 Port Forwarding
kubectl port-forward pod/my-pod 8080:80
Forwards a local port to a port on a pod.kubectl port-forward pod/my-pod 8080:80
6. Label and Annotate Resources
6.1 Label Resources
kubectl label pod my-pod environment=production
Adds a label to a pod.kubectl label pod my-pod environment=production
6.2 Annotate Resources
kubectl annotate pod my-pod description='My production pod'
Adds an annotation to a pod.kubectl annotate pod my-pod description='My production pod'
7. Role-Based Access Control (RBAC)
7.1 List Roles and RoleBindings
kubectl get roles
Lists roles in the current namespace.kubectl get roles
kubectl get rolebindings
Lists role bindings in the current namespace.kubectl get rolebindings
8. Networking
8.1 Expose Resources
kubectl expose pod redis --port=6379 --name redis-service --type=ClusterIP
Exposes a pod with a service.kubectl expose pod redis --port=6379 --name redis-service --type=ClusterIP
8.2 Manage Ingress
kubectl get ingress
Lists all ingress resources.kubectl get ingress
kubectl describe ingress my-ingress
Provides detailed information about a specific ingress.kubectl describe ingress my-ingress
9. Debugging with kubectl
9.1 Debug a Node
kubectl debug node/my-node --image=busybox
Creates a debugging container on a node.
kubectl debug node/my-node --image=busybox
#### **9.2 Debug a Pod**
- **`kubectl debug pod/my-pod -it --image=busybox --target=my-container`**
Attaches a debugging container to a running pod.
```sh
kubectl debug pod/my-pod -it --image=busybox --target=my-container
10. Miscellaneous Commands
10.1 Run a Pod Temporarily
kubectl run my-shell --rm -i --tty --image busybox -- /bin/sh
Runs a temporary shell pod for debugging.kubectl run my-shell --rm -i --tty --image busybox -- /bin/sh
10.2 Taint and Tolerate Nodes
kubectl taint nodes my-node key=value:NoSchedule
Adds a taint to a node.kubectl taint nodes my-node key=value:NoSchedule
Conclusion
This Article covers all the essential kubectl
commands. This comprehensive guide should provide a solid foundation to learn and practice all the kubectl
commands.
Best of Luck!