How to Use the Command 'kubectl get' (with Examples)
The kubectl get
command is a vital tool in the Kubernetes ecosystem, providing users with the capability to fetch and display information about various Kubernetes objects and resources within a cluster. As an essential component for Kubernetes management, this command allows for clear insights into the cluster’s state, enabling administrators and developers to verify configurations, troubleshoot issues, and ensure operational continuity. The command offers a versatile syntax that can fetch data across different levels of resource specificity, from namespaces down to individual pods and services.
Use Case 1: Get All Namespaces in the Current Cluster
Code:
kubectl get namespaces
Motivation: Understanding the organizational structure of your cluster is paramount. Each namespace can represent different environments (e.g., development, testing, production), teams, projects, or any other logical division for managing resources. It’s vital to have a clear overview of all namespaces to manage these divisions effectively.
Explanation: The command retrieves a list of all existing namespaces within the currently connected Kubernetes cluster. No additional arguments are required as it defaults to fetching the global namespace information.
Example Output:
NAME STATUS AGE
default Active 10d
kube-system Active 10d
kube-public Active 10d
custom-namespace Active 7d
Use Case 2: Get Nodes in a Specified Namespace
Code:
kubectl get nodes --namespace namespace
Motivation: Nodes are the physical or virtual machine instances where your containers run. Knowing the nodes associated with a specific namespace helps in understanding resource allocation and workload distribution configurations, especially in customized or multi-tenant habitats.
Explanation: In this scenario, the command attempts to list nodes within a specified namespace. However, it’s essential to note that nodes are a cluster-wide resource; they are technically not scoped to namespaces. The command can be used to enforce namespace context, but the output remains the same as clusters don’t segregate node visibility by namespace.
Example Output:
NAME STATUS ROLES AGE VERSION
node1.example Ready control-plane,master 10d v1.21.0
node2.example Ready <none> 10d v1.21.0
Use Case 3: Get Pods in a Specified Namespace
Code:
kubectl get pods --namespace namespace
Motivation: Pods are the smallest deployable units in Kubernetes and often represent a single instance of a running process. Retrieving all pods in a specific namespace is crucial for monitoring and managing application instances, troubleshooting failures, and analyzing performance and deployments within that scope.
Explanation: The command fetches a list of all pods operating in the specified namespace. By using the --namespace
flag followed by the desired namespace name, it narrows the context of the operation to only those pods relevant to that namespace.
Example Output:
NAME READY STATUS RESTARTS AGE
app-frontend-1 1/1 Running 1 2d
app-backend-1 1/1 Running 0 2d
database-1 1/1 Running 0 2d
Use Case 4: Get Deployments in a Specified Namespace
Code:
kubectl get deployments --namespace namespace
Motivation: Deployments manage the creation and updating of pods in a Kubernetes environment. Viewing deployments in a specific namespace helps in monitoring rollouts, updates, and the overall health of applications as defined by deployment configurations.
Explanation: This command lists all deployment objects located within a specified namespace, accessed through the --namespace
flag. It provides a summary of deployments, their current state, and other pertinent details, enabling users to quickly assess and manage deployments.
Example Output:
NAME READY UP-TO-DATE AVAILABLE AGE
frontend 3/3 3 3 10d
backend 3/3 3 3 10d
database 1/1 1 1 10d
Use Case 5: Get Services in a Specified Namespace
Code:
kubectl get services --namespace namespace
Motivation: Services abstract and expose a set of pods as a network service. This is crucial for enabling access and providing load balancing. Viewing services in a specific namespace is key in understanding how applications are exposed and interacted with externally and internally.
Explanation: The command outputs all service resources in the specified namespace, utilizing the --namespace
flag. This includes information about types, cluster IPs, and ports, offering insights into how applications are mapped and accessed within that namespace.
Example Output:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
backend-svc ClusterIP 10.96.0.1 <none> 80/TCP 10d
frontend-svc NodePort 10.96.0.2 <none> 80:30001/TCP 10d
Use Case 6: Get All Resources in a Specified Namespace
Code:
kubectl get all --namespace namespace
Motivation: A holistic view of all resources within a specific namespace is invaluable for comprehensive management and diagnostic purposes. It aids in ensuring that deployed resources align with intended configurations and detecting any anomalies or issues.
Explanation: This command retrieves a comprehensive list of all resources—such as pods, services, and deployments—within the specified namespace. The --namespace
flag scopes the query to restrict the output to the desired namespace’s resources.
Example Output:
NAME READY STATUS RESTARTS AGE
pod/app-frontend-1 1/1 Running 1 2d
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/backend-svc ClusterIP 10.96.0.1 <none> 80/TCP 10d
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/app 3/3 3 3 10d
Use Case 7: Get Kubernetes Objects Defined in a YAML Manifest File
Code:
kubectl get --file path/to/manifest.yaml
Motivation: YAML manifest files define Kubernetes object configurations. Fetching information directly from these files is critical for verification purposes and ensuring that the deployed state matches intended configurations as described in the manifest.
Explanation: By specifying the --file
option followed by the path to a YAML manifest, this command reads the file, interprets the configurations, and fetches the current status or definitions of the specified resources in the cluster.
Example Output:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/web NodePort 10.96.0.1 <none> 80:30001/TCP 2d
NAME READY STATUS RESTARTS AGE
pod/web-5d69f7b6cb-2btnj 1/1 Running 0 2d
Conclusion
The kubectl get
command is a cornerstone of Kubernetes management, providing a wide array of options to extract and display critical information about your cluster’s resources. Whether you’re managing namespaces, examining pods, or verifying deployments and services, kubectl get
offers the versatility and detail necessary to maintain seamless Kubernetes operations.