How to use the command "kubectl-get" (with examples)
In Kubernetes, the kubectl get
command is used to fetch information about Kubernetes objects and resources within a cluster. It allows users to retrieve details such as namespaces, nodes, pods, deployments, services, and more. This article will provide eight different use cases of the kubectl get
command, each showcasing a specific scenario and providing relevant code examples.
Use Case 1: Get all namespaces in the current cluster
To get a list of all namespaces in the current cluster, you can use the following command:
kubectl get namespaces
Motivation: This use case is useful when you need to quickly retrieve a list of all namespaces available within the cluster. By executing this command, you can see the namespaces and their associated resources, helping you to understand the structure and organization of your Kubernetes environment.
Example:
$ kubectl get namespaces
Output:
NAME STATUS AGE
default Active 2d
kube-system Active 2d
kube-public Active 2d
Explanation: The command kubectl get namespaces
displays the NAME
, STATUS
, and AGE
of each namespace. In the above example, there are three namespaces: default
, kube-system
, and kube-public
. The status is displayed as Active
, indicating that they are all currently in use.
Use Case 2: Get nodes in a specified namespace
To retrieve a list of nodes within a specified namespace, you can use the following command:
kubectl get nodes -n namespace
Motivation: This scenario is beneficial to identify and inspect the nodes allocated to a particular namespace. By retrieving this information, you can determine the available resources and their current status.
Example:
$ kubectl get nodes -n my-namespace
Output:
NAME STATUS ROLES AGE VERSION
node1 Ready worker 2d v1.20.0
node2 Ready worker 2d v1.20.0
Explanation: The command kubectl get nodes -n my-namespace
displays the details of the nodes within the specified namespace, including the node NAME
, STATUS
, ROLES
, AGE
, and VERSION
. In this example, there are two nodes (node1
and node2
) in the namespace my-namespace
, and both nodes are marked as Ready
.
Use Case 3: Get pods in a specified namespace
To fetch a list of pods within a specific namespace, use the following command:
kubectl get pods -n namespace
Motivation: Retrieving a list of pods within a given namespace is essential for monitoring the state and health of your applications. By executing this command, you can view the pods’ current status, labels, and other relevant information.
Example:
$ kubectl get pods -n my-namespace
Output:
NAME READY STATUS RESTARTS AGE
my-pod1 1/1 Running 0 1d
my-pod2 2/2 Running 0 2d
Explanation: The command kubectl get pods -n my-namespace
displays the NAME
, READY
, STATUS
, RESTARTS
, and AGE
of each pod within the specified namespace. In the above example, there are two pods (my-pod1
and my-pod2
) in the my-namespace
namespace, both of which are currently Running
with zero restarts.
Use Case 4: Get deployments in a specified namespace
To obtain the list of deployments within a specified namespace, you can use the following command:
kubectl get deployments -n namespace
Motivation: When managing applications in Kubernetes, it is crucial to have visibility into the deployed resources. This command provides information about deployments, including the number of replicas, available updates, and observed replicas.
Example:
$ kubectl get deployments -n my-namespace
Output:
NAME READY UP-TO-DATE AVAILABLE AGE
my-deployment1 1/1 1 1 2d
my-deployment2 2/2 2 2 3d
Explanation: The command kubectl get deployments -n my-namespace
displays the NAME
, READY
, UP-TO-DATE
, AVAILABLE
, and AGE
information for each deployment in the specified namespace. In the above example, there are two deployments (my-deployment1
and my-deployment2
) within the my-namespace
namespace. The information shows that both deployments have the desired number of replicas (READY
), are up-to-date in terms of available updates (UP-TO-DATE
), and have all replicas available (AVAILABLE
).
Use Case 5: Get services in a specified namespace
To fetch a list of services within a particular namespace, use the following command:
kubectl get services -n namespace
Motivation: Services act as a crucial bridge between containers and other resources in Kubernetes. Retrieving information about services helps you monitor the endpoints, load balancer IPs, and other properties associated with those services.
Example:
$ kubectl get services -n my-namespace
Output:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
my-service1 LoadBalancer 10.100.123.123 <pending> 80:30888/TCP 3d
my-service2 ClusterIP 10.100.123.124 <none> 8080:32456/TCP 4d
Explanation: The command kubectl get services -n my-namespace
displays the NAME
, TYPE
, CLUSTER-IP
, EXTERNAL-IP
, PORT(S)
, and AGE
for each service within the specified namespace. In the example above, there are two services (my-service1
and my-service2
) in the my-namespace
namespace. The information reveals the service type (LoadBalancer
and ClusterIP
), the associated IP addresses (CLUSTER-IP
and EXTERNAL-IP
), and the port mappings (PORT(S)
).
Use Case 6: Get all resources in a specified namespace
To obtain all resources (including pods, services, deployments, etc.) within a specific namespace, you can use the following command:
kubectl get all -n namespace
Motivation: This use case is particularly helpful when you want to have a comprehensive view of all resources within a given namespace. It allows you to examine the state of all associated objects in one consolidated output.
Example:
$ kubectl get all -n my-namespace
Output:
NAME READY STATUS RESTARTS AGE
pod/my-pod1 1/1 Running 0 1d
pod/my-pod2 2/2 Running 0 2d
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/my-service1 LoadBalancer 10.100.123.123 <pending> 80:30888/TCP 3d
NAME STATUS ROLES AGE VERSION
node/node1 Ready worker 2d v1.20.0
node/node2 Ready worker 2d v1.20.0
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/my-deployment1 1/1 1 1 2d
[...]
Explanation: The command kubectl get all -n my-namespace
fetches all resources, including pods, services, deployments, and nodes in the specified namespace. The output provides a comprehensive list, with each object’s relevant details.
Use Case 7: Get Kubernetes objects defined in a YAML manifest
To retrieve the Kubernetes objects defined in a YAML manifest file, you can use the following command:
kubectl get -f path/to/manifest.yaml
Motivation: This use case allows you to obtain information about the objects defined in a YAML file. It is useful when you want to verify the state of resources created from a manifest or check their current state.
Example:
$ kubectl get -f my-manifest.yaml
Output:
NAME READY STATUS RESTARTS AGE
my-pod 1/1 Running 0 1d
my-service ClusterIP 10.100.123.123 <none> 8080/TCP 3d
my-deployment 2/2 Running 1 4d
Explanation: The command kubectl get -f my-manifest.yaml
retrieves information about the objects defined in the given manifest file (my-manifest.yaml
). The output displays the NAME
, READY
, STATUS
, RESTARTS
, and AGE
of each object. In this example, there is a pod (my-pod
), a service (my-service
), and a deployment (my-deployment
) defined in the manifest file.
Conclusion
The kubectl get
command is a versatile tool that allows users to retrieve information about various Kubernetes objects and resources. Whether you need to obtain a list of namespaces, inspect pods within a namespace, or view deployments and their status, kubectl get
provides a straightforward solution. By utilizing the different use cases shown in this article, you will be better equipped to monitor and manage your Kubernetes clusters effectively.