0
0
Kubernetesdevops~5 mins

kubectl get for listing resources in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you want to see what is running or available in your Kubernetes cluster, you use the kubectl get command. It lists resources like pods, services, or deployments so you can check their status and details.
When you want to check if your application pods are running correctly.
When you need to see all services available in your cluster to understand network access.
When you want to verify the status of deployments after applying changes.
When you want to list all namespaces to see how your cluster is organized.
When you want to quickly get details about any Kubernetes resource by name or type.
Commands
This command lists all pods in the current namespace, showing their names, readiness, status, restarts, and age. It helps you see if your pods are running or have issues.
Terminal
kubectl get pods
Expected OutputExpected
NAME READY STATUS RESTARTS AGE my-app-pod-1 1/1 Running 0 10m my-app-pod-2 1/1 Running 0 10m
This command lists all services in the current namespace, showing their names, types, cluster IPs, external IPs, ports, and age. It helps you understand how your apps are exposed.
Terminal
kubectl get services
Expected OutputExpected
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE my-app-svc ClusterIP 10.96.0.1 <none> 80/TCP 15m
This command lists all deployments in the current namespace, showing their names, ready replicas, total replicas, updated replicas, and age. It helps you check if your deployments are healthy.
Terminal
kubectl get deployments
Expected OutputExpected
NAME READY UP-TO-DATE AVAILABLE AGE my-app 2/2 2 2 20m
This command lists all pods in the kube-system namespace, which is where Kubernetes system components run. It helps you check the health of core cluster services.
Terminal
kubectl get pods -n kube-system
Expected OutputExpected
NAME READY STATUS RESTARTS AGE coredns-558bd4d5db-7k9qv 1/1 Running 0 1h etcd-minikube 1/1 Running 0 1h
-n - Specify the namespace to list resources from
Key Concept

If you remember nothing else from this pattern, remember: kubectl get shows you the current state of Kubernetes resources so you can monitor and troubleshoot your cluster.

Common Mistakes
Running kubectl get without specifying the resource type
kubectl get requires a resource type like pods or services to know what to list; otherwise, it shows an error.
Always specify the resource type, for example, kubectl get pods or kubectl get services.
Not specifying the namespace when resources are in a different namespace
kubectl get defaults to the current namespace, so it won't show resources in other namespaces unless specified.
Use the -n flag to specify the namespace, for example, kubectl get pods -n kube-system.
Summary
kubectl get lists Kubernetes resources like pods, services, and deployments.
Use kubectl get with the resource type to see their current status and details.
Add the -n flag to specify a namespace if your resources are not in the default namespace.