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

Related

  • The Production-Ready Kubernetes Service Checklist
  • Optimizing Prometheus Queries With PromQL
  • Demystifying Kubernetes in 5 Minutes
  • Strengthening Your Kubernetes Cluster With Pod Security Admission

Trending

  • Building a DevOps-Ready Internal Developer Platform: A Hands-On Guide to Golden Paths, Self-Service, and Automated Delivery Pipelines
  • Designing API-First EMR Architectures in .NET: Enabling Modular Growth in Compliance-Driven Systems
  • DevOps and Platform Engineering Readiness Checklist: Everything Needed for a Scalable, Secure, High-Velocity Delivery Platform
  • How to Format Articles for DZone
  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
Likes (2)
Comment
Save
Tweet
Share
2.9K 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

  • The Production-Ready Kubernetes Service Checklist
  • Optimizing Prometheus Queries With PromQL
  • Demystifying Kubernetes in 5 Minutes
  • Strengthening Your Kubernetes Cluster With Pod Security Admission

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook