DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Enterprise AI Trend Report: Gain insights on ethical AI, MLOps, generative AI, large language models, and much more.

2024 Cloud survey: Share your insights on microservices, containers, K8s, CI/CD, and DevOps (+ enter a $750 raffle!) for our Trend Reports.

PostgreSQL: Learn about the open-source RDBMS' advanced capabilities, core components, common commands and functions, and general DBA tasks.

AI Automation Essentials. Check out the latest Refcard on all things AI automation, including model training, data security, and more.

Related

  • Establishing a Highly Available Kubernetes Cluster on AWS With Kops
  • Secure the Cluster: A Blazing Kubernetes Developer’s Guide to Security
  • GitOps: Flux vs Argo CD
  • The Open Source Way to Rightsize Kubernetes With One Click

Trending

  • Harmonizing AI: Crafting Personalized Song Suggestions
  • Deploying to Heroku With GitLab CI/CD
  • C4 PlantUML: Effortless Software Documentation
  • Code Complexity in Practice
  1. DZone
  2. Software Design and Architecture
  3. Containers
  4. Mastering Daily Kubernetes Operations: A Guide To Useful kubectl Commands for Software Engineers

Mastering Daily Kubernetes Operations: A Guide To Useful kubectl Commands for Software Engineers

Learn about a variety of commands in kubectl, a critical tool for software engineers for managing and troubleshooting Kubernetes environments.

By 
Elias Naduvath Varghese user avatar
Elias Naduvath Varghese
·
Mar. 26, 24 · Tutorial
Like (1)
Save
Tweet
Share
1.4K Views

Join the DZone community and get the full member experience.

Join For Free

kubectl, the command-line interface for running commands against Kubernetes clusters, is a vital tool for any software engineer working with Kubernetes. It offers a myriad of commands, each with its own set of options, making it a powerful tool for managing and troubleshooting Kubernetes environments. This article aims to elucidate some of the most useful kubectl commands that software engineers use in their day-to-day operations.

1. Checking the Cluster Status

Before initiating any operation, it's crucial to get the cluster's status. Here are a few commands that help you do that:

  • kubectl cluster-info: This command provides basic information about the cluster and its primary services.
  • kubectl get nodes: This command lists all nodes that can be used to host applications.

2. Working With Pods

Pods are the smallest deployable units in Kubernetes. The following commands help manage them:

  • kubectl get pods: This command lists all Pods in the default namespace.
  • kubectl describe pod [pod-name]: To get detailed information about a specific Pod, including events and state
  • kubectl logs [pod-name]: This command shows the logs of the specified Pod, helpful for debugging.
  • kubectl exec -it [pod-name] -- /bin/bash: This command opens an interactive shell inside the specified Pod, useful for debugging and inspection.

3. Working With Deployments

Deployments are a higher-level concept that manages Pods. Here are some useful commands for dealing with deployments:

  • kubectl get deployments: This command lists all deployments in the default namespace.
  • kubectl describe deployment [deployment-name]: This command provides detailed information about a specific deployment.
  • kubectl scale deployment [deployment-name] --replicas=[number-of-replicas]: This command helps scale a deployment by increasing or decreasing the number of replicas.
  • kubectl rollout status deployment [deployment-name]: This command shows the status of the deployment rollout.

4. Working With Services

Services are an abstract way to expose applications running on a set of Pods. The following commands can be used to manage services:

  • kubectl get services: This command lists all services in the default namespace.
  • kubectl describe service [service-name]: This command provides detailed information about a specific service.
  • kubectl expose deployment [deployment-name] --type=NodePort --name=[service-name]: This command exposes a deployment as a service, making it accessible within the cluster or from the internet.

5. Working With ConfigMaps and Secrets

ConfigMaps and Secrets are Kubernetes objects that allow you to separate your application's configuration from your code. Here are some commands to help manage them:

  • kubectl get configmaps: This command lists all ConfigMaps in the default namespace.
  • kubectl get secrets: This command lists all secrets in the default namespace.
  • kubectl create configmap [configmap-name] --from-file=[path-to-file]: This command creates a new ConfigMap from a file.
  • kubectl create secret generic [secret-name] --from-literal=key=value: This command creates a new secret.

6. Debugging and Troubleshooting

Kubernetes offers several commands to help find and correct issues:

  • kubectl top node: This command shows the CPU and memory usage of each node, which can be useful for identifying nodes that are under a lot of load.
  • kubectl top pod: This command shows the CPU and memory usage of each Pod, which can be useful for identifying Pods that are using a lot of resources.
  • kubectl get events --sort-by=.metadata.creationTimestamp: This command lists all events in the default namespace, sorted by their creation time. This can be helpful for identifying recent issues that might have occurred in the cluster.

7. Cleanup

Kubernetes provides commands for cleaning up resources:

  • kubectl delete pod [pod-name]: This command deletes the specified Pod.
  • kubectl delete deployment [deployment-name]: This command deletes the specified deployment.
  • kubectl delete service [service-name]: This command deletes the specified service.
  • kubectl delete all --all: This command deletes all resources in the default namespace. Be careful with this one!

8. Working With Namespaces

Namespaces are used in environments with many users spread across multiple teams. Here are some commands related to managing them:

  • kubectl get namespaces: Lists all namespaces in your cluster
  • kubectl create namespace [namespace-name]: Creates a new namespace
  • kubectl config set-context --current --namespace=[namespace-name]: Changes the namespace for the current context

9. Managing Persistent Volumes

Persistent volumes provide ways for pods to store data. Here are some commands to work with them:

  • kubectl get pv: Lists all persistent volumes
  • kubectl describe pv [volume-name]: Provides detailed information about a specific volume
  • kubectl get pvc: Lists all persistent volume claims, which are requests for storage by a user

10. Dealing With Nodes

Nodes are worker machines in Kubernetes and are a crucial part of the system. Here are some commands related to nodes:

  • kubectl cordon [node-name]: Marks the node as unschedulable, preventing new Pods from being scheduled on the node
  • kubectl uncordon [node-name]: Removes the unschedulable mark from the node, allowing new pods to be scheduled on the node
  • kubectl drain [node-name]: Drains the node in preparation for maintenance

11. Resource Quotas and Limit Ranges

These commands are useful for managing the consumption of compute resources:

  • kubectl get quota: Lists all resource quotas in the current namespace
  • kubectl describe limitrange [limit-range-name]: Provides detailed information about a specific limit range

12. Accessing API Objects

These commands allow you to access raw API objects:

  • kubectl api-resources: Lists all API resources available on the server
  • kubectl explain [resource]: Provides documentation for the resource

Conclusion

Mastering kubectl commands is essential for efficiently managing Kubernetes clusters. While it may seem daunting at first, with regular use, these commands will become second nature. The commands listed above are just the tip of the iceberg; kubectl offers many more commands and options to explore. Remember, the flexibility of kubectl commands makes it a vital tool for any software engineer dealing with Kubernetes. The commands listed in this guide are just a subset of what kubectl can do. To explore more commands, you can always refer to the official Kubernetes documentation or use the kubectl help command.

Kubernetes cluster Command (computing) pods

Opinions expressed by DZone contributors are their own.

Related

  • Establishing a Highly Available Kubernetes Cluster on AWS With Kops
  • Secure the Cluster: A Blazing Kubernetes Developer’s Guide to Security
  • GitOps: Flux vs Argo CD
  • The Open Source Way to Rightsize Kubernetes With One Click

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: