• Software Letters
  • Posts
  • Essential kubectl Commands: The Top 100 You Must Know for Effective Kubernetes Management

Essential kubectl Commands: The Top 100 You Must Know for Effective Kubernetes Management

From Basic Operations to Advanced Techniques

Introduction

Kubernetes has become the de facto standard for container orchestration, allowing developers and DevOps teams to manage containerized applications at scale. At the heart of Kubernetes management is kubectl, a powerful command-line tool that interacts with your Kubernetes cluster. In this comprehensive guide, we'll explore the top kubectl commands that every Kubernetes user should know. Whether you're just starting out or you're a seasoned pro, mastering these commands will enhance your ability to manage and troubleshoot your applications effectively.

Basic Commands

Understanding the basic kubectl commands is essential for any Kubernetes user. These commands are the foundation of managing resources within a cluster.

kubectl version

Displays the client and server version information for kubectl and the Kubernetes cluster.

kubectl version --short

This command is useful for verifying the compatibility between your kubectl client and the Kubernetes cluster.

kubectl get

Retrieves information about resources in your cluster. Commonly used with various resource types such as pods, services, and nodes.

kubectl get pods 
kubectl get services 
kubectl get nodes

You can also use the -o option to get the output in different formats such as JSON or YAML:

kubectl get pods -o json 
kubectl get services -o yaml

kubectl describe

Provides detailed information about a resource, including its status, events, and configuration. This command is very helpful for debugging and understanding the state of a resource.

kubectl describe pod [pod-name] 
kubectl describe service [service-name]

kubectl create

Creates a resource from a file or from standard input. This command is often used to create resources defined in YAML or JSON files.

kubectl create -f [file.yaml] 
kubectl create namespace [namespace-name]

kubectl delete

Deletes resources by filenames, stdin, resources, and names. It's essential for cleaning up resources that are no longer needed.

kubectl delete pod [pod-name] 
kubectl delete -f [file.yaml]

Namespace Management

Namespaces help organize cluster resources between multiple users and projects, providing a way to divide cluster resources.

kubectl get namespaces

Lists all namespaces in the cluster.

kubectl get namespaces

Namespaces can be used to create isolated environments within the same cluster, useful for staging and production environments.

kubectl create namespace

Creates a new namespace.

kubectl create namespace [namespace-name]

This command is helpful when you need to isolate resources for different projects or teams.

kubectl delete namespace

Deletes a specified namespace and all the resources within it. Be cautious when using this command as it will remove all resources in the namespace.

kubectl delete namespace [namespace-name]

Pod Management

Pods are the smallest deployable units in Kubernetes, and managing them effectively is crucial for application performance and stability.

kubectl get pods

Lists all pods in the specified namespace or in the default namespace.

kubectl get pods 
kubectl get pods -n [namespace]

kubectl describe pod

Provides detailed information about a specific pod, including its state, events, and resource usage.

kubectl describe pod [pod-name]

This command is essential for troubleshooting pod issues and understanding their status.

kubectl delete pod

Deletes a specified pod.

kubectl delete pod [pod-name]

Deleting a pod forces Kubernetes to create a new one if it's managed by a Deployment or ReplicaSet.

kubectl exec

Executes a command in a container in the specified pod. This is very useful for debugging running applications.

kubectl exec -it [pod-name] -- [command]

For example, you can open a shell inside a running container:

kubectl exec -it [pod-name] -- /bin/bash

Service Management

Services in Kubernetes define logical sets of pods and policies to access them. Here are some essential service commands:

kubectl get services

Lists all services in the specified namespace or in the default namespace.

kubectl get services 
kubectl get services -n [namespace]

kubectl describe service

Provides detailed information about a specific service, including its endpoints and selector.

kubectl describe service [service-name]

This command helps in understanding how traffic is routed to the pods.

kubectl delete service

Deletes a specified service.

kubectl delete service [service-name]

kubectl expose

Exposes a resource as a new Kubernetes service. This command is often used to expose pods, deployments, or replica sets as services.

kubectl expose pod [pod-name] --port=[port] --target-port=[target-port]

For example, you can expose a deployment as a service:

kubectl expose deployment [deployment-name] --type=LoadBalancer --port=80 --target-port=8080

Deployment Management

Deployments manage the deployment of application instances and help ensure that the desired number of pods are running.

kubectl get deployments

Lists all deployments in the specified namespace or in the default namespace.

kubectl get deployments kubectl get deployments -n [namespace]

kubectl describe deployment

Provides detailed information about a specific deployment, including its strategy, status, and events.

kubectl describe deployment [deployment-name]

This command is useful for understanding the current state and configuration of your deployments.

kubectl create deployment

Creates a new deployment.

kubectl create deployment [deployment-name] --image=[image]

For example, you can create a deployment using a specific image:

kubectl create deployment nginx-deployment --image=nginx

kubectl delete deployment

Deletes a specified deployment.

kubectl delete deployment [deployment-name]

kubectl rollout status

Checks the status of a rollout.

kubectl rollout status deployment/[deployment-name]

This command is helpful for monitoring the progress of a deployment update.

ConfigMap and Secret Management

ConfigMaps and Secrets are used to manage configuration data and sensitive information.

kubectl get configmaps

Lists all ConfigMaps in the specified namespace or in the default namespace.

kubectl get configmaps 
kubectl get configmaps -n [namespace]

kubectl create configmap

Creates a new ConfigMap from a file, literal value, or environment file.

