kubectl get deployments is a fundamental Kubernetes command that helps developers, DevOps engineers, and administrators view and manage their application deployments easily. It allows users to list all deployments in a namespace or across the entire cluster, making it easier to monitor the state and configuration of deployed applications.
Understanding how to use this command effectively is crucial for managing modern containerized applications in Kubernetes environments. In this guide, we’ll break down how it works, show examples, and explain advanced use cases.
What is a Kubernetes Deployment?
A Kubernetes Deployment is an abstraction layer that manages replica sets and pods. It allows you to:
- Automatically create and update pods
- Perform rolling updates and rollbacks
- Maintain the desired number of pods at all times
Using kubectl get deployments
, you can quickly get an overview of all deployments and their current state.
Understanding kubectl Get Deployments
Basic Syntax
kubectl get deployments
This command lists all deployments in the current namespace. You can also specify a namespace:
kubectl get deployments -n my-namespace
Output Columns
The command output includes the following columns:
- NAME: Name of the deployment
- READY: Ready pods vs total pods
- UP-TO-DATE: Pods with the latest template
- AVAILABLE: Available pods
- AGE: Time since deployment creation
Practical Use Cases
1. List All Deployments
kubectl get deployments
This gives an overview of all deployments in the current namespace.
2. List Deployments in All Namespaces
kubectl get deploy --all-namespaces
This is useful for cluster-wide monitoring.
3. Show Detailed Deployment Info
kubectl get deploy my-app -o yaml
This provides all metadata, specs, and status of a specific deployment.
Filtering and Custom Columns
You can customize the output using the -o custom-columns
flag:
kubectl get deploy -o custom-columns=NAME:.metadata.name,REPLICAS:.spec.replicas
This makes it easy to focus on specific fields, especially in scripts.
Watch for Deployment Changes
To continuously monitor a deployment:
kubectl get deploy -w
This updates the terminal in real-time when there are changes.
YAML Example: A Sample Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
After applying this YAML using kubectl apply -f
, you can use kubectl get deployments
to view it.
Troubleshooting with kubectl Get Deployments
If a deployment isn’t behaving as expected, kubectl get deployments
helps identify issues such as:
- Pods not ready
- Mismatched replica counts
- Incorrect image versions
Use it along with kubectl describe deployment
and kubectl get pods
for a full diagnosis.
kubectl Get Deployments in CI/CD Pipelines
This command is commonly used in CI/CD pipelines to:
- Verify successful rollout
- Confirm pod health before progressing
- Gather logs for debugging
Automation scripts often use kubectl get deployments
to ensure infrastructure readiness before executing the next stage.
kubectl Get Deployments and RBAC
User permissions can affect access to deployments. If you get a forbidden
error, check your Role-Based Access Control (RBAC) configuration:
kubectl auth can-i get deployments
If not, you may need a role binding that includes the required permissions.
Best Practices
- Use namespaces to logically separate environments (e.g., dev, staging, prod)
- Regularly monitor deployments with watch mode
- Pair this command with logging and metrics tools like Prometheus
- Automate health checks using scripting
FAQs About kubectl Get Deployments
Q1: How do I list deployments in a specific namespace?
Use kubectl get deployments -n your-namespace
.
Q2: What does the READY column mean?
It shows how many pods are ready vs. how many are desired (e.g., 2/3).
Q3: Can I get output in JSON format?
Yes. Use kubectl get deployments -o json
.
Q4: How do I check if a deployment is complete?
The AVAILABLE
and UP-TO-DATE
columns should match the desired replicas.
Q5: Why do I see an empty list?
Ensure you are in the correct namespace or use --all-namespaces
.
With this guide, you now have a complete understanding of how to use kubectl get deployments effectively. It’s a vital command in any Kubernetes user’s toolkit for managing and monitoring application deployments.