Usage and Verification
Metrics Collection Mechanisms
graph TD
A[Metrics Collection] --> B[Node Metrics]
A --> C[Pod Metrics]
B --> D[CPU Usage]
B --> E[Memory Consumption]
C --> F[Container-level Resources]
Basic Metrics Commands
Retrieving Node Metrics
## List node resource usage
kubectl top nodes
## Detailed node metrics with sorting
kubectl top nodes --sort-by=cpu
Retrieving Pod Metrics
## List pod resource consumption
kubectl top pods -A
## Namespace-specific pod metrics
kubectl top pods -n default
Advanced Metrics Exploration
Custom Resource Queries
## Get specific node metrics
kubectl get --raw "/apis/metrics.k8s.io/v1beta1/nodes" | jq .
## Retrieve pod-level metrics
kubectl get --raw "/apis/metrics.k8s.io/v1beta1/pods" | jq .
Horizontal Pod Autoscaler Integration
HPA Configuration Example
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: web-app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web-app
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
targetAverageUtilization: 70
Verification Techniques
Verification Method |
Command |
Purpose |
Deployment Status |
kubectl get deployment -n kube-system |
Check Metrics Server installation |
Resource Metrics |
kubectl top nodes |
Validate metrics collection |
API Availability |
kubectl api-resources | grep metrics |
Confirm metrics API |
Monitoring Best Practices
- Regularly validate metrics collection
- Set appropriate resource thresholds
- Monitor cluster performance
- Use comprehensive logging
Troubleshooting Metrics Issues
## Check Metrics Server logs
kubectl logs -n kube-system deployment/metrics-server
## Verify cluster connectivity
kubectl get --raw '/readyz?verbose'
Metrics Server provides real-time resource insights, enabling efficient Kubernetes cluster management and optimization in LabEx environments.