kubectl create configmap [configmap-name] --from-file=[file-path] 
kubectl create configmap [configmap-name] --from-literal=[key=value]

For example, you can create a ConfigMap from a file:

kubectl create configmap my-config --from-file=config.yaml

kubectl get secrets

Lists all Secrets in the specified namespace or in the default namespace.

kubectl get secrets 
kubectl get secrets -n [namespace]

kubectl create secret

Creates a new Secret from a file, literal value, or Docker registry credentials.

kubectl create secret generic [secret-name] --from-file=[file-path] 
kubectl create secret generic [secret-name] --from-literal=[key=value] 
kubectl create secret docker-registry [secret-name] --docker-username=[username] --docker-password=[password] --docker-email=[email]

For example, you can create a generic secret:

kubectl create secret generic my-secret --from-literal=password=my-secret-password

Logs and Monitoring

Monitoring and accessing logs is essential for diagnosing issues and ensuring the health of your applications.

kubectl logs

Fetches logs from a container in a pod.

kubectl logs [pod-name] 
kubectl logs [pod-name] -c [container-name]

This command is crucial for debugging issues within your pods.

kubectl top pod

Displays resource (CPU/memory) usage of pods.

kubectl top pod 
kubectl top pod -n [namespace]

This command helps in monitoring the resource usage of your pods.

kubectl top node

Displays resource (CPU/memory) usage of nodes.

kubectl top node

Monitoring node resources is essential for understanding the overall health and capacity of your cluster.

Scaling and Autoscaling

Scaling applications and setting up autoscaling ensures that your application can handle varying loads.

kubectl scale

Scales a deployment to a specified number of replicas.

kubectl scale deployment [deployment-name] --replicas=[number]

For example, you can scale a deployment to 3 replicas:

kubectl scale deployment nginx-deployment --replicas=3

kubectl autoscale

Sets up autoscaling for a deployment based on CPU utilization or other metrics.

kubectl autoscale deployment [deployment-name] --min=[min-replicas] --max=[max-replicas] --cpu-percent=[percentage]

For example, you can set up autoscaling for a deployment:

kubectl autoscale deployment nginx-deployment --min=1 --max=10 --cpu-percent=80

Role-Based Access Control (RBAC)

RBAC is crucial for managing permissions and access within a Kubernetes cluster.

kubectl get roles

Lists all roles in the specified namespace or in the default namespace.

kubectl get roles 
kubectl get roles -n [namespace]

kubectl create role

Creates a new role with specified rules.

kubectl create role [role-name] --verb=[verb] --resource=[resource]

For example, you can create a role that allows reading pods:

kubectl create role pod-reader --verb=get,list,watch --resource=pods

kubectl get rolebindings

Lists all role bindings in the specified namespace or in the default namespace.

kubectl get rolebindings 
kubectl get rolebindings -n [namespace]

kubectl create rolebinding

Creates a new role binding to bind a role to a user, group, or service account.

kubectl create rolebinding [rolebinding-name] --role=[role-name] --user=[user-name]

For example, you can bind a role to a user:

kubectl create rolebinding pod-reader-binding --role=pod-reader --user=example-user

Cluster Management

Managing the cluster itself involves commands to retrieve cluster information and manage nodes.

kubectl cluster-info

Displays information about the Kubernetes cluster.

kubectl cluster-info

This command provides an overview of the cluster, including master and DNS addresses.

kubectl get nodes

Lists all nodes in the cluster.

kubectl get nodes

Understanding the state of your nodes is crucial for maintaining cluster health.

kubectl drain

Safely evicts all pods from a node before maintenance.

kubectl drain [node-name]

This command is useful for performing maintenance on nodes without disrupting services.

kubectl cordon

Marks a node as unschedulable to prevent new pods from being scheduled on it.

kubectl cordon [node-name]

kubectl uncordon

Marks a node as schedulable to allow new pods to be scheduled on it.

kubectl uncordon [node-name]

Advanced Commands and Tips

These advanced commands and tips can help you perform more complex operations and improve your workflow.

kubectl apply

Applies a configuration to a resource by file or stdin.

kubectl apply -f [file.yaml]

This command is typically used to apply changes incrementally to resources.

kubectl patch

Updates a resource with a patch.

kubectl patch [resource-type] [resource-name] --patch [json-patch]

For example, you can patch a deployment to update its image:

kubectl patch deployment [deployment-name] -p '{"spec":{"template":{"spec":{"containers":[{"name":"nginx","image":"nginx:1.15.4"}]}}}}'

kubectl replace

Replaces a resource by file or stdin.

kubectl replace -f [file.yaml]

kubectl annotate

Adds or updates an annotation on a resource.

kubectl annotate [resource-type] [resource-name] [annotation-key]=[annotation-value]

For example, you can add an annotation to a pod:

kubectl annotate pod [pod-name] description="This is my pod"

kubectl label

Adds or updates a label on a resource.

kubectl label [resource-type] [resource-name] [label-key]=[label-value]

For example, you can add a label to a pod:

kubectl label pod [pod-name] app=myapp

Conclusion

Mastering kubectl commands is a fundamental skill for anyone working with Kubernetes. This guide has covered the top 100 commands that will help you manage your Kubernetes resources efficiently. By practicing these commands and incorporating them into your daily workflow, you can become more proficient in managing and troubleshooting your Kubernetes applications.

For further learning, consider exploring the Kubernetes documentation, joining community forums, and experimenting with different kubectl commands in a test environment. With time and practice, you'll gain deeper insights and become a Kubernetes expert.