Overview
Pods are the smallest deployable units in Kubernetes and OpenShift. ThePod class provides a Pythonic interface for creating, managing, and interacting with pods, including executing commands and retrieving logs.
Creating a Pod
Basic Pod Creation
Create a simple pod with a single container:Pod with Advanced Configuration
Create a pod with resource limits, security context, and volumes:The
containers parameter is required when creating a pod. If not provided, a MissingRequiredArgumentError will be raised.Using Context Managers
Context managers provide automatic cleanup:Executing Commands in Pods
Theexecute() method runs commands inside a pod container:
Retrieving Pod Logs
Get logs from a pod using thelog() method:
Querying Pods
Get Pods with Label Selector
Query pods using label selectors:Access Pod Properties
Pod Scheduling and Affinity
Node Selector
Schedule pods on specific nodes:Pod Affinity
Init Containers
Use init containers for setup tasks:Troubleshooting
Pod Not Starting
Command Execution Failures
Resource Cleanup
Ensure proper cleanup of pod resources:Best Practices
- Use Context Managers: Always prefer context managers for automatic cleanup
- Set Resource Limits: Define CPU and memory limits to prevent resource exhaustion
- Use Readiness Probes: Add health checks to ensure containers are ready
- Label Your Pods: Use meaningful labels for easier querying and management
- Handle Errors: Always wrap pod operations in try-except blocks
- Wait for Status: Use
wait_for_status()before performing operations on pods