Skip to main content

Overview

Pods are the smallest deployable units in Kubernetes and OpenShift. The Pod 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

The execute() method runs commands inside a pod container:
The execute() method will raise an ExecOnPodError if the command fails (returns non-zero exit code), unless ignore_rc=True is specified.

Retrieving Pod Logs

Get logs from a pod using the log() 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

If execute() times out, increase the timeout parameter or check if the pod is in Running state before executing commands.

Resource Cleanup

Ensure proper cleanup of pod resources:

Best Practices

  1. Use Context Managers: Always prefer context managers for automatic cleanup
  2. Set Resource Limits: Define CPU and memory limits to prevent resource exhaustion
  3. Use Readiness Probes: Add health checks to ensure containers are ready
  4. Label Your Pods: Use meaningful labels for easier querying and management
  5. Handle Errors: Always wrap pod operations in try-except blocks
  6. Wait for Status: Use wait_for_status() before performing operations on pods