DEV Community

Cover image for Top Kubernetes Commands for Developers
Suleiman Dibirov
Suleiman Dibirov

Posted on

Top Kubernetes Commands for Developers

Kubernetes, an open-source platform for automating the deployment, scaling, and operation of application containers, has become a fundamental tool for modern software development.
Here are some of the top Kubernetes commands every developer should know, along with comments explaining their usage:

1. kubectl get

The kubectl get command is essential for retrieving information about Kubernetes resources. It allows you to list various resources such as pods, nodes, services, deployments, etc.

  • Examples:
# List all pods in the current namespace
kubectl get pods

# List all services in the current namespace
kubectl get services

# List all nodes in the cluster
kubectl get nodes

# List all deployments in the current namespace
kubectl get deployments
Enter fullscreen mode Exit fullscreen mode

2. kubectl describe

The kubectl describe command provides detailed information about a specific resource. This is useful for debugging and understanding the state and events of a resource.

  • Examples:
# Show detailed information about a specific pod
kubectl describe pod <pod-name>

# Show detailed information about a specific service
kubectl describe service <service-name>

# Show detailed information about a specific node
kubectl describe node <node-name>

# Show detailed information about a specific deployment
kubectl describe deployment <deployment-name>
Enter fullscreen mode Exit fullscreen mode

3. kubectl logs

The kubectl logs command fetches the logs of a specific pod or container. This is crucial for debugging application issues.

  • Examples:
# Retrieve the logs from a specific pod
kubectl logs <pod-name>

# Retrieve the logs from a specific container in a pod
kubectl logs <pod-name> -c <container-name>

# Retrieve the logs from the previous instance of a container in a pod
kubectl logs <pod-name> -c <container-name> --previous

# Retrieve logs from the last 5 minutes
kubectl logs <pod-name> --since=5m
Enter fullscreen mode Exit fullscreen mode

4. kubectl exec

The kubectl exec command allows you to execute commands inside a container. This is particularly useful for debugging and inspecting the state of your application from within the container.

  • Examples:
# Start an interactive shell session in a specific pod
kubectl exec -it <pod-name> -- /bin/bash

# Execute a specific command in a specific pod
kubectl exec -it <pod-name> -- <command>
Enter fullscreen mode Exit fullscreen mode

5. kubectl apply

The kubectl apply command applies changes to a resource by filename or stdin. It's commonly used to create or update resources defined in YAML or JSON files.

  • Examples:
# Apply changes from a specific YAML file
kubectl apply -f <filename.yaml>

# Apply changes from all YAML files in a directory
kubectl apply -f <directory-with-yaml-files>
Enter fullscreen mode Exit fullscreen mode

6. kubectl delete

The kubectl delete command removes resources from your cluster. It's essential for cleaning up resources that are no longer needed.

  • Examples:
# Delete a specific pod
kubectl delete pod <pod-name>

# Delete a specific service
kubectl delete service <service-name>

# Delete a specific deployment
kubectl delete deployment <deployment-name>

# Delete resources defined in a specific YAML file
kubectl delete -f <filename.yaml>
Enter fullscreen mode Exit fullscreen mode

7. kubectl scale

The kubectl scale command adjusts the number of replicas for a deployment, replication controller, or replica set. This is useful for scaling your application up or down.

  • Examples:
# Scale a deployment to a specific number of replicas
kubectl scale --replicas=<number> deployment/<deployment-name>
Enter fullscreen mode Exit fullscreen mode

8. kubectl rollout

The kubectl rollout command manages the rollout of a resource. It can be used to view, pause, resume, and undo deployments.

  • Examples:
# Check the status of a deployment rollout
kubectl rollout status deployment/<deployment-name>

# View the rollout history of a deployment
kubectl rollout history deployment/<deployment-name>

# Undo the last rollout of a deployment
kubectl rollout undo deployment/<deployment-name>
Enter fullscreen mode Exit fullscreen mode

9. kubectl port-forward

The kubectl port-forward command forwards one or more local ports to a pod. This is helpful for accessing a service running in a pod from your local machine.

  • Examples:
# Forward a local port to a port on a specific pod
kubectl port-forward pod/<pod-name> <local-port>:<pod-port>
Enter fullscreen mode Exit fullscreen mode

10. kubectl config

The kubectl config command manages kubeconfig files. It can set context, display the current context, and modify configuration settings.

  • Examples:
# View the current kubeconfig settings
kubectl config view

# List all contexts in the kubeconfig file
kubectl config get-contexts

# Switch to a specific context
kubectl config use-context <context-name>
Enter fullscreen mode Exit fullscreen mode

Conclusion

Mastering these Kubernetes commands will significantly improve your efficiency and effectiveness as a developer. Whether you're managing deployments, debugging issues, or scaling applications, these commands provide the foundation you need to work confidently with Kubernetes.

Top comments (0